]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/testhttp.c
Fix a compile error in libcups (<rdar://problem/14467141>)
[thirdparty/cups.git] / cups / testhttp.c
CommitLineData
ef416fc2 1/*
f2d18633 2 * "$Id$"
ef416fc2 3 *
71e16022 4 * HTTP test program for CUPS.
ef416fc2 5 *
cb7f98ee 6 * Copyright 2007-2013 by Apple Inc.
ed486911 7 * Copyright 1997-2006 by Easy Software Products.
ef416fc2 8 *
9 * These coded instructions, statements, and computer programs are the
bc44d920 10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
ef416fc2 14 *
15 * This file is subject to the Apple OS-Developed Software exception.
16 *
17 * Contents:
18 *
19 * main() - Main entry.
20 */
21
22/*
23 * Include necessary headers...
24 */
25
71e16022 26#include "string-private.h"
1f0275e3 27#include "http-private.h"
ef416fc2 28
29
30/*
31 * Types and structures...
32 */
33
34typedef struct uri_test_s /**** URI test cases ****/
35{
36 http_uri_status_t result; /* Expected return value */
37 const char *uri, /* URI */
38 *scheme, /* Scheme string */
39 *username, /* Username:password string */
40 *hostname, /* Hostname string */
41 *resource; /* Resource string */
42 int port, /* Port number */
86c809d9
MS
43 assemble_port, /* Port number for httpAssembleURI() */
44 assemble_coding;/* Coding for httpAssembleURI() */
ef416fc2 45} uri_test_t;
46
47
48/*
49 * Local globals...
50 */
51
52static uri_test_t uri_tests[] = /* URI test data */
53 {
54 /* Start with valid URIs */
cb7f98ee 55 { HTTP_URI_STATUS_OK, "file:/filename",
86c809d9
MS
56 "file", "", "", "/filename", 0, 0,
57 HTTP_URI_CODING_MOST },
cb7f98ee 58 { HTTP_URI_STATUS_OK, "file:/filename%20with%20spaces",
86c809d9
MS
59 "file", "", "", "/filename with spaces", 0, 0,
60 HTTP_URI_CODING_MOST },
cb7f98ee 61 { HTTP_URI_STATUS_OK, "file:///filename",
86c809d9
MS
62 "file", "", "", "/filename", 0, 0,
63 HTTP_URI_CODING_MOST },
cb7f98ee 64 { HTTP_URI_STATUS_OK, "file:///filename%20with%20spaces",
86c809d9
MS
65 "file", "", "", "/filename with spaces", 0, 0,
66 HTTP_URI_CODING_MOST },
cb7f98ee 67 { HTTP_URI_STATUS_OK, "file://localhost/filename",
86c809d9
MS
68 "file", "", "localhost", "/filename", 0, 0,
69 HTTP_URI_CODING_MOST },
cb7f98ee 70 { HTTP_URI_STATUS_OK, "file://localhost/filename%20with%20spaces",
86c809d9
MS
71 "file", "", "localhost", "/filename with spaces", 0, 0,
72 HTTP_URI_CODING_MOST },
cb7f98ee 73 { HTTP_URI_STATUS_OK, "http://server/",
86c809d9
MS
74 "http", "", "server", "/", 80, 0,
75 HTTP_URI_CODING_MOST },
cb7f98ee 76 { HTTP_URI_STATUS_OK, "http://username@server/",
86c809d9
MS
77 "http", "username", "server", "/", 80, 0,
78 HTTP_URI_CODING_MOST },
cb7f98ee 79 { HTTP_URI_STATUS_OK, "http://username:passwor%64@server/",
86c809d9
MS
80 "http", "username:password", "server", "/", 80, 0,
81 HTTP_URI_CODING_MOST },
cb7f98ee 82 { HTTP_URI_STATUS_OK, "http://username:passwor%64@server:8080/",
86c809d9
MS
83 "http", "username:password", "server", "/", 8080, 8080,
84 HTTP_URI_CODING_MOST },
cb7f98ee 85 { HTTP_URI_STATUS_OK, "http://username:passwor%64@server:8080/directory/filename",
86c809d9
MS
86 "http", "username:password", "server", "/directory/filename", 8080, 8080,
87 HTTP_URI_CODING_MOST },
cb7f98ee 88 { HTTP_URI_STATUS_OK, "http://[2000::10:100]:631/ipp",
86c809d9
MS
89 "http", "", "2000::10:100", "/ipp", 631, 631,
90 HTTP_URI_CODING_MOST },
cb7f98ee 91 { HTTP_URI_STATUS_OK, "https://username:passwor%64@server/directory/filename",
86c809d9
MS
92 "https", "username:password", "server", "/directory/filename", 443, 0,
93 HTTP_URI_CODING_MOST },
cb7f98ee 94 { HTTP_URI_STATUS_OK, "ipp://username:passwor%64@[::1]/ipp",
86c809d9
MS
95 "ipp", "username:password", "::1", "/ipp", 631, 0,
96 HTTP_URI_CODING_MOST },
cb7f98ee 97 { HTTP_URI_STATUS_OK, "lpd://server/queue?reserve=yes",
86c809d9
MS
98 "lpd", "", "server", "/queue?reserve=yes", 515, 0,
99 HTTP_URI_CODING_MOST },
cb7f98ee 100 { HTTP_URI_STATUS_OK, "mailto:user@domain.com",
86c809d9
MS
101 "mailto", "", "", "user@domain.com", 0, 0,
102 HTTP_URI_CODING_MOST },
cb7f98ee 103 { HTTP_URI_STATUS_OK, "socket://server/",
86c809d9
MS
104 "socket", "", "server", "/", 9100, 0,
105 HTTP_URI_CODING_MOST },
cb7f98ee 106 { HTTP_URI_STATUS_OK, "socket://192.168.1.1:9101/",
86c809d9
MS
107 "socket", "", "192.168.1.1", "/", 9101, 9101,
108 HTTP_URI_CODING_MOST },
cb7f98ee 109 { HTTP_URI_STATUS_OK, "ipp://username:password@[v1.fe80::200:1234:5678:9abc+eth0]:999/ipp",
86c809d9
MS
110 "ipp", "username:password", "fe80::200:1234:5678:9abc%eth0", "/ipp", 999, 999,
111 HTTP_URI_CODING_MOST },
112 { HTTP_URI_STATUS_OK, "ipp://username:password@[fe80::200:1234:5678:9abc%25eth0]:999/ipp",
113 "ipp", "username:password", "fe80::200:1234:5678:9abc%eth0", "/ipp", 999, 999,
114 HTTP_URI_CODING_MOST | HTTP_URI_CODING_RFC6874 },
cb7f98ee 115 { HTTP_URI_STATUS_OK, "http://server/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400",
86c809d9
MS
116 "http", "", "server", "/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400", 80, 0,
117 HTTP_URI_CODING_MOST },
cb7f98ee 118 { HTTP_URI_STATUS_OK, "lpd://Acme%20Laser%20(01%3A23%3A45).local._tcp._printer/",
86c809d9
MS
119 "lpd", "", "Acme Laser (01:23:45).local._tcp._printer", "/", 515, 0,
120 HTTP_URI_CODING_MOST },
cb7f98ee 121 { HTTP_URI_STATUS_OK, "ipp://HP%20Officejet%204500%20G510n-z%20%40%20Will's%20MacBook%20Pro%2015%22._ipp._tcp.local./",
86c809d9
MS
122 "ipp", "", "HP Officejet 4500 G510n-z @ Will's MacBook Pro 15\"._ipp._tcp.local.", "/", 631, 0,
123 HTTP_URI_CODING_MOST },
ff862e50
MS
124 { HTTP_URI_STATUS_OK, "ipp://%22%23%2F%3A%3C%3E%3F%40%5B%5C%5D%5E%60%7B%7C%7D/",
125 "ipp", "", "\"#/:<>?@[\\]^`{|}", "/", 631, 0,
126 HTTP_URI_CODING_MOST },
ef416fc2 127
128 /* Missing scheme */
cb7f98ee 129 { HTTP_URI_STATUS_MISSING_SCHEME, "/path/to/file/index.html",
86c809d9
MS
130 "file", "", "", "/path/to/file/index.html", 0, 0,
131 HTTP_URI_CODING_MOST },
cb7f98ee 132 { HTTP_URI_STATUS_MISSING_SCHEME, "//server/ipp",
86c809d9
MS
133 "ipp", "", "server", "/ipp", 631, 0,
134 HTTP_URI_CODING_MOST },
ef416fc2 135
136 /* Unknown scheme */
cb7f98ee 137 { HTTP_URI_STATUS_UNKNOWN_SCHEME, "vendor://server/resource",
86c809d9
MS
138 "vendor", "", "server", "/resource", 0, 0,
139 HTTP_URI_CODING_MOST },
ef416fc2 140
141 /* Missing resource */
cb7f98ee 142 { HTTP_URI_STATUS_MISSING_RESOURCE, "socket://[::192.168.2.1]",
86c809d9
MS
143 "socket", "", "::192.168.2.1", "/", 9100, 0,
144 HTTP_URI_CODING_MOST },
cb7f98ee 145 { HTTP_URI_STATUS_MISSING_RESOURCE, "socket://192.168.1.1:9101",
86c809d9
MS
146 "socket", "", "192.168.1.1", "/", 9101, 0,
147 HTTP_URI_CODING_MOST },
ef416fc2 148
149 /* Bad URI */
cb7f98ee 150 { HTTP_URI_STATUS_BAD_URI, "",
86c809d9
MS
151 "", "", "", "", 0, 0,
152 HTTP_URI_CODING_MOST },
ef416fc2 153
154 /* Bad scheme */
cb7f98ee 155 { HTTP_URI_STATUS_BAD_SCHEME, "bad_scheme://server/resource",
86c809d9
MS
156 "", "", "", "", 0, 0,
157 HTTP_URI_CODING_MOST },
ef416fc2 158
159 /* Bad username */
cb7f98ee 160 { HTTP_URI_STATUS_BAD_USERNAME, "http://username:passwor%6@server/resource",
86c809d9
MS
161 "http", "", "", "", 80, 0,
162 HTTP_URI_CODING_MOST },
ef416fc2 163
164 /* Bad hostname */
cb7f98ee 165 { HTTP_URI_STATUS_BAD_HOSTNAME, "http://[/::1]/index.html",
86c809d9
MS
166 "http", "", "", "", 80, 0,
167 HTTP_URI_CODING_MOST },
cb7f98ee 168 { HTTP_URI_STATUS_BAD_HOSTNAME, "http://[",
86c809d9
MS
169 "http", "", "", "", 80, 0,
170 HTTP_URI_CODING_MOST },
cb7f98ee 171 { HTTP_URI_STATUS_BAD_HOSTNAME, "http://serve%7/index.html",
86c809d9
MS
172 "http", "", "", "", 80, 0,
173 HTTP_URI_CODING_MOST },
6961465f 174 { HTTP_URI_STATUS_BAD_HOSTNAME, "http://server with spaces/index.html",
86c809d9
MS
175 "http", "", "", "", 80, 0,
176 HTTP_URI_CODING_MOST },
ff862e50
MS
177 { HTTP_URI_STATUS_BAD_HOSTNAME, "ipp://\"#/:<>?@[\\]^`{|}/",
178 "ipp", "", "", "", 631, 0,
179 HTTP_URI_CODING_MOST },
ef416fc2 180
181 /* Bad port number */
cb7f98ee 182 { HTTP_URI_STATUS_BAD_PORT, "http://127.0.0.1:9999a/index.html",
86c809d9
MS
183 "http", "", "127.0.0.1", "", 0, 0,
184 HTTP_URI_CODING_MOST },
ef416fc2 185
186 /* Bad resource */
cb7f98ee 187 { HTTP_URI_STATUS_BAD_RESOURCE, "http://server/index.html%",
86c809d9
MS
188 "http", "", "server", "", 80, 0,
189 HTTP_URI_CODING_MOST },
6961465f 190 { HTTP_URI_STATUS_BAD_RESOURCE, "http://server/index with spaces.html",
86c809d9
MS
191 "http", "", "server", "", 80, 0,
192 HTTP_URI_CODING_MOST }
ef416fc2 193 };
194static const char * const base64_tests[][2] =
195 {
196 { "A", "QQ==" },
197 /* 010000 01 */
198 { "AB", "QUI=" },
199 /* 010000 010100 0010 */
200 { "ABC", "QUJD" },
201 /* 010000 010100 001001 000011 */
202 { "ABCD", "QUJDRA==" },
203 /* 010000 010100 001001 000011 010001 00 */
204 { "ABCDE", "QUJDREU=" },
205 /* 010000 010100 001001 000011 010001 000100 0101 */
206 { "ABCDEF", "QUJDREVG" },
207 /* 010000 010100 001001 000011 010001 000100 010101 000110 */
208 };
209
210
211/*
212 * 'main()' - Main entry.
213 */
214
215int /* O - Exit status */
216main(int argc, /* I - Number of command-line arguments */
217 char *argv[]) /* I - Command-line arguments */
218{
219 int i, j, k; /* Looping vars */
220 http_t *http; /* HTTP connection */
83e08001 221 http_encryption_t encryption; /* Encryption type */
ef416fc2 222 http_status_t status; /* Status of GET command */
223 int failures; /* Number of test failures */
224 char buffer[8192]; /* Input buffer */
225 long bytes; /* Number of bytes read */
226 FILE *out; /* Output file */
227 char encode[256], /* Base64-encoded string */
228 decode[256]; /* Base64-decoded string */
229 int decodelen; /* Length of decoded string */
230 char scheme[HTTP_MAX_URI], /* Scheme from URI */
231 hostname[HTTP_MAX_URI], /* Hostname from URI */
232 username[HTTP_MAX_URI], /* Username:password from URI */
233 resource[HTTP_MAX_URI]; /* Resource from URI */
234 int port; /* Port number from URI */
235 http_uri_status_t uri_status; /* Status of URI separation */
236 http_addrlist_t *addrlist, /* Address list */
237 *addr; /* Current address */
238 off_t length, total; /* Length and total bytes */
239 time_t start, current; /* Start and end time */
a469f8a5 240 const char *encoding; /* Negotiated Content-Encoding */
ef416fc2 241 static const char * const uri_status_strings[] =
242 {
cb7f98ee
MS
243 "HTTP_URI_STATUS_OVERFLOW",
244 "HTTP_URI_STATUS_BAD_ARGUMENTS",
245 "HTTP_URI_STATUS_BAD_RESOURCE",
246 "HTTP_URI_STATUS_BAD_PORT",
247 "HTTP_URI_STATUS_BAD_HOSTNAME",
248 "HTTP_URI_STATUS_BAD_USERNAME",
249 "HTTP_URI_STATUS_BAD_SCHEME",
250 "HTTP_URI_STATUS_BAD_URI",
251 "HTTP_URI_STATUS_OK",
252 "HTTP_URI_STATUS_MISSING_SCHEME",
253 "HTTP_URI_STATUS_UNKNOWN_SCHEME",
254 "HTTP_URI_STATUS_MISSING_RESOURCE"
ef416fc2 255 };
256
257
258 /*
259 * Do API tests if we don't have a URL on the command-line...
260 */
261
262 if (argc == 1)
263 {
264 failures = 0;
265
266 /*
267 * httpGetDateString()/httpGetDateTime()
268 */
269
270 fputs("httpGetDateString()/httpGetDateTime(): ", stdout);
271
272 start = time(NULL);
5a9febac 273 strlcpy(buffer, httpGetDateString(start), sizeof(buffer));
ef416fc2 274 current = httpGetDateTime(buffer);
275
276 i = (int)(current - start);
277 if (i < 0)
278 i = -i;
279
280 if (!i)
281 puts("PASS");
282 else
283 {
284 failures ++;
285 puts("FAIL");
286 printf(" Difference is %d seconds, %02d:%02d:%02d...\n", i, i / 3600,
287 (i / 60) % 60, i % 60);
288 printf(" httpGetDateString(%d) returned \"%s\"\n", (int)start, buffer);
289 printf(" httpGetDateTime(\"%s\") returned %d\n", buffer, (int)current);
290 printf(" httpGetDateString(%d) returned \"%s\"\n", (int)current,
291 httpGetDateString(current));
292 }
293
294 /*
295 * httpDecode64_2()/httpEncode64_2()
296 */
297
298 fputs("httpDecode64_2()/httpEncode64_2(): ", stdout);
299
300 for (i = 0, j = 0; i < (int)(sizeof(base64_tests) / sizeof(base64_tests[0])); i ++)
301 {
302 httpEncode64_2(encode, sizeof(encode), base64_tests[i][0],
7a0cbd5e 303 (int)strlen(base64_tests[i][0]));
ef416fc2 304 decodelen = (int)sizeof(decode);
305 httpDecode64_2(decode, &decodelen, base64_tests[i][1]);
306
307 if (strcmp(decode, base64_tests[i][0]))
308 {
309 failures ++;
310
311 if (j)
312 {
313 puts("FAIL");
314 j = 1;
315 }
316
317 printf(" httpDecode64_2() returned \"%s\", expected \"%s\"...\n",
318 decode, base64_tests[i][0]);
319 }
320
321 if (strcmp(encode, base64_tests[i][1]))
322 {
323 failures ++;
324
325 if (j)
326 {
327 puts("FAIL");
328 j = 1;
329 }
330
331 printf(" httpEncode64_2() returned \"%s\", expected \"%s\"...\n",
332 encode, base64_tests[i][1]);
333 }
334 }
335
336 if (!j)
337 puts("PASS");
338
339 /*
340 * httpGetHostname()
341 */
342
343 fputs("httpGetHostname(): ", stdout);
344
757d2cad 345 if (httpGetHostname(NULL, hostname, sizeof(hostname)))
ef416fc2 346 printf("PASS (%s)\n", hostname);
347 else
348 {
349 failures ++;
350 puts("FAIL");
351 }
352
353 /*
354 * httpAddrGetList()
355 */
356
75bd9771 357 printf("httpAddrGetList(%s): ", hostname);
ef416fc2 358
359 addrlist = httpAddrGetList(hostname, AF_UNSPEC, NULL);
360 if (addrlist)
361 {
ed486911 362 for (i = 0, addr = addrlist; addr; i ++, addr = addr->next)
363 {
364 char numeric[1024]; /* Numeric IP address */
365
366
367 httpAddrString(&(addr->addr), numeric, sizeof(numeric));
368 if (!strcmp(numeric, "UNKNOWN"))
369 break;
370 }
371
372 if (addr)
373 printf("FAIL (bad address for %s)\n", hostname);
374 else
375 printf("PASS (%d address(es) for %s)\n", i, hostname);
ef416fc2 376
ef416fc2 377 httpAddrFreeList(addrlist);
378 }
58dc1933
MS
379 else if (isdigit(hostname[0] & 255))
380 {
381 puts("FAIL (ignored because hostname is numeric)");
382 }
ef416fc2 383 else
384 {
385 failures ++;
386 puts("FAIL");
387 }
388
389 /*
390 * Test httpSeparateURI()...
391 */
392
393 fputs("httpSeparateURI(): ", stdout);
394 for (i = 0, j = 0; i < (int)(sizeof(uri_tests) / sizeof(uri_tests[0])); i ++)
395 {
a4d04587 396 uri_status = httpSeparateURI(HTTP_URI_CODING_MOST,
397 uri_tests[i].uri, scheme, sizeof(scheme),
ef416fc2 398 username, sizeof(username),
399 hostname, sizeof(hostname), &port,
400 resource, sizeof(resource));
401 if (uri_status != uri_tests[i].result ||
402 strcmp(scheme, uri_tests[i].scheme) ||
403 strcmp(username, uri_tests[i].username) ||
404 strcmp(hostname, uri_tests[i].hostname) ||
405 port != uri_tests[i].port ||
406 strcmp(resource, uri_tests[i].resource))
407 {
408 failures ++;
409
410 if (!j)
411 {
412 puts("FAIL");
413 j = 1;
414 }
415
416 printf(" \"%s\":\n", uri_tests[i].uri);
417
418 if (uri_status != uri_tests[i].result)
419 printf(" Returned %s instead of %s\n",
420 uri_status_strings[uri_status + 8],
421 uri_status_strings[uri_tests[i].result + 8]);
422
423 if (strcmp(scheme, uri_tests[i].scheme))
424 printf(" Scheme \"%s\" instead of \"%s\"\n",
425 scheme, uri_tests[i].scheme);
426
427 if (strcmp(username, uri_tests[i].username))
428 printf(" Username \"%s\" instead of \"%s\"\n",
429 username, uri_tests[i].username);
430
431 if (strcmp(hostname, uri_tests[i].hostname))
432 printf(" Hostname \"%s\" instead of \"%s\"\n",
433 hostname, uri_tests[i].hostname);
434
435 if (port != uri_tests[i].port)
436 printf(" Port %d instead of %d\n",
437 port, uri_tests[i].port);
438
439 if (strcmp(resource, uri_tests[i].resource))
440 printf(" Resource \"%s\" instead of \"%s\"\n",
441 resource, uri_tests[i].resource);
442 }
443 }
444
445 if (!j)
446 printf("PASS (%d URIs tested)\n",
447 (int)(sizeof(uri_tests) / sizeof(uri_tests[0])));
448
449 /*
450 * Test httpAssembleURI()...
451 */
452
453 fputs("httpAssembleURI(): ", stdout);
454 for (i = 0, j = 0, k = 0;
455 i < (int)(sizeof(uri_tests) / sizeof(uri_tests[0]));
456 i ++)
cb7f98ee 457 if (uri_tests[i].result == HTTP_URI_STATUS_OK &&
ef416fc2 458 !strstr(uri_tests[i].uri, "%64") &&
459 strstr(uri_tests[i].uri, "//"))
460 {
461 k ++;
86c809d9 462 uri_status = httpAssembleURI(uri_tests[i].assemble_coding,
a4d04587 463 buffer, sizeof(buffer),
ef416fc2 464 uri_tests[i].scheme,
465 uri_tests[i].username,
466 uri_tests[i].hostname,
467 uri_tests[i].assemble_port,
468 uri_tests[i].resource);
469
cb7f98ee 470 if (uri_status != HTTP_URI_STATUS_OK)
ef416fc2 471 {
472 failures ++;
473
474 if (!j)
475 {
476 puts("FAIL");
477 j = 1;
478 }
479
480 printf(" \"%s\": %s\n", uri_tests[i].uri,
481 uri_status_strings[uri_status + 8]);
482 }
483 else if (strcmp(buffer, uri_tests[i].uri))
484 {
485 failures ++;
486
487 if (!j)
488 {
489 puts("FAIL");
490 j = 1;
491 }
492
493 printf(" \"%s\": assembled = \"%s\"\n", uri_tests[i].uri,
494 buffer);
495 }
496 }
497
498 if (!j)
499 printf("PASS (%d URIs tested)\n", k);
500
71f294ce
MS
501 /*
502 * httpAssembleUUID
503 */
504
505 fputs("httpAssembleUUID: ", stdout);
506 httpAssembleUUID("hostname.example.com", 631, "printer", 12345, buffer,
507 sizeof(buffer));
508 if (strncmp(buffer, "urn:uuid:", 9))
509 {
510 printf("FAIL (%s)\n", buffer);
511 failures ++;
512 }
513 else
514 printf("PASS (%s)\n", buffer);
515
ef416fc2 516 /*
517 * Show a summary and return...
518 */
519
520 if (failures)
521 printf("\n%d TESTS FAILED!\n", failures);
522 else
523 puts("\nALL TESTS PASSED!");
524
525 return (failures);
526 }
1f0275e3
MS
527 else if (strstr(argv[1], "._tcp"))
528 {
529 /*
530 * Test resolving an mDNS name.
531 */
532
533 char resolved[1024]; /* Resolved URI */
534
535
eac3a0a0 536 printf("_httpResolveURI(%s, _HTTP_RESOLVE_DEFAULT): ", argv[1]);
1f0275e3
MS
537 fflush(stdout);
538
eac3a0a0
MS
539 if (!_httpResolveURI(argv[1], resolved, sizeof(resolved),
540 _HTTP_RESOLVE_DEFAULT, NULL, NULL))
1f0275e3
MS
541 {
542 puts("FAIL");
543 return (1);
544 }
eac3a0a0
MS
545 else
546 printf("PASS (%s)\n", resolved);
547
548 printf("_httpResolveURI(%s, _HTTP_RESOLVE_FQDN): ", argv[1]);
549 fflush(stdout);
550
551 if (!_httpResolveURI(argv[1], resolved, sizeof(resolved),
552 _HTTP_RESOLVE_FQDN, NULL, NULL))
553 {
554 puts("FAIL");
555 return (1);
556 }
557 else if (strstr(resolved, ".local:"))
558 {
559 printf("FAIL (%s)\n", resolved);
560 return (1);
561 }
1f0275e3
MS
562 else
563 {
564 printf("PASS (%s)\n", resolved);
565 return (0);
566 }
567 }
dfd5680b
MS
568 else if (!strcmp(argv[1], "-u") && argc == 3)
569 {
570 /*
571 * Test URI separation...
572 */
573
574 uri_status = httpSeparateURI(HTTP_URI_CODING_ALL, argv[2], scheme,
575 sizeof(scheme), username, sizeof(username),
576 hostname, sizeof(hostname), &port,
577 resource, sizeof(resource));
578 printf("uri_status = %s\n", uri_status_strings[uri_status + 8]);
579 printf("scheme = \"%s\"\n", scheme);
580 printf("username = \"%s\"\n", username);
581 printf("hostname = \"%s\"\n", hostname);
582 printf("port = %d\n", port);
583 printf("resource = \"%s\"\n", resource);
584
585 return (0);
586 }
ef416fc2 587
588 /*
589 * Test HTTP GET requests...
590 */
591
592 http = NULL;
593 out = stdout;
594
595 for (i = 1; i < argc; i ++)
596 {
597 if (!strcmp(argv[i], "-o"))
598 {
599 i ++;
600 if (i >= argc)
601 break;
602
603 out = fopen(argv[i], "wb");
604 continue;
605 }
606
a4d04587 607 httpSeparateURI(HTTP_URI_CODING_MOST, argv[i], scheme, sizeof(scheme),
608 username, sizeof(username),
ef416fc2 609 hostname, sizeof(hostname), &port,
610 resource, sizeof(resource));
611
83e08001
MS
612 if (!_cups_strcasecmp(scheme, "https") || !_cups_strcasecmp(scheme, "ipps") ||
613 port == 443)
cb7f98ee 614 encryption = HTTP_ENCRYPTION_ALWAYS;
83e08001 615 else
cb7f98ee 616 encryption = HTTP_ENCRYPTION_IF_REQUESTED;
83e08001 617
cb7f98ee
MS
618 http = httpConnect2(hostname, port, NULL, AF_UNSPEC, encryption, 1, 30000,
619 NULL);
ef416fc2 620 if (http == NULL)
621 {
622 perror(hostname);
623 continue;
624 }
a469f8a5 625 printf("Checking file \"%s\"...\n", resource);
ef416fc2 626 httpClearFields(http);
627 httpSetField(http, HTTP_FIELD_ACCEPT_LANGUAGE, "en");
a469f8a5 628 httpHead(http, resource);
cb7f98ee 629 while ((status = httpUpdate(http)) == HTTP_STATUS_CONTINUE);
a469f8a5 630
cb7f98ee 631 if (status == HTTP_STATUS_OK)
a469f8a5
MS
632 puts("HEAD OK:");
633 else
634 printf("HEAD failed with status %d...\n", status);
635
636 encoding = httpGetContentEncoding(http);
637
638 printf("Requesting file \"%s\" (Accept-Encoding: %s)...\n", resource,
639 encoding ? encoding : "identity");
640 httpClearFields(http);
641 httpSetField(http, HTTP_FIELD_ACCEPT_LANGUAGE, "en");
642 httpSetField(http, HTTP_FIELD_ACCEPT_ENCODING, encoding);
ef416fc2 643 httpGet(http, resource);
cb7f98ee 644 while ((status = httpUpdate(http)) == HTTP_STATUS_CONTINUE);
ef416fc2 645
cb7f98ee 646 if (status == HTTP_STATUS_OK)
ef416fc2 647 puts("GET OK:");
648 else
649 printf("GET failed with status %d...\n", status);
650
ef416fc2 651 start = time(NULL);
652 length = httpGetLength2(http);
653 total = 0;
654
a4d04587 655 while ((bytes = httpRead2(http, buffer, sizeof(buffer))) > 0)
ef416fc2 656 {
657 total += bytes;
658 fwrite(buffer, bytes, 1, out);
659 if (out != stdout)
660 {
661 current = time(NULL);
662 if (current == start) current ++;
663 printf("\r" CUPS_LLFMT "/" CUPS_LLFMT " bytes ("
e1d6a774 664 CUPS_LLFMT " bytes/sec) ", CUPS_LLCAST total,
665 CUPS_LLCAST length, CUPS_LLCAST (total / (current - start)));
ef416fc2 666 fflush(stdout);
667 }
668 }
669 }
670
671 puts("Closing connection to server...");
672 httpClose(http);
673
674 if (out != stdout)
675 fclose(out);
676
677 return (0);
678}
679
680
681/*
f2d18633 682 * End of "$Id$".
ef416fc2 683 */