2 * HTTP test program for CUPS.
4 * Copyright © 2007-2018 by Apple Inc.
5 * Copyright © 1997-2006 by Easy Software Products.
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/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers...
20 #include "cups-private.h"
24 * Types and structures...
27 typedef struct uri_test_s
/**** URI test cases ****/
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() */
45 static uri_test_t uri_tests
[] = /* URI test data */
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
},
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
},
133 { HTTP_URI_STATUS_UNKNOWN_SCHEME
, "vendor://server/resource",
134 "vendor", "", "server", "/resource", 0, 0,
135 HTTP_URI_CODING_MOST
},
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
},
146 { HTTP_URI_STATUS_BAD_URI
, "",
147 "", "", "", "", 0, 0,
148 HTTP_URI_CODING_MOST
},
151 { HTTP_URI_STATUS_BAD_SCHEME
, "bad_scheme://server/resource",
152 "", "", "", "", 0, 0,
153 HTTP_URI_CODING_MOST
},
156 { HTTP_URI_STATUS_BAD_USERNAME
, "http://username:passwor%6@server/resource",
157 "http", "", "", "", 80, 0,
158 HTTP_URI_CODING_MOST
},
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
},
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
},
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
}
193 static const char * const base64_tests
[][2] =
198 /* 010000 010100 0010 */
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 */
211 * 'main()' - Main entry.
214 int /* O - Exit status */
215 main(int argc
, /* I - Number of command-line arguments */
216 char *argv
[]) /* I - Command-line arguments */
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
[] =
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"
258 * Do API tests if we don't have a URL on the command-line...
266 * httpGetDateString()/httpGetDateTime()
269 fputs("httpGetDateString()/httpGetDateTime(): ", stdout
);
272 strlcpy(buffer
, httpGetDateString(start
), sizeof(buffer
));
273 current
= httpGetDateTime(buffer
);
275 i
= (int)(current
- start
);
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
));
294 * httpDecode64_2()/httpEncode64_2()
297 fputs("httpDecode64_2()/httpEncode64_2(): ", stdout
);
299 for (i
= 0, j
= 0; i
< (int)(sizeof(base64_tests
) / sizeof(base64_tests
[0])); i
++)
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]);
306 if (strcmp(decode
, base64_tests
[i
][0]))
316 printf(" httpDecode64_2() returned \"%s\", expected \"%s\"...\n",
317 decode
, base64_tests
[i
][0]);
320 if (strcmp(encode
, base64_tests
[i
][1]))
330 printf(" httpEncode64_2() returned \"%s\", expected \"%s\"...\n",
331 encode
, base64_tests
[i
][1]);
342 fputs("httpGetHostname(): ", stdout
);
344 if (httpGetHostname(NULL
, hostname
, sizeof(hostname
)))
345 printf("PASS (%s)\n", hostname
);
356 printf("httpAddrGetList(%s): ", hostname
);
358 addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, NULL
);
361 for (i
= 0, addr
= addrlist
; addr
; i
++, addr
= addr
->next
)
363 char numeric
[1024]; /* Numeric IP address */
366 httpAddrString(&(addr
->addr
), numeric
, sizeof(numeric
));
367 if (!strcmp(numeric
, "UNKNOWN"))
372 printf("FAIL (bad address for %s)\n", hostname
);
374 printf("PASS (%d address(es) for %s)\n", i
, hostname
);
376 httpAddrFreeList(addrlist
);
378 else if (isdigit(hostname
[0] & 255))
380 puts("FAIL (ignored because hostname is numeric)");
389 * Test httpSeparateURI()...
392 fputs("httpSeparateURI(): ", stdout
);
393 for (i
= 0, j
= 0; i
< (int)(sizeof(uri_tests
) / sizeof(uri_tests
[0])); i
++)
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
))
415 printf(" \"%s\":\n", uri_tests
[i
].uri
);
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]);
422 if (strcmp(scheme
, uri_tests
[i
].scheme
))
423 printf(" Scheme \"%s\" instead of \"%s\"\n",
424 scheme
, uri_tests
[i
].scheme
);
426 if (strcmp(username
, uri_tests
[i
].username
))
427 printf(" Username \"%s\" instead of \"%s\"\n",
428 username
, uri_tests
[i
].username
);
430 if (strcmp(hostname
, uri_tests
[i
].hostname
))
431 printf(" Hostname \"%s\" instead of \"%s\"\n",
432 hostname
, uri_tests
[i
].hostname
);
434 if (port
!= uri_tests
[i
].port
)
435 printf(" Port %d instead of %d\n",
436 port
, uri_tests
[i
].port
);
438 if (strcmp(resource
, uri_tests
[i
].resource
))
439 printf(" Resource \"%s\" instead of \"%s\"\n",
440 resource
, uri_tests
[i
].resource
);
445 printf("PASS (%d URIs tested)\n",
446 (int)(sizeof(uri_tests
) / sizeof(uri_tests
[0])));
449 * Test httpAssembleURI()...
452 fputs("httpAssembleURI(): ", stdout
);
453 for (i
= 0, j
= 0, k
= 0;
454 i
< (int)(sizeof(uri_tests
) / sizeof(uri_tests
[0]));
456 if (uri_tests
[i
].result
== HTTP_URI_STATUS_OK
&&
457 !strstr(uri_tests
[i
].uri
, "%64") &&
458 strstr(uri_tests
[i
].uri
, "//"))
461 uri_status
= httpAssembleURI(uri_tests
[i
].assemble_coding
,
462 buffer
, sizeof(buffer
),
464 uri_tests
[i
].username
,
465 uri_tests
[i
].hostname
,
466 uri_tests
[i
].assemble_port
,
467 uri_tests
[i
].resource
);
469 if (uri_status
!= HTTP_URI_STATUS_OK
)
479 printf(" \"%s\": %s\n", uri_tests
[i
].uri
,
480 uri_status_strings
[uri_status
+ 8]);
482 else if (strcmp(buffer
, uri_tests
[i
].uri
))
492 printf(" \"%s\": assembled = \"%s\"\n", uri_tests
[i
].uri
,
498 printf("PASS (%d URIs tested)\n", k
);
504 fputs("httpAssembleUUID: ", stdout
);
505 httpAssembleUUID("hostname.example.com", 631, "printer", 12345, buffer
,
507 if (strncmp(buffer
, "urn:uuid:", 9))
509 printf("FAIL (%s)\n", buffer
);
513 printf("PASS (%s)\n", buffer
);
516 * Show a summary and return...
520 printf("\n%d TESTS FAILED!\n", failures
);
522 puts("\nALL TESTS PASSED!");
526 else if (strstr(argv
[1], "._tcp"))
529 * Test resolving an mDNS name.
532 char resolved
[1024]; /* Resolved URI */
535 printf("_httpResolveURI(%s, _HTTP_RESOLVE_DEFAULT): ", argv
[1]);
538 if (!_httpResolveURI(argv
[1], resolved
, sizeof(resolved
),
539 _HTTP_RESOLVE_DEFAULT
, NULL
, NULL
))
545 printf("PASS (%s)\n", resolved
);
547 printf("_httpResolveURI(%s, _HTTP_RESOLVE_FQDN): ", argv
[1]);
550 if (!_httpResolveURI(argv
[1], resolved
, sizeof(resolved
),
551 _HTTP_RESOLVE_FQDN
, NULL
, NULL
))
556 else if (strstr(resolved
, ".local:"))
558 printf("FAIL (%s)\n", resolved
);
563 printf("PASS (%s)\n", resolved
);
567 else if (!strcmp(argv
[1], "-u") && argc
== 3)
570 * Test URI separation...
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
);
588 * Test HTTP GET requests...
594 for (i
= 1; i
< argc
; i
++)
596 if (!strcmp(argv
[i
], "-o"))
602 out
= fopen(argv
[i
], "wb");
606 httpSeparateURI(HTTP_URI_CODING_MOST
, argv
[i
], scheme
, sizeof(scheme
),
607 username
, sizeof(username
),
608 hostname
, sizeof(hostname
), &port
,
609 resource
, sizeof(resource
));
611 if (!_cups_strcasecmp(scheme
, "https") || !_cups_strcasecmp(scheme
, "ipps") ||
613 encryption
= HTTP_ENCRYPTION_ALWAYS
;
615 encryption
= HTTP_ENCRYPTION_IF_REQUESTED
;
617 http
= httpConnect2(hostname
, port
, NULL
, AF_UNSPEC
, encryption
, 1, 30000, NULL
);
624 if (httpIsEncrypted(http
))
628 static const char *trusts
[] = { "OK", "Invalid", "Changed", "Expired", "Renewed", "Unknown" };
629 if (!httpCopyCredentials(http
, &creds
))
631 cups_array_t
*lcreds
;
632 http_trust_t trust
= httpCredentialsGetTrust(creds
, hostname
);
634 httpCredentialsString(creds
, info
, sizeof(info
));
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
);
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
);
647 if (lcreds
&& cupsArrayCount(creds
) == cupsArrayCount(lcreds
))
649 http_credential_t
*cred
, *lcred
;
651 for (i
= 1, cred
= (http_credential_t
*)cupsArrayFirst(creds
), lcred
= (http_credential_t
*)cupsArrayFirst(lcreds
);
653 i
++, cred
= (http_credential_t
*)cupsArrayNext(creds
), lcred
= (http_credential_t
*)cupsArrayNext(lcreds
))
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
);
660 printf(" Credential #%d: Matches\n", i
);
664 if (trust
!= HTTP_TRUST_OK
)
666 printf("SaveCredentials: %d\n", httpSaveCredentials(NULL
, creds
, hostname
));
667 trust
= httpCredentialsGetTrust(creds
, hostname
);
668 printf("New Trust: %s\n", trusts
[trust
]);
671 httpFreeCredentials(creds
);
674 puts("No credentials!");
677 printf("Checking file \"%s\"...\n", resource
);
681 if (!_cups_strcasecmp(httpGetField(http
, HTTP_FIELD_CONNECTION
), "close"))
683 httpClearFields(http
);
684 if (httpReconnect2(http
, 30000, NULL
))
686 status
= HTTP_STATUS_ERROR
;
691 httpClearFields(http
);
692 httpSetField(http
, HTTP_FIELD_AUTHORIZATION
, httpGetAuthString(http
));
693 httpSetField(http
, HTTP_FIELD_ACCEPT_LANGUAGE
, "en");
694 if (httpHead(http
, resource
))
696 if (httpReconnect2(http
, 30000, NULL
))
698 status
= HTTP_STATUS_ERROR
;
703 status
= HTTP_STATUS_UNAUTHORIZED
;
708 while ((status
= httpUpdate(http
)) == HTTP_STATUS_CONTINUE
);
710 if (status
== HTTP_STATUS_UNAUTHORIZED
)
713 * Flush any error message...
719 * See if we can do authentication...
722 if (cupsDoAuthentication(http
, "GET", resource
))
724 status
= HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
;
728 if (httpReconnect2(http
, 30000, NULL
))
730 status
= HTTP_STATUS_ERROR
;
737 else if (status
== HTTP_STATUS_UPGRADE_REQUIRED
)
739 /* Flush any error message... */
743 if (httpReconnect2(http
, 30000, NULL
))
745 status
= HTTP_STATUS_ERROR
;
749 /* Upgrade with encryption... */
750 httpEncryption(http
, HTTP_ENCRYPTION_REQUIRED
);
752 /* Try again, this time with encryption enabled... */
755 #endif /* HAVE_SSL */
757 while (status
== HTTP_STATUS_UNAUTHORIZED
||
758 status
== HTTP_STATUS_UPGRADE_REQUIRED
);
760 if (status
== HTTP_STATUS_OK
)
763 printf("HEAD failed with status %d...\n", status
);
765 encoding
= httpGetContentEncoding(http
);
767 printf("Requesting file \"%s\" (Accept-Encoding: %s)...\n", resource
,
768 encoding
? encoding
: "identity");
772 if (!_cups_strcasecmp(httpGetField(http
, HTTP_FIELD_CONNECTION
), "close"))
774 httpClearFields(http
);
775 if (httpReconnect2(http
, 30000, NULL
))
777 status
= HTTP_STATUS_ERROR
;
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
);
787 if (httpGet(http
, resource
))
789 if (httpReconnect2(http
, 30000, NULL
))
791 status
= HTTP_STATUS_ERROR
;
796 status
= HTTP_STATUS_UNAUTHORIZED
;
801 while ((status
= httpUpdate(http
)) == HTTP_STATUS_CONTINUE
);
803 if (status
== HTTP_STATUS_UNAUTHORIZED
)
806 * Flush any error message...
812 * See if we can do authentication...
815 if (cupsDoAuthentication(http
, "GET", resource
))
817 status
= HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
;
821 if (httpReconnect2(http
, 30000, NULL
))
823 status
= HTTP_STATUS_ERROR
;
830 else if (status
== HTTP_STATUS_UPGRADE_REQUIRED
)
832 /* Flush any error message... */
836 if (httpReconnect2(http
, 30000, NULL
))
838 status
= HTTP_STATUS_ERROR
;
842 /* Upgrade with encryption... */
843 httpEncryption(http
, HTTP_ENCRYPTION_REQUIRED
);
845 /* Try again, this time with encryption enabled... */
848 #endif /* HAVE_SSL */
850 while (status
== HTTP_STATUS_UNAUTHORIZED
|| status
== HTTP_STATUS_UPGRADE_REQUIRED
);
852 if (status
== HTTP_STATUS_OK
)
855 printf("GET failed with status %d...\n", status
);
858 length
= httpGetLength2(http
);
861 while ((bytes
= httpRead2(http
, buffer
, sizeof(buffer
))) > 0)
864 fwrite(buffer
, (size_t)bytes
, 1, out
);
867 current
= time(NULL
);
868 if (current
== start
)
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
)));
882 puts("Closing connection to server...");