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