2 * HTTP test program for CUPS.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include "cups-private.h"
18 * Types and structures...
21 typedef struct uri_test_s
/**** URI test cases ****/
23 http_uri_status_t result
; /* Expected return value */
24 const char *uri
, /* URI */
25 *scheme
, /* Scheme string */
26 *username
, /* Username:password string */
27 *hostname
, /* Hostname string */
28 *resource
; /* Resource string */
29 int port
, /* Port number */
30 assemble_port
; /* Port number for httpAssembleURI() */
31 http_uri_coding_t assemble_coding
;/* Coding for httpAssembleURI() */
39 static uri_test_t uri_tests
[] = /* URI test data */
41 /* Start with valid URIs */
42 { HTTP_URI_STATUS_OK
, "file:/filename",
43 "file", "", "", "/filename", 0, 0,
44 HTTP_URI_CODING_MOST
},
45 { HTTP_URI_STATUS_OK
, "file:/filename%20with%20spaces",
46 "file", "", "", "/filename with spaces", 0, 0,
47 HTTP_URI_CODING_MOST
},
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://localhost/filename",
55 "file", "", "localhost", "/filename", 0, 0,
56 HTTP_URI_CODING_MOST
},
57 { HTTP_URI_STATUS_OK
, "file://localhost/filename%20with%20spaces",
58 "file", "", "localhost", "/filename with spaces", 0, 0,
59 HTTP_URI_CODING_MOST
},
60 { HTTP_URI_STATUS_OK
, "http://server/",
61 "http", "", "server", "/", 80, 0,
62 HTTP_URI_CODING_MOST
},
63 { HTTP_URI_STATUS_OK
, "http://username@server/",
64 "http", "username", "server", "/", 80, 0,
65 HTTP_URI_CODING_MOST
},
66 { HTTP_URI_STATUS_OK
, "http://username:passwor%64@server/",
67 "http", "username:password", "server", "/", 80, 0,
68 HTTP_URI_CODING_MOST
},
69 { HTTP_URI_STATUS_OK
, "http://username:passwor%64@server:8080/",
70 "http", "username:password", "server", "/", 8080, 8080,
71 HTTP_URI_CODING_MOST
},
72 { HTTP_URI_STATUS_OK
, "http://username:passwor%64@server:8080/directory/filename",
73 "http", "username:password", "server", "/directory/filename", 8080, 8080,
74 HTTP_URI_CODING_MOST
},
75 { HTTP_URI_STATUS_OK
, "http://[2000::10:100]:631/ipp",
76 "http", "", "2000::10:100", "/ipp", 631, 631,
77 HTTP_URI_CODING_MOST
},
78 { HTTP_URI_STATUS_OK
, "https://username:passwor%64@server/directory/filename",
79 "https", "username:password", "server", "/directory/filename", 443, 0,
80 HTTP_URI_CODING_MOST
},
81 { HTTP_URI_STATUS_OK
, "ipp://username:passwor%64@[::1]/ipp",
82 "ipp", "username:password", "::1", "/ipp", 631, 0,
83 HTTP_URI_CODING_MOST
},
84 { HTTP_URI_STATUS_OK
, "lpd://server/queue?reserve=yes",
85 "lpd", "", "server", "/queue?reserve=yes", 515, 0,
86 HTTP_URI_CODING_MOST
},
87 { HTTP_URI_STATUS_OK
, "mailto:user@domain.com",
88 "mailto", "", "", "user@domain.com", 0, 0,
89 HTTP_URI_CODING_MOST
},
90 { HTTP_URI_STATUS_OK
, "socket://server/",
91 "socket", "", "server", "/", 9100, 0,
92 HTTP_URI_CODING_MOST
},
93 { HTTP_URI_STATUS_OK
, "socket://192.168.1.1:9101/",
94 "socket", "", "192.168.1.1", "/", 9101, 9101,
95 HTTP_URI_CODING_MOST
},
96 { HTTP_URI_STATUS_OK
, "tel:8005551212",
97 "tel", "", "", "8005551212", 0, 0,
98 HTTP_URI_CODING_MOST
},
99 { HTTP_URI_STATUS_OK
, "ipp://username:password@[v1.fe80::200:1234:5678:9abc+eth0]:999/ipp",
100 "ipp", "username:password", "fe80::200:1234:5678:9abc%eth0", "/ipp", 999, 999,
101 HTTP_URI_CODING_MOST
},
102 { HTTP_URI_STATUS_OK
, "ipp://username:password@[fe80::200:1234:5678:9abc%25eth0]:999/ipp",
103 "ipp", "username:password", "fe80::200:1234:5678:9abc%eth0", "/ipp", 999, 999,
104 (http_uri_coding_t
)(HTTP_URI_CODING_MOST
| HTTP_URI_CODING_RFC6874
) },
105 { HTTP_URI_STATUS_OK
, "http://server/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400",
106 "http", "", "server", "/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400", 80, 0,
107 HTTP_URI_CODING_MOST
},
108 { HTTP_URI_STATUS_OK
, "lpd://Acme%20Laser%20(01%3A23%3A45).local._tcp._printer/",
109 "lpd", "", "Acme Laser (01:23:45).local._tcp._printer", "/", 515, 0,
110 HTTP_URI_CODING_MOST
},
111 { HTTP_URI_STATUS_OK
, "ipp://HP%20Officejet%204500%20G510n-z%20%40%20Will's%20MacBook%20Pro%2015%22._ipp._tcp.local./",
112 "ipp", "", "HP Officejet 4500 G510n-z @ Will's MacBook Pro 15\"._ipp._tcp.local.", "/", 631, 0,
113 HTTP_URI_CODING_MOST
},
114 { HTTP_URI_STATUS_OK
, "ipp://%22%23%2F%3A%3C%3E%3F%40%5B%5C%5D%5E%60%7B%7C%7D/",
115 "ipp", "", "\"#/:<>?@[\\]^`{|}", "/", 631, 0,
116 HTTP_URI_CODING_MOST
},
119 { HTTP_URI_STATUS_MISSING_SCHEME
, "/path/to/file/index.html",
120 "file", "", "", "/path/to/file/index.html", 0, 0,
121 HTTP_URI_CODING_MOST
},
122 { HTTP_URI_STATUS_MISSING_SCHEME
, "//server/ipp",
123 "ipp", "", "server", "/ipp", 631, 0,
124 HTTP_URI_CODING_MOST
},
127 { HTTP_URI_STATUS_UNKNOWN_SCHEME
, "vendor://server/resource",
128 "vendor", "", "server", "/resource", 0, 0,
129 HTTP_URI_CODING_MOST
},
131 /* Missing resource */
132 { HTTP_URI_STATUS_MISSING_RESOURCE
, "socket://[::192.168.2.1]",
133 "socket", "", "::192.168.2.1", "/", 9100, 0,
134 HTTP_URI_CODING_MOST
},
135 { HTTP_URI_STATUS_MISSING_RESOURCE
, "socket://192.168.1.1:9101",
136 "socket", "", "192.168.1.1", "/", 9101, 0,
137 HTTP_URI_CODING_MOST
},
140 { HTTP_URI_STATUS_BAD_URI
, "",
141 "", "", "", "", 0, 0,
142 HTTP_URI_CODING_MOST
},
145 { HTTP_URI_STATUS_BAD_SCHEME
, "bad_scheme://server/resource",
146 "", "", "", "", 0, 0,
147 HTTP_URI_CODING_MOST
},
150 { HTTP_URI_STATUS_BAD_USERNAME
, "http://username:passwor%6@server/resource",
151 "http", "", "", "", 80, 0,
152 HTTP_URI_CODING_MOST
},
155 { HTTP_URI_STATUS_BAD_HOSTNAME
, "http://[/::1]/index.html",
156 "http", "", "", "", 80, 0,
157 HTTP_URI_CODING_MOST
},
158 { HTTP_URI_STATUS_BAD_HOSTNAME
, "http://[",
159 "http", "", "", "", 80, 0,
160 HTTP_URI_CODING_MOST
},
161 { HTTP_URI_STATUS_BAD_HOSTNAME
, "http://serve%7/index.html",
162 "http", "", "", "", 80, 0,
163 HTTP_URI_CODING_MOST
},
164 { HTTP_URI_STATUS_BAD_HOSTNAME
, "http://server with spaces/index.html",
165 "http", "", "", "", 80, 0,
166 HTTP_URI_CODING_MOST
},
167 { HTTP_URI_STATUS_BAD_HOSTNAME
, "ipp://\"#/:<>?@[\\]^`{|}/",
168 "ipp", "", "", "", 631, 0,
169 HTTP_URI_CODING_MOST
},
171 /* Bad port number */
172 { HTTP_URI_STATUS_BAD_PORT
, "http://127.0.0.1:9999a/index.html",
173 "http", "", "127.0.0.1", "", 0, 0,
174 HTTP_URI_CODING_MOST
},
177 { HTTP_URI_STATUS_BAD_RESOURCE
, "http://server/index.html%",
178 "http", "", "server", "", 80, 0,
179 HTTP_URI_CODING_MOST
},
180 { HTTP_URI_STATUS_BAD_RESOURCE
, "http://server/index with spaces.html",
181 "http", "", "server", "", 80, 0,
182 HTTP_URI_CODING_MOST
}
184 static const char * const base64_tests
[][2] =
189 /* 010000 010100 0010 */
191 /* 010000 010100 001001 000011 */
192 { "ABCD", "QUJDRA==" },
193 /* 010000 010100 001001 000011 010001 00 */
194 { "ABCDE", "QUJDREU=" },
195 /* 010000 010100 001001 000011 010001 000100 0101 */
196 { "ABCDEF", "QUJDREVG" },
197 /* 010000 010100 001001 000011 010001 000100 010101 000110 */
202 * 'main()' - Main entry.
205 int /* O - Exit status */
206 main(int argc
, /* I - Number of command-line arguments */
207 char *argv
[]) /* I - Command-line arguments */
209 int i
, j
, k
; /* Looping vars */
210 http_t
*http
; /* HTTP connection */
211 http_encryption_t encryption
; /* Encryption type */
212 http_status_t status
; /* Status of GET command */
213 int failures
; /* Number of test failures */
214 char buffer
[8192]; /* Input buffer */
215 long bytes
; /* Number of bytes read */
216 FILE *out
; /* Output file */
217 char encode
[256], /* Base64-encoded string */
218 decode
[256]; /* Base64-decoded string */
219 int decodelen
; /* Length of decoded string */
220 char scheme
[HTTP_MAX_URI
], /* Scheme from URI */
221 hostname
[HTTP_MAX_URI
], /* Hostname from URI */
222 username
[HTTP_MAX_URI
], /* Username:password from URI */
223 resource
[HTTP_MAX_URI
]; /* Resource from URI */
224 int port
; /* Port number from URI */
225 http_uri_status_t uri_status
; /* Status of URI separation */
226 http_addrlist_t
*addrlist
, /* Address list */
227 *addr
; /* Current address */
228 off_t length
, total
; /* Length and total bytes */
229 time_t start
, current
; /* Start and end time */
230 const char *encoding
; /* Negotiated Content-Encoding */
231 static const char * const uri_status_strings
[] =
233 "HTTP_URI_STATUS_OVERFLOW",
234 "HTTP_URI_STATUS_BAD_ARGUMENTS",
235 "HTTP_URI_STATUS_BAD_RESOURCE",
236 "HTTP_URI_STATUS_BAD_PORT",
237 "HTTP_URI_STATUS_BAD_HOSTNAME",
238 "HTTP_URI_STATUS_BAD_USERNAME",
239 "HTTP_URI_STATUS_BAD_SCHEME",
240 "HTTP_URI_STATUS_BAD_URI",
241 "HTTP_URI_STATUS_OK",
242 "HTTP_URI_STATUS_MISSING_SCHEME",
243 "HTTP_URI_STATUS_UNKNOWN_SCHEME",
244 "HTTP_URI_STATUS_MISSING_RESOURCE"
249 * Do API tests if we don't have a URL on the command-line...
257 * httpGetDateString()/httpGetDateTime()
260 fputs("httpGetDateString()/httpGetDateTime(): ", stdout
);
263 strlcpy(buffer
, httpGetDateString(start
), sizeof(buffer
));
264 current
= httpGetDateTime(buffer
);
266 i
= (int)(current
- start
);
276 printf(" Difference is %d seconds, %02d:%02d:%02d...\n", i
, i
/ 3600,
277 (i
/ 60) % 60, i
% 60);
278 printf(" httpGetDateString(%d) returned \"%s\"\n", (int)start
, buffer
);
279 printf(" httpGetDateTime(\"%s\") returned %d\n", buffer
, (int)current
);
280 printf(" httpGetDateString(%d) returned \"%s\"\n", (int)current
,
281 httpGetDateString(current
));
285 * httpDecode64_2()/httpEncode64_2()
288 fputs("httpDecode64_2()/httpEncode64_2(): ", stdout
);
290 for (i
= 0, j
= 0; i
< (int)(sizeof(base64_tests
) / sizeof(base64_tests
[0])); i
++)
292 httpEncode64_2(encode
, sizeof(encode
), base64_tests
[i
][0],
293 (int)strlen(base64_tests
[i
][0]));
294 decodelen
= (int)sizeof(decode
);
295 httpDecode64_2(decode
, &decodelen
, base64_tests
[i
][1]);
297 if (strcmp(decode
, base64_tests
[i
][0]))
307 printf(" httpDecode64_2() returned \"%s\", expected \"%s\"...\n",
308 decode
, base64_tests
[i
][0]);
311 if (strcmp(encode
, base64_tests
[i
][1]))
321 printf(" httpEncode64_2() returned \"%s\", expected \"%s\"...\n",
322 encode
, base64_tests
[i
][1]);
333 fputs("httpGetHostname(): ", stdout
);
335 if (httpGetHostname(NULL
, hostname
, sizeof(hostname
)))
336 printf("PASS (%s)\n", hostname
);
347 printf("httpAddrGetList(%s): ", hostname
);
349 addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, NULL
);
352 for (i
= 0, addr
= addrlist
; addr
; i
++, addr
= addr
->next
)
354 char numeric
[1024]; /* Numeric IP address */
357 httpAddrString(&(addr
->addr
), numeric
, sizeof(numeric
));
358 if (!strcmp(numeric
, "UNKNOWN"))
363 printf("FAIL (bad address for %s)\n", hostname
);
365 printf("PASS (%d address(es) for %s)\n", i
, hostname
);
367 httpAddrFreeList(addrlist
);
369 else if (isdigit(hostname
[0] & 255))
371 puts("FAIL (ignored because hostname is numeric)");
380 * Test httpSeparateURI()...
383 fputs("httpSeparateURI(): ", stdout
);
384 for (i
= 0, j
= 0; i
< (int)(sizeof(uri_tests
) / sizeof(uri_tests
[0])); i
++)
386 uri_status
= httpSeparateURI(HTTP_URI_CODING_MOST
,
387 uri_tests
[i
].uri
, scheme
, sizeof(scheme
),
388 username
, sizeof(username
),
389 hostname
, sizeof(hostname
), &port
,
390 resource
, sizeof(resource
));
391 if (uri_status
!= uri_tests
[i
].result
||
392 strcmp(scheme
, uri_tests
[i
].scheme
) ||
393 strcmp(username
, uri_tests
[i
].username
) ||
394 strcmp(hostname
, uri_tests
[i
].hostname
) ||
395 port
!= uri_tests
[i
].port
||
396 strcmp(resource
, uri_tests
[i
].resource
))
406 printf(" \"%s\":\n", uri_tests
[i
].uri
);
408 if (uri_status
!= uri_tests
[i
].result
)
409 printf(" Returned %s instead of %s\n",
410 uri_status_strings
[uri_status
+ 8],
411 uri_status_strings
[uri_tests
[i
].result
+ 8]);
413 if (strcmp(scheme
, uri_tests
[i
].scheme
))
414 printf(" Scheme \"%s\" instead of \"%s\"\n",
415 scheme
, uri_tests
[i
].scheme
);
417 if (strcmp(username
, uri_tests
[i
].username
))
418 printf(" Username \"%s\" instead of \"%s\"\n",
419 username
, uri_tests
[i
].username
);
421 if (strcmp(hostname
, uri_tests
[i
].hostname
))
422 printf(" Hostname \"%s\" instead of \"%s\"\n",
423 hostname
, uri_tests
[i
].hostname
);
425 if (port
!= uri_tests
[i
].port
)
426 printf(" Port %d instead of %d\n",
427 port
, uri_tests
[i
].port
);
429 if (strcmp(resource
, uri_tests
[i
].resource
))
430 printf(" Resource \"%s\" instead of \"%s\"\n",
431 resource
, uri_tests
[i
].resource
);
436 printf("PASS (%d URIs tested)\n",
437 (int)(sizeof(uri_tests
) / sizeof(uri_tests
[0])));
440 * Test httpAssembleURI()...
443 fputs("httpAssembleURI(): ", stdout
);
444 for (i
= 0, j
= 0, k
= 0;
445 i
< (int)(sizeof(uri_tests
) / sizeof(uri_tests
[0]));
447 if (uri_tests
[i
].result
== HTTP_URI_STATUS_OK
&&
448 !strstr(uri_tests
[i
].uri
, "%64") &&
449 strstr(uri_tests
[i
].uri
, "//"))
452 uri_status
= httpAssembleURI(uri_tests
[i
].assemble_coding
,
453 buffer
, sizeof(buffer
),
455 uri_tests
[i
].username
,
456 uri_tests
[i
].hostname
,
457 uri_tests
[i
].assemble_port
,
458 uri_tests
[i
].resource
);
460 if (uri_status
!= HTTP_URI_STATUS_OK
)
470 printf(" \"%s\": %s\n", uri_tests
[i
].uri
,
471 uri_status_strings
[uri_status
+ 8]);
473 else if (strcmp(buffer
, uri_tests
[i
].uri
))
483 printf(" \"%s\": assembled = \"%s\"\n", uri_tests
[i
].uri
,
489 printf("PASS (%d URIs tested)\n", k
);
495 fputs("httpAssembleUUID: ", stdout
);
496 httpAssembleUUID("hostname.example.com", 631, "printer", 12345, buffer
,
498 if (strncmp(buffer
, "urn:uuid:", 9))
500 printf("FAIL (%s)\n", buffer
);
504 printf("PASS (%s)\n", buffer
);
507 * Show a summary and return...
511 printf("\n%d TESTS FAILED!\n", failures
);
513 puts("\nALL TESTS PASSED!");
517 else if (strstr(argv
[1], "._tcp"))
520 * Test resolving an mDNS name.
523 char resolved
[1024]; /* Resolved URI */
526 printf("_httpResolveURI(%s, _HTTP_RESOLVE_DEFAULT): ", argv
[1]);
529 if (!_httpResolveURI(argv
[1], resolved
, sizeof(resolved
),
530 _HTTP_RESOLVE_DEFAULT
, NULL
, NULL
))
536 printf("PASS (%s)\n", resolved
);
538 printf("_httpResolveURI(%s, _HTTP_RESOLVE_FQDN): ", argv
[1]);
541 if (!_httpResolveURI(argv
[1], resolved
, sizeof(resolved
),
542 _HTTP_RESOLVE_FQDN
, NULL
, NULL
))
547 else if (strstr(resolved
, ".local:"))
549 printf("FAIL (%s)\n", resolved
);
554 printf("PASS (%s)\n", resolved
);
558 else if (!strcmp(argv
[1], "-u") && argc
== 3)
561 * Test URI separation...
564 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
, argv
[2], scheme
,
565 sizeof(scheme
), username
, sizeof(username
),
566 hostname
, sizeof(hostname
), &port
,
567 resource
, sizeof(resource
));
568 printf("uri_status = %s\n", uri_status_strings
[uri_status
+ 8]);
569 printf("scheme = \"%s\"\n", scheme
);
570 printf("username = \"%s\"\n", username
);
571 printf("hostname = \"%s\"\n", hostname
);
572 printf("port = %d\n", port
);
573 printf("resource = \"%s\"\n", resource
);
579 * Test HTTP GET requests...
585 for (i
= 1; i
< argc
; i
++)
587 if (!strcmp(argv
[i
], "-o"))
593 out
= fopen(argv
[i
], "wb");
597 httpSeparateURI(HTTP_URI_CODING_MOST
, argv
[i
], scheme
, sizeof(scheme
),
598 username
, sizeof(username
),
599 hostname
, sizeof(hostname
), &port
,
600 resource
, sizeof(resource
));
602 if (!_cups_strcasecmp(scheme
, "https") || !_cups_strcasecmp(scheme
, "ipps") ||
604 encryption
= HTTP_ENCRYPTION_ALWAYS
;
606 encryption
= HTTP_ENCRYPTION_IF_REQUESTED
;
608 http
= httpConnect2(hostname
, port
, NULL
, AF_UNSPEC
, encryption
, 1, 30000, NULL
);
615 if (httpIsEncrypted(http
))
619 static const char *trusts
[] = { "OK", "Invalid", "Changed", "Expired", "Renewed", "Unknown" };
620 if (!httpCopyCredentials(http
, &creds
))
622 cups_array_t
*lcreds
;
623 http_trust_t trust
= httpCredentialsGetTrust(creds
, hostname
);
625 httpCredentialsString(creds
, info
, sizeof(info
));
627 printf("Count: %d\n", cupsArrayCount(creds
));
628 printf("Trust: %s\n", trusts
[trust
]);
629 printf("Expiration: %s\n", httpGetDateString(httpCredentialsGetExpiration(creds
)));
630 printf("IsValidName: %d\n", httpCredentialsAreValidForName(creds
, hostname
));
631 printf("String: \"%s\"\n", info
);
633 printf("LoadCredentials: %d\n", httpLoadCredentials(NULL
, &lcreds
, hostname
));
634 httpCredentialsString(lcreds
, info
, sizeof(info
));
635 printf(" Count: %d\n", cupsArrayCount(lcreds
));
636 printf(" String: \"%s\"\n", info
);
638 if (lcreds
&& cupsArrayCount(creds
) == cupsArrayCount(lcreds
))
640 http_credential_t
*cred
, *lcred
;
642 for (i
= 1, cred
= (http_credential_t
*)cupsArrayFirst(creds
), lcred
= (http_credential_t
*)cupsArrayFirst(lcreds
);
644 i
++, cred
= (http_credential_t
*)cupsArrayNext(creds
), lcred
= (http_credential_t
*)cupsArrayNext(lcreds
))
646 if (cred
->datalen
!= lcred
->datalen
)
647 printf(" Credential #%d: Different lengths (saved=%d, current=%d)\n", i
, (int)cred
->datalen
, (int)lcred
->datalen
);
648 else if (memcmp(cred
->data
, lcred
->data
, cred
->datalen
))
649 printf(" Credential #%d: Different data\n", i
);
651 printf(" Credential #%d: Matches\n", i
);
655 if (trust
!= HTTP_TRUST_OK
)
657 printf("SaveCredentials: %d\n", httpSaveCredentials(NULL
, creds
, hostname
));
658 trust
= httpCredentialsGetTrust(creds
, hostname
);
659 printf("New Trust: %s\n", trusts
[trust
]);
662 httpFreeCredentials(creds
);
665 puts("No credentials!");
668 printf("Checking file \"%s\"...\n", resource
);
672 if (!_cups_strcasecmp(httpGetField(http
, HTTP_FIELD_CONNECTION
), "close"))
674 httpClearFields(http
);
675 if (httpReconnect2(http
, 30000, NULL
))
677 status
= HTTP_STATUS_ERROR
;
682 httpClearFields(http
);
683 httpSetField(http
, HTTP_FIELD_AUTHORIZATION
, httpGetAuthString(http
));
684 httpSetField(http
, HTTP_FIELD_ACCEPT_LANGUAGE
, "en");
685 if (httpHead(http
, resource
))
687 if (httpReconnect2(http
, 30000, NULL
))
689 status
= HTTP_STATUS_ERROR
;
694 status
= HTTP_STATUS_UNAUTHORIZED
;
699 while ((status
= httpUpdate(http
)) == HTTP_STATUS_CONTINUE
);
701 if (status
== HTTP_STATUS_UNAUTHORIZED
)
704 * Flush any error message...
710 * See if we can do authentication...
713 if (cupsDoAuthentication(http
, "GET", resource
))
715 status
= HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
;
719 if (httpReconnect2(http
, 30000, NULL
))
721 status
= HTTP_STATUS_ERROR
;
728 else if (status
== HTTP_STATUS_UPGRADE_REQUIRED
)
730 /* Flush any error message... */
734 if (httpReconnect2(http
, 30000, NULL
))
736 status
= HTTP_STATUS_ERROR
;
740 /* Upgrade with encryption... */
741 httpEncryption(http
, HTTP_ENCRYPTION_REQUIRED
);
743 /* Try again, this time with encryption enabled... */
746 #endif /* HAVE_SSL */
748 while (status
== HTTP_STATUS_UNAUTHORIZED
||
749 status
== HTTP_STATUS_UPGRADE_REQUIRED
);
751 if (status
== HTTP_STATUS_OK
)
754 printf("HEAD failed with status %d...\n", status
);
756 encoding
= httpGetContentEncoding(http
);
758 printf("Requesting file \"%s\" (Accept-Encoding: %s)...\n", resource
,
759 encoding
? encoding
: "identity");
763 if (!_cups_strcasecmp(httpGetField(http
, HTTP_FIELD_CONNECTION
), "close"))
765 httpClearFields(http
);
766 if (httpReconnect2(http
, 30000, NULL
))
768 status
= HTTP_STATUS_ERROR
;
773 httpClearFields(http
);
774 httpSetField(http
, HTTP_FIELD_AUTHORIZATION
, httpGetAuthString(http
));
775 httpSetField(http
, HTTP_FIELD_ACCEPT_LANGUAGE
, "en");
776 httpSetField(http
, HTTP_FIELD_ACCEPT_ENCODING
, encoding
);
778 if (httpGet(http
, resource
))
780 if (httpReconnect2(http
, 30000, NULL
))
782 status
= HTTP_STATUS_ERROR
;
787 status
= HTTP_STATUS_UNAUTHORIZED
;
792 while ((status
= httpUpdate(http
)) == HTTP_STATUS_CONTINUE
);
794 if (status
== HTTP_STATUS_UNAUTHORIZED
)
797 * Flush any error message...
803 * See if we can do authentication...
806 if (cupsDoAuthentication(http
, "GET", resource
))
808 status
= HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
;
812 if (httpReconnect2(http
, 30000, NULL
))
814 status
= HTTP_STATUS_ERROR
;
821 else if (status
== HTTP_STATUS_UPGRADE_REQUIRED
)
823 /* Flush any error message... */
827 if (httpReconnect2(http
, 30000, NULL
))
829 status
= HTTP_STATUS_ERROR
;
833 /* Upgrade with encryption... */
834 httpEncryption(http
, HTTP_ENCRYPTION_REQUIRED
);
836 /* Try again, this time with encryption enabled... */
839 #endif /* HAVE_SSL */
841 while (status
== HTTP_STATUS_UNAUTHORIZED
|| status
== HTTP_STATUS_UPGRADE_REQUIRED
);
843 if (status
== HTTP_STATUS_OK
)
846 printf("GET failed with status %d...\n", status
);
849 length
= httpGetLength2(http
);
852 while ((bytes
= httpRead2(http
, buffer
, sizeof(buffer
))) > 0)
855 fwrite(buffer
, (size_t)bytes
, 1, out
);
858 current
= time(NULL
);
859 if (current
== start
)
862 printf("\r" CUPS_LLFMT
"/" CUPS_LLFMT
" bytes ("
863 CUPS_LLFMT
" bytes/sec) ", CUPS_LLCAST total
,
864 CUPS_LLCAST length
, CUPS_LLCAST (total
/ (current
- start
)));
873 puts("Closing connection to server...");