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