]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/testhttp.c
Generalize the input validation of some kinds of attributes.
[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 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * missing or damaged, see the license at "http://www.cups.org/".
12 *
13 * This file is subject to the Apple OS-Developed Software exception.
14 */
15
16 /*
17 * Include necessary headers...
18 */
19
20 #include "cups-private.h"
21
22
23 /*
24 * Types and structures...
25 */
26
27 typedef struct uri_test_s /**** URI test cases ****/
28 {
29 http_uri_status_t result; /* Expected return value */
30 const char *uri, /* URI */
31 *scheme, /* Scheme string */
32 *username, /* Username:password string */
33 *hostname, /* Hostname string */
34 *resource; /* Resource string */
35 int port, /* Port number */
36 assemble_port; /* Port number for httpAssembleURI() */
37 http_uri_coding_t assemble_coding;/* Coding for httpAssembleURI() */
38 } uri_test_t;
39
40
41 /*
42 * Local globals...
43 */
44
45 static uri_test_t uri_tests[] = /* URI test data */
46 {
47 /* Start with valid URIs */
48 { HTTP_URI_STATUS_OK, "file:/filename",
49 "file", "", "", "/filename", 0, 0,
50 HTTP_URI_CODING_MOST },
51 { HTTP_URI_STATUS_OK, "file:/filename%20with%20spaces",
52 "file", "", "", "/filename with spaces", 0, 0,
53 HTTP_URI_CODING_MOST },
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://localhost/filename",
61 "file", "", "localhost", "/filename", 0, 0,
62 HTTP_URI_CODING_MOST },
63 { HTTP_URI_STATUS_OK, "file://localhost/filename%20with%20spaces",
64 "file", "", "localhost", "/filename with spaces", 0, 0,
65 HTTP_URI_CODING_MOST },
66 { HTTP_URI_STATUS_OK, "http://server/",
67 "http", "", "server", "/", 80, 0,
68 HTTP_URI_CODING_MOST },
69 { HTTP_URI_STATUS_OK, "http://username@server/",
70 "http", "username", "server", "/", 80, 0,
71 HTTP_URI_CODING_MOST },
72 { HTTP_URI_STATUS_OK, "http://username:passwor%64@server/",
73 "http", "username:password", "server", "/", 80, 0,
74 HTTP_URI_CODING_MOST },
75 { HTTP_URI_STATUS_OK, "http://username:passwor%64@server:8080/",
76 "http", "username:password", "server", "/", 8080, 8080,
77 HTTP_URI_CODING_MOST },
78 { HTTP_URI_STATUS_OK, "http://username:passwor%64@server:8080/directory/filename",
79 "http", "username:password", "server", "/directory/filename", 8080, 8080,
80 HTTP_URI_CODING_MOST },
81 { HTTP_URI_STATUS_OK, "http://[2000::10:100]:631/ipp",
82 "http", "", "2000::10:100", "/ipp", 631, 631,
83 HTTP_URI_CODING_MOST },
84 { HTTP_URI_STATUS_OK, "https://username:passwor%64@server/directory/filename",
85 "https", "username:password", "server", "/directory/filename", 443, 0,
86 HTTP_URI_CODING_MOST },
87 { HTTP_URI_STATUS_OK, "ipp://username:passwor%64@[::1]/ipp",
88 "ipp", "username:password", "::1", "/ipp", 631, 0,
89 HTTP_URI_CODING_MOST },
90 { HTTP_URI_STATUS_OK, "lpd://server/queue?reserve=yes",
91 "lpd", "", "server", "/queue?reserve=yes", 515, 0,
92 HTTP_URI_CODING_MOST },
93 { HTTP_URI_STATUS_OK, "mailto:user@domain.com",
94 "mailto", "", "", "user@domain.com", 0, 0,
95 HTTP_URI_CODING_MOST },
96 { HTTP_URI_STATUS_OK, "socket://server/",
97 "socket", "", "server", "/", 9100, 0,
98 HTTP_URI_CODING_MOST },
99 { HTTP_URI_STATUS_OK, "socket://192.168.1.1:9101/",
100 "socket", "", "192.168.1.1", "/", 9101, 9101,
101 HTTP_URI_CODING_MOST },
102 { HTTP_URI_STATUS_OK, "tel:8005551212",
103 "tel", "", "", "8005551212", 0, 0,
104 HTTP_URI_CODING_MOST },
105 { HTTP_URI_STATUS_OK, "ipp://username:password@[v1.fe80::200:1234:5678:9abc+eth0]:999/ipp",
106 "ipp", "username:password", "fe80::200:1234:5678:9abc%eth0", "/ipp", 999, 999,
107 HTTP_URI_CODING_MOST },
108 { HTTP_URI_STATUS_OK, "ipp://username:password@[fe80::200:1234:5678:9abc%25eth0]:999/ipp",
109 "ipp", "username:password", "fe80::200:1234:5678:9abc%eth0", "/ipp", 999, 999,
110 (http_uri_coding_t)(HTTP_URI_CODING_MOST | HTTP_URI_CODING_RFC6874) },
111 { HTTP_URI_STATUS_OK, "http://server/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400",
112 "http", "", "server", "/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400", 80, 0,
113 HTTP_URI_CODING_MOST },
114 { HTTP_URI_STATUS_OK, "lpd://Acme%20Laser%20(01%3A23%3A45).local._tcp._printer/",
115 "lpd", "", "Acme Laser (01:23:45).local._tcp._printer", "/", 515, 0,
116 HTTP_URI_CODING_MOST },
117 { HTTP_URI_STATUS_OK, "ipp://HP%20Officejet%204500%20G510n-z%20%40%20Will's%20MacBook%20Pro%2015%22._ipp._tcp.local./",
118 "ipp", "", "HP Officejet 4500 G510n-z @ Will's MacBook Pro 15\"._ipp._tcp.local.", "/", 631, 0,
119 HTTP_URI_CODING_MOST },
120 { HTTP_URI_STATUS_OK, "ipp://%22%23%2F%3A%3C%3E%3F%40%5B%5C%5D%5E%60%7B%7C%7D/",
121 "ipp", "", "\"#/:<>?@[\\]^`{|}", "/", 631, 0,
122 HTTP_URI_CODING_MOST },
123
124 /* Missing scheme */
125 { HTTP_URI_STATUS_MISSING_SCHEME, "/path/to/file/index.html",
126 "file", "", "", "/path/to/file/index.html", 0, 0,
127 HTTP_URI_CODING_MOST },
128 { HTTP_URI_STATUS_MISSING_SCHEME, "//server/ipp",
129 "ipp", "", "server", "/ipp", 631, 0,
130 HTTP_URI_CODING_MOST },
131
132 /* Unknown scheme */
133 { HTTP_URI_STATUS_UNKNOWN_SCHEME, "vendor://server/resource",
134 "vendor", "", "server", "/resource", 0, 0,
135 HTTP_URI_CODING_MOST },
136
137 /* Missing resource */
138 { HTTP_URI_STATUS_MISSING_RESOURCE, "socket://[::192.168.2.1]",
139 "socket", "", "::192.168.2.1", "/", 9100, 0,
140 HTTP_URI_CODING_MOST },
141 { HTTP_URI_STATUS_MISSING_RESOURCE, "socket://192.168.1.1:9101",
142 "socket", "", "192.168.1.1", "/", 9101, 0,
143 HTTP_URI_CODING_MOST },
144
145 /* Bad URI */
146 { HTTP_URI_STATUS_BAD_URI, "",
147 "", "", "", "", 0, 0,
148 HTTP_URI_CODING_MOST },
149
150 /* Bad scheme */
151 { HTTP_URI_STATUS_BAD_SCHEME, "bad_scheme://server/resource",
152 "", "", "", "", 0, 0,
153 HTTP_URI_CODING_MOST },
154
155 /* Bad username */
156 { HTTP_URI_STATUS_BAD_USERNAME, "http://username:passwor%6@server/resource",
157 "http", "", "", "", 80, 0,
158 HTTP_URI_CODING_MOST },
159
160 /* Bad hostname */
161 { HTTP_URI_STATUS_BAD_HOSTNAME, "http://[/::1]/index.html",
162 "http", "", "", "", 80, 0,
163 HTTP_URI_CODING_MOST },
164 { HTTP_URI_STATUS_BAD_HOSTNAME, "http://[",
165 "http", "", "", "", 80, 0,
166 HTTP_URI_CODING_MOST },
167 { HTTP_URI_STATUS_BAD_HOSTNAME, "http://serve%7/index.html",
168 "http", "", "", "", 80, 0,
169 HTTP_URI_CODING_MOST },
170 { HTTP_URI_STATUS_BAD_HOSTNAME, "http://server with spaces/index.html",
171 "http", "", "", "", 80, 0,
172 HTTP_URI_CODING_MOST },
173 { HTTP_URI_STATUS_BAD_HOSTNAME, "ipp://\"#/:<>?@[\\]^`{|}/",
174 "ipp", "", "", "", 631, 0,
175 HTTP_URI_CODING_MOST },
176
177 /* Bad port number */
178 { HTTP_URI_STATUS_BAD_PORT, "http://127.0.0.1:9999a/index.html",
179 "http", "", "127.0.0.1", "", 0, 0,
180 HTTP_URI_CODING_MOST },
181
182 /* Bad resource */
183 { HTTP_URI_STATUS_BAD_RESOURCE, "mailto:\r\nbla",
184 "mailto", "", "", "", 0, 0,
185 HTTP_URI_CODING_MOST },
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, NULL);
618 if (http == NULL)
619 {
620 perror(hostname);
621 continue;
622 }
623
624 if (httpIsEncrypted(http))
625 {
626 cups_array_t *creds;
627 char info[1024];
628 static const char *trusts[] = { "OK", "Invalid", "Changed", "Expired", "Renewed", "Unknown" };
629 if (!httpCopyCredentials(http, &creds))
630 {
631 cups_array_t *lcreds;
632 http_trust_t trust = httpCredentialsGetTrust(creds, hostname);
633
634 httpCredentialsString(creds, info, sizeof(info));
635
636 printf("Count: %d\n", cupsArrayCount(creds));
637 printf("Trust: %s\n", trusts[trust]);
638 printf("Expiration: %s\n", httpGetDateString(httpCredentialsGetExpiration(creds)));
639 printf("IsValidName: %d\n", httpCredentialsAreValidForName(creds, hostname));
640 printf("String: \"%s\"\n", info);
641
642 printf("LoadCredentials: %d\n", httpLoadCredentials(NULL, &lcreds, hostname));
643 httpCredentialsString(lcreds, info, sizeof(info));
644 printf(" Count: %d\n", cupsArrayCount(lcreds));
645 printf(" String: \"%s\"\n", info);
646
647 if (lcreds && cupsArrayCount(creds) == cupsArrayCount(lcreds))
648 {
649 http_credential_t *cred, *lcred;
650
651 for (i = 1, cred = (http_credential_t *)cupsArrayFirst(creds), lcred = (http_credential_t *)cupsArrayFirst(lcreds);
652 cred && lcred;
653 i ++, cred = (http_credential_t *)cupsArrayNext(creds), lcred = (http_credential_t *)cupsArrayNext(lcreds))
654 {
655 if (cred->datalen != lcred->datalen)
656 printf(" Credential #%d: Different lengths (saved=%d, current=%d)\n", i, (int)cred->datalen, (int)lcred->datalen);
657 else if (memcmp(cred->data, lcred->data, cred->datalen))
658 printf(" Credential #%d: Different data\n", i);
659 else
660 printf(" Credential #%d: Matches\n", i);
661 }
662 }
663
664 if (trust != HTTP_TRUST_OK)
665 {
666 printf("SaveCredentials: %d\n", httpSaveCredentials(NULL, creds, hostname));
667 trust = httpCredentialsGetTrust(creds, hostname);
668 printf("New Trust: %s\n", trusts[trust]);
669 }
670
671 httpFreeCredentials(creds);
672 }
673 else
674 puts("No credentials!");
675 }
676
677 printf("Checking file \"%s\"...\n", resource);
678
679 do
680 {
681 if (!_cups_strcasecmp(httpGetField(http, HTTP_FIELD_CONNECTION), "close"))
682 {
683 httpClearFields(http);
684 if (httpReconnect2(http, 30000, NULL))
685 {
686 status = HTTP_STATUS_ERROR;
687 break;
688 }
689 }
690
691 httpClearFields(http);
692 httpSetField(http, HTTP_FIELD_AUTHORIZATION, httpGetAuthString(http));
693 httpSetField(http, HTTP_FIELD_ACCEPT_LANGUAGE, "en");
694 if (httpHead(http, resource))
695 {
696 if (httpReconnect2(http, 30000, NULL))
697 {
698 status = HTTP_STATUS_ERROR;
699 break;
700 }
701 else
702 {
703 status = HTTP_STATUS_UNAUTHORIZED;
704 continue;
705 }
706 }
707
708 while ((status = httpUpdate(http)) == HTTP_STATUS_CONTINUE);
709
710 if (status == HTTP_STATUS_UNAUTHORIZED)
711 {
712 /*
713 * Flush any error message...
714 */
715
716 httpFlush(http);
717
718 /*
719 * See if we can do authentication...
720 */
721
722 if (cupsDoAuthentication(http, "GET", resource))
723 {
724 status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED;
725 break;
726 }
727
728 if (httpReconnect2(http, 30000, NULL))
729 {
730 status = HTTP_STATUS_ERROR;
731 break;
732 }
733
734 continue;
735 }
736 #ifdef HAVE_SSL
737 else if (status == HTTP_STATUS_UPGRADE_REQUIRED)
738 {
739 /* Flush any error message... */
740 httpFlush(http);
741
742 /* Reconnect... */
743 if (httpReconnect2(http, 30000, NULL))
744 {
745 status = HTTP_STATUS_ERROR;
746 break;
747 }
748
749 /* Upgrade with encryption... */
750 httpEncryption(http, HTTP_ENCRYPTION_REQUIRED);
751
752 /* Try again, this time with encryption enabled... */
753 continue;
754 }
755 #endif /* HAVE_SSL */
756 }
757 while (status == HTTP_STATUS_UNAUTHORIZED ||
758 status == HTTP_STATUS_UPGRADE_REQUIRED);
759
760 if (status == HTTP_STATUS_OK)
761 puts("HEAD OK:");
762 else
763 printf("HEAD failed with status %d...\n", status);
764
765 encoding = httpGetContentEncoding(http);
766
767 printf("Requesting file \"%s\" (Accept-Encoding: %s)...\n", resource,
768 encoding ? encoding : "identity");
769
770 do
771 {
772 if (!_cups_strcasecmp(httpGetField(http, HTTP_FIELD_CONNECTION), "close"))
773 {
774 httpClearFields(http);
775 if (httpReconnect2(http, 30000, NULL))
776 {
777 status = HTTP_STATUS_ERROR;
778 break;
779 }
780 }
781
782 httpClearFields(http);
783 httpSetField(http, HTTP_FIELD_AUTHORIZATION, httpGetAuthString(http));
784 httpSetField(http, HTTP_FIELD_ACCEPT_LANGUAGE, "en");
785 httpSetField(http, HTTP_FIELD_ACCEPT_ENCODING, encoding);
786
787 if (httpGet(http, resource))
788 {
789 if (httpReconnect2(http, 30000, NULL))
790 {
791 status = HTTP_STATUS_ERROR;
792 break;
793 }
794 else
795 {
796 status = HTTP_STATUS_UNAUTHORIZED;
797 continue;
798 }
799 }
800
801 while ((status = httpUpdate(http)) == HTTP_STATUS_CONTINUE);
802
803 if (status == HTTP_STATUS_UNAUTHORIZED)
804 {
805 /*
806 * Flush any error message...
807 */
808
809 httpFlush(http);
810
811 /*
812 * See if we can do authentication...
813 */
814
815 if (cupsDoAuthentication(http, "GET", resource))
816 {
817 status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED;
818 break;
819 }
820
821 if (httpReconnect2(http, 30000, NULL))
822 {
823 status = HTTP_STATUS_ERROR;
824 break;
825 }
826
827 continue;
828 }
829 #ifdef HAVE_SSL
830 else if (status == HTTP_STATUS_UPGRADE_REQUIRED)
831 {
832 /* Flush any error message... */
833 httpFlush(http);
834
835 /* Reconnect... */
836 if (httpReconnect2(http, 30000, NULL))
837 {
838 status = HTTP_STATUS_ERROR;
839 break;
840 }
841
842 /* Upgrade with encryption... */
843 httpEncryption(http, HTTP_ENCRYPTION_REQUIRED);
844
845 /* Try again, this time with encryption enabled... */
846 continue;
847 }
848 #endif /* HAVE_SSL */
849 }
850 while (status == HTTP_STATUS_UNAUTHORIZED || status == HTTP_STATUS_UPGRADE_REQUIRED);
851
852 if (status == HTTP_STATUS_OK)
853 puts("GET OK:");
854 else
855 printf("GET failed with status %d...\n", status);
856
857 start = time(NULL);
858 length = httpGetLength2(http);
859 total = 0;
860
861 while ((bytes = httpRead2(http, buffer, sizeof(buffer))) > 0)
862 {
863 total += bytes;
864 fwrite(buffer, (size_t)bytes, 1, out);
865 if (out != stdout)
866 {
867 current = time(NULL);
868 if (current == start)
869 current ++;
870
871 printf("\r" CUPS_LLFMT "/" CUPS_LLFMT " bytes ("
872 CUPS_LLFMT " bytes/sec) ", CUPS_LLCAST total,
873 CUPS_LLCAST length, CUPS_LLCAST (total / (current - start)));
874 fflush(stdout);
875 }
876 }
877 }
878
879 if (out != stdout)
880 putchar('\n');
881
882 puts("Closing connection to server...");
883 httpClose(http);
884
885 if (out != stdout)
886 fclose(out);
887
888 return (0);
889 }