1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 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 * upload pausing
26 * </DESC>
27 */
28 /* This is based on the poc client of issue #11769
29 */
30 #include <stdio.h>
31 #include <string.h>
32 #include <sys/time.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <curl/curl.h>
36 #include <curl/mprintf.h>
37
log_line_start(FILE * log,const char * idsbuf,curl_infotype type)38 static void log_line_start(FILE *log, const char *idsbuf, curl_infotype type)
39 {
40 /*
41 * This is the trace look that is similar to what libcurl makes on its
42 * own.
43 */
44 static const char * const s_infotype[] = {
45 "* ", "< ", "> ", "{ ", "} ", "{ ", "} "
46 };
47 if(idsbuf && *idsbuf)
48 fprintf(log, "%s%s", idsbuf, s_infotype[type]);
49 else
50 fputs(s_infotype[type], log);
51 }
52
53 #define TRC_IDS_FORMAT_IDS_1 "[%" CURL_FORMAT_CURL_OFF_T "-x] "
54 #define TRC_IDS_FORMAT_IDS_2 "[%" CURL_FORMAT_CURL_OFF_T "-%" \
55 CURL_FORMAT_CURL_OFF_T "] "
56 /*
57 ** callback for CURLOPT_DEBUGFUNCTION
58 */
debug_cb(CURL * handle,curl_infotype type,char * data,size_t size,void * userdata)59 static int debug_cb(CURL *handle, curl_infotype type,
60 char *data, size_t size,
61 void *userdata)
62 {
63 FILE *output = stderr;
64 static int newl = 0;
65 static int traced_data = 0;
66 char idsbuf[60];
67 curl_off_t xfer_id, conn_id;
68
69 (void)handle; /* not used */
70 (void)userdata;
71
72 if(!curl_easy_getinfo(handle, CURLINFO_XFER_ID, &xfer_id) && xfer_id >= 0) {
73 if(!curl_easy_getinfo(handle, CURLINFO_CONN_ID, &conn_id) &&
74 conn_id >= 0) {
75 curl_msnprintf(idsbuf, sizeof(idsbuf), TRC_IDS_FORMAT_IDS_2,
76 xfer_id, conn_id);
77 }
78 else {
79 curl_msnprintf(idsbuf, sizeof(idsbuf), TRC_IDS_FORMAT_IDS_1, xfer_id);
80 }
81 }
82 else
83 idsbuf[0] = 0;
84
85 switch(type) {
86 case CURLINFO_HEADER_OUT:
87 if(size > 0) {
88 size_t st = 0;
89 size_t i;
90 for(i = 0; i < size - 1; i++) {
91 if(data[i] == '\n') { /* LF */
92 if(!newl) {
93 log_line_start(output, idsbuf, type);
94 }
95 (void)fwrite(data + st, i - st + 1, 1, output);
96 st = i + 1;
97 newl = 0;
98 }
99 }
100 if(!newl)
101 log_line_start(output, idsbuf, type);
102 (void)fwrite(data + st, i - st + 1, 1, output);
103 }
104 newl = (size && (data[size - 1] != '\n')) ? 1 : 0;
105 traced_data = 0;
106 break;
107 case CURLINFO_TEXT:
108 case CURLINFO_HEADER_IN:
109 if(!newl)
110 log_line_start(output, idsbuf, type);
111 (void)fwrite(data, size, 1, output);
112 newl = (size && (data[size - 1] != '\n')) ? 1 : 0;
113 traced_data = 0;
114 break;
115 case CURLINFO_DATA_OUT:
116 case CURLINFO_DATA_IN:
117 case CURLINFO_SSL_DATA_IN:
118 case CURLINFO_SSL_DATA_OUT:
119 if(!traced_data) {
120 if(!newl)
121 log_line_start(output, idsbuf, type);
122 fprintf(output, "[%ld bytes data]\n", (long)size);
123 newl = 0;
124 traced_data = 1;
125 }
126 break;
127 default: /* nada */
128 newl = 0;
129 traced_data = 1;
130 break;
131 }
132
133 return 0;
134 }
135
136 #define PAUSE_READ_AFTER 1
137 static size_t total_read = 0;
138
read_callback(char * ptr,size_t size,size_t nmemb,void * userdata)139 static size_t read_callback(char *ptr, size_t size, size_t nmemb,
140 void *userdata)
141 {
142 (void)size;
143 (void)nmemb;
144 (void)userdata;
145 if(total_read >= PAUSE_READ_AFTER) {
146 fprintf(stderr, "read_callback, return PAUSE\n");
147 return CURL_READFUNC_PAUSE;
148 }
149 else {
150 ptr[0] = '\n';
151 ++total_read;
152 fprintf(stderr, "read_callback, return 1 byte\n");
153 return 1;
154 }
155 }
156
progress_callback(void * clientp,double dltotal,double dlnow,double ultotal,double ulnow)157 static int progress_callback(void *clientp,
158 double dltotal,
159 double dlnow,
160 double ultotal,
161 double ulnow)
162 {
163 (void)dltotal;
164 (void)dlnow;
165 (void)ultotal;
166 (void)ulnow;
167 (void)clientp;
168 #if 0
169 /* Used to unpause on progress, but keeping for now. */
170 {
171 CURL *curl = (CURL *)clientp;
172 curl_easy_pause(curl, CURLPAUSE_CONT);
173 /* curl_easy_pause(curl, CURLPAUSE_RECV_CONT); */
174 }
175 #endif
176 return 0;
177 }
178
err(void)179 static int err(void)
180 {
181 fprintf(stderr, "something unexpected went wrong - bailing out!\n");
182 exit(2);
183 }
184
185
186
main(int argc,char * argv[])187 int main(int argc, char *argv[])
188 {
189 CURL *curl;
190 CURLcode rc = CURLE_OK;
191 CURLU *cu;
192 struct curl_slist *resolve = NULL;
193 char resolve_buf[1024];
194 char *url, *host = NULL, *port = NULL;
195
196 if(argc != 2) {
197 fprintf(stderr, "ERROR: need URL as argument\n");
198 return 2;
199 }
200 url = argv[1];
201
202 curl_global_init(CURL_GLOBAL_DEFAULT);
203 curl_global_trace("ids,time");
204
205 cu = curl_url();
206 if(!cu) {
207 fprintf(stderr, "out of memory\n");
208 exit(1);
209 }
210 if(curl_url_set(cu, CURLUPART_URL, url, 0)) {
211 fprintf(stderr, "not a URL: '%s'\n", url);
212 exit(1);
213 }
214 if(curl_url_get(cu, CURLUPART_HOST, &host, 0)) {
215 fprintf(stderr, "could not get host of '%s'\n", url);
216 exit(1);
217 }
218 if(curl_url_get(cu, CURLUPART_PORT, &port, 0)) {
219 fprintf(stderr, "could not get port of '%s'\n", url);
220 exit(1);
221 }
222 memset(&resolve, 0, sizeof(resolve));
223 curl_msnprintf(resolve_buf, sizeof(resolve_buf)-1,
224 "%s:%s:127.0.0.1", host, port);
225 resolve = curl_slist_append(resolve, resolve_buf);
226
227 curl = curl_easy_init();
228 if(!curl) {
229 fprintf(stderr, "out of memory\n");
230 exit(1);
231 }
232 /* We want to use our own read function. */
233 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
234
235 /* It will help us to continue the read function. */
236 curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, progress_callback);
237 curl_easy_setopt(curl, CURLOPT_XFERINFODATA, curl);
238 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
239
240 /* It will help us to ensure that keepalive does not help. */
241 curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
242 curl_easy_setopt(curl, CURLOPT_TCP_KEEPIDLE, 1L);
243 curl_easy_setopt(curl, CURLOPT_TCP_KEEPINTVL, 1L);
244
245 /* Enable uploading. */
246 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
247 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
248
249 if(curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L) != CURLE_OK ||
250 curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, debug_cb)
251 != CURLE_OK ||
252 curl_easy_setopt(curl, CURLOPT_RESOLVE, resolve) != CURLE_OK)
253 err();
254
255 curl_easy_setopt(curl, CURLOPT_URL, url);
256 rc = curl_easy_perform(curl);
257
258 if(curl) {
259 curl_easy_cleanup(curl);
260 }
261
262 curl_slist_free_all(resolve);
263 curl_free(host);
264 curl_free(port);
265 curl_url_cleanup(cu);
266 curl_global_cleanup();
267
268 return (int)rc;
269 }
270