2 * HTTP test program for CUPS.
4 * Copyright 2007-2014 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
, "http://server/index.html%",
184 "http", "", "server", "", 80, 0,
185 HTTP_URI_CODING_MOST
},
186 { HTTP_URI_STATUS_BAD_RESOURCE
, "http://server/index with spaces.html",
187 "http", "", "server", "", 80, 0,
188 HTTP_URI_CODING_MOST
}
190 static const char * const base64_tests
[][2] =
195 /* 010000 010100 0010 */
197 /* 010000 010100 001001 000011 */
198 { "ABCD", "QUJDRA==" },
199 /* 010000 010100 001001 000011 010001 00 */
200 { "ABCDE", "QUJDREU=" },
201 /* 010000 010100 001001 000011 010001 000100 0101 */
202 { "ABCDEF", "QUJDREVG" },
203 /* 010000 010100 001001 000011 010001 000100 010101 000110 */
208 * 'main()' - Main entry.
211 int /* O - Exit status */
212 main(int argc
, /* I - Number of command-line arguments */
213 char *argv
[]) /* I - Command-line arguments */
215 int i
, j
, k
; /* Looping vars */
216 http_t
*http
; /* HTTP connection */
217 http_encryption_t encryption
; /* Encryption type */
218 http_status_t status
; /* Status of GET command */
219 int failures
; /* Number of test failures */
220 char buffer
[8192]; /* Input buffer */
221 long bytes
; /* Number of bytes read */
222 FILE *out
; /* Output file */
223 char encode
[256], /* Base64-encoded string */
224 decode
[256]; /* Base64-decoded string */
225 int decodelen
; /* Length of decoded string */
226 char scheme
[HTTP_MAX_URI
], /* Scheme from URI */
227 hostname
[HTTP_MAX_URI
], /* Hostname from URI */
228 username
[HTTP_MAX_URI
], /* Username:password from URI */
229 resource
[HTTP_MAX_URI
]; /* Resource from URI */
230 int port
; /* Port number from URI */
231 http_uri_status_t uri_status
; /* Status of URI separation */
232 http_addrlist_t
*addrlist
, /* Address list */
233 *addr
; /* Current address */
234 off_t length
, total
; /* Length and total bytes */
235 time_t start
, current
; /* Start and end time */
236 const char *encoding
; /* Negotiated Content-Encoding */
237 static const char * const uri_status_strings
[] =
239 "HTTP_URI_STATUS_OVERFLOW",
240 "HTTP_URI_STATUS_BAD_ARGUMENTS",
241 "HTTP_URI_STATUS_BAD_RESOURCE",
242 "HTTP_URI_STATUS_BAD_PORT",
243 "HTTP_URI_STATUS_BAD_HOSTNAME",
244 "HTTP_URI_STATUS_BAD_USERNAME",
245 "HTTP_URI_STATUS_BAD_SCHEME",
246 "HTTP_URI_STATUS_BAD_URI",
247 "HTTP_URI_STATUS_OK",
248 "HTTP_URI_STATUS_MISSING_SCHEME",
249 "HTTP_URI_STATUS_UNKNOWN_SCHEME",
250 "HTTP_URI_STATUS_MISSING_RESOURCE"
255 * Do API tests if we don't have a URL on the command-line...
263 * httpGetDateString()/httpGetDateTime()
266 fputs("httpGetDateString()/httpGetDateTime(): ", stdout
);
269 strlcpy(buffer
, httpGetDateString(start
), sizeof(buffer
));
270 current
= httpGetDateTime(buffer
);
272 i
= (int)(current
- start
);
282 printf(" Difference is %d seconds, %02d:%02d:%02d...\n", i
, i
/ 3600,
283 (i
/ 60) % 60, i
% 60);
284 printf(" httpGetDateString(%d) returned \"%s\"\n", (int)start
, buffer
);
285 printf(" httpGetDateTime(\"%s\") returned %d\n", buffer
, (int)current
);
286 printf(" httpGetDateString(%d) returned \"%s\"\n", (int)current
,
287 httpGetDateString(current
));
291 * httpDecode64_2()/httpEncode64_2()
294 fputs("httpDecode64_2()/httpEncode64_2(): ", stdout
);
296 for (i
= 0, j
= 0; i
< (int)(sizeof(base64_tests
) / sizeof(base64_tests
[0])); i
++)
298 httpEncode64_2(encode
, sizeof(encode
), base64_tests
[i
][0],
299 (int)strlen(base64_tests
[i
][0]));
300 decodelen
= (int)sizeof(decode
);
301 httpDecode64_2(decode
, &decodelen
, base64_tests
[i
][1]);
303 if (strcmp(decode
, base64_tests
[i
][0]))
313 printf(" httpDecode64_2() returned \"%s\", expected \"%s\"...\n",
314 decode
, base64_tests
[i
][0]);
317 if (strcmp(encode
, base64_tests
[i
][1]))
327 printf(" httpEncode64_2() returned \"%s\", expected \"%s\"...\n",
328 encode
, base64_tests
[i
][1]);
339 fputs("httpGetHostname(): ", stdout
);
341 if (httpGetHostname(NULL
, hostname
, sizeof(hostname
)))
342 printf("PASS (%s)\n", hostname
);
353 printf("httpAddrGetList(%s): ", hostname
);
355 addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, NULL
);
358 for (i
= 0, addr
= addrlist
; addr
; i
++, addr
= addr
->next
)
360 char numeric
[1024]; /* Numeric IP address */
363 httpAddrString(&(addr
->addr
), numeric
, sizeof(numeric
));
364 if (!strcmp(numeric
, "UNKNOWN"))
369 printf("FAIL (bad address for %s)\n", hostname
);
371 printf("PASS (%d address(es) for %s)\n", i
, hostname
);
373 httpAddrFreeList(addrlist
);
375 else if (isdigit(hostname
[0] & 255))
377 puts("FAIL (ignored because hostname is numeric)");
386 * Test httpSeparateURI()...
389 fputs("httpSeparateURI(): ", stdout
);
390 for (i
= 0, j
= 0; i
< (int)(sizeof(uri_tests
) / sizeof(uri_tests
[0])); i
++)
392 uri_status
= httpSeparateURI(HTTP_URI_CODING_MOST
,
393 uri_tests
[i
].uri
, scheme
, sizeof(scheme
),
394 username
, sizeof(username
),
395 hostname
, sizeof(hostname
), &port
,
396 resource
, sizeof(resource
));
397 if (uri_status
!= uri_tests
[i
].result
||
398 strcmp(scheme
, uri_tests
[i
].scheme
) ||
399 strcmp(username
, uri_tests
[i
].username
) ||
400 strcmp(hostname
, uri_tests
[i
].hostname
) ||
401 port
!= uri_tests
[i
].port
||
402 strcmp(resource
, uri_tests
[i
].resource
))
412 printf(" \"%s\":\n", uri_tests
[i
].uri
);
414 if (uri_status
!= uri_tests
[i
].result
)
415 printf(" Returned %s instead of %s\n",
416 uri_status_strings
[uri_status
+ 8],
417 uri_status_strings
[uri_tests
[i
].result
+ 8]);
419 if (strcmp(scheme
, uri_tests
[i
].scheme
))
420 printf(" Scheme \"%s\" instead of \"%s\"\n",
421 scheme
, uri_tests
[i
].scheme
);
423 if (strcmp(username
, uri_tests
[i
].username
))
424 printf(" Username \"%s\" instead of \"%s\"\n",
425 username
, uri_tests
[i
].username
);
427 if (strcmp(hostname
, uri_tests
[i
].hostname
))
428 printf(" Hostname \"%s\" instead of \"%s\"\n",
429 hostname
, uri_tests
[i
].hostname
);
431 if (port
!= uri_tests
[i
].port
)
432 printf(" Port %d instead of %d\n",
433 port
, uri_tests
[i
].port
);
435 if (strcmp(resource
, uri_tests
[i
].resource
))
436 printf(" Resource \"%s\" instead of \"%s\"\n",
437 resource
, uri_tests
[i
].resource
);
442 printf("PASS (%d URIs tested)\n",
443 (int)(sizeof(uri_tests
) / sizeof(uri_tests
[0])));
446 * Test httpAssembleURI()...
449 fputs("httpAssembleURI(): ", stdout
);
450 for (i
= 0, j
= 0, k
= 0;
451 i
< (int)(sizeof(uri_tests
) / sizeof(uri_tests
[0]));
453 if (uri_tests
[i
].result
== HTTP_URI_STATUS_OK
&&
454 !strstr(uri_tests
[i
].uri
, "%64") &&
455 strstr(uri_tests
[i
].uri
, "//"))
458 uri_status
= httpAssembleURI(uri_tests
[i
].assemble_coding
,
459 buffer
, sizeof(buffer
),
461 uri_tests
[i
].username
,
462 uri_tests
[i
].hostname
,
463 uri_tests
[i
].assemble_port
,
464 uri_tests
[i
].resource
);
466 if (uri_status
!= HTTP_URI_STATUS_OK
)
476 printf(" \"%s\": %s\n", uri_tests
[i
].uri
,
477 uri_status_strings
[uri_status
+ 8]);
479 else if (strcmp(buffer
, uri_tests
[i
].uri
))
489 printf(" \"%s\": assembled = \"%s\"\n", uri_tests
[i
].uri
,
495 printf("PASS (%d URIs tested)\n", k
);
501 fputs("httpAssembleUUID: ", stdout
);
502 httpAssembleUUID("hostname.example.com", 631, "printer", 12345, buffer
,
504 if (strncmp(buffer
, "urn:uuid:", 9))
506 printf("FAIL (%s)\n", buffer
);
510 printf("PASS (%s)\n", buffer
);
513 * Show a summary and return...
517 printf("\n%d TESTS FAILED!\n", failures
);
519 puts("\nALL TESTS PASSED!");
523 else if (strstr(argv
[1], "._tcp"))
526 * Test resolving an mDNS name.
529 char resolved
[1024]; /* Resolved URI */
532 printf("_httpResolveURI(%s, _HTTP_RESOLVE_DEFAULT): ", argv
[1]);
535 if (!_httpResolveURI(argv
[1], resolved
, sizeof(resolved
),
536 _HTTP_RESOLVE_DEFAULT
, NULL
, NULL
))
542 printf("PASS (%s)\n", resolved
);
544 printf("_httpResolveURI(%s, _HTTP_RESOLVE_FQDN): ", argv
[1]);
547 if (!_httpResolveURI(argv
[1], resolved
, sizeof(resolved
),
548 _HTTP_RESOLVE_FQDN
, NULL
, NULL
))
553 else if (strstr(resolved
, ".local:"))
555 printf("FAIL (%s)\n", resolved
);
560 printf("PASS (%s)\n", resolved
);
564 else if (!strcmp(argv
[1], "-u") && argc
== 3)
567 * Test URI separation...
570 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
, argv
[2], scheme
,
571 sizeof(scheme
), username
, sizeof(username
),
572 hostname
, sizeof(hostname
), &port
,
573 resource
, sizeof(resource
));
574 printf("uri_status = %s\n", uri_status_strings
[uri_status
+ 8]);
575 printf("scheme = \"%s\"\n", scheme
);
576 printf("username = \"%s\"\n", username
);
577 printf("hostname = \"%s\"\n", hostname
);
578 printf("port = %d\n", port
);
579 printf("resource = \"%s\"\n", resource
);
585 * Test HTTP GET requests...
591 for (i
= 1; i
< argc
; i
++)
593 if (!strcmp(argv
[i
], "-o"))
599 out
= fopen(argv
[i
], "wb");
603 httpSeparateURI(HTTP_URI_CODING_MOST
, argv
[i
], scheme
, sizeof(scheme
),
604 username
, sizeof(username
),
605 hostname
, sizeof(hostname
), &port
,
606 resource
, sizeof(resource
));
608 if (!_cups_strcasecmp(scheme
, "https") || !_cups_strcasecmp(scheme
, "ipps") ||
610 encryption
= HTTP_ENCRYPTION_ALWAYS
;
612 encryption
= HTTP_ENCRYPTION_IF_REQUESTED
;
614 http
= httpConnect2(hostname
, port
, NULL
, AF_UNSPEC
, encryption
, 1, 30000, NULL
);
621 if (httpIsEncrypted(http
))
625 static const char *trusts
[] = { "OK", "Invalid", "Changed", "Expired", "Renewed", "Unknown" };
626 if (!httpCopyCredentials(http
, &creds
))
628 cups_array_t
*lcreds
;
629 http_trust_t trust
= httpCredentialsGetTrust(creds
, hostname
);
631 httpCredentialsString(creds
, info
, sizeof(info
));
633 printf("Count: %d\n", cupsArrayCount(creds
));
634 printf("Trust: %s\n", trusts
[trust
]);
635 printf("Expiration: %s\n", httpGetDateString(httpCredentialsGetExpiration(creds
)));
636 printf("IsValidName: %d\n", httpCredentialsAreValidForName(creds
, hostname
));
637 printf("String: \"%s\"\n", info
);
639 printf("LoadCredentials: %d\n", httpLoadCredentials(NULL
, &lcreds
, hostname
));
640 httpCredentialsString(lcreds
, info
, sizeof(info
));
641 printf(" Count: %d\n", cupsArrayCount(lcreds
));
642 printf(" String: \"%s\"\n", info
);
644 if (lcreds
&& cupsArrayCount(creds
) == cupsArrayCount(lcreds
))
646 http_credential_t
*cred
, *lcred
;
648 for (i
= 1, cred
= (http_credential_t
*)cupsArrayFirst(creds
), lcred
= (http_credential_t
*)cupsArrayFirst(lcreds
);
650 i
++, cred
= (http_credential_t
*)cupsArrayNext(creds
), lcred
= (http_credential_t
*)cupsArrayNext(lcreds
))
652 if (cred
->datalen
!= lcred
->datalen
)
653 printf(" Credential #%d: Different lengths (saved=%d, current=%d)\n", i
, (int)cred
->datalen
, (int)lcred
->datalen
);
654 else if (memcmp(cred
->data
, lcred
->data
, cred
->datalen
))
655 printf(" Credential #%d: Different data\n", i
);
657 printf(" Credential #%d: Matches\n", i
);
661 if (trust
!= HTTP_TRUST_OK
)
663 printf("SaveCredentials: %d\n", httpSaveCredentials(NULL
, creds
, hostname
));
664 trust
= httpCredentialsGetTrust(creds
, hostname
);
665 printf("New Trust: %s\n", trusts
[trust
]);
668 httpFreeCredentials(creds
);
671 puts("No credentials!");
674 printf("Checking file \"%s\"...\n", resource
);
678 if (!_cups_strcasecmp(httpGetField(http
, HTTP_FIELD_CONNECTION
), "close"))
680 httpClearFields(http
);
681 if (httpReconnect2(http
, 30000, NULL
))
683 status
= HTTP_STATUS_ERROR
;
688 httpClearFields(http
);
689 httpSetField(http
, HTTP_FIELD_AUTHORIZATION
, httpGetAuthString(http
));
690 httpSetField(http
, HTTP_FIELD_ACCEPT_LANGUAGE
, "en");
691 if (httpHead(http
, resource
))
693 if (httpReconnect2(http
, 30000, NULL
))
695 status
= HTTP_STATUS_ERROR
;
700 status
= HTTP_STATUS_UNAUTHORIZED
;
705 while ((status
= httpUpdate(http
)) == HTTP_STATUS_CONTINUE
);
707 if (status
== HTTP_STATUS_UNAUTHORIZED
)
710 * Flush any error message...
716 * See if we can do authentication...
719 if (cupsDoAuthentication(http
, "GET", resource
))
721 status
= HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
;
725 if (httpReconnect2(http
, 30000, NULL
))
727 status
= HTTP_STATUS_ERROR
;
734 else if (status
== HTTP_STATUS_UPGRADE_REQUIRED
)
736 /* Flush any error message... */
740 if (httpReconnect2(http
, 30000, NULL
))
742 status
= HTTP_STATUS_ERROR
;
746 /* Upgrade with encryption... */
747 httpEncryption(http
, HTTP_ENCRYPTION_REQUIRED
);
749 /* Try again, this time with encryption enabled... */
752 #endif /* HAVE_SSL */
754 while (status
== HTTP_STATUS_UNAUTHORIZED
||
755 status
== HTTP_STATUS_UPGRADE_REQUIRED
);
757 if (status
== HTTP_STATUS_OK
)
760 printf("HEAD failed with status %d...\n", status
);
762 encoding
= httpGetContentEncoding(http
);
764 printf("Requesting file \"%s\" (Accept-Encoding: %s)...\n", resource
,
765 encoding
? encoding
: "identity");
769 if (!_cups_strcasecmp(httpGetField(http
, HTTP_FIELD_CONNECTION
), "close"))
771 httpClearFields(http
);
772 if (httpReconnect2(http
, 30000, NULL
))
774 status
= HTTP_STATUS_ERROR
;
779 httpClearFields(http
);
780 httpSetField(http
, HTTP_FIELD_AUTHORIZATION
, httpGetAuthString(http
));
781 httpSetField(http
, HTTP_FIELD_ACCEPT_LANGUAGE
, "en");
782 httpSetField(http
, HTTP_FIELD_ACCEPT_ENCODING
, encoding
);
784 if (httpGet(http
, resource
))
786 if (httpReconnect2(http
, 30000, NULL
))
788 status
= HTTP_STATUS_ERROR
;
793 status
= HTTP_STATUS_UNAUTHORIZED
;
798 while ((status
= httpUpdate(http
)) == HTTP_STATUS_CONTINUE
);
800 if (status
== HTTP_STATUS_UNAUTHORIZED
)
803 * Flush any error message...
809 * See if we can do authentication...
812 if (cupsDoAuthentication(http
, "GET", resource
))
814 status
= HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
;
818 if (httpReconnect2(http
, 30000, NULL
))
820 status
= HTTP_STATUS_ERROR
;
827 else if (status
== HTTP_STATUS_UPGRADE_REQUIRED
)
829 /* Flush any error message... */
833 if (httpReconnect2(http
, 30000, NULL
))
835 status
= HTTP_STATUS_ERROR
;
839 /* Upgrade with encryption... */
840 httpEncryption(http
, HTTP_ENCRYPTION_REQUIRED
);
842 /* Try again, this time with encryption enabled... */
845 #endif /* HAVE_SSL */
847 while (status
== HTTP_STATUS_UNAUTHORIZED
|| status
== HTTP_STATUS_UPGRADE_REQUIRED
);
849 if (status
== HTTP_STATUS_OK
)
852 printf("GET failed with status %d...\n", status
);
855 length
= httpGetLength2(http
);
858 while ((bytes
= httpRead2(http
, buffer
, sizeof(buffer
))) > 0)
861 fwrite(buffer
, (size_t)bytes
, 1, out
);
864 current
= time(NULL
);
865 if (current
== start
)
868 printf("\r" CUPS_LLFMT
"/" CUPS_LLFMT
" bytes ("
869 CUPS_LLFMT
" bytes/sec) ", CUPS_LLCAST total
,
870 CUPS_LLCAST length
, CUPS_LLCAST (total
/ (current
- start
)));
879 puts("Closing connection to server...");