blob: 7da740a095d3e6c63c763a86eac65d9493c5a5a5 [file] [log] [blame]
xf.li6c8fc1e2023-08-12 00:11:09 -07001/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@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.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 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24/* <DESC>
25 * An example of curl_easy_send() and curl_easy_recv() usage.
26 * </DESC>
27 */
28
29#include <stdio.h>
30#include <string.h>
31#include <curl/curl.h>
32
33/* Auxiliary function that waits on the socket. */
34static int wait_on_socket(curl_socket_t sockfd, int for_recv, long timeout_ms)
35{
36 struct timeval tv;
37 fd_set infd, outfd, errfd;
38 int res;
39
40 tv.tv_sec = timeout_ms / 1000;
41 tv.tv_usec = (timeout_ms % 1000) * 1000;
42
43 FD_ZERO(&infd);
44 FD_ZERO(&outfd);
45 FD_ZERO(&errfd);
46
47 FD_SET(sockfd, &errfd); /* always check for error */
48
49 if(for_recv) {
50 FD_SET(sockfd, &infd);
51 }
52 else {
53 FD_SET(sockfd, &outfd);
54 }
55
56 /* select() returns the number of signalled sockets or -1 */
57 res = select((int)sockfd + 1, &infd, &outfd, &errfd, &tv);
58 return res;
59}
60
61int main(void)
62{
63 CURL *curl;
64 /* Minimalistic http request */
65 const char *request = "GET / HTTP/1.0\r\nHost: example.com\r\n\r\n";
66 size_t request_len = strlen(request);
67
68 /* A general note of caution here: if you are using curl_easy_recv() or
69 curl_easy_send() to implement HTTP or _any_ other protocol libcurl
70 supports "natively", you are doing it wrong and you should stop.
71
72 This example uses HTTP only to show how to use this API, it does not
73 suggest that writing an application doing this is sensible.
74 */
75
76 curl = curl_easy_init();
77 if(curl) {
78 CURLcode res;
79 curl_socket_t sockfd;
80 size_t nsent_total = 0;
81
82 curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
83 /* Do not do the transfer - only connect to host */
84 curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
85 res = curl_easy_perform(curl);
86
87 if(res != CURLE_OK) {
88 printf("Error: %s\n", curl_easy_strerror(res));
89 return 1;
90 }
91
92 /* Extract the socket from the curl handle - we will need it for
93 waiting. */
94 res = curl_easy_getinfo(curl, CURLINFO_ACTIVESOCKET, &sockfd);
95
96 if(res != CURLE_OK) {
97 printf("Error: %s\n", curl_easy_strerror(res));
98 return 1;
99 }
100
101 printf("Sending request.\n");
102
103 do {
104 /* Warning: This example program may loop indefinitely.
105 * A production-quality program must define a timeout and exit this loop
106 * as soon as the timeout has expired. */
107 size_t nsent;
108 do {
109 nsent = 0;
110 res = curl_easy_send(curl, request + nsent_total,
111 request_len - nsent_total, &nsent);
112 nsent_total += nsent;
113
114 if(res == CURLE_AGAIN && !wait_on_socket(sockfd, 0, 60000L)) {
115 printf("Error: timeout.\n");
116 return 1;
117 }
118 } while(res == CURLE_AGAIN);
119
120 if(res != CURLE_OK) {
121 printf("Error: %s\n", curl_easy_strerror(res));
122 return 1;
123 }
124
125 printf("Sent %lu bytes.\n", (unsigned long)nsent);
126
127 } while(nsent_total < request_len);
128
129 printf("Reading response.\n");
130
131 for(;;) {
132 /* Warning: This example program may loop indefinitely (see above). */
133 char buf[1024];
134 size_t nread;
135 do {
136 nread = 0;
137 res = curl_easy_recv(curl, buf, sizeof(buf), &nread);
138
139 if(res == CURLE_AGAIN && !wait_on_socket(sockfd, 1, 60000L)) {
140 printf("Error: timeout.\n");
141 return 1;
142 }
143 } while(res == CURLE_AGAIN);
144
145 if(res != CURLE_OK) {
146 printf("Error: %s\n", curl_easy_strerror(res));
147 break;
148 }
149
150 if(nread == 0) {
151 /* end of the response */
152 break;
153 }
154
155 printf("Received %lu bytes.\n", (unsigned long)nread);
156 }
157
158 /* always cleanup */
159 curl_easy_cleanup(curl);
160 }
161 return 0;
162}