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