]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
b19ccc9e | 2 | * "$Id: testhttp.c 7742 2008-07-15 20:23:09Z mike $" |
ef416fc2 | 3 | * |
71e16022 | 4 | * HTTP test program for CUPS. |
ef416fc2 | 5 | * |
07ed0e9a | 6 | * Copyright 2007-2011 by Apple Inc. |
ed486911 | 7 | * Copyright 1997-2006 by Easy Software Products. |
ef416fc2 | 8 | * |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 10 | * property of Apple Inc. and are protected by Federal copyright |
11 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
12 | * which should have been included with this file. If this file is | |
13 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
ef416fc2 | 14 | * |
15 | * This file is subject to the Apple OS-Developed Software exception. | |
16 | * | |
17 | * Contents: | |
18 | * | |
19 | * main() - Main entry. | |
20 | */ | |
21 | ||
22 | /* | |
23 | * Include necessary headers... | |
24 | */ | |
25 | ||
71e16022 | 26 | #include "string-private.h" |
1f0275e3 | 27 | #include "http-private.h" |
ef416fc2 | 28 | |
29 | ||
30 | /* | |
31 | * Types and structures... | |
32 | */ | |
33 | ||
34 | typedef struct uri_test_s /**** URI test cases ****/ | |
35 | { | |
36 | http_uri_status_t result; /* Expected return value */ | |
37 | const char *uri, /* URI */ | |
38 | *scheme, /* Scheme string */ | |
39 | *username, /* Username:password string */ | |
40 | *hostname, /* Hostname string */ | |
41 | *resource; /* Resource string */ | |
42 | int port, /* Port number */ | |
43 | assemble_port; /* Port number for httpAssembleURI() */ | |
44 | } uri_test_t; | |
45 | ||
46 | ||
47 | /* | |
48 | * Local globals... | |
49 | */ | |
50 | ||
51 | static uri_test_t uri_tests[] = /* URI test data */ | |
52 | { | |
53 | /* Start with valid URIs */ | |
54 | { HTTP_URI_OK, "file:/filename", | |
55 | "file", "", "", "/filename", 0, 0 }, | |
56 | { HTTP_URI_OK, "file:/filename%20with%20spaces", | |
57 | "file", "", "", "/filename with spaces", 0, 0 }, | |
58 | { HTTP_URI_OK, "file:///filename", | |
59 | "file", "", "", "/filename", 0, 0 }, | |
60 | { HTTP_URI_OK, "file:///filename%20with%20spaces", | |
61 | "file", "", "", "/filename with spaces", 0, 0 }, | |
62 | { HTTP_URI_OK, "file://localhost/filename", | |
63 | "file", "", "localhost", "/filename", 0, 0 }, | |
64 | { HTTP_URI_OK, "file://localhost/filename%20with%20spaces", | |
65 | "file", "", "localhost", "/filename with spaces", 0, 0 }, | |
66 | { HTTP_URI_OK, "http://server/", | |
67 | "http", "", "server", "/", 80, 0 }, | |
68 | { HTTP_URI_OK, "http://username@server/", | |
69 | "http", "username", "server", "/", 80, 0 }, | |
70 | { HTTP_URI_OK, "http://username:passwor%64@server/", | |
71 | "http", "username:password", "server", "/", 80, 0 }, | |
72 | { HTTP_URI_OK, "http://username:passwor%64@server:8080/", | |
73 | "http", "username:password", "server", "/", 8080, 8080 }, | |
74 | { HTTP_URI_OK, "http://username:passwor%64@server:8080/directory/filename", | |
75 | "http", "username:password", "server", "/directory/filename", 8080, 8080 }, | |
2abf387c | 76 | { HTTP_URI_OK, "http://[2000::10:100]:631/ipp", |
77 | "http", "", "2000::10:100", "/ipp", 631, 631 }, | |
ef416fc2 | 78 | { HTTP_URI_OK, "https://username:passwor%64@server/directory/filename", |
79 | "https", "username:password", "server", "/directory/filename", 443, 0 }, | |
80 | { HTTP_URI_OK, "ipp://username:passwor%64@[::1]/ipp", | |
81 | "ipp", "username:password", "::1", "/ipp", 631, 0 }, | |
82 | { HTTP_URI_OK, "lpd://server/queue?reserve=yes", | |
83 | "lpd", "", "server", "/queue?reserve=yes", 515, 0 }, | |
84 | { HTTP_URI_OK, "mailto:user@domain.com", | |
85 | "mailto", "", "", "user@domain.com", 0, 0 }, | |
86 | { HTTP_URI_OK, "socket://server/", | |
87 | "socket", "", "server", "/", 9100, 0 }, | |
80ca4592 | 88 | { HTTP_URI_OK, "socket://192.168.1.1:9101/", |
89 | "socket", "", "192.168.1.1", "/", 9101, 9101 }, | |
ef416fc2 | 90 | { HTTP_URI_OK, "ipp://username:password@[v1.fe80::200:1234:5678:9abc+eth0]:999/ipp", |
91 | "ipp", "username:password", "fe80::200:1234:5678:9abc%eth0", "/ipp", 999, 999 }, | |
fa73b229 | 92 | { HTTP_URI_OK, "http://server/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400", |
93 | "http", "", "server", "/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400", 80, 0 }, | |
c9fc04c6 MS |
94 | { HTTP_URI_OK, "lpd://Acme%20Laser%20(01%3A23%3A45).local._tcp._printer/", |
95 | "lpd", "", "Acme Laser (01:23:45).local._tcp._printer", "/", 515, 0 }, | |
7cf5915e MS |
96 | { HTTP_URI_OK, "ipp://HP%20Officejet%204500%20G510n-z%20%40%20Will's%20MacBook%20Pro%2015%22._ipp._tcp.local./", |
97 | "ipp", "", "HP Officejet 4500 G510n-z @ Will's MacBook Pro 15\"._ipp._tcp.local.", "/", 631, 0 }, | |
ef416fc2 | 98 | |
99 | /* Missing scheme */ | |
100 | { HTTP_URI_MISSING_SCHEME, "/path/to/file/index.html", | |
101 | "file", "", "", "/path/to/file/index.html", 0, 0 }, | |
102 | { HTTP_URI_MISSING_SCHEME, "//server/ipp", | |
103 | "ipp", "", "server", "/ipp", 631, 0 }, | |
104 | ||
105 | /* Unknown scheme */ | |
106 | { HTTP_URI_UNKNOWN_SCHEME, "vendor://server/resource", | |
107 | "vendor", "", "server", "/resource", 0, 0 }, | |
108 | ||
109 | /* Missing resource */ | |
110 | { HTTP_URI_MISSING_RESOURCE, "socket://[::192.168.2.1]", | |
111 | "socket", "", "::192.168.2.1", "/", 9100, 0 }, | |
d6ae789d | 112 | { HTTP_URI_MISSING_RESOURCE, "socket://192.168.1.1:9101", |
113 | "socket", "", "192.168.1.1", "/", 9101 }, | |
ef416fc2 | 114 | |
115 | /* Bad URI */ | |
116 | { HTTP_URI_BAD_URI, "", | |
117 | "", "", "", "", 0, 0 }, | |
118 | ||
119 | /* Bad scheme */ | |
120 | { HTTP_URI_BAD_SCHEME, "bad_scheme://server/resource", | |
121 | "", "", "", "", 0, 0 }, | |
122 | ||
123 | /* Bad username */ | |
124 | { HTTP_URI_BAD_USERNAME, "http://username:passwor%6@server/resource", | |
125 | "http", "", "", "", 80, 0 }, | |
126 | ||
127 | /* Bad hostname */ | |
128 | { HTTP_URI_BAD_HOSTNAME, "http://[/::1]/index.html", | |
129 | "http", "", "", "", 80, 0 }, | |
130 | { HTTP_URI_BAD_HOSTNAME, "http://[", | |
131 | "http", "", "", "", 80, 0 }, | |
132 | { HTTP_URI_BAD_HOSTNAME, "http://serve%7/index.html", | |
133 | "http", "", "", "", 80, 0 }, | |
134 | ||
135 | /* Bad port number */ | |
136 | { HTTP_URI_BAD_PORT, "http://127.0.0.1:9999a/index.html", | |
137 | "http", "", "127.0.0.1", "", 0, 0 }, | |
138 | ||
139 | /* Bad resource */ | |
140 | { HTTP_URI_BAD_RESOURCE, "http://server/index.html%", | |
141 | "http", "", "server", "", 80, 0 } | |
142 | }; | |
143 | static const char * const base64_tests[][2] = | |
144 | { | |
145 | { "A", "QQ==" }, | |
146 | /* 010000 01 */ | |
147 | { "AB", "QUI=" }, | |
148 | /* 010000 010100 0010 */ | |
149 | { "ABC", "QUJD" }, | |
150 | /* 010000 010100 001001 000011 */ | |
151 | { "ABCD", "QUJDRA==" }, | |
152 | /* 010000 010100 001001 000011 010001 00 */ | |
153 | { "ABCDE", "QUJDREU=" }, | |
154 | /* 010000 010100 001001 000011 010001 000100 0101 */ | |
155 | { "ABCDEF", "QUJDREVG" }, | |
156 | /* 010000 010100 001001 000011 010001 000100 010101 000110 */ | |
157 | }; | |
158 | ||
159 | ||
160 | /* | |
161 | * 'main()' - Main entry. | |
162 | */ | |
163 | ||
164 | int /* O - Exit status */ | |
165 | main(int argc, /* I - Number of command-line arguments */ | |
166 | char *argv[]) /* I - Command-line arguments */ | |
167 | { | |
168 | int i, j, k; /* Looping vars */ | |
169 | http_t *http; /* HTTP connection */ | |
170 | http_status_t status; /* Status of GET command */ | |
171 | int failures; /* Number of test failures */ | |
172 | char buffer[8192]; /* Input buffer */ | |
173 | long bytes; /* Number of bytes read */ | |
174 | FILE *out; /* Output file */ | |
175 | char encode[256], /* Base64-encoded string */ | |
176 | decode[256]; /* Base64-decoded string */ | |
177 | int decodelen; /* Length of decoded string */ | |
178 | char scheme[HTTP_MAX_URI], /* Scheme from URI */ | |
179 | hostname[HTTP_MAX_URI], /* Hostname from URI */ | |
180 | username[HTTP_MAX_URI], /* Username:password from URI */ | |
181 | resource[HTTP_MAX_URI]; /* Resource from URI */ | |
182 | int port; /* Port number from URI */ | |
183 | http_uri_status_t uri_status; /* Status of URI separation */ | |
184 | http_addrlist_t *addrlist, /* Address list */ | |
185 | *addr; /* Current address */ | |
186 | off_t length, total; /* Length and total bytes */ | |
187 | time_t start, current; /* Start and end time */ | |
188 | static const char * const uri_status_strings[] = | |
189 | { | |
190 | "HTTP_URI_OVERFLOW", | |
191 | "HTTP_URI_BAD_ARGUMENTS", | |
192 | "HTTP_URI_BAD_RESOURCE", | |
193 | "HTTP_URI_BAD_PORT", | |
194 | "HTTP_URI_BAD_HOSTNAME", | |
195 | "HTTP_URI_BAD_USERNAME", | |
196 | "HTTP_URI_BAD_SCHEME", | |
197 | "HTTP_URI_BAD_URI", | |
198 | "HTTP_URI_OK", | |
199 | "HTTP_URI_MISSING_SCHEME", | |
200 | "HTTP_URI_UNKNOWN_SCHEME", | |
201 | "HTTP_URI_MISSING_RESOURCE" | |
202 | }; | |
203 | ||
204 | ||
205 | /* | |
206 | * Do API tests if we don't have a URL on the command-line... | |
207 | */ | |
208 | ||
209 | if (argc == 1) | |
210 | { | |
211 | failures = 0; | |
212 | ||
213 | /* | |
214 | * httpGetDateString()/httpGetDateTime() | |
215 | */ | |
216 | ||
217 | fputs("httpGetDateString()/httpGetDateTime(): ", stdout); | |
218 | ||
219 | start = time(NULL); | |
220 | strcpy(buffer, httpGetDateString(start)); | |
221 | current = httpGetDateTime(buffer); | |
222 | ||
223 | i = (int)(current - start); | |
224 | if (i < 0) | |
225 | i = -i; | |
226 | ||
227 | if (!i) | |
228 | puts("PASS"); | |
229 | else | |
230 | { | |
231 | failures ++; | |
232 | puts("FAIL"); | |
233 | printf(" Difference is %d seconds, %02d:%02d:%02d...\n", i, i / 3600, | |
234 | (i / 60) % 60, i % 60); | |
235 | printf(" httpGetDateString(%d) returned \"%s\"\n", (int)start, buffer); | |
236 | printf(" httpGetDateTime(\"%s\") returned %d\n", buffer, (int)current); | |
237 | printf(" httpGetDateString(%d) returned \"%s\"\n", (int)current, | |
238 | httpGetDateString(current)); | |
239 | } | |
240 | ||
241 | /* | |
242 | * httpDecode64_2()/httpEncode64_2() | |
243 | */ | |
244 | ||
245 | fputs("httpDecode64_2()/httpEncode64_2(): ", stdout); | |
246 | ||
247 | for (i = 0, j = 0; i < (int)(sizeof(base64_tests) / sizeof(base64_tests[0])); i ++) | |
248 | { | |
249 | httpEncode64_2(encode, sizeof(encode), base64_tests[i][0], | |
7a0cbd5e | 250 | (int)strlen(base64_tests[i][0])); |
ef416fc2 | 251 | decodelen = (int)sizeof(decode); |
252 | httpDecode64_2(decode, &decodelen, base64_tests[i][1]); | |
253 | ||
254 | if (strcmp(decode, base64_tests[i][0])) | |
255 | { | |
256 | failures ++; | |
257 | ||
258 | if (j) | |
259 | { | |
260 | puts("FAIL"); | |
261 | j = 1; | |
262 | } | |
263 | ||
264 | printf(" httpDecode64_2() returned \"%s\", expected \"%s\"...\n", | |
265 | decode, base64_tests[i][0]); | |
266 | } | |
267 | ||
268 | if (strcmp(encode, base64_tests[i][1])) | |
269 | { | |
270 | failures ++; | |
271 | ||
272 | if (j) | |
273 | { | |
274 | puts("FAIL"); | |
275 | j = 1; | |
276 | } | |
277 | ||
278 | printf(" httpEncode64_2() returned \"%s\", expected \"%s\"...\n", | |
279 | encode, base64_tests[i][1]); | |
280 | } | |
281 | } | |
282 | ||
283 | if (!j) | |
284 | puts("PASS"); | |
285 | ||
286 | /* | |
287 | * httpGetHostname() | |
288 | */ | |
289 | ||
290 | fputs("httpGetHostname(): ", stdout); | |
291 | ||
757d2cad | 292 | if (httpGetHostname(NULL, hostname, sizeof(hostname))) |
ef416fc2 | 293 | printf("PASS (%s)\n", hostname); |
294 | else | |
295 | { | |
296 | failures ++; | |
297 | puts("FAIL"); | |
298 | } | |
299 | ||
300 | /* | |
301 | * httpAddrGetList() | |
302 | */ | |
303 | ||
75bd9771 | 304 | printf("httpAddrGetList(%s): ", hostname); |
ef416fc2 | 305 | |
306 | addrlist = httpAddrGetList(hostname, AF_UNSPEC, NULL); | |
307 | if (addrlist) | |
308 | { | |
ed486911 | 309 | for (i = 0, addr = addrlist; addr; i ++, addr = addr->next) |
310 | { | |
311 | char numeric[1024]; /* Numeric IP address */ | |
312 | ||
313 | ||
314 | httpAddrString(&(addr->addr), numeric, sizeof(numeric)); | |
315 | if (!strcmp(numeric, "UNKNOWN")) | |
316 | break; | |
317 | } | |
318 | ||
319 | if (addr) | |
320 | printf("FAIL (bad address for %s)\n", hostname); | |
321 | else | |
322 | printf("PASS (%d address(es) for %s)\n", i, hostname); | |
ef416fc2 | 323 | |
ef416fc2 | 324 | httpAddrFreeList(addrlist); |
325 | } | |
58dc1933 MS |
326 | else if (isdigit(hostname[0] & 255)) |
327 | { | |
328 | puts("FAIL (ignored because hostname is numeric)"); | |
329 | } | |
ef416fc2 | 330 | else |
331 | { | |
332 | failures ++; | |
333 | puts("FAIL"); | |
334 | } | |
335 | ||
336 | /* | |
337 | * Test httpSeparateURI()... | |
338 | */ | |
339 | ||
340 | fputs("httpSeparateURI(): ", stdout); | |
341 | for (i = 0, j = 0; i < (int)(sizeof(uri_tests) / sizeof(uri_tests[0])); i ++) | |
342 | { | |
a4d04587 | 343 | uri_status = httpSeparateURI(HTTP_URI_CODING_MOST, |
344 | uri_tests[i].uri, scheme, sizeof(scheme), | |
ef416fc2 | 345 | username, sizeof(username), |
346 | hostname, sizeof(hostname), &port, | |
347 | resource, sizeof(resource)); | |
348 | if (uri_status != uri_tests[i].result || | |
349 | strcmp(scheme, uri_tests[i].scheme) || | |
350 | strcmp(username, uri_tests[i].username) || | |
351 | strcmp(hostname, uri_tests[i].hostname) || | |
352 | port != uri_tests[i].port || | |
353 | strcmp(resource, uri_tests[i].resource)) | |
354 | { | |
355 | failures ++; | |
356 | ||
357 | if (!j) | |
358 | { | |
359 | puts("FAIL"); | |
360 | j = 1; | |
361 | } | |
362 | ||
363 | printf(" \"%s\":\n", uri_tests[i].uri); | |
364 | ||
365 | if (uri_status != uri_tests[i].result) | |
366 | printf(" Returned %s instead of %s\n", | |
367 | uri_status_strings[uri_status + 8], | |
368 | uri_status_strings[uri_tests[i].result + 8]); | |
369 | ||
370 | if (strcmp(scheme, uri_tests[i].scheme)) | |
371 | printf(" Scheme \"%s\" instead of \"%s\"\n", | |
372 | scheme, uri_tests[i].scheme); | |
373 | ||
374 | if (strcmp(username, uri_tests[i].username)) | |
375 | printf(" Username \"%s\" instead of \"%s\"\n", | |
376 | username, uri_tests[i].username); | |
377 | ||
378 | if (strcmp(hostname, uri_tests[i].hostname)) | |
379 | printf(" Hostname \"%s\" instead of \"%s\"\n", | |
380 | hostname, uri_tests[i].hostname); | |
381 | ||
382 | if (port != uri_tests[i].port) | |
383 | printf(" Port %d instead of %d\n", | |
384 | port, uri_tests[i].port); | |
385 | ||
386 | if (strcmp(resource, uri_tests[i].resource)) | |
387 | printf(" Resource \"%s\" instead of \"%s\"\n", | |
388 | resource, uri_tests[i].resource); | |
389 | } | |
390 | } | |
391 | ||
392 | if (!j) | |
393 | printf("PASS (%d URIs tested)\n", | |
394 | (int)(sizeof(uri_tests) / sizeof(uri_tests[0]))); | |
395 | ||
396 | /* | |
397 | * Test httpAssembleURI()... | |
398 | */ | |
399 | ||
400 | fputs("httpAssembleURI(): ", stdout); | |
401 | for (i = 0, j = 0, k = 0; | |
402 | i < (int)(sizeof(uri_tests) / sizeof(uri_tests[0])); | |
403 | i ++) | |
404 | if (uri_tests[i].result == HTTP_URI_OK && | |
405 | !strstr(uri_tests[i].uri, "%64") && | |
406 | strstr(uri_tests[i].uri, "//")) | |
407 | { | |
408 | k ++; | |
a4d04587 | 409 | uri_status = httpAssembleURI(HTTP_URI_CODING_MOST, |
410 | buffer, sizeof(buffer), | |
ef416fc2 | 411 | uri_tests[i].scheme, |
412 | uri_tests[i].username, | |
413 | uri_tests[i].hostname, | |
414 | uri_tests[i].assemble_port, | |
415 | uri_tests[i].resource); | |
416 | ||
417 | if (uri_status != HTTP_URI_OK) | |
418 | { | |
419 | failures ++; | |
420 | ||
421 | if (!j) | |
422 | { | |
423 | puts("FAIL"); | |
424 | j = 1; | |
425 | } | |
426 | ||
427 | printf(" \"%s\": %s\n", uri_tests[i].uri, | |
428 | uri_status_strings[uri_status + 8]); | |
429 | } | |
430 | else if (strcmp(buffer, uri_tests[i].uri)) | |
431 | { | |
432 | failures ++; | |
433 | ||
434 | if (!j) | |
435 | { | |
436 | puts("FAIL"); | |
437 | j = 1; | |
438 | } | |
439 | ||
440 | printf(" \"%s\": assembled = \"%s\"\n", uri_tests[i].uri, | |
441 | buffer); | |
442 | } | |
443 | } | |
444 | ||
445 | if (!j) | |
446 | printf("PASS (%d URIs tested)\n", k); | |
447 | ||
448 | /* | |
449 | * Show a summary and return... | |
450 | */ | |
451 | ||
452 | if (failures) | |
453 | printf("\n%d TESTS FAILED!\n", failures); | |
454 | else | |
455 | puts("\nALL TESTS PASSED!"); | |
456 | ||
457 | return (failures); | |
458 | } | |
1f0275e3 MS |
459 | else if (strstr(argv[1], "._tcp")) |
460 | { | |
461 | /* | |
462 | * Test resolving an mDNS name. | |
463 | */ | |
464 | ||
465 | char resolved[1024]; /* Resolved URI */ | |
466 | ||
467 | ||
468 | printf("_httpResolveURI(%s): ", argv[1]); | |
469 | fflush(stdout); | |
470 | ||
07ed0e9a | 471 | if (!_httpResolveURI(argv[1], resolved, sizeof(resolved), 1, NULL, NULL)) |
1f0275e3 MS |
472 | { |
473 | puts("FAIL"); | |
474 | return (1); | |
475 | } | |
476 | else | |
477 | { | |
478 | printf("PASS (%s)\n", resolved); | |
479 | return (0); | |
480 | } | |
481 | } | |
dfd5680b MS |
482 | else if (!strcmp(argv[1], "-u") && argc == 3) |
483 | { | |
484 | /* | |
485 | * Test URI separation... | |
486 | */ | |
487 | ||
488 | uri_status = httpSeparateURI(HTTP_URI_CODING_ALL, argv[2], scheme, | |
489 | sizeof(scheme), username, sizeof(username), | |
490 | hostname, sizeof(hostname), &port, | |
491 | resource, sizeof(resource)); | |
492 | printf("uri_status = %s\n", uri_status_strings[uri_status + 8]); | |
493 | printf("scheme = \"%s\"\n", scheme); | |
494 | printf("username = \"%s\"\n", username); | |
495 | printf("hostname = \"%s\"\n", hostname); | |
496 | printf("port = %d\n", port); | |
497 | printf("resource = \"%s\"\n", resource); | |
498 | ||
499 | return (0); | |
500 | } | |
ef416fc2 | 501 | |
502 | /* | |
503 | * Test HTTP GET requests... | |
504 | */ | |
505 | ||
506 | http = NULL; | |
507 | out = stdout; | |
508 | ||
509 | for (i = 1; i < argc; i ++) | |
510 | { | |
511 | if (!strcmp(argv[i], "-o")) | |
512 | { | |
513 | i ++; | |
514 | if (i >= argc) | |
515 | break; | |
516 | ||
517 | out = fopen(argv[i], "wb"); | |
518 | continue; | |
519 | } | |
520 | ||
a4d04587 | 521 | httpSeparateURI(HTTP_URI_CODING_MOST, argv[i], scheme, sizeof(scheme), |
522 | username, sizeof(username), | |
ef416fc2 | 523 | hostname, sizeof(hostname), &port, |
524 | resource, sizeof(resource)); | |
525 | ||
526 | http = httpConnectEncrypt(hostname, port, HTTP_ENCRYPT_IF_REQUESTED); | |
527 | if (http == NULL) | |
528 | { | |
529 | perror(hostname); | |
530 | continue; | |
531 | } | |
532 | printf("Requesting file \"%s\"...\n", resource); | |
533 | httpClearFields(http); | |
534 | httpSetField(http, HTTP_FIELD_ACCEPT_LANGUAGE, "en"); | |
535 | httpGet(http, resource); | |
536 | while ((status = httpUpdate(http)) == HTTP_CONTINUE); | |
537 | ||
538 | if (status == HTTP_OK) | |
539 | puts("GET OK:"); | |
540 | else | |
541 | printf("GET failed with status %d...\n", status); | |
542 | ||
543 | ||
544 | start = time(NULL); | |
545 | length = httpGetLength2(http); | |
546 | total = 0; | |
547 | ||
a4d04587 | 548 | while ((bytes = httpRead2(http, buffer, sizeof(buffer))) > 0) |
ef416fc2 | 549 | { |
550 | total += bytes; | |
551 | fwrite(buffer, bytes, 1, out); | |
552 | if (out != stdout) | |
553 | { | |
554 | current = time(NULL); | |
555 | if (current == start) current ++; | |
556 | printf("\r" CUPS_LLFMT "/" CUPS_LLFMT " bytes (" | |
e1d6a774 | 557 | CUPS_LLFMT " bytes/sec) ", CUPS_LLCAST total, |
558 | CUPS_LLCAST length, CUPS_LLCAST (total / (current - start))); | |
ef416fc2 | 559 | fflush(stdout); |
560 | } | |
561 | } | |
562 | } | |
563 | ||
564 | puts("Closing connection to server..."); | |
565 | httpClose(http); | |
566 | ||
567 | if (out != stdout) | |
568 | fclose(out); | |
569 | ||
570 | return (0); | |
571 | } | |
572 | ||
573 | ||
574 | /* | |
b19ccc9e | 575 | * End of "$Id: testhttp.c 7742 2008-07-15 20:23:09Z mike $". |
ef416fc2 | 576 | */ |