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