]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/testhttp.c
Reconnect as needed prior to sending a GET, HEAD, or PUT request.
[thirdparty/cups.git] / cups / testhttp.c
1 /*
2 * "$Id$"
3 *
4 * HTTP test program for CUPS.
5 *
6 * Copyright 2007-2013 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
8 *
9 * These coded instructions, statements, and computer programs are the
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/".
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
26 #include "cups-private.h"
27
28
29 /*
30 * Types and structures...
31 */
32
33 typedef struct uri_test_s /**** URI test cases ****/
34 {
35 http_uri_status_t result; /* Expected return value */
36 const char *uri, /* URI */
37 *scheme, /* Scheme string */
38 *username, /* Username:password string */
39 *hostname, /* Hostname string */
40 *resource; /* Resource string */
41 int port, /* Port number */
42 assemble_port, /* Port number for httpAssembleURI() */
43 assemble_coding;/* Coding 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_STATUS_OK, "file:/filename",
55 "file", "", "", "/filename", 0, 0,
56 HTTP_URI_CODING_MOST },
57 { HTTP_URI_STATUS_OK, "file:/filename%20with%20spaces",
58 "file", "", "", "/filename with spaces", 0, 0,
59 HTTP_URI_CODING_MOST },
60 { HTTP_URI_STATUS_OK, "file:///filename",
61 "file", "", "", "/filename", 0, 0,
62 HTTP_URI_CODING_MOST },
63 { HTTP_URI_STATUS_OK, "file:///filename%20with%20spaces",
64 "file", "", "", "/filename with spaces", 0, 0,
65 HTTP_URI_CODING_MOST },
66 { HTTP_URI_STATUS_OK, "file://localhost/filename",
67 "file", "", "localhost", "/filename", 0, 0,
68 HTTP_URI_CODING_MOST },
69 { HTTP_URI_STATUS_OK, "file://localhost/filename%20with%20spaces",
70 "file", "", "localhost", "/filename with spaces", 0, 0,
71 HTTP_URI_CODING_MOST },
72 { HTTP_URI_STATUS_OK, "http://server/",
73 "http", "", "server", "/", 80, 0,
74 HTTP_URI_CODING_MOST },
75 { HTTP_URI_STATUS_OK, "http://username@server/",
76 "http", "username", "server", "/", 80, 0,
77 HTTP_URI_CODING_MOST },
78 { HTTP_URI_STATUS_OK, "http://username:passwor%64@server/",
79 "http", "username:password", "server", "/", 80, 0,
80 HTTP_URI_CODING_MOST },
81 { HTTP_URI_STATUS_OK, "http://username:passwor%64@server:8080/",
82 "http", "username:password", "server", "/", 8080, 8080,
83 HTTP_URI_CODING_MOST },
84 { HTTP_URI_STATUS_OK, "http://username:passwor%64@server:8080/directory/filename",
85 "http", "username:password", "server", "/directory/filename", 8080, 8080,
86 HTTP_URI_CODING_MOST },
87 { HTTP_URI_STATUS_OK, "http://[2000::10:100]:631/ipp",
88 "http", "", "2000::10:100", "/ipp", 631, 631,
89 HTTP_URI_CODING_MOST },
90 { HTTP_URI_STATUS_OK, "https://username:passwor%64@server/directory/filename",
91 "https", "username:password", "server", "/directory/filename", 443, 0,
92 HTTP_URI_CODING_MOST },
93 { HTTP_URI_STATUS_OK, "ipp://username:passwor%64@[::1]/ipp",
94 "ipp", "username:password", "::1", "/ipp", 631, 0,
95 HTTP_URI_CODING_MOST },
96 { HTTP_URI_STATUS_OK, "lpd://server/queue?reserve=yes",
97 "lpd", "", "server", "/queue?reserve=yes", 515, 0,
98 HTTP_URI_CODING_MOST },
99 { HTTP_URI_STATUS_OK, "mailto:user@domain.com",
100 "mailto", "", "", "user@domain.com", 0, 0,
101 HTTP_URI_CODING_MOST },
102 { HTTP_URI_STATUS_OK, "socket://server/",
103 "socket", "", "server", "/", 9100, 0,
104 HTTP_URI_CODING_MOST },
105 { HTTP_URI_STATUS_OK, "socket://192.168.1.1:9101/",
106 "socket", "", "192.168.1.1", "/", 9101, 9101,
107 HTTP_URI_CODING_MOST },
108 { HTTP_URI_STATUS_OK, "ipp://username:password@[v1.fe80::200:1234:5678:9abc+eth0]:999/ipp",
109 "ipp", "username:password", "fe80::200:1234:5678:9abc%eth0", "/ipp", 999, 999,
110 HTTP_URI_CODING_MOST },
111 { HTTP_URI_STATUS_OK, "ipp://username:password@[fe80::200:1234:5678:9abc%25eth0]:999/ipp",
112 "ipp", "username:password", "fe80::200:1234:5678:9abc%eth0", "/ipp", 999, 999,
113 HTTP_URI_CODING_MOST | HTTP_URI_CODING_RFC6874 },
114 { HTTP_URI_STATUS_OK, "http://server/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400",
115 "http", "", "server", "/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400", 80, 0,
116 HTTP_URI_CODING_MOST },
117 { HTTP_URI_STATUS_OK, "lpd://Acme%20Laser%20(01%3A23%3A45).local._tcp._printer/",
118 "lpd", "", "Acme Laser (01:23:45).local._tcp._printer", "/", 515, 0,
119 HTTP_URI_CODING_MOST },
120 { HTTP_URI_STATUS_OK, "ipp://HP%20Officejet%204500%20G510n-z%20%40%20Will's%20MacBook%20Pro%2015%22._ipp._tcp.local./",
121 "ipp", "", "HP Officejet 4500 G510n-z @ Will's MacBook Pro 15\"._ipp._tcp.local.", "/", 631, 0,
122 HTTP_URI_CODING_MOST },
123 { HTTP_URI_STATUS_OK, "ipp://%22%23%2F%3A%3C%3E%3F%40%5B%5C%5D%5E%60%7B%7C%7D/",
124 "ipp", "", "\"#/:<>?@[\\]^`{|}", "/", 631, 0,
125 HTTP_URI_CODING_MOST },
126
127 /* Missing scheme */
128 { HTTP_URI_STATUS_MISSING_SCHEME, "/path/to/file/index.html",
129 "file", "", "", "/path/to/file/index.html", 0, 0,
130 HTTP_URI_CODING_MOST },
131 { HTTP_URI_STATUS_MISSING_SCHEME, "//server/ipp",
132 "ipp", "", "server", "/ipp", 631, 0,
133 HTTP_URI_CODING_MOST },
134
135 /* Unknown scheme */
136 { HTTP_URI_STATUS_UNKNOWN_SCHEME, "vendor://server/resource",
137 "vendor", "", "server", "/resource", 0, 0,
138 HTTP_URI_CODING_MOST },
139
140 /* Missing resource */
141 { HTTP_URI_STATUS_MISSING_RESOURCE, "socket://[::192.168.2.1]",
142 "socket", "", "::192.168.2.1", "/", 9100, 0,
143 HTTP_URI_CODING_MOST },
144 { HTTP_URI_STATUS_MISSING_RESOURCE, "socket://192.168.1.1:9101",
145 "socket", "", "192.168.1.1", "/", 9101, 0,
146 HTTP_URI_CODING_MOST },
147
148 /* Bad URI */
149 { HTTP_URI_STATUS_BAD_URI, "",
150 "", "", "", "", 0, 0,
151 HTTP_URI_CODING_MOST },
152
153 /* Bad scheme */
154 { HTTP_URI_STATUS_BAD_SCHEME, "bad_scheme://server/resource",
155 "", "", "", "", 0, 0,
156 HTTP_URI_CODING_MOST },
157
158 /* Bad username */
159 { HTTP_URI_STATUS_BAD_USERNAME, "http://username:passwor%6@server/resource",
160 "http", "", "", "", 80, 0,
161 HTTP_URI_CODING_MOST },
162
163 /* Bad hostname */
164 { HTTP_URI_STATUS_BAD_HOSTNAME, "http://[/::1]/index.html",
165 "http", "", "", "", 80, 0,
166 HTTP_URI_CODING_MOST },
167 { HTTP_URI_STATUS_BAD_HOSTNAME, "http://[",
168 "http", "", "", "", 80, 0,
169 HTTP_URI_CODING_MOST },
170 { HTTP_URI_STATUS_BAD_HOSTNAME, "http://serve%7/index.html",
171 "http", "", "", "", 80, 0,
172 HTTP_URI_CODING_MOST },
173 { HTTP_URI_STATUS_BAD_HOSTNAME, "http://server with spaces/index.html",
174 "http", "", "", "", 80, 0,
175 HTTP_URI_CODING_MOST },
176 { HTTP_URI_STATUS_BAD_HOSTNAME, "ipp://\"#/:<>?@[\\]^`{|}/",
177 "ipp", "", "", "", 631, 0,
178 HTTP_URI_CODING_MOST },
179
180 /* Bad port number */
181 { HTTP_URI_STATUS_BAD_PORT, "http://127.0.0.1:9999a/index.html",
182 "http", "", "127.0.0.1", "", 0, 0,
183 HTTP_URI_CODING_MOST },
184
185 /* Bad resource */
186 { HTTP_URI_STATUS_BAD_RESOURCE, "http://server/index.html%",
187 "http", "", "server", "", 80, 0,
188 HTTP_URI_CODING_MOST },
189 { HTTP_URI_STATUS_BAD_RESOURCE, "http://server/index with spaces.html",
190 "http", "", "server", "", 80, 0,
191 HTTP_URI_CODING_MOST }
192 };
193 static const char * const base64_tests[][2] =
194 {
195 { "A", "QQ==" },
196 /* 010000 01 */
197 { "AB", "QUI=" },
198 /* 010000 010100 0010 */
199 { "ABC", "QUJD" },
200 /* 010000 010100 001001 000011 */
201 { "ABCD", "QUJDRA==" },
202 /* 010000 010100 001001 000011 010001 00 */
203 { "ABCDE", "QUJDREU=" },
204 /* 010000 010100 001001 000011 010001 000100 0101 */
205 { "ABCDEF", "QUJDREVG" },
206 /* 010000 010100 001001 000011 010001 000100 010101 000110 */
207 };
208
209
210 /*
211 * 'main()' - Main entry.
212 */
213
214 int /* O - Exit status */
215 main(int argc, /* I - Number of command-line arguments */
216 char *argv[]) /* I - Command-line arguments */
217 {
218 int i, j, k; /* Looping vars */
219 http_t *http; /* HTTP connection */
220 http_encryption_t encryption; /* Encryption type */
221 http_status_t status; /* Status of GET command */
222 int failures; /* Number of test failures */
223 char buffer[8192]; /* Input buffer */
224 long bytes; /* Number of bytes read */
225 FILE *out; /* Output file */
226 char encode[256], /* Base64-encoded string */
227 decode[256]; /* Base64-decoded string */
228 int decodelen; /* Length of decoded string */
229 char scheme[HTTP_MAX_URI], /* Scheme from URI */
230 hostname[HTTP_MAX_URI], /* Hostname from URI */
231 username[HTTP_MAX_URI], /* Username:password from URI */
232 resource[HTTP_MAX_URI]; /* Resource from URI */
233 int port; /* Port number from URI */
234 http_uri_status_t uri_status; /* Status of URI separation */
235 http_addrlist_t *addrlist, /* Address list */
236 *addr; /* Current address */
237 off_t length, total; /* Length and total bytes */
238 time_t start, current; /* Start and end time */
239 const char *encoding; /* Negotiated Content-Encoding */
240 static const char * const uri_status_strings[] =
241 {
242 "HTTP_URI_STATUS_OVERFLOW",
243 "HTTP_URI_STATUS_BAD_ARGUMENTS",
244 "HTTP_URI_STATUS_BAD_RESOURCE",
245 "HTTP_URI_STATUS_BAD_PORT",
246 "HTTP_URI_STATUS_BAD_HOSTNAME",
247 "HTTP_URI_STATUS_BAD_USERNAME",
248 "HTTP_URI_STATUS_BAD_SCHEME",
249 "HTTP_URI_STATUS_BAD_URI",
250 "HTTP_URI_STATUS_OK",
251 "HTTP_URI_STATUS_MISSING_SCHEME",
252 "HTTP_URI_STATUS_UNKNOWN_SCHEME",
253 "HTTP_URI_STATUS_MISSING_RESOURCE"
254 };
255
256
257 /*
258 * Do API tests if we don't have a URL on the command-line...
259 */
260
261 if (argc == 1)
262 {
263 failures = 0;
264
265 /*
266 * httpGetDateString()/httpGetDateTime()
267 */
268
269 fputs("httpGetDateString()/httpGetDateTime(): ", stdout);
270
271 start = time(NULL);
272 strlcpy(buffer, httpGetDateString(start), sizeof(buffer));
273 current = httpGetDateTime(buffer);
274
275 i = (int)(current - start);
276 if (i < 0)
277 i = -i;
278
279 if (!i)
280 puts("PASS");
281 else
282 {
283 failures ++;
284 puts("FAIL");
285 printf(" Difference is %d seconds, %02d:%02d:%02d...\n", i, i / 3600,
286 (i / 60) % 60, i % 60);
287 printf(" httpGetDateString(%d) returned \"%s\"\n", (int)start, buffer);
288 printf(" httpGetDateTime(\"%s\") returned %d\n", buffer, (int)current);
289 printf(" httpGetDateString(%d) returned \"%s\"\n", (int)current,
290 httpGetDateString(current));
291 }
292
293 /*
294 * httpDecode64_2()/httpEncode64_2()
295 */
296
297 fputs("httpDecode64_2()/httpEncode64_2(): ", stdout);
298
299 for (i = 0, j = 0; i < (int)(sizeof(base64_tests) / sizeof(base64_tests[0])); i ++)
300 {
301 httpEncode64_2(encode, sizeof(encode), base64_tests[i][0],
302 (int)strlen(base64_tests[i][0]));
303 decodelen = (int)sizeof(decode);
304 httpDecode64_2(decode, &decodelen, base64_tests[i][1]);
305
306 if (strcmp(decode, base64_tests[i][0]))
307 {
308 failures ++;
309
310 if (j)
311 {
312 puts("FAIL");
313 j = 1;
314 }
315
316 printf(" httpDecode64_2() returned \"%s\", expected \"%s\"...\n",
317 decode, base64_tests[i][0]);
318 }
319
320 if (strcmp(encode, base64_tests[i][1]))
321 {
322 failures ++;
323
324 if (j)
325 {
326 puts("FAIL");
327 j = 1;
328 }
329
330 printf(" httpEncode64_2() returned \"%s\", expected \"%s\"...\n",
331 encode, base64_tests[i][1]);
332 }
333 }
334
335 if (!j)
336 puts("PASS");
337
338 /*
339 * httpGetHostname()
340 */
341
342 fputs("httpGetHostname(): ", stdout);
343
344 if (httpGetHostname(NULL, hostname, sizeof(hostname)))
345 printf("PASS (%s)\n", hostname);
346 else
347 {
348 failures ++;
349 puts("FAIL");
350 }
351
352 /*
353 * httpAddrGetList()
354 */
355
356 printf("httpAddrGetList(%s): ", hostname);
357
358 addrlist = httpAddrGetList(hostname, AF_UNSPEC, NULL);
359 if (addrlist)
360 {
361 for (i = 0, addr = addrlist; addr; i ++, addr = addr->next)
362 {
363 char numeric[1024]; /* Numeric IP address */
364
365
366 httpAddrString(&(addr->addr), numeric, sizeof(numeric));
367 if (!strcmp(numeric, "UNKNOWN"))
368 break;
369 }
370
371 if (addr)
372 printf("FAIL (bad address for %s)\n", hostname);
373 else
374 printf("PASS (%d address(es) for %s)\n", i, hostname);
375
376 httpAddrFreeList(addrlist);
377 }
378 else if (isdigit(hostname[0] & 255))
379 {
380 puts("FAIL (ignored because hostname is numeric)");
381 }
382 else
383 {
384 failures ++;
385 puts("FAIL");
386 }
387
388 /*
389 * Test httpSeparateURI()...
390 */
391
392 fputs("httpSeparateURI(): ", stdout);
393 for (i = 0, j = 0; i < (int)(sizeof(uri_tests) / sizeof(uri_tests[0])); i ++)
394 {
395 uri_status = httpSeparateURI(HTTP_URI_CODING_MOST,
396 uri_tests[i].uri, scheme, sizeof(scheme),
397 username, sizeof(username),
398 hostname, sizeof(hostname), &port,
399 resource, sizeof(resource));
400 if (uri_status != uri_tests[i].result ||
401 strcmp(scheme, uri_tests[i].scheme) ||
402 strcmp(username, uri_tests[i].username) ||
403 strcmp(hostname, uri_tests[i].hostname) ||
404 port != uri_tests[i].port ||
405 strcmp(resource, uri_tests[i].resource))
406 {
407 failures ++;
408
409 if (!j)
410 {
411 puts("FAIL");
412 j = 1;
413 }
414
415 printf(" \"%s\":\n", uri_tests[i].uri);
416
417 if (uri_status != uri_tests[i].result)
418 printf(" Returned %s instead of %s\n",
419 uri_status_strings[uri_status + 8],
420 uri_status_strings[uri_tests[i].result + 8]);
421
422 if (strcmp(scheme, uri_tests[i].scheme))
423 printf(" Scheme \"%s\" instead of \"%s\"\n",
424 scheme, uri_tests[i].scheme);
425
426 if (strcmp(username, uri_tests[i].username))
427 printf(" Username \"%s\" instead of \"%s\"\n",
428 username, uri_tests[i].username);
429
430 if (strcmp(hostname, uri_tests[i].hostname))
431 printf(" Hostname \"%s\" instead of \"%s\"\n",
432 hostname, uri_tests[i].hostname);
433
434 if (port != uri_tests[i].port)
435 printf(" Port %d instead of %d\n",
436 port, uri_tests[i].port);
437
438 if (strcmp(resource, uri_tests[i].resource))
439 printf(" Resource \"%s\" instead of \"%s\"\n",
440 resource, uri_tests[i].resource);
441 }
442 }
443
444 if (!j)
445 printf("PASS (%d URIs tested)\n",
446 (int)(sizeof(uri_tests) / sizeof(uri_tests[0])));
447
448 /*
449 * Test httpAssembleURI()...
450 */
451
452 fputs("httpAssembleURI(): ", stdout);
453 for (i = 0, j = 0, k = 0;
454 i < (int)(sizeof(uri_tests) / sizeof(uri_tests[0]));
455 i ++)
456 if (uri_tests[i].result == HTTP_URI_STATUS_OK &&
457 !strstr(uri_tests[i].uri, "%64") &&
458 strstr(uri_tests[i].uri, "//"))
459 {
460 k ++;
461 uri_status = httpAssembleURI(uri_tests[i].assemble_coding,
462 buffer, sizeof(buffer),
463 uri_tests[i].scheme,
464 uri_tests[i].username,
465 uri_tests[i].hostname,
466 uri_tests[i].assemble_port,
467 uri_tests[i].resource);
468
469 if (uri_status != HTTP_URI_STATUS_OK)
470 {
471 failures ++;
472
473 if (!j)
474 {
475 puts("FAIL");
476 j = 1;
477 }
478
479 printf(" \"%s\": %s\n", uri_tests[i].uri,
480 uri_status_strings[uri_status + 8]);
481 }
482 else if (strcmp(buffer, uri_tests[i].uri))
483 {
484 failures ++;
485
486 if (!j)
487 {
488 puts("FAIL");
489 j = 1;
490 }
491
492 printf(" \"%s\": assembled = \"%s\"\n", uri_tests[i].uri,
493 buffer);
494 }
495 }
496
497 if (!j)
498 printf("PASS (%d URIs tested)\n", k);
499
500 /*
501 * httpAssembleUUID
502 */
503
504 fputs("httpAssembleUUID: ", stdout);
505 httpAssembleUUID("hostname.example.com", 631, "printer", 12345, buffer,
506 sizeof(buffer));
507 if (strncmp(buffer, "urn:uuid:", 9))
508 {
509 printf("FAIL (%s)\n", buffer);
510 failures ++;
511 }
512 else
513 printf("PASS (%s)\n", buffer);
514
515 /*
516 * Show a summary and return...
517 */
518
519 if (failures)
520 printf("\n%d TESTS FAILED!\n", failures);
521 else
522 puts("\nALL TESTS PASSED!");
523
524 return (failures);
525 }
526 else if (strstr(argv[1], "._tcp"))
527 {
528 /*
529 * Test resolving an mDNS name.
530 */
531
532 char resolved[1024]; /* Resolved URI */
533
534
535 printf("_httpResolveURI(%s, _HTTP_RESOLVE_DEFAULT): ", argv[1]);
536 fflush(stdout);
537
538 if (!_httpResolveURI(argv[1], resolved, sizeof(resolved),
539 _HTTP_RESOLVE_DEFAULT, NULL, NULL))
540 {
541 puts("FAIL");
542 return (1);
543 }
544 else
545 printf("PASS (%s)\n", resolved);
546
547 printf("_httpResolveURI(%s, _HTTP_RESOLVE_FQDN): ", argv[1]);
548 fflush(stdout);
549
550 if (!_httpResolveURI(argv[1], resolved, sizeof(resolved),
551 _HTTP_RESOLVE_FQDN, NULL, NULL))
552 {
553 puts("FAIL");
554 return (1);
555 }
556 else if (strstr(resolved, ".local:"))
557 {
558 printf("FAIL (%s)\n", resolved);
559 return (1);
560 }
561 else
562 {
563 printf("PASS (%s)\n", resolved);
564 return (0);
565 }
566 }
567 else if (!strcmp(argv[1], "-u") && argc == 3)
568 {
569 /*
570 * Test URI separation...
571 */
572
573 uri_status = httpSeparateURI(HTTP_URI_CODING_ALL, argv[2], scheme,
574 sizeof(scheme), username, sizeof(username),
575 hostname, sizeof(hostname), &port,
576 resource, sizeof(resource));
577 printf("uri_status = %s\n", uri_status_strings[uri_status + 8]);
578 printf("scheme = \"%s\"\n", scheme);
579 printf("username = \"%s\"\n", username);
580 printf("hostname = \"%s\"\n", hostname);
581 printf("port = %d\n", port);
582 printf("resource = \"%s\"\n", resource);
583
584 return (0);
585 }
586
587 /*
588 * Test HTTP GET requests...
589 */
590
591 http = NULL;
592 out = stdout;
593
594 for (i = 1; i < argc; i ++)
595 {
596 if (!strcmp(argv[i], "-o"))
597 {
598 i ++;
599 if (i >= argc)
600 break;
601
602 out = fopen(argv[i], "wb");
603 continue;
604 }
605
606 httpSeparateURI(HTTP_URI_CODING_MOST, argv[i], scheme, sizeof(scheme),
607 username, sizeof(username),
608 hostname, sizeof(hostname), &port,
609 resource, sizeof(resource));
610
611 if (!_cups_strcasecmp(scheme, "https") || !_cups_strcasecmp(scheme, "ipps") ||
612 port == 443)
613 encryption = HTTP_ENCRYPTION_ALWAYS;
614 else
615 encryption = HTTP_ENCRYPTION_IF_REQUESTED;
616
617 http = httpConnect2(hostname, port, NULL, AF_UNSPEC, encryption, 1, 30000,
618 NULL);
619 if (http == NULL)
620 {
621 perror(hostname);
622 continue;
623 }
624 printf("Checking file \"%s\"...\n", resource);
625
626 do
627 {
628 if (!_cups_strcasecmp(httpGetField(http, HTTP_FIELD_CONNECTION), "close"))
629 {
630 httpClearFields(http);
631 if (httpReconnect2(http, 30000, NULL))
632 {
633 status = HTTP_STATUS_ERROR;
634 break;
635 }
636 }
637
638 httpClearFields(http);
639 httpSetField(http, HTTP_FIELD_AUTHORIZATION, httpGetAuthString(http));
640 httpSetField(http, HTTP_FIELD_ACCEPT_LANGUAGE, "en");
641 if (httpHead(http, resource))
642 {
643 if (httpReconnect2(http, 30000, NULL))
644 {
645 status = HTTP_STATUS_ERROR;
646 break;
647 }
648 else
649 {
650 status = HTTP_STATUS_UNAUTHORIZED;
651 continue;
652 }
653 }
654
655 while ((status = httpUpdate(http)) == HTTP_STATUS_CONTINUE);
656
657 if (status == HTTP_STATUS_UNAUTHORIZED)
658 {
659 /*
660 * Flush any error message...
661 */
662
663 httpFlush(http);
664
665 /*
666 * See if we can do authentication...
667 */
668
669 if (cupsDoAuthentication(http, "GET", resource))
670 {
671 status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED;
672 break;
673 }
674
675 if (httpReconnect2(http, 30000, NULL))
676 {
677 status = HTTP_STATUS_ERROR;
678 break;
679 }
680
681 continue;
682 }
683 #ifdef HAVE_SSL
684 else if (status == HTTP_STATUS_UPGRADE_REQUIRED)
685 {
686 /* Flush any error message... */
687 httpFlush(http);
688
689 /* Reconnect... */
690 if (httpReconnect2(http, 30000, NULL))
691 {
692 status = HTTP_STATUS_ERROR;
693 break;
694 }
695
696 /* Upgrade with encryption... */
697 httpEncryption(http, HTTP_ENCRYPTION_REQUIRED);
698
699 /* Try again, this time with encryption enabled... */
700 continue;
701 }
702 #endif /* HAVE_SSL */
703 }
704 while (status == HTTP_STATUS_UNAUTHORIZED ||
705 status == HTTP_STATUS_UPGRADE_REQUIRED);
706
707 if (status == HTTP_STATUS_OK)
708 puts("HEAD OK:");
709 else
710 printf("HEAD failed with status %d...\n", status);
711
712 encoding = httpGetContentEncoding(http);
713
714 printf("Requesting file \"%s\" (Accept-Encoding: %s)...\n", resource,
715 encoding ? encoding : "identity");
716
717 do
718 {
719 if (!_cups_strcasecmp(httpGetField(http, HTTP_FIELD_CONNECTION), "close"))
720 {
721 httpClearFields(http);
722 if (httpReconnect2(http, 30000, NULL))
723 {
724 status = HTTP_STATUS_ERROR;
725 break;
726 }
727 }
728
729 httpClearFields(http);
730 httpSetField(http, HTTP_FIELD_AUTHORIZATION, httpGetAuthString(http));
731 httpSetField(http, HTTP_FIELD_ACCEPT_LANGUAGE, "en");
732 httpSetField(http, HTTP_FIELD_ACCEPT_ENCODING, encoding);
733
734 if (httpGet(http, resource))
735 {
736 if (httpReconnect2(http, 30000, NULL))
737 {
738 status = HTTP_STATUS_ERROR;
739 break;
740 }
741 else
742 {
743 status = HTTP_STATUS_UNAUTHORIZED;
744 continue;
745 }
746 }
747
748 while ((status = httpUpdate(http)) == HTTP_STATUS_CONTINUE);
749
750 if (status == HTTP_STATUS_UNAUTHORIZED)
751 {
752 /*
753 * Flush any error message...
754 */
755
756 httpFlush(http);
757
758 /*
759 * See if we can do authentication...
760 */
761
762 if (cupsDoAuthentication(http, "GET", resource))
763 {
764 status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED;
765 break;
766 }
767
768 if (httpReconnect2(http, 30000, NULL))
769 {
770 status = HTTP_STATUS_ERROR;
771 break;
772 }
773
774 continue;
775 }
776 #ifdef HAVE_SSL
777 else if (status == HTTP_STATUS_UPGRADE_REQUIRED)
778 {
779 /* Flush any error message... */
780 httpFlush(http);
781
782 /* Reconnect... */
783 if (httpReconnect2(http, 30000, NULL))
784 {
785 status = HTTP_STATUS_ERROR;
786 break;
787 }
788
789 /* Upgrade with encryption... */
790 httpEncryption(http, HTTP_ENCRYPTION_REQUIRED);
791
792 /* Try again, this time with encryption enabled... */
793 continue;
794 }
795 #endif /* HAVE_SSL */
796 }
797 while (status == HTTP_STATUS_UNAUTHORIZED || status == HTTP_STATUS_UPGRADE_REQUIRED);
798
799 if (status == HTTP_STATUS_OK)
800 puts("GET OK:");
801 else
802 printf("GET failed with status %d...\n", status);
803
804 start = time(NULL);
805 length = httpGetLength2(http);
806 total = 0;
807
808 while ((bytes = httpRead2(http, buffer, sizeof(buffer))) > 0)
809 {
810 total += bytes;
811 fwrite(buffer, bytes, 1, out);
812 if (out != stdout)
813 {
814 current = time(NULL);
815 if (current == start) current ++;
816 printf("\r" CUPS_LLFMT "/" CUPS_LLFMT " bytes ("
817 CUPS_LLFMT " bytes/sec) ", CUPS_LLCAST total,
818 CUPS_LLCAST length, CUPS_LLCAST (total / (current - start)));
819 fflush(stdout);
820 }
821 }
822 }
823
824 puts("Closing connection to server...");
825 httpClose(http);
826
827 if (out != stdout)
828 fclose(out);
829
830 return (0);
831 }
832
833
834 /*
835 * End of "$Id$".
836 */