1 /*************************************************************************** 2 * _ _ ____ _ 3 * Project ___| | | | _ \| | 4 * / __| | | | |_) | | 5 * | (__| |_| | _ <| |___ 6 * \___|\___/|_| \_\_____| 7 * 8 * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel (at) haxx.se>, et al. 9 * 10 * This software is licensed as described in the file COPYING, which 11 * you should have received as part of this distribution. The terms 12 * are also available at https://curl.haxx.se/docs/copyright.html. 13 * 14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell 15 * copies of the Software, and permit persons to whom the Software is 16 * furnished to do so, under the terms of the COPYING file. 17 * 18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 19 * KIND, either express or implied. 20 * 21 ***************************************************************************/ 22 /* <DESC> 23 * An example of curl_easy_send() and curl_easy_recv() usage. 24 * </DESC> 25 */ 26 27 #include <stdio.h> 28 #include <string.h> 29 #include <curl/curl.h> 30 31 /* Auxiliary function that waits on the socket. */ 32 static int wait_on_socket(curl_socket_t sockfd, int for_recv, long timeout_ms) 33 { 34 struct timeval tv; 35 fd_set infd, outfd, errfd; 36 int res; 37 38 tv.tv_sec = timeout_ms / 1000; 39 tv.tv_usec= (timeout_ms % 1000) * 1000; 40 41 FD_ZERO(&infd); 42 FD_ZERO(&outfd); 43 FD_ZERO(&errfd); 44 45 FD_SET(sockfd, &errfd); /* always check for error */ 46 47 if(for_recv) { 48 FD_SET(sockfd, &infd); 49 } 50 else { 51 FD_SET(sockfd, &outfd); 52 } 53 54 /* select() returns the number of signalled sockets or -1 */ 55 res = select(sockfd + 1, &infd, &outfd, &errfd, &tv); 56 return res; 57 } 58 59 int main(void) 60 { 61 CURL *curl; 62 CURLcode res; 63 /* Minimalistic http request */ 64 const char *request = "GET / HTTP/1.0\r\nHost: example.com\r\n\r\n"; 65 curl_socket_t sockfd; /* socket */ 66 long sockextr; 67 size_t iolen; 68 curl_off_t nread; 69 70 /* A general note of caution here: if you're using curl_easy_recv() or 71 curl_easy_send() to implement HTTP or _any_ other protocol libcurl 72 supports "natively", you're doing it wrong and you should stop. 73 74 This example uses HTTP only to show how to use this API, it does not 75 suggest that writing an application doing this is sensible. 76 */ 77 78 curl = curl_easy_init(); 79 if(curl) { 80 curl_easy_setopt(curl, CURLOPT_URL, "http://example.com"); 81 /* Do not do the transfer - only connect to host */ 82 curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L); 83 res = curl_easy_perform(curl); 84 85 if(CURLE_OK != res) { 86 printf("Error: %s\n", strerror(res)); 87 return 1; 88 } 89 90 /* Extract the socket from the curl handle - we'll need it for waiting. 91 * Note that this API takes a pointer to a 'long' while we use 92 * curl_socket_t for sockets otherwise. 93 */ 94 res = curl_easy_getinfo(curl, CURLINFO_LASTSOCKET, &sockextr); 95 96 if(CURLE_OK != res) { 97 printf("Error: %s\n", curl_easy_strerror(res)); 98 return 1; 99 } 100 101 sockfd = (curl_socket_t)sockextr; 102 103 /* wait for the socket to become ready for sending */ 104 if(!wait_on_socket(sockfd, 0, 60000L)) { 105 printf("Error: timeout.\n"); 106 return 1; 107 } 108 109 puts("Sending request."); 110 /* Send the request. Real applications should check the iolen 111 * to see if all the request has been sent */ 112 res = curl_easy_send(curl, request, strlen(request), &iolen); 113 114 if(CURLE_OK != res) { 115 printf("Error: %s\n", curl_easy_strerror(res)); 116 return 1; 117 } 118 puts("Reading response."); 119 120 /* read the response */ 121 for(;;) { 122 char buf[1024]; 123 124 wait_on_socket(sockfd, 1, 60000L); 125 res = curl_easy_recv(curl, buf, 1024, &iolen); 126 127 if(CURLE_OK != res) 128 break; 129 130 nread = (curl_off_t)iolen; 131 132 printf("Received %" CURL_FORMAT_CURL_OFF_T " bytes.\n", nread); 133 } 134 135 /* always cleanup */ 136 curl_easy_cleanup(curl); 137 } 138 return 0; 139 } 140