]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
bc44d920 | 2 | * "$Id: http.c 6649 2007-07-11 21:46:42Z mike $" |
ef416fc2 | 3 | * |
4 | * HTTP routines for the Common UNIX Printing System (CUPS). | |
5 | * | |
bc44d920 | 6 | * Copyright 2007 by Apple Inc. |
b86bc4cf | 7 | * Copyright 1997-2007 by Easy Software Products, all rights reserved. |
ef416fc2 | 8 | * |
f7deaa1a | 9 | * This file contains Kerberos support code, copyright 2006 by |
10 | * Jelmer Vernooij. | |
11 | * | |
ef416fc2 | 12 | * These coded instructions, statements, and computer programs are the |
bc44d920 | 13 | * property of Apple Inc. and are protected by Federal copyright |
14 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
15 | * which should have been included with this file. If this file is | |
16 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
ef416fc2 | 17 | * |
18 | * This file is subject to the Apple OS-Developed Software exception. | |
19 | * | |
20 | * Contents: | |
21 | * | |
411affcf | 22 | * _httpBIOMethods() - Get the OpenSSL BIO methods for HTTP connections. |
ecdc0628 | 23 | * httpBlocking() - Set blocking/non-blocking behavior on a connection. |
ef416fc2 | 24 | * httpCheck() - Check to see if there is a pending response from |
25 | * the server. | |
26 | * httpClearCookie() - Clear the cookie value(s). | |
ecdc0628 | 27 | * httpClearFields() - Clear HTTP request fields. |
ef416fc2 | 28 | * httpClose() - Close an HTTP connection... |
29 | * httpConnect() - Connect to a HTTP server. | |
30 | * httpConnectEncrypt() - Connect to a HTTP server using encryption. | |
31 | * httpDelete() - Send a DELETE request to the server. | |
32 | * httpEncryption() - Set the required encryption on the link. | |
ecdc0628 | 33 | * httpError() - Get the last error on a connection. |
ef416fc2 | 34 | * httpFlush() - Flush data from a HTTP connection. |
35 | * httpFlushWrite() - Flush data in write buffer. | |
36 | * httpGet() - Send a GET request to the server. | |
ecdc0628 | 37 | * httpGetBlocking() - Get the blocking/non-block state of a connection. |
38 | * httpGetCookie() - Get any cookie data from the response. | |
39 | * httpGetFd() - Get the file descriptor associated with a | |
40 | * connection. | |
41 | * httpGetField() - Get a field value from a request/response. | |
ef416fc2 | 42 | * httpGetLength() - Get the amount of data remaining from the |
43 | * content-length or transfer-encoding fields. | |
44 | * httpGetLength2() - Get the amount of data remaining from the | |
45 | * content-length or transfer-encoding fields. | |
ecdc0628 | 46 | * httpGetStatus() - Get the status of the last HTTP request. |
ef416fc2 | 47 | * httpGetSubField() - Get a sub-field value. |
48 | * httpGets() - Get a line of text from a HTTP connection. | |
49 | * httpHead() - Send a HEAD request to the server. | |
50 | * httpInitialize() - Initialize the HTTP interface library and set the | |
51 | * default HTTP proxy (if any). | |
52 | * httpOptions() - Send an OPTIONS request to the server. | |
53 | * httpPost() - Send a POST request to the server. | |
54 | * httpPrintf() - Print a formatted string to a HTTP connection. | |
55 | * httpPut() - Send a PUT request to the server. | |
56 | * httpRead() - Read data from a HTTP connection. | |
a4d04587 | 57 | * httpRead2() - Read data from a HTTP connection. |
411affcf | 58 | * _httpReadCDSA() - Read function for the CDSA library. |
59 | * _httpReadGNUTLS() - Read function for the GNU TLS library. | |
ef416fc2 | 60 | * httpReconnect() - Reconnect to a HTTP server... |
61 | * httpSetCookie() - Set the cookie value(s)... | |
b423cd4c | 62 | * httpSetExpect() - Set the Expect: header in a request. |
ef416fc2 | 63 | * httpSetField() - Set the value of an HTTP header. |
64 | * httpSetLength() - Set the content-length and transfer-encoding. | |
65 | * httpTrace() - Send an TRACE request to the server. | |
66 | * httpUpdate() - Update the current HTTP state for incoming data. | |
67 | * httpWait() - Wait for data available on a connection. | |
68 | * httpWrite() - Write data to a HTTP connection. | |
a4d04587 | 69 | * httpWrite2() - Write data to a HTTP connection. |
411affcf | 70 | * _httpWriteCDSA() - Write function for the CDSA library. |
71 | * _httpWriteGNUTLS() - Write function for the GNU TLS library. | |
72 | * http_bio_ctrl() - Control the HTTP connection. | |
73 | * http_bio_free() - Free OpenSSL data. | |
74 | * http_bio_new() - Initialize an OpenSSL BIO structure. | |
75 | * http_bio_puts() - Send a string for OpenSSL. | |
76 | * http_bio_read() - Read data for OpenSSL. | |
77 | * http_bio_write() - Write data for OpenSSL. | |
ef416fc2 | 78 | * http_field() - Return the field index for a field name. |
79 | * http_read_ssl() - Read from a SSL/TLS connection. | |
80 | * http_send() - Send a request with all fields and the trailing | |
81 | * blank line. | |
82 | * http_setup_ssl() - Set up SSL/TLS on a connection. | |
83 | * http_shutdown_ssl() - Shut down SSL/TLS on a connection. | |
84 | * http_upgrade() - Force upgrade to TLS encryption. | |
85 | * http_wait() - Wait for data available on a connection. | |
86 | * http_write() - Write data to a connection. | |
87 | * http_write_ssl() - Write to a SSL/TLS connection. | |
88 | */ | |
89 | ||
90 | /* | |
91 | * Include necessary headers... | |
92 | */ | |
93 | ||
94 | #include "http-private.h" | |
95 | #include "globals.h" | |
96 | #include "debug.h" | |
97 | #include <stdlib.h> | |
98 | #include <fcntl.h> | |
99 | #include <errno.h> | |
100 | #ifndef WIN32 | |
101 | # include <signal.h> | |
102 | # include <sys/time.h> | |
103 | # include <sys/resource.h> | |
104 | #endif /* !WIN32 */ | |
f7deaa1a | 105 | #ifdef HAVE_POLL |
106 | # include <sys/poll.h> | |
107 | #endif /* HAVE_POLL */ | |
ef416fc2 | 108 | |
109 | ||
110 | /* | |
111 | * Some operating systems have done away with the Fxxxx constants for | |
112 | * the fcntl() call; this works around that "feature"... | |
113 | */ | |
114 | ||
115 | #ifndef FNONBLK | |
116 | # define FNONBLK O_NONBLOCK | |
117 | #endif /* !FNONBLK */ | |
118 | ||
119 | ||
120 | /* | |
121 | * Local functions... | |
122 | */ | |
123 | ||
124 | static http_field_t http_field(const char *name); | |
125 | static int http_send(http_t *http, http_state_t request, | |
126 | const char *uri); | |
411affcf | 127 | static int http_wait(http_t *http, int msec, int usessl); |
ef416fc2 | 128 | static int http_write(http_t *http, const char *buffer, |
129 | int length); | |
130 | static int http_write_chunk(http_t *http, const char *buffer, | |
131 | int length); | |
132 | #ifdef HAVE_SSL | |
133 | static int http_read_ssl(http_t *http, char *buf, int len); | |
134 | static int http_setup_ssl(http_t *http); | |
135 | static void http_shutdown_ssl(http_t *http); | |
136 | static int http_upgrade(http_t *http); | |
137 | static int http_write_ssl(http_t *http, const char *buf, int len); | |
138 | #endif /* HAVE_SSL */ | |
139 | ||
140 | ||
141 | /* | |
142 | * Local globals... | |
143 | */ | |
144 | ||
145 | static const char * const http_fields[] = | |
146 | { | |
147 | "Accept-Language", | |
148 | "Accept-Ranges", | |
149 | "Authorization", | |
150 | "Connection", | |
151 | "Content-Encoding", | |
152 | "Content-Language", | |
153 | "Content-Length", | |
154 | "Content-Location", | |
155 | "Content-MD5", | |
156 | "Content-Range", | |
157 | "Content-Type", | |
158 | "Content-Version", | |
159 | "Date", | |
160 | "Host", | |
161 | "If-Modified-Since", | |
162 | "If-Unmodified-since", | |
163 | "Keep-Alive", | |
164 | "Last-Modified", | |
165 | "Link", | |
166 | "Location", | |
167 | "Range", | |
168 | "Referer", | |
169 | "Retry-After", | |
170 | "Transfer-Encoding", | |
171 | "Upgrade", | |
172 | "User-Agent", | |
173 | "WWW-Authenticate" | |
174 | }; | |
175 | ||
176 | ||
411affcf | 177 | #if defined(HAVE_SSL) && defined(HAVE_LIBSSL) |
178 | /* | |
179 | * BIO methods for OpenSSL... | |
180 | */ | |
181 | ||
182 | static int http_bio_write(BIO *h, const char *buf, int num); | |
183 | static int http_bio_read(BIO *h, char *buf, int size); | |
184 | static int http_bio_puts(BIO *h, const char *str); | |
185 | static long http_bio_ctrl(BIO *h, int cmd, long arg1, void *arg2); | |
186 | static int http_bio_new(BIO *h); | |
187 | static int http_bio_free(BIO *data); | |
188 | ||
189 | static BIO_METHOD http_bio_methods = | |
190 | { | |
191 | BIO_TYPE_SOCKET, | |
192 | "http", | |
193 | http_bio_write, | |
194 | http_bio_read, | |
195 | http_bio_puts, | |
196 | NULL, /* http_bio_gets, */ | |
197 | http_bio_ctrl, | |
198 | http_bio_new, | |
199 | http_bio_free, | |
200 | NULL, | |
201 | }; | |
202 | ||
203 | ||
204 | /* | |
205 | * '_httpBIOMethods()' - Get the OpenSSL BIO methods for HTTP connections. | |
206 | */ | |
207 | ||
208 | BIO_METHOD * /* O - BIO methods for OpenSSL */ | |
209 | _httpBIOMethods(void) | |
210 | { | |
211 | return (&http_bio_methods); | |
212 | } | |
213 | #endif /* HAVE_SSL && HAVE_LIBSSL */ | |
214 | ||
215 | ||
ecdc0628 | 216 | /* |
217 | * 'httpBlocking()' - Set blocking/non-blocking behavior on a connection. | |
218 | */ | |
219 | ||
220 | void | |
221 | httpBlocking(http_t *http, /* I - HTTP connection */ | |
222 | int b) /* I - 1 = blocking, 0 = non-blocking */ | |
223 | { | |
224 | if (http) | |
225 | http->blocking = b; | |
226 | } | |
227 | ||
228 | ||
ef416fc2 | 229 | /* |
230 | * 'httpCheck()' - Check to see if there is a pending response from the server. | |
231 | */ | |
232 | ||
ecdc0628 | 233 | int /* O - 0 = no data, 1 = data available */ |
234 | httpCheck(http_t *http) /* I - HTTP connection */ | |
ef416fc2 | 235 | { |
236 | return (httpWait(http, 0)); | |
237 | } | |
238 | ||
239 | ||
240 | /* | |
241 | * 'httpClearCookie()' - Clear the cookie value(s). | |
242 | * | |
243 | * @since CUPS 1.1.19@ | |
244 | */ | |
245 | ||
246 | void | |
ecdc0628 | 247 | httpClearCookie(http_t *http) /* I - HTTP connection */ |
ef416fc2 | 248 | { |
249 | if (!http) | |
250 | return; | |
251 | ||
252 | if (http->cookie) | |
253 | { | |
254 | free(http->cookie); | |
255 | http->cookie = NULL; | |
256 | } | |
257 | } | |
258 | ||
259 | ||
ecdc0628 | 260 | /* |
261 | * 'httpClearFields()' - Clear HTTP request fields. | |
262 | */ | |
263 | ||
264 | void | |
265 | httpClearFields(http_t *http) /* I - HTTP connection */ | |
266 | { | |
267 | if (http) | |
268 | { | |
269 | memset(http->fields, 0, sizeof(http->fields)); | |
07725fee | 270 | if (http->hostname[0] == '/') |
271 | httpSetField(http, HTTP_FIELD_HOST, "localhost"); | |
272 | else | |
273 | httpSetField(http, HTTP_FIELD_HOST, http->hostname); | |
b423cd4c | 274 | |
f7deaa1a | 275 | if (http->field_authorization) |
276 | { | |
277 | free(http->field_authorization); | |
278 | http->field_authorization = NULL; | |
279 | } | |
280 | ||
b423cd4c | 281 | http->expect = (http_status_t)0; |
ecdc0628 | 282 | } |
283 | } | |
284 | ||
285 | ||
ef416fc2 | 286 | /* |
287 | * 'httpClose()' - Close an HTTP connection... | |
288 | */ | |
289 | ||
290 | void | |
ecdc0628 | 291 | httpClose(http_t *http) /* I - HTTP connection */ |
ef416fc2 | 292 | { |
f7deaa1a | 293 | #ifdef HAVE_GSSAPI |
294 | OM_uint32 minor_status, /* Minor status code */ | |
295 | major_status; /* Major status code */ | |
296 | #endif /* HAVE_GSSAPI */ | |
297 | ||
298 | ||
ef416fc2 | 299 | DEBUG_printf(("httpClose(http=%p)\n", http)); |
300 | ||
301 | if (!http) | |
302 | return; | |
303 | ||
304 | httpAddrFreeList(http->addrlist); | |
305 | ||
ef416fc2 | 306 | if (http->cookie) |
307 | free(http->cookie); | |
308 | ||
309 | #ifdef HAVE_SSL | |
310 | if (http->tls) | |
311 | http_shutdown_ssl(http); | |
312 | #endif /* HAVE_SSL */ | |
313 | ||
314 | #ifdef WIN32 | |
315 | closesocket(http->fd); | |
316 | #else | |
317 | close(http->fd); | |
318 | #endif /* WIN32 */ | |
319 | ||
f7deaa1a | 320 | #ifdef HAVE_GSSAPI |
321 | if (http->gssctx != GSS_C_NO_CONTEXT) | |
322 | major_status = gss_delete_sec_context(&minor_status, &http->gssctx, | |
323 | GSS_C_NO_BUFFER); | |
324 | ||
325 | if (http->gssname != GSS_C_NO_NAME) | |
326 | major_status = gss_release_name(&minor_status, &http->gssname); | |
327 | #endif /* HAVE_GSSAPI */ | |
328 | ||
b94498cf | 329 | #ifdef HAVE_AUTHORIZATION_H |
330 | if (http->auth_ref) | |
331 | AuthorizationFree(http->auth_ref, kAuthorizationFlagDefaults); | |
332 | #endif /* HAVE_AUTHORIZATION_H */ | |
333 | ||
f7deaa1a | 334 | httpClearFields(http); |
335 | ||
336 | if (http->authstring && http->authstring != http->_authstring) | |
337 | free(http->authstring); | |
338 | ||
ef416fc2 | 339 | free(http); |
340 | } | |
341 | ||
342 | ||
343 | /* | |
344 | * 'httpConnect()' - Connect to a HTTP server. | |
345 | */ | |
346 | ||
347 | http_t * /* O - New HTTP connection */ | |
348 | httpConnect(const char *host, /* I - Host to connect to */ | |
349 | int port) /* I - Port number */ | |
350 | { | |
351 | http_encryption_t encryption; /* Type of encryption to use */ | |
352 | ||
353 | ||
354 | /* | |
355 | * Set the default encryption status... | |
356 | */ | |
357 | ||
358 | if (port == 443) | |
359 | encryption = HTTP_ENCRYPT_ALWAYS; | |
360 | else | |
361 | encryption = HTTP_ENCRYPT_IF_REQUESTED; | |
362 | ||
363 | return (httpConnectEncrypt(host, port, encryption)); | |
364 | } | |
365 | ||
366 | ||
367 | /* | |
368 | * 'httpConnectEncrypt()' - Connect to a HTTP server using encryption. | |
369 | */ | |
370 | ||
371 | http_t * /* O - New HTTP connection */ | |
372 | httpConnectEncrypt( | |
373 | const char *host, /* I - Host to connect to */ | |
374 | int port, /* I - Port number */ | |
375 | http_encryption_t encryption) /* I - Type of encryption to use */ | |
376 | { | |
377 | http_t *http; /* New HTTP connection */ | |
378 | http_addrlist_t *addrlist; /* Host address data */ | |
379 | char service[255]; /* Service name */ | |
380 | ||
381 | ||
382 | DEBUG_printf(("httpConnectEncrypt(host=\"%s\", port=%d, encryption=%d)\n", | |
383 | host ? host : "(null)", port, encryption)); | |
384 | ||
385 | if (!host) | |
386 | return (NULL); | |
387 | ||
388 | httpInitialize(); | |
389 | ||
390 | /* | |
391 | * Lookup the host... | |
392 | */ | |
393 | ||
394 | sprintf(service, "%d", port); | |
395 | ||
396 | if ((addrlist = httpAddrGetList(host, AF_UNSPEC, service)) == NULL) | |
397 | return (NULL); | |
398 | ||
399 | /* | |
400 | * Allocate memory for the structure... | |
401 | */ | |
402 | ||
403 | http = calloc(sizeof(http_t), 1); | |
404 | if (http == NULL) | |
405 | return (NULL); | |
406 | ||
407 | http->version = HTTP_1_1; | |
408 | http->blocking = 1; | |
409 | http->activity = time(NULL); | |
410 | http->fd = -1; | |
411 | ||
f7deaa1a | 412 | #ifdef HAVE_GSSAPI |
413 | http->gssctx = GSS_C_NO_CONTEXT; | |
414 | http->gssname = GSS_C_NO_NAME; | |
415 | #endif /* HAVE_GSSAPI */ | |
416 | ||
ef416fc2 | 417 | /* |
418 | * Set the encryption status... | |
419 | */ | |
420 | ||
421 | if (port == 443) /* Always use encryption for https */ | |
422 | http->encryption = HTTP_ENCRYPT_ALWAYS; | |
423 | else | |
424 | http->encryption = encryption; | |
425 | ||
426 | /* | |
427 | * Loop through the addresses we have until one of them connects... | |
428 | */ | |
429 | ||
430 | strlcpy(http->hostname, host, sizeof(http->hostname)); | |
431 | ||
432 | /* | |
433 | * Connect to the remote system... | |
434 | */ | |
435 | ||
436 | http->addrlist = addrlist; | |
437 | ||
438 | if (!httpReconnect(http)) | |
439 | return (http); | |
440 | ||
441 | /* | |
442 | * Could not connect to any known address - bail out! | |
443 | */ | |
444 | ||
445 | httpAddrFreeList(addrlist); | |
446 | ||
447 | free(http); | |
448 | ||
449 | return (NULL); | |
450 | } | |
451 | ||
452 | ||
453 | /* | |
454 | * 'httpDelete()' - Send a DELETE request to the server. | |
455 | */ | |
456 | ||
457 | int /* O - Status of call (0 = success) */ | |
ecdc0628 | 458 | httpDelete(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 459 | const char *uri) /* I - URI to delete */ |
460 | { | |
461 | return (http_send(http, HTTP_DELETE, uri)); | |
462 | } | |
463 | ||
464 | ||
465 | /* | |
466 | * 'httpEncryption()' - Set the required encryption on the link. | |
467 | */ | |
468 | ||
469 | int /* O - -1 on error, 0 on success */ | |
ecdc0628 | 470 | httpEncryption(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 471 | http_encryption_t e) /* I - New encryption preference */ |
472 | { | |
473 | DEBUG_printf(("httpEncryption(http=%p, e=%d)\n", http, e)); | |
474 | ||
475 | #ifdef HAVE_SSL | |
476 | if (!http) | |
477 | return (0); | |
478 | ||
479 | http->encryption = e; | |
480 | ||
481 | if ((http->encryption == HTTP_ENCRYPT_ALWAYS && !http->tls) || | |
482 | (http->encryption == HTTP_ENCRYPT_NEVER && http->tls)) | |
483 | return (httpReconnect(http)); | |
484 | else if (http->encryption == HTTP_ENCRYPT_REQUIRED && !http->tls) | |
485 | return (http_upgrade(http)); | |
486 | else | |
487 | return (0); | |
488 | #else | |
489 | if (e == HTTP_ENCRYPT_ALWAYS || e == HTTP_ENCRYPT_REQUIRED) | |
490 | return (-1); | |
491 | else | |
492 | return (0); | |
493 | #endif /* HAVE_SSL */ | |
494 | } | |
495 | ||
496 | ||
ecdc0628 | 497 | /* |
498 | * 'httpError()' - Get the last error on a connection. | |
499 | */ | |
500 | ||
501 | int /* O - Error code (errno) value */ | |
502 | httpError(http_t *http) /* I - HTTP connection */ | |
503 | { | |
504 | if (http) | |
505 | return (http->error); | |
506 | else | |
507 | return (EINVAL); | |
508 | } | |
509 | ||
510 | ||
ef416fc2 | 511 | /* |
512 | * 'httpFlush()' - Flush data from a HTTP connection. | |
513 | */ | |
514 | ||
515 | void | |
ecdc0628 | 516 | httpFlush(http_t *http) /* I - HTTP connection */ |
ef416fc2 | 517 | { |
518 | char buffer[8192]; /* Junk buffer */ | |
fa73b229 | 519 | int blocking; /* To block or not to block */ |
ef416fc2 | 520 | |
521 | ||
522 | DEBUG_printf(("httpFlush(http=%p), state=%d\n", http, http->state)); | |
523 | ||
fa73b229 | 524 | /* |
525 | * Temporarily set non-blocking mode so we don't get stuck in httpRead()... | |
526 | */ | |
527 | ||
528 | blocking = http->blocking; | |
529 | http->blocking = 0; | |
530 | ||
531 | /* | |
532 | * Read any data we can... | |
533 | */ | |
534 | ||
a4d04587 | 535 | while (httpRead2(http, buffer, sizeof(buffer)) > 0); |
fa73b229 | 536 | |
537 | /* | |
538 | * Restore blocking and reset the connection if we didn't get all of | |
539 | * the remaining data... | |
540 | */ | |
541 | ||
542 | http->blocking = blocking; | |
543 | ||
544 | if (http->state != HTTP_WAITING && http->fd >= 0) | |
545 | { | |
546 | /* | |
547 | * Didn't get the data back, so close the current connection. | |
548 | */ | |
549 | ||
550 | http->state = HTTP_WAITING; | |
551 | ||
552 | #ifdef HAVE_SSL | |
553 | if (http->tls) | |
554 | http_shutdown_ssl(http); | |
555 | #endif /* HAVE_SSL */ | |
556 | ||
557 | #ifdef WIN32 | |
558 | closesocket(http->fd); | |
559 | #else | |
560 | close(http->fd); | |
561 | #endif /* WIN32 */ | |
562 | ||
563 | http->fd = -1; | |
564 | } | |
ef416fc2 | 565 | } |
566 | ||
567 | ||
568 | /* | |
569 | * 'httpFlushWrite()' - Flush data in write buffer. | |
570 | * | |
571 | * @since CUPS 1.2@ | |
572 | */ | |
573 | ||
574 | int /* O - Bytes written or -1 on error */ | |
ecdc0628 | 575 | httpFlushWrite(http_t *http) /* I - HTTP connection */ |
ef416fc2 | 576 | { |
577 | int bytes; /* Bytes written */ | |
578 | ||
579 | ||
580 | DEBUG_printf(("httpFlushWrite(http=%p)\n", http)); | |
581 | ||
582 | if (!http || !http->wused) | |
583 | return (0); | |
584 | ||
585 | if (http->data_encoding == HTTP_ENCODE_CHUNKED) | |
586 | bytes = http_write_chunk(http, http->wbuffer, http->wused); | |
587 | else | |
588 | bytes = http_write(http, http->wbuffer, http->wused); | |
589 | ||
590 | http->wused = 0; | |
591 | ||
592 | return (bytes); | |
593 | } | |
594 | ||
595 | ||
596 | /* | |
597 | * 'httpGet()' - Send a GET request to the server. | |
598 | */ | |
599 | ||
600 | int /* O - Status of call (0 = success) */ | |
ecdc0628 | 601 | httpGet(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 602 | const char *uri) /* I - URI to get */ |
603 | { | |
604 | return (http_send(http, HTTP_GET, uri)); | |
605 | } | |
606 | ||
607 | ||
ecdc0628 | 608 | /* |
609 | * 'httpGetBlocking()' - Get the blocking/non-block state of a connection. | |
610 | * | |
611 | * @since CUPS 1.2@ | |
612 | */ | |
613 | ||
614 | int /* O - 1 if blocking, 0 if non-blocking */ | |
615 | httpGetBlocking(http_t *http) /* I - HTTP connection */ | |
616 | { | |
617 | return (http ? http->blocking : 0); | |
618 | } | |
619 | ||
620 | ||
621 | /* | |
622 | * 'httpGetCookie()' - Get any cookie data from the response. | |
757d2cad | 623 | * |
624 | * @since CUPS 1.1.19@ | |
ecdc0628 | 625 | */ |
626 | ||
627 | const char * /* O - Cookie data or NULL */ | |
628 | httpGetCookie(http_t *http) /* I - HTTP connecion */ | |
629 | { | |
630 | return (http ? http->cookie : NULL); | |
631 | } | |
632 | ||
633 | ||
634 | /* | |
635 | * 'httpGetFd()' - Get the file descriptor associated with a connection. | |
636 | * | |
637 | * @since CUPS 1.2@ | |
638 | */ | |
639 | ||
640 | int /* O - File descriptor or -1 if none */ | |
641 | httpGetFd(http_t *http) /* I - HTTP connection */ | |
642 | { | |
643 | return (http ? http->fd : -1); | |
644 | } | |
645 | ||
646 | ||
647 | /* | |
648 | * 'httpGetField()' - Get a field value from a request/response. | |
649 | */ | |
650 | ||
651 | const char * /* O - Field value */ | |
652 | httpGetField(http_t *http, /* I - HTTP connection */ | |
653 | http_field_t field) /* I - Field to get */ | |
654 | { | |
655 | if (!http || field <= HTTP_FIELD_UNKNOWN || field >= HTTP_FIELD_MAX) | |
656 | return (NULL); | |
f7deaa1a | 657 | else if (field == HTTP_FIELD_AUTHORIZATION && |
658 | http->field_authorization) | |
659 | { | |
660 | /* | |
661 | * Special case for WWW-Authenticate: as its contents can be | |
662 | * longer than HTTP_MAX_VALUE... | |
663 | */ | |
664 | ||
665 | return (http->field_authorization); | |
666 | } | |
ecdc0628 | 667 | else |
668 | return (http->fields[field]); | |
669 | } | |
670 | ||
671 | ||
672 | /* | |
673 | * 'httpGetLength()' - Get the amount of data remaining from the | |
674 | * content-length or transfer-encoding fields. | |
675 | * | |
676 | * This function is deprecated and will not return lengths larger than | |
677 | * 2^31 - 1; use httpGetLength2() instead. | |
678 | * | |
679 | * @deprecated@ | |
680 | */ | |
681 | ||
682 | int /* O - Content length */ | |
683 | httpGetLength(http_t *http) /* I - HTTP connection */ | |
684 | { | |
685 | /* | |
686 | * Get the read content length and return the 32-bit value. | |
687 | */ | |
688 | ||
689 | if (http) | |
690 | { | |
691 | httpGetLength2(http); | |
692 | ||
693 | return (http->_data_remaining); | |
694 | } | |
695 | else | |
696 | return (-1); | |
697 | } | |
698 | ||
699 | ||
700 | /* | |
701 | * 'httpGetLength2()' - Get the amount of data remaining from the | |
702 | * content-length or transfer-encoding fields. | |
703 | * | |
704 | * This function returns the complete content length, even for | |
705 | * content larger than 2^31 - 1. | |
706 | * | |
707 | * @since CUPS 1.2@ | |
708 | */ | |
709 | ||
710 | off_t /* O - Content length */ | |
711 | httpGetLength2(http_t *http) /* I - HTTP connection */ | |
712 | { | |
713 | DEBUG_printf(("httpGetLength2(http=%p), state=%d\n", http, http->state)); | |
714 | ||
715 | if (!http) | |
716 | return (-1); | |
717 | ||
718 | if (!strcasecmp(http->fields[HTTP_FIELD_TRANSFER_ENCODING], "chunked")) | |
719 | { | |
720 | DEBUG_puts("httpGetLength2: chunked request!"); | |
721 | ||
722 | http->data_encoding = HTTP_ENCODE_CHUNKED; | |
723 | http->data_remaining = 0; | |
724 | } | |
725 | else | |
726 | { | |
727 | http->data_encoding = HTTP_ENCODE_LENGTH; | |
728 | ||
729 | /* | |
730 | * The following is a hack for HTTP servers that don't send a | |
731 | * content-length or transfer-encoding field... | |
732 | * | |
733 | * If there is no content-length then the connection must close | |
734 | * after the transfer is complete... | |
735 | */ | |
736 | ||
b86bc4cf | 737 | if (!http->fields[HTTP_FIELD_CONTENT_LENGTH][0]) |
738 | { | |
739 | /* | |
740 | * Default content length is 0 for errors and 2^31-1 for other | |
741 | * successful requests... | |
742 | */ | |
743 | ||
744 | if (http->status >= HTTP_MULTIPLE_CHOICES) | |
745 | http->data_remaining = 0; | |
746 | else | |
747 | http->data_remaining = 2147483647; | |
748 | } | |
ecdc0628 | 749 | else |
750 | http->data_remaining = strtoll(http->fields[HTTP_FIELD_CONTENT_LENGTH], | |
751 | NULL, 10); | |
752 | ||
753 | DEBUG_printf(("httpGetLength2: content_length=" CUPS_LLFMT "\n", | |
754 | CUPS_LLCAST http->data_remaining)); | |
755 | } | |
756 | ||
757 | if (http->data_remaining <= INT_MAX) | |
758 | http->_data_remaining = (int)http->data_remaining; | |
759 | else | |
760 | http->_data_remaining = INT_MAX; | |
761 | ||
762 | return (http->data_remaining); | |
763 | } | |
764 | ||
765 | ||
766 | /* | |
767 | * 'httpGetStatus()' - Get the status of the last HTTP request. | |
768 | * | |
769 | * @since CUPS 1.2@ | |
770 | */ | |
771 | ||
772 | http_status_t /* O - HTTP status */ | |
773 | httpGetStatus(http_t *http) /* I - HTTP connection */ | |
774 | { | |
775 | return (http ? http->status : HTTP_ERROR); | |
776 | } | |
777 | ||
778 | ||
ef416fc2 | 779 | /* |
780 | * 'httpGetSubField()' - Get a sub-field value. | |
781 | * | |
782 | * @deprecated@ | |
783 | */ | |
784 | ||
785 | char * /* O - Value or NULL */ | |
ecdc0628 | 786 | httpGetSubField(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 787 | http_field_t field, /* I - Field index */ |
788 | const char *name, /* I - Name of sub-field */ | |
789 | char *value) /* O - Value string */ | |
790 | { | |
791 | return (httpGetSubField2(http, field, name, value, HTTP_MAX_VALUE)); | |
792 | } | |
793 | ||
794 | ||
795 | /* | |
796 | * 'httpGetSubField2()' - Get a sub-field value. | |
797 | * | |
798 | * @since CUPS 1.2@ | |
799 | */ | |
800 | ||
801 | char * /* O - Value or NULL */ | |
ecdc0628 | 802 | httpGetSubField2(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 803 | http_field_t field, /* I - Field index */ |
804 | const char *name, /* I - Name of sub-field */ | |
805 | char *value, /* O - Value string */ | |
806 | int valuelen) /* I - Size of value buffer */ | |
807 | { | |
808 | const char *fptr; /* Pointer into field */ | |
809 | char temp[HTTP_MAX_VALUE], /* Temporary buffer for name */ | |
810 | *ptr, /* Pointer into string buffer */ | |
811 | *end; /* End of value buffer */ | |
812 | ||
813 | DEBUG_printf(("httpGetSubField2(http=%p, field=%d, name=\"%s\", value=%p, valuelen=%d)\n", | |
814 | http, field, name, value, valuelen)); | |
815 | ||
816 | if (!http || !name || !value || valuelen < 2 || | |
ecdc0628 | 817 | field <= HTTP_FIELD_UNKNOWN || field >= HTTP_FIELD_MAX) |
ef416fc2 | 818 | return (NULL); |
819 | ||
820 | end = value + valuelen - 1; | |
821 | ||
822 | for (fptr = http->fields[field]; *fptr;) | |
823 | { | |
824 | /* | |
825 | * Skip leading whitespace... | |
826 | */ | |
827 | ||
828 | while (isspace(*fptr & 255)) | |
829 | fptr ++; | |
830 | ||
831 | if (*fptr == ',') | |
832 | { | |
833 | fptr ++; | |
834 | continue; | |
835 | } | |
836 | ||
837 | /* | |
838 | * Get the sub-field name... | |
839 | */ | |
840 | ||
841 | for (ptr = temp; | |
842 | *fptr && *fptr != '=' && !isspace(*fptr & 255) && | |
843 | ptr < (temp + sizeof(temp) - 1); | |
844 | *ptr++ = *fptr++); | |
845 | ||
846 | *ptr = '\0'; | |
847 | ||
848 | DEBUG_printf(("httpGetSubField: name=\"%s\"\n", temp)); | |
849 | ||
850 | /* | |
851 | * Skip trailing chars up to the '='... | |
852 | */ | |
853 | ||
854 | while (isspace(*fptr & 255)) | |
855 | fptr ++; | |
856 | ||
857 | if (!*fptr) | |
858 | break; | |
859 | ||
860 | if (*fptr != '=') | |
861 | continue; | |
862 | ||
863 | /* | |
864 | * Skip = and leading whitespace... | |
865 | */ | |
866 | ||
867 | fptr ++; | |
868 | ||
869 | while (isspace(*fptr & 255)) | |
870 | fptr ++; | |
871 | ||
872 | if (*fptr == '\"') | |
873 | { | |
874 | /* | |
875 | * Read quoted string... | |
876 | */ | |
877 | ||
878 | for (ptr = value, fptr ++; | |
879 | *fptr && *fptr != '\"' && ptr < end; | |
880 | *ptr++ = *fptr++); | |
881 | ||
882 | *ptr = '\0'; | |
883 | ||
884 | while (*fptr && *fptr != '\"') | |
885 | fptr ++; | |
886 | ||
887 | if (*fptr) | |
888 | fptr ++; | |
889 | } | |
890 | else | |
891 | { | |
892 | /* | |
893 | * Read unquoted string... | |
894 | */ | |
895 | ||
896 | for (ptr = value; | |
897 | *fptr && !isspace(*fptr & 255) && *fptr != ',' && ptr < end; | |
898 | *ptr++ = *fptr++); | |
899 | ||
900 | *ptr = '\0'; | |
901 | ||
902 | while (*fptr && !isspace(*fptr & 255) && *fptr != ',') | |
903 | fptr ++; | |
904 | } | |
905 | ||
906 | DEBUG_printf(("httpGetSubField: value=\"%s\"\n", value)); | |
907 | ||
908 | /* | |
909 | * See if this is the one... | |
910 | */ | |
911 | ||
912 | if (!strcmp(name, temp)) | |
913 | return (value); | |
914 | } | |
915 | ||
916 | value[0] = '\0'; | |
917 | ||
918 | return (NULL); | |
919 | } | |
920 | ||
921 | ||
ef416fc2 | 922 | /* |
923 | * 'httpGets()' - Get a line of text from a HTTP connection. | |
924 | */ | |
925 | ||
926 | char * /* O - Line or NULL */ | |
927 | httpGets(char *line, /* I - Line to read into */ | |
928 | int length, /* I - Max length of buffer */ | |
ecdc0628 | 929 | http_t *http) /* I - HTTP connection */ |
ef416fc2 | 930 | { |
931 | char *lineptr, /* Pointer into line */ | |
932 | *lineend, /* End of line */ | |
933 | *bufptr, /* Pointer into input buffer */ | |
934 | *bufend; /* Pointer to end of buffer */ | |
935 | int bytes, /* Number of bytes read */ | |
936 | eol; /* End-of-line? */ | |
937 | ||
938 | ||
939 | DEBUG_printf(("httpGets(line=%p, length=%d, http=%p)\n", line, length, http)); | |
940 | ||
941 | if (http == NULL || line == NULL) | |
942 | return (NULL); | |
943 | ||
944 | /* | |
945 | * Read a line from the buffer... | |
946 | */ | |
947 | ||
948 | lineptr = line; | |
949 | lineend = line + length - 1; | |
950 | eol = 0; | |
951 | ||
952 | while (lineptr < lineend) | |
953 | { | |
954 | /* | |
955 | * Pre-load the buffer as needed... | |
956 | */ | |
957 | ||
958 | #ifdef WIN32 | |
959 | WSASetLastError(0); | |
960 | #else | |
961 | errno = 0; | |
962 | #endif /* WIN32 */ | |
963 | ||
964 | while (http->used == 0) | |
965 | { | |
966 | /* | |
967 | * No newline; see if there is more data to be read... | |
968 | */ | |
969 | ||
411affcf | 970 | if (!http->blocking && !http_wait(http, 10000, 1)) |
89d46774 | 971 | { |
972 | DEBUG_puts("httpGets: Timed out!"); | |
b86bc4cf | 973 | #ifdef WIN32 |
974 | http->error = WSAETIMEDOUT; | |
975 | #else | |
89d46774 | 976 | http->error = ETIMEDOUT; |
b86bc4cf | 977 | #endif /* WIN32 */ |
ef416fc2 | 978 | return (NULL); |
89d46774 | 979 | } |
ef416fc2 | 980 | |
981 | #ifdef HAVE_SSL | |
982 | if (http->tls) | |
983 | bytes = http_read_ssl(http, http->buffer + http->used, | |
984 | HTTP_MAX_BUFFER - http->used); | |
985 | else | |
986 | #endif /* HAVE_SSL */ | |
987 | bytes = recv(http->fd, http->buffer + http->used, | |
988 | HTTP_MAX_BUFFER - http->used, 0); | |
989 | ||
990 | DEBUG_printf(("httpGets: read %d bytes...\n", bytes)); | |
991 | ||
992 | if (bytes < 0) | |
993 | { | |
994 | /* | |
995 | * Nope, can't get a line this time... | |
996 | */ | |
997 | ||
998 | #ifdef WIN32 | |
999 | if (WSAGetLastError() != http->error) | |
1000 | { | |
1001 | http->error = WSAGetLastError(); | |
1002 | continue; | |
1003 | } | |
1004 | ||
1005 | DEBUG_printf(("httpGets: recv() error %d!\n", WSAGetLastError())); | |
1006 | #else | |
1007 | DEBUG_printf(("httpGets: recv() error %d!\n", errno)); | |
1008 | ||
1009 | if (errno == EINTR) | |
1010 | continue; | |
1011 | else if (errno != http->error) | |
1012 | { | |
1013 | http->error = errno; | |
1014 | continue; | |
1015 | } | |
1016 | #endif /* WIN32 */ | |
1017 | ||
1018 | return (NULL); | |
1019 | } | |
1020 | else if (bytes == 0) | |
1021 | { | |
1022 | http->error = EPIPE; | |
1023 | ||
1024 | return (NULL); | |
1025 | } | |
1026 | ||
1027 | /* | |
1028 | * Yup, update the amount used... | |
1029 | */ | |
1030 | ||
1031 | http->used += bytes; | |
1032 | } | |
1033 | ||
1034 | /* | |
1035 | * Now copy as much of the current line as possible... | |
1036 | */ | |
1037 | ||
1038 | for (bufptr = http->buffer, bufend = http->buffer + http->used; | |
1039 | lineptr < lineend && bufptr < bufend;) | |
1040 | { | |
1041 | if (*bufptr == 0x0a) | |
1042 | { | |
1043 | eol = 1; | |
1044 | bufptr ++; | |
1045 | break; | |
1046 | } | |
1047 | else if (*bufptr == 0x0d) | |
1048 | bufptr ++; | |
1049 | else | |
1050 | *lineptr++ = *bufptr++; | |
1051 | } | |
1052 | ||
b86bc4cf | 1053 | http->used -= (int)(bufptr - http->buffer); |
ef416fc2 | 1054 | if (http->used > 0) |
1055 | memmove(http->buffer, bufptr, http->used); | |
1056 | ||
1057 | if (eol) | |
1058 | { | |
1059 | /* | |
1060 | * End of line... | |
1061 | */ | |
1062 | ||
1063 | http->activity = time(NULL); | |
1064 | ||
1065 | *lineptr = '\0'; | |
1066 | ||
1067 | DEBUG_printf(("httpGets: Returning \"%s\"\n", line)); | |
1068 | ||
1069 | return (line); | |
1070 | } | |
1071 | } | |
1072 | ||
1073 | DEBUG_puts("httpGets: No new line available!"); | |
1074 | ||
1075 | return (NULL); | |
1076 | } | |
1077 | ||
1078 | ||
1079 | /* | |
1080 | * 'httpHead()' - Send a HEAD request to the server. | |
1081 | */ | |
1082 | ||
1083 | int /* O - Status of call (0 = success) */ | |
ecdc0628 | 1084 | httpHead(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 1085 | const char *uri) /* I - URI for head */ |
1086 | { | |
1087 | return (http_send(http, HTTP_HEAD, uri)); | |
1088 | } | |
1089 | ||
1090 | ||
1091 | /* | |
1092 | * 'httpInitialize()' - Initialize the HTTP interface library and set the | |
1093 | * default HTTP proxy (if any). | |
1094 | */ | |
1095 | ||
1096 | void | |
1097 | httpInitialize(void) | |
1098 | { | |
1099 | #ifdef HAVE_LIBSSL | |
1100 | # ifndef WIN32 | |
1101 | struct timeval curtime; /* Current time in microseconds */ | |
1102 | # endif /* !WIN32 */ | |
1103 | int i; /* Looping var */ | |
1104 | unsigned char data[1024]; /* Seed data */ | |
1105 | #endif /* HAVE_LIBSSL */ | |
1106 | ||
1107 | #ifdef WIN32 | |
1108 | WSADATA winsockdata; /* WinSock data */ | |
1109 | static int initialized = 0; /* Has WinSock been initialized? */ | |
1110 | ||
1111 | ||
1112 | if (!initialized) | |
1113 | WSAStartup(MAKEWORD(1,1), &winsockdata); | |
fa73b229 | 1114 | #elif !defined(SO_NOSIGPIPE) |
ef416fc2 | 1115 | /* |
1116 | * Ignore SIGPIPE signals... | |
1117 | */ | |
1118 | ||
fa73b229 | 1119 | # ifdef HAVE_SIGSET |
1120 | sigset(SIGPIPE, SIG_IGN); | |
1121 | # elif defined(HAVE_SIGACTION) | |
1122 | struct sigaction action; /* POSIX sigaction data */ | |
1123 | ||
1124 | ||
ef416fc2 | 1125 | memset(&action, 0, sizeof(action)); |
1126 | action.sa_handler = SIG_IGN; | |
1127 | sigaction(SIGPIPE, &action, NULL); | |
fa73b229 | 1128 | # else |
ef416fc2 | 1129 | signal(SIGPIPE, SIG_IGN); |
fa73b229 | 1130 | # endif /* !SO_NOSIGPIPE */ |
ef416fc2 | 1131 | #endif /* WIN32 */ |
1132 | ||
1133 | #ifdef HAVE_GNUTLS | |
1134 | gnutls_global_init(); | |
1135 | #endif /* HAVE_GNUTLS */ | |
1136 | ||
1137 | #ifdef HAVE_LIBSSL | |
1138 | SSL_load_error_strings(); | |
1139 | SSL_library_init(); | |
1140 | ||
1141 | /* | |
1142 | * Using the current time is a dubious random seed, but on some systems | |
1143 | * it is the best we can do (on others, this seed isn't even used...) | |
1144 | */ | |
1145 | ||
1146 | #ifdef WIN32 | |
1147 | #else | |
1148 | gettimeofday(&curtime, NULL); | |
1149 | srand(curtime.tv_sec + curtime.tv_usec); | |
1150 | #endif /* WIN32 */ | |
1151 | ||
1152 | for (i = 0; i < sizeof(data); i ++) | |
1153 | data[i] = rand(); /* Yes, this is a poor source of random data... */ | |
1154 | ||
1155 | RAND_seed(&data, sizeof(data)); | |
1156 | #endif /* HAVE_LIBSSL */ | |
1157 | } | |
1158 | ||
1159 | ||
1160 | /* | |
1161 | * 'httpOptions()' - Send an OPTIONS request to the server. | |
1162 | */ | |
1163 | ||
1164 | int /* O - Status of call (0 = success) */ | |
ecdc0628 | 1165 | httpOptions(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 1166 | const char *uri) /* I - URI for options */ |
1167 | { | |
1168 | return (http_send(http, HTTP_OPTIONS, uri)); | |
1169 | } | |
1170 | ||
1171 | ||
1172 | /* | |
1173 | * 'httpPost()' - Send a POST request to the server. | |
1174 | */ | |
1175 | ||
1176 | int /* O - Status of call (0 = success) */ | |
ecdc0628 | 1177 | httpPost(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 1178 | const char *uri) /* I - URI for post */ |
1179 | { | |
1180 | return (http_send(http, HTTP_POST, uri)); | |
1181 | } | |
1182 | ||
1183 | ||
1184 | /* | |
1185 | * 'httpPrintf()' - Print a formatted string to a HTTP connection. | |
ecdc0628 | 1186 | * |
1187 | * @private@ | |
ef416fc2 | 1188 | */ |
1189 | ||
1190 | int /* O - Number of bytes written */ | |
ecdc0628 | 1191 | httpPrintf(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 1192 | const char *format, /* I - printf-style format string */ |
1193 | ...) /* I - Additional args as needed */ | |
1194 | { | |
1195 | int bytes; /* Number of bytes to write */ | |
1196 | char buf[16384]; /* Buffer for formatted string */ | |
1197 | va_list ap; /* Variable argument pointer */ | |
1198 | ||
1199 | ||
1200 | DEBUG_printf(("httpPrintf(http=%p, format=\"%s\", ...)\n", http, format)); | |
1201 | ||
1202 | va_start(ap, format); | |
1203 | bytes = vsnprintf(buf, sizeof(buf), format, ap); | |
1204 | va_end(ap); | |
1205 | ||
1206 | DEBUG_printf(("httpPrintf: %s", buf)); | |
1207 | ||
d09495fa | 1208 | if (http->data_encoding == HTTP_ENCODE_FIELDS) |
1209 | return (httpWrite2(http, buf, bytes)); | |
1210 | else | |
ef416fc2 | 1211 | { |
d09495fa | 1212 | if (http->wused) |
1213 | { | |
1214 | DEBUG_puts(" flushing existing data..."); | |
ef416fc2 | 1215 | |
d09495fa | 1216 | if (httpFlushWrite(http) < 0) |
1217 | return (-1); | |
1218 | } | |
ef416fc2 | 1219 | |
d09495fa | 1220 | return (http_write(http, buf, bytes)); |
1221 | } | |
ef416fc2 | 1222 | } |
1223 | ||
1224 | ||
1225 | /* | |
1226 | * 'httpPut()' - Send a PUT request to the server. | |
1227 | */ | |
1228 | ||
1229 | int /* O - Status of call (0 = success) */ | |
ecdc0628 | 1230 | httpPut(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 1231 | const char *uri) /* I - URI to put */ |
1232 | { | |
1233 | return (http_send(http, HTTP_PUT, uri)); | |
1234 | } | |
1235 | ||
1236 | ||
1237 | /* | |
1238 | * 'httpRead()' - Read data from a HTTP connection. | |
a4d04587 | 1239 | * |
1240 | * This function is deprecated. Use the httpRead2() function which can | |
1241 | * read more than 2GB of data. | |
1242 | * | |
1243 | * @deprecated@ | |
ef416fc2 | 1244 | */ |
1245 | ||
1246 | int /* O - Number of bytes read */ | |
ecdc0628 | 1247 | httpRead(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 1248 | char *buffer, /* I - Buffer for data */ |
1249 | int length) /* I - Maximum number of bytes */ | |
1250 | { | |
a4d04587 | 1251 | return ((int)httpRead2(http, buffer, length)); |
1252 | } | |
1253 | ||
1254 | ||
1255 | /* | |
1256 | * 'httpRead2()' - Read data from a HTTP connection. | |
ecdc0628 | 1257 | * |
1258 | * @since CUPS 1.2@ | |
a4d04587 | 1259 | */ |
1260 | ||
1261 | ssize_t /* O - Number of bytes read */ | |
ecdc0628 | 1262 | httpRead2(http_t *http, /* I - HTTP connection */ |
a4d04587 | 1263 | char *buffer, /* I - Buffer for data */ |
1264 | size_t length) /* I - Maximum number of bytes */ | |
1265 | { | |
1266 | ssize_t bytes; /* Bytes read */ | |
ef416fc2 | 1267 | char len[32]; /* Length string */ |
1268 | ||
1269 | ||
1270 | DEBUG_printf(("httpRead(http=%p, buffer=%p, length=%d)\n", | |
1271 | http, buffer, length)); | |
1272 | ||
1273 | if (http == NULL || buffer == NULL) | |
1274 | return (-1); | |
1275 | ||
1276 | http->activity = time(NULL); | |
1277 | ||
1278 | if (length <= 0) | |
1279 | return (0); | |
1280 | ||
1281 | if (http->data_encoding == HTTP_ENCODE_CHUNKED && | |
1282 | http->data_remaining <= 0) | |
1283 | { | |
a4d04587 | 1284 | DEBUG_puts("httpRead2: Getting chunk length..."); |
ef416fc2 | 1285 | |
1286 | if (httpGets(len, sizeof(len), http) == NULL) | |
1287 | { | |
a4d04587 | 1288 | DEBUG_puts("httpRead2: Could not get length!"); |
ef416fc2 | 1289 | return (0); |
1290 | } | |
1291 | ||
1292 | http->data_remaining = strtoll(len, NULL, 16); | |
1293 | if (http->data_remaining < 0) | |
1294 | { | |
a4d04587 | 1295 | DEBUG_puts("httpRead2: Negative chunk length!"); |
ef416fc2 | 1296 | return (0); |
1297 | } | |
1298 | } | |
1299 | ||
a4d04587 | 1300 | DEBUG_printf(("httpRead2: data_remaining=" CUPS_LLFMT "\n", |
ef416fc2 | 1301 | CUPS_LLCAST http->data_remaining)); |
1302 | ||
1303 | if (http->data_remaining <= 0) | |
1304 | { | |
1305 | /* | |
1306 | * A zero-length chunk ends a transfer; unless we are reading POST | |
1307 | * data, go idle... | |
1308 | */ | |
1309 | ||
1310 | if (http->data_encoding == HTTP_ENCODE_CHUNKED) | |
1311 | httpGets(len, sizeof(len), http); | |
1312 | ||
1313 | if (http->state == HTTP_POST_RECV) | |
1314 | http->state ++; | |
1315 | else | |
1316 | http->state = HTTP_WAITING; | |
1317 | ||
1318 | /* | |
1319 | * Prevent future reads for this request... | |
1320 | */ | |
1321 | ||
1322 | http->data_encoding = HTTP_ENCODE_LENGTH; | |
1323 | ||
1324 | return (0); | |
1325 | } | |
b86bc4cf | 1326 | else if (length > (size_t)http->data_remaining) |
1327 | length = (size_t)http->data_remaining; | |
ef416fc2 | 1328 | |
1329 | if (http->used == 0 && length <= 256) | |
1330 | { | |
1331 | /* | |
1332 | * Buffer small reads for better performance... | |
1333 | */ | |
1334 | ||
ed486911 | 1335 | if (!http->blocking && !httpWait(http, 10000)) |
ef416fc2 | 1336 | return (0); |
1337 | ||
1338 | if (http->data_remaining > sizeof(http->buffer)) | |
1339 | bytes = sizeof(http->buffer); | |
1340 | else | |
1341 | bytes = http->data_remaining; | |
1342 | ||
1343 | #ifdef HAVE_SSL | |
1344 | if (http->tls) | |
1345 | bytes = http_read_ssl(http, http->buffer, bytes); | |
1346 | else | |
1347 | #endif /* HAVE_SSL */ | |
1348 | { | |
a4d04587 | 1349 | DEBUG_printf(("httpRead2: reading %d bytes from socket into buffer...\n", |
ef416fc2 | 1350 | bytes)); |
1351 | ||
1352 | bytes = recv(http->fd, http->buffer, bytes, 0); | |
1353 | ||
a4d04587 | 1354 | DEBUG_printf(("httpRead2: read %d bytes from socket into buffer...\n", |
ef416fc2 | 1355 | bytes)); |
1356 | } | |
1357 | ||
1358 | if (bytes > 0) | |
1359 | http->used = bytes; | |
1360 | else if (bytes < 0) | |
1361 | { | |
1362 | #ifdef WIN32 | |
1363 | http->error = WSAGetLastError(); | |
1364 | return (-1); | |
1365 | #else | |
1366 | if (errno != EINTR) | |
1367 | { | |
1368 | http->error = errno; | |
1369 | return (-1); | |
1370 | } | |
1371 | #endif /* WIN32 */ | |
1372 | } | |
1373 | else | |
1374 | { | |
1375 | http->error = EPIPE; | |
1376 | return (0); | |
1377 | } | |
1378 | } | |
1379 | ||
1380 | if (http->used > 0) | |
1381 | { | |
b86bc4cf | 1382 | if (length > (size_t)http->used) |
1383 | length = (size_t)http->used; | |
ef416fc2 | 1384 | |
b86bc4cf | 1385 | bytes = (ssize_t)length; |
ef416fc2 | 1386 | |
a4d04587 | 1387 | DEBUG_printf(("httpRead2: grabbing %d bytes from input buffer...\n", bytes)); |
ef416fc2 | 1388 | |
1389 | memcpy(buffer, http->buffer, length); | |
b86bc4cf | 1390 | http->used -= (int)length; |
ef416fc2 | 1391 | |
1392 | if (http->used > 0) | |
1393 | memmove(http->buffer, http->buffer + length, http->used); | |
1394 | } | |
1395 | #ifdef HAVE_SSL | |
1396 | else if (http->tls) | |
1397 | { | |
ed486911 | 1398 | if (!http->blocking && !httpWait(http, 10000)) |
ef416fc2 | 1399 | return (0); |
1400 | ||
b86bc4cf | 1401 | bytes = (ssize_t)http_read_ssl(http, buffer, (int)length); |
ef416fc2 | 1402 | } |
1403 | #endif /* HAVE_SSL */ | |
1404 | else | |
1405 | { | |
ed486911 | 1406 | if (!http->blocking && !httpWait(http, 10000)) |
ef416fc2 | 1407 | return (0); |
1408 | ||
a4d04587 | 1409 | DEBUG_printf(("httpRead2: reading %d bytes from socket...\n", length)); |
ef416fc2 | 1410 | |
b86bc4cf | 1411 | #ifdef WIN32 |
1412 | bytes = (ssize_t)recv(http->fd, buffer, (int)length, 0); | |
1413 | #else | |
ef416fc2 | 1414 | while ((bytes = recv(http->fd, buffer, length, 0)) < 0) |
1415 | if (errno != EINTR) | |
1416 | break; | |
b86bc4cf | 1417 | #endif /* WIN32 */ |
ef416fc2 | 1418 | |
a4d04587 | 1419 | DEBUG_printf(("httpRead2: read %d bytes from socket...\n", bytes)); |
ef416fc2 | 1420 | } |
1421 | ||
1422 | if (bytes > 0) | |
1423 | { | |
1424 | http->data_remaining -= bytes; | |
1425 | ||
1426 | if (http->data_remaining <= INT_MAX) | |
1427 | http->_data_remaining = (int)http->data_remaining; | |
1428 | else | |
1429 | http->_data_remaining = INT_MAX; | |
1430 | } | |
1431 | else if (bytes < 0) | |
1432 | { | |
1433 | #ifdef WIN32 | |
1434 | http->error = WSAGetLastError(); | |
1435 | #else | |
1436 | if (errno == EINTR) | |
1437 | bytes = 0; | |
1438 | else | |
1439 | http->error = errno; | |
1440 | #endif /* WIN32 */ | |
1441 | } | |
1442 | else | |
1443 | { | |
1444 | http->error = EPIPE; | |
1445 | return (0); | |
1446 | } | |
1447 | ||
1448 | if (http->data_remaining == 0) | |
1449 | { | |
1450 | if (http->data_encoding == HTTP_ENCODE_CHUNKED) | |
1451 | httpGets(len, sizeof(len), http); | |
1452 | ||
1453 | if (http->data_encoding != HTTP_ENCODE_CHUNKED) | |
1454 | { | |
1455 | if (http->state == HTTP_POST_RECV) | |
1456 | http->state ++; | |
1457 | else | |
1458 | http->state = HTTP_WAITING; | |
1459 | } | |
1460 | } | |
1461 | ||
1462 | #ifdef DEBUG | |
1463 | { | |
1464 | int i, j, ch; | |
a4d04587 | 1465 | printf("httpRead2: Read %d bytes:\n", bytes); |
ef416fc2 | 1466 | for (i = 0; i < bytes; i += 16) |
1467 | { | |
1468 | printf(" "); | |
1469 | ||
1470 | for (j = 0; j < 16 && (i + j) < bytes; j ++) | |
1471 | printf(" %02X", buffer[i + j] & 255); | |
1472 | ||
1473 | while (j < 16) | |
1474 | { | |
1475 | printf(" "); | |
1476 | j ++; | |
1477 | } | |
1478 | ||
1479 | printf(" "); | |
1480 | for (j = 0; j < 16 && (i + j) < bytes; j ++) | |
1481 | { | |
1482 | ch = buffer[i + j] & 255; | |
1483 | ||
fa73b229 | 1484 | if (ch < ' ' || ch >= 127) |
ef416fc2 | 1485 | ch = '.'; |
1486 | ||
1487 | putchar(ch); | |
1488 | } | |
1489 | putchar('\n'); | |
1490 | } | |
1491 | } | |
1492 | #endif /* DEBUG */ | |
1493 | ||
1494 | return (bytes); | |
1495 | } | |
1496 | ||
1497 | ||
1498 | #if defined(HAVE_SSL) && defined(HAVE_CDSASSL) | |
1499 | /* | |
411affcf | 1500 | * '_httpReadCDSA()' - Read function for the CDSA library. |
ef416fc2 | 1501 | */ |
1502 | ||
1503 | OSStatus /* O - -1 on error, 0 on success */ | |
1504 | _httpReadCDSA( | |
1505 | SSLConnectionRef connection, /* I - SSL/TLS connection */ | |
1506 | void *data, /* I - Data buffer */ | |
1507 | size_t *dataLength) /* IO - Number of bytes */ | |
1508 | { | |
411affcf | 1509 | OSStatus result; /* Return value */ |
1510 | ssize_t bytes; /* Number of bytes read */ | |
1511 | http_t *http; /* HTTP connection */ | |
1512 | ||
1513 | ||
1514 | http = (http_t *)connection; | |
e53920b9 | 1515 | |
411affcf | 1516 | if (!http->blocking) |
1517 | { | |
1518 | /* | |
1519 | * Make sure we have data before we read... | |
1520 | */ | |
e53920b9 | 1521 | |
411affcf | 1522 | if (!http_wait(http, 10000, 0)) |
1523 | { | |
1524 | http->error = ETIMEDOUT; | |
1525 | return (-1); | |
1526 | } | |
1527 | } | |
ef416fc2 | 1528 | |
b423cd4c | 1529 | do |
411affcf | 1530 | { |
1531 | bytes = recv(http->fd, data, *dataLength, 0); | |
1532 | } | |
b423cd4c | 1533 | while (bytes == -1 && errno == EINTR); |
fa73b229 | 1534 | |
b423cd4c | 1535 | if (bytes == *dataLength) |
411affcf | 1536 | { |
b423cd4c | 1537 | result = 0; |
411affcf | 1538 | } |
b423cd4c | 1539 | else if (bytes > 0) |
1540 | { | |
1541 | *dataLength = bytes; | |
1542 | result = errSSLWouldBlock; | |
1543 | } | |
1544 | else | |
1545 | { | |
1546 | *dataLength = 0; | |
fa73b229 | 1547 | |
1548 | if (bytes == 0) | |
ed486911 | 1549 | result = errSSLClosedGraceful; |
b423cd4c | 1550 | else if (errno == EAGAIN) |
1551 | result = errSSLWouldBlock; | |
b423cd4c | 1552 | else |
ed486911 | 1553 | result = errSSLClosedAbort; |
ef416fc2 | 1554 | } |
b423cd4c | 1555 | |
411affcf | 1556 | return (result); |
ef416fc2 | 1557 | } |
1558 | #endif /* HAVE_SSL && HAVE_CDSASSL */ | |
1559 | ||
1560 | ||
411affcf | 1561 | #if defined(HAVE_SSL) && defined(HAVE_GNUTLS) |
1562 | /* | |
1563 | * '_httpReadGNUTLS()' - Read function for the GNU TLS library. | |
1564 | */ | |
1565 | ||
1566 | ssize_t /* O - Number of bytes read or -1 on error */ | |
1567 | _httpReadGNUTLS( | |
1568 | gnutls_transport_ptr ptr, /* I - HTTP connection */ | |
1569 | void *data, /* I - Buffer */ | |
1570 | size_t length) /* I - Number of bytes to read */ | |
1571 | { | |
1572 | http_t *http; /* HTTP connection */ | |
1573 | ||
1574 | ||
1575 | http = (http_t *)ptr; | |
1576 | ||
1577 | if (!http->blocking) | |
1578 | { | |
1579 | /* | |
1580 | * Make sure we have data before we read... | |
1581 | */ | |
1582 | ||
1583 | if (!http_wait(http, 10000, 0)) | |
1584 | { | |
1585 | http->error = ETIMEDOUT; | |
1586 | return (-1); | |
1587 | } | |
1588 | } | |
1589 | ||
1590 | return (recv(http->fd, data, length, 0)); | |
1591 | } | |
1592 | #endif /* HAVE_SSL && HAVE_GNUTLS */ | |
1593 | ||
1594 | ||
ef416fc2 | 1595 | /* |
ecdc0628 | 1596 | * 'httpReconnect()' - Reconnect to a HTTP server. |
ef416fc2 | 1597 | */ |
1598 | ||
1599 | int /* O - 0 on success, non-zero on failure */ | |
ecdc0628 | 1600 | httpReconnect(http_t *http) /* I - HTTP connection */ |
ef416fc2 | 1601 | { |
1602 | http_addrlist_t *addr; /* Connected address */ | |
1603 | ||
1604 | ||
1605 | DEBUG_printf(("httpReconnect(http=%p)\n", http)); | |
1606 | ||
1607 | if (!http) | |
1608 | return (-1); | |
1609 | ||
1610 | #ifdef HAVE_SSL | |
1611 | if (http->tls) | |
1612 | http_shutdown_ssl(http); | |
1613 | #endif /* HAVE_SSL */ | |
1614 | ||
1615 | /* | |
1616 | * Close any previously open socket... | |
1617 | */ | |
1618 | ||
1619 | if (http->fd >= 0) | |
bd7854cb | 1620 | { |
ef416fc2 | 1621 | #ifdef WIN32 |
1622 | closesocket(http->fd); | |
1623 | #else | |
1624 | close(http->fd); | |
1625 | #endif /* WIN32 */ | |
1626 | ||
bd7854cb | 1627 | http->fd = -1; |
1628 | } | |
1629 | ||
ef416fc2 | 1630 | /* |
1631 | * Connect to the server... | |
1632 | */ | |
1633 | ||
1634 | if ((addr = httpAddrConnect(http->addrlist, &(http->fd))) == NULL) | |
1635 | { | |
1636 | /* | |
1637 | * Unable to connect... | |
1638 | */ | |
1639 | ||
1640 | #ifdef WIN32 | |
1641 | http->error = WSAGetLastError(); | |
1642 | #else | |
1643 | http->error = errno; | |
1644 | #endif /* WIN32 */ | |
1645 | http->status = HTTP_ERROR; | |
1646 | ||
1647 | return (-1); | |
1648 | } | |
1649 | ||
1650 | http->hostaddr = &(addr->addr); | |
1651 | http->error = 0; | |
1652 | http->status = HTTP_CONTINUE; | |
1653 | ||
1654 | #ifdef HAVE_SSL | |
1655 | if (http->encryption == HTTP_ENCRYPT_ALWAYS) | |
1656 | { | |
1657 | /* | |
1658 | * Always do encryption via SSL. | |
1659 | */ | |
1660 | ||
1661 | if (http_setup_ssl(http) != 0) | |
1662 | { | |
f7deaa1a | 1663 | # ifdef WIN32 |
ef416fc2 | 1664 | closesocket(http->fd); |
f7deaa1a | 1665 | # else |
ef416fc2 | 1666 | close(http->fd); |
f7deaa1a | 1667 | # endif /* WIN32 */ |
ef416fc2 | 1668 | |
1669 | return (-1); | |
1670 | } | |
1671 | } | |
1672 | else if (http->encryption == HTTP_ENCRYPT_REQUIRED) | |
1673 | return (http_upgrade(http)); | |
1674 | #endif /* HAVE_SSL */ | |
1675 | ||
1676 | return (0); | |
1677 | } | |
1678 | ||
1679 | ||
1680 | /* | |
1681 | * 'httpSetCookie()' - Set the cookie value(s)... | |
1682 | * | |
1683 | * @since CUPS 1.1.19@ | |
1684 | */ | |
1685 | ||
1686 | void | |
1687 | httpSetCookie(http_t *http, /* I - Connection */ | |
1688 | const char *cookie) /* I - Cookie string */ | |
1689 | { | |
1690 | if (!http) | |
1691 | return; | |
1692 | ||
1693 | if (http->cookie) | |
1694 | free(http->cookie); | |
1695 | ||
1696 | if (cookie) | |
1697 | http->cookie = strdup(cookie); | |
1698 | else | |
1699 | http->cookie = NULL; | |
1700 | } | |
1701 | ||
1702 | ||
b423cd4c | 1703 | /* |
1704 | * 'httpSetExpect()' - Set the Expect: header in a request. | |
1705 | * | |
1706 | * Currently only HTTP_CONTINUE is supported for the "expect" argument. | |
1707 | * | |
1708 | * @since CUPS 1.2@ | |
1709 | */ | |
1710 | ||
1711 | void | |
1712 | httpSetExpect(http_t *http, /* I - HTTP connection */ | |
1713 | http_status_t expect) /* I - HTTP status to expect (HTTP_CONTINUE) */ | |
1714 | { | |
1715 | if (http) | |
1716 | http->expect = expect; | |
1717 | } | |
1718 | ||
1719 | ||
ef416fc2 | 1720 | /* |
1721 | * 'httpSetField()' - Set the value of an HTTP header. | |
1722 | */ | |
1723 | ||
1724 | void | |
ecdc0628 | 1725 | httpSetField(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 1726 | http_field_t field, /* I - Field index */ |
1727 | const char *value) /* I - Value */ | |
1728 | { | |
1729 | if (http == NULL || | |
1730 | field < HTTP_FIELD_ACCEPT_LANGUAGE || | |
1731 | field > HTTP_FIELD_WWW_AUTHENTICATE || | |
1732 | value == NULL) | |
1733 | return; | |
1734 | ||
1735 | strlcpy(http->fields[field], value, HTTP_MAX_VALUE); | |
f7deaa1a | 1736 | |
1737 | /* | |
1738 | * Special case for Authorization: as its contents can be | |
1739 | * longer than HTTP_MAX_VALUE | |
1740 | */ | |
1741 | ||
1742 | if (field == HTTP_FIELD_AUTHORIZATION) | |
1743 | { | |
1744 | if (http->field_authorization) | |
1745 | free(http->field_authorization); | |
1746 | ||
1747 | http->field_authorization = strdup(value); | |
1748 | } | |
ef416fc2 | 1749 | } |
1750 | ||
1751 | ||
1752 | /* | |
1753 | * 'httpSetLength()' - Set the content-length and content-encoding. | |
1754 | * | |
1755 | * @since CUPS 1.2@ | |
1756 | */ | |
1757 | ||
1758 | void | |
ecdc0628 | 1759 | httpSetLength(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 1760 | size_t length) /* I - Length (0 for chunked) */ |
1761 | { | |
1762 | if (!http) | |
1763 | return; | |
1764 | ||
1765 | if (!length) | |
1766 | { | |
1767 | strcpy(http->fields[HTTP_FIELD_TRANSFER_ENCODING], "chunked"); | |
1768 | http->fields[HTTP_FIELD_CONTENT_LENGTH][0] = '\0'; | |
1769 | } | |
1770 | else | |
1771 | { | |
1772 | http->fields[HTTP_FIELD_TRANSFER_ENCODING][0] = '\0'; | |
1773 | snprintf(http->fields[HTTP_FIELD_CONTENT_LENGTH], HTTP_MAX_VALUE, | |
1774 | CUPS_LLFMT, CUPS_LLCAST length); | |
1775 | } | |
1776 | } | |
1777 | ||
1778 | ||
1779 | /* | |
1780 | * 'httpTrace()' - Send an TRACE request to the server. | |
1781 | */ | |
1782 | ||
1783 | int /* O - Status of call (0 = success) */ | |
ecdc0628 | 1784 | httpTrace(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 1785 | const char *uri) /* I - URI for trace */ |
1786 | { | |
1787 | return (http_send(http, HTTP_TRACE, uri)); | |
1788 | } | |
1789 | ||
1790 | ||
1791 | /* | |
1792 | * 'httpUpdate()' - Update the current HTTP state for incoming data. | |
1793 | */ | |
1794 | ||
1795 | http_status_t /* O - HTTP status */ | |
ecdc0628 | 1796 | httpUpdate(http_t *http) /* I - HTTP connection */ |
ef416fc2 | 1797 | { |
1798 | char line[32768], /* Line from connection... */ | |
1799 | *value; /* Pointer to value on line */ | |
1800 | http_field_t field; /* Field index */ | |
1801 | int major, minor, /* HTTP version numbers */ | |
1802 | status; /* Request status */ | |
1803 | ||
1804 | ||
1805 | DEBUG_printf(("httpUpdate(http=%p), state=%d\n", http, http->state)); | |
1806 | ||
1807 | /* | |
1808 | * Flush pending data, if any... | |
1809 | */ | |
1810 | ||
1811 | if (http->wused) | |
1812 | { | |
1813 | DEBUG_puts(" flushing buffer..."); | |
1814 | ||
1815 | if (httpFlushWrite(http) < 0) | |
1816 | return (HTTP_ERROR); | |
1817 | } | |
1818 | ||
1819 | /* | |
1820 | * If we haven't issued any commands, then there is nothing to "update"... | |
1821 | */ | |
1822 | ||
1823 | if (http->state == HTTP_WAITING) | |
1824 | return (HTTP_CONTINUE); | |
1825 | ||
1826 | /* | |
1827 | * Grab all of the lines we can from the connection... | |
1828 | */ | |
1829 | ||
1830 | while (httpGets(line, sizeof(line), http) != NULL) | |
1831 | { | |
1832 | DEBUG_printf(("httpUpdate: Got \"%s\"\n", line)); | |
1833 | ||
1834 | if (line[0] == '\0') | |
1835 | { | |
1836 | /* | |
1837 | * Blank line means the start of the data section (if any). Return | |
1838 | * the result code, too... | |
1839 | * | |
1840 | * If we get status 100 (HTTP_CONTINUE), then we *don't* change states. | |
1841 | * Instead, we just return HTTP_CONTINUE to the caller and keep on | |
1842 | * tryin'... | |
1843 | */ | |
1844 | ||
1845 | if (http->status == HTTP_CONTINUE) | |
1846 | return (http->status); | |
1847 | ||
1848 | if (http->status < HTTP_BAD_REQUEST) | |
1849 | http->digest_tries = 0; | |
1850 | ||
1851 | #ifdef HAVE_SSL | |
1852 | if (http->status == HTTP_SWITCHING_PROTOCOLS && !http->tls) | |
1853 | { | |
1854 | if (http_setup_ssl(http) != 0) | |
1855 | { | |
1856 | # ifdef WIN32 | |
1857 | closesocket(http->fd); | |
1858 | # else | |
1859 | close(http->fd); | |
1860 | # endif /* WIN32 */ | |
1861 | ||
1862 | return (HTTP_ERROR); | |
1863 | } | |
1864 | ||
1865 | return (HTTP_CONTINUE); | |
1866 | } | |
1867 | #endif /* HAVE_SSL */ | |
1868 | ||
1869 | httpGetLength2(http); | |
1870 | ||
1871 | switch (http->state) | |
1872 | { | |
1873 | case HTTP_GET : | |
1874 | case HTTP_POST : | |
1875 | case HTTP_POST_RECV : | |
1876 | case HTTP_PUT : | |
1877 | http->state ++; | |
1878 | case HTTP_POST_SEND : | |
e1d6a774 | 1879 | case HTTP_HEAD : |
ef416fc2 | 1880 | break; |
1881 | ||
1882 | default : | |
1883 | http->state = HTTP_WAITING; | |
1884 | break; | |
1885 | } | |
1886 | ||
1887 | return (http->status); | |
1888 | } | |
1889 | else if (strncmp(line, "HTTP/", 5) == 0) | |
1890 | { | |
1891 | /* | |
1892 | * Got the beginning of a response... | |
1893 | */ | |
1894 | ||
1895 | if (sscanf(line, "HTTP/%d.%d%d", &major, &minor, &status) != 3) | |
1896 | return (HTTP_ERROR); | |
1897 | ||
1898 | http->version = (http_version_t)(major * 100 + minor); | |
1899 | http->status = (http_status_t)status; | |
1900 | } | |
1901 | else if ((value = strchr(line, ':')) != NULL) | |
1902 | { | |
1903 | /* | |
1904 | * Got a value... | |
1905 | */ | |
1906 | ||
1907 | *value++ = '\0'; | |
1908 | while (isspace(*value & 255)) | |
1909 | value ++; | |
1910 | ||
1911 | /* | |
1912 | * Be tolerants of servers that send unknown attribute fields... | |
1913 | */ | |
1914 | ||
1915 | if (!strcasecmp(line, "expect")) | |
1916 | { | |
1917 | /* | |
1918 | * "Expect: 100-continue" or similar... | |
1919 | */ | |
1920 | ||
1921 | http->expect = (http_status_t)atoi(value); | |
1922 | } | |
1923 | else if (!strcasecmp(line, "cookie")) | |
1924 | { | |
1925 | /* | |
1926 | * "Cookie: name=value[; name=value ...]" - replaces previous cookies... | |
1927 | */ | |
1928 | ||
1929 | httpSetCookie(http, value); | |
1930 | } | |
1931 | else if ((field = http_field(line)) == HTTP_FIELD_UNKNOWN) | |
1932 | { | |
1933 | DEBUG_printf(("httpUpdate: unknown field %s seen!\n", line)); | |
1934 | continue; | |
1935 | } | |
1936 | else | |
1937 | httpSetField(http, field, value); | |
1938 | } | |
1939 | else | |
1940 | { | |
1941 | http->status = HTTP_ERROR; | |
1942 | return (HTTP_ERROR); | |
1943 | } | |
1944 | } | |
1945 | ||
1946 | /* | |
1947 | * See if there was an error... | |
1948 | */ | |
1949 | ||
1950 | if (http->error == EPIPE && http->status > HTTP_CONTINUE) | |
1951 | return (http->status); | |
1952 | ||
1953 | if (http->error) | |
1954 | { | |
1955 | DEBUG_printf(("httpUpdate: socket error %d - %s\n", http->error, | |
1956 | strerror(http->error))); | |
1957 | http->status = HTTP_ERROR; | |
1958 | return (HTTP_ERROR); | |
1959 | } | |
1960 | ||
1961 | /* | |
1962 | * If we haven't already returned, then there is nothing new... | |
1963 | */ | |
1964 | ||
1965 | return (HTTP_CONTINUE); | |
1966 | } | |
1967 | ||
1968 | ||
1969 | /* | |
1970 | * 'httpWait()' - Wait for data available on a connection. | |
1971 | * | |
1972 | * @since CUPS 1.1.19@ | |
1973 | */ | |
1974 | ||
1975 | int /* O - 1 if data is available, 0 otherwise */ | |
ecdc0628 | 1976 | httpWait(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 1977 | int msec) /* I - Milliseconds to wait */ |
1978 | { | |
1979 | /* | |
1980 | * First see if there is data in the buffer... | |
1981 | */ | |
1982 | ||
1983 | if (http == NULL) | |
1984 | return (0); | |
1985 | ||
1986 | if (http->used) | |
1987 | return (1); | |
1988 | ||
8ca02f3c | 1989 | /* |
1990 | * Flush pending data, if any... | |
1991 | */ | |
1992 | ||
1993 | if (http->wused) | |
1994 | { | |
1995 | if (httpFlushWrite(http) < 0) | |
1996 | return (0); | |
1997 | } | |
1998 | ||
ef416fc2 | 1999 | /* |
2000 | * If not, check the SSL/TLS buffers and do a select() on the connection... | |
2001 | */ | |
2002 | ||
411affcf | 2003 | return (http_wait(http, msec, 1)); |
ef416fc2 | 2004 | } |
2005 | ||
2006 | ||
2007 | /* | |
2008 | * 'httpWrite()' - Write data to a HTTP connection. | |
a4d04587 | 2009 | * |
2010 | * This function is deprecated. Use the httpWrite2() function which can | |
2011 | * write more than 2GB of data. | |
2012 | * | |
2013 | * @deprecated@ | |
ef416fc2 | 2014 | */ |
2015 | ||
2016 | int /* O - Number of bytes written */ | |
ecdc0628 | 2017 | httpWrite(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 2018 | const char *buffer, /* I - Buffer for data */ |
2019 | int length) /* I - Number of bytes to write */ | |
2020 | { | |
a4d04587 | 2021 | return ((int)httpWrite2(http, buffer, length)); |
2022 | } | |
2023 | ||
2024 | ||
2025 | /* | |
2026 | * 'httpWrite2()' - Write data to a HTTP connection. | |
ecdc0628 | 2027 | * |
2028 | * @since CUPS 1.2@ | |
a4d04587 | 2029 | */ |
2030 | ||
2031 | ssize_t /* O - Number of bytes written */ | |
ecdc0628 | 2032 | httpWrite2(http_t *http, /* I - HTTP connection */ |
a4d04587 | 2033 | const char *buffer, /* I - Buffer for data */ |
2034 | size_t length) /* I - Number of bytes to write */ | |
2035 | { | |
2036 | ssize_t bytes; /* Bytes written */ | |
ef416fc2 | 2037 | |
2038 | ||
2039 | DEBUG_printf(("httpWrite(http=%p, buffer=%p, length=%d)\n", http, | |
2040 | buffer, length)); | |
2041 | ||
2042 | /* | |
2043 | * Range check input... | |
2044 | */ | |
2045 | ||
2046 | if (http == NULL || buffer == NULL) | |
2047 | return (-1); | |
2048 | ||
2049 | /* | |
2050 | * Mark activity on the connection... | |
2051 | */ | |
2052 | ||
2053 | http->activity = time(NULL); | |
2054 | ||
2055 | /* | |
2056 | * Buffer small writes for better performance... | |
2057 | */ | |
2058 | ||
2059 | if (length > 0) | |
2060 | { | |
2061 | if (http->wused && (length + http->wused) > sizeof(http->wbuffer)) | |
2062 | { | |
2063 | DEBUG_printf((" flushing buffer (wused=%d, length=%d)\n", | |
2064 | http->wused, length)); | |
2065 | ||
2066 | httpFlushWrite(http); | |
2067 | } | |
2068 | ||
07725fee | 2069 | if ((length + http->wused) <= sizeof(http->wbuffer)) |
ef416fc2 | 2070 | { |
2071 | /* | |
2072 | * Write to buffer... | |
2073 | */ | |
2074 | ||
2075 | DEBUG_printf((" copying %d bytes to wbuffer...\n", length)); | |
2076 | ||
2077 | memcpy(http->wbuffer + http->wused, buffer, length); | |
b86bc4cf | 2078 | http->wused += (int)length; |
2079 | bytes = (ssize_t)length; | |
ef416fc2 | 2080 | } |
2081 | else | |
2082 | { | |
2083 | /* | |
2084 | * Otherwise write the data directly... | |
2085 | */ | |
2086 | ||
2087 | DEBUG_printf((" writing %d bytes to socket...\n", length)); | |
2088 | ||
2089 | if (http->data_encoding == HTTP_ENCODE_CHUNKED) | |
b86bc4cf | 2090 | bytes = (ssize_t)http_write_chunk(http, buffer, (int)length); |
ef416fc2 | 2091 | else |
b86bc4cf | 2092 | bytes = (ssize_t)http_write(http, buffer, (int)length); |
ef416fc2 | 2093 | |
2094 | DEBUG_printf((" wrote %d bytes...\n", bytes)); | |
2095 | } | |
2096 | ||
2097 | if (http->data_encoding == HTTP_ENCODE_LENGTH) | |
2098 | http->data_remaining -= bytes; | |
2099 | } | |
2100 | else | |
2101 | bytes = 0; | |
2102 | ||
2103 | /* | |
2104 | * Handle end-of-request processing... | |
2105 | */ | |
2106 | ||
2107 | if ((http->data_encoding == HTTP_ENCODE_CHUNKED && length == 0) || | |
2108 | (http->data_encoding == HTTP_ENCODE_LENGTH && http->data_remaining == 0)) | |
2109 | { | |
2110 | /* | |
2111 | * Finished with the transfer; unless we are sending POST or PUT | |
2112 | * data, go idle... | |
2113 | */ | |
2114 | ||
2115 | DEBUG_puts("httpWrite: changing states..."); | |
2116 | ||
2117 | if (http->wused) | |
2118 | httpFlushWrite(http); | |
2119 | ||
2120 | if (http->data_encoding == HTTP_ENCODE_CHUNKED) | |
2121 | { | |
2122 | /* | |
2123 | * Send a 0-length chunk at the end of the request... | |
2124 | */ | |
2125 | ||
2126 | http_write(http, "0\r\n\r\n", 5); | |
2127 | ||
2128 | /* | |
2129 | * Reset the data state... | |
2130 | */ | |
2131 | ||
2132 | http->data_encoding = HTTP_ENCODE_LENGTH; | |
2133 | http->data_remaining = 0; | |
2134 | } | |
2135 | ||
2136 | if (http->state == HTTP_POST_RECV) | |
2137 | http->state ++; | |
2138 | else if (http->state == HTTP_PUT_RECV) | |
2139 | http->state = HTTP_STATUS; | |
2140 | else | |
2141 | http->state = HTTP_WAITING; | |
2142 | } | |
2143 | ||
2144 | return (bytes); | |
2145 | } | |
2146 | ||
2147 | ||
2148 | #if defined(HAVE_SSL) && defined(HAVE_CDSASSL) | |
2149 | /* | |
411affcf | 2150 | * '_httpWriteCDSA()' - Write function for the CDSA library. |
ef416fc2 | 2151 | */ |
2152 | ||
2153 | OSStatus /* O - -1 on error, 0 on success */ | |
2154 | _httpWriteCDSA( | |
2155 | SSLConnectionRef connection, /* I - SSL/TLS connection */ | |
2156 | const void *data, /* I - Data buffer */ | |
2157 | size_t *dataLength) /* IO - Number of bytes */ | |
2158 | { | |
411affcf | 2159 | OSStatus result; /* Return value */ |
2160 | ssize_t bytes; /* Number of bytes read */ | |
2161 | http_t *http; /* HTTP connection */ | |
e53920b9 | 2162 | |
2163 | ||
411affcf | 2164 | http = (http_t *)connection; |
ef416fc2 | 2165 | |
b423cd4c | 2166 | do |
411affcf | 2167 | { |
2168 | bytes = write(http->fd, data, *dataLength); | |
2169 | } | |
b423cd4c | 2170 | while (bytes == -1 && errno == EINTR); |
fa73b229 | 2171 | |
b423cd4c | 2172 | if (bytes == *dataLength) |
411affcf | 2173 | { |
b423cd4c | 2174 | result = 0; |
411affcf | 2175 | } |
b423cd4c | 2176 | else if (bytes >= 0) |
2177 | { | |
2178 | *dataLength = bytes; | |
2179 | result = errSSLWouldBlock; | |
2180 | } | |
2181 | else | |
2182 | { | |
2183 | *dataLength = 0; | |
2184 | ||
fa73b229 | 2185 | if (errno == EAGAIN) |
b423cd4c | 2186 | result = errSSLWouldBlock; |
b423cd4c | 2187 | else |
ed486911 | 2188 | result = errSSLClosedAbort; |
ef416fc2 | 2189 | } |
b423cd4c | 2190 | |
411affcf | 2191 | return (result); |
ef416fc2 | 2192 | } |
2193 | #endif /* HAVE_SSL && HAVE_CDSASSL */ | |
2194 | ||
2195 | ||
411affcf | 2196 | #if defined(HAVE_SSL) && defined(HAVE_GNUTLS) |
2197 | /* | |
2198 | * '_httpWriteGNUTLS()' - Write function for the GNU TLS library. | |
2199 | */ | |
2200 | ||
2201 | ssize_t /* O - Number of bytes written or -1 on error */ | |
2202 | _httpWriteGNUTLS( | |
2203 | gnutls_transport_ptr ptr, /* I - HTTP connection */ | |
2204 | const void *data, /* I - Data buffer */ | |
2205 | size_t length) /* I - Number of bytes to write */ | |
2206 | { | |
2207 | return (send(((http_t *)ptr)->fd, data, length, 0)); | |
2208 | } | |
2209 | #endif /* HAVE_SSL && HAVE_GNUTLS */ | |
2210 | ||
2211 | ||
2212 | #if defined(HAVE_SSL) && defined(HAVE_LIBSSL) | |
2213 | /* | |
2214 | * 'http_bio_ctrl()' - Control the HTTP connection. | |
2215 | */ | |
2216 | ||
2217 | static long /* O - Result/data */ | |
2218 | http_bio_ctrl(BIO *h, /* I - BIO data */ | |
2219 | int cmd, /* I - Control command */ | |
2220 | long arg1, /* I - First argument */ | |
2221 | void *arg2) /* I - Second argument */ | |
2222 | { | |
2223 | switch (cmd) | |
2224 | { | |
2225 | default : | |
2226 | return (0); | |
2227 | ||
2228 | case BIO_CTRL_RESET : | |
2229 | h->ptr = NULL; | |
2230 | return (0); | |
2231 | ||
2232 | case BIO_C_SET_FILE_PTR : | |
2233 | h->ptr = arg2; | |
2234 | h->init = 1; | |
2235 | return (1); | |
2236 | ||
2237 | case BIO_C_GET_FILE_PTR : | |
2238 | if (arg2) | |
2239 | { | |
2240 | *((void **)arg2) = h->ptr; | |
2241 | return (1); | |
2242 | } | |
2243 | else | |
2244 | return (0); | |
2245 | ||
2246 | case BIO_CTRL_DUP : | |
2247 | case BIO_CTRL_FLUSH : | |
2248 | return (1); | |
2249 | } | |
2250 | } | |
2251 | ||
2252 | ||
2253 | /* | |
2254 | * 'http_bio_free()' - Free OpenSSL data. | |
2255 | */ | |
2256 | ||
2257 | static int /* O - 1 on success, 0 on failure */ | |
2258 | http_bio_free(BIO *h) /* I - BIO data */ | |
2259 | { | |
2260 | if (!h) | |
2261 | return (0); | |
2262 | ||
2263 | if (h->shutdown) | |
2264 | { | |
2265 | h->init = 0; | |
2266 | h->flags = 0; | |
2267 | } | |
2268 | ||
2269 | return (1); | |
2270 | } | |
2271 | ||
2272 | ||
2273 | /* | |
2274 | * 'http_bio_new()' - Initialize an OpenSSL BIO structure. | |
2275 | */ | |
2276 | ||
2277 | static int /* O - 1 on success, 0 on failure */ | |
2278 | http_bio_new(BIO *h) /* I - BIO data */ | |
2279 | { | |
2280 | if (!h) | |
2281 | return (0); | |
2282 | ||
2283 | h->init = 0; | |
2284 | h->num = 0; | |
2285 | h->ptr = NULL; | |
2286 | h->flags = 0; | |
2287 | ||
2288 | return (1); | |
2289 | } | |
2290 | ||
2291 | ||
2292 | /* | |
2293 | * 'http_bio_puts()' - Send a string for OpenSSL. | |
2294 | */ | |
2295 | ||
2296 | static int /* O - Bytes written */ | |
2297 | http_bio_puts(BIO *h, /* I - BIO data */ | |
2298 | const char *str) /* I - String to write */ | |
2299 | { | |
b86bc4cf | 2300 | #ifdef WIN32 |
2301 | return (send(((http_t *)h->ptr)->fd, str, (int)strlen(str), 0)); | |
2302 | #else | |
411affcf | 2303 | return (send(((http_t *)h->ptr)->fd, str, strlen(str), 0)); |
b86bc4cf | 2304 | #endif /* WIN32 */ |
411affcf | 2305 | } |
2306 | ||
2307 | ||
2308 | /* | |
2309 | * 'http_bio_read()' - Read data for OpenSSL. | |
2310 | */ | |
2311 | ||
2312 | static int /* O - Bytes read */ | |
2313 | http_bio_read(BIO *h, /* I - BIO data */ | |
2314 | char *buf, /* I - Buffer */ | |
2315 | int size) /* I - Number of bytes to read */ | |
2316 | { | |
2317 | http_t *http; /* HTTP connection */ | |
2318 | ||
2319 | ||
2320 | http = (http_t *)h->ptr; | |
2321 | ||
2322 | if (!http->blocking) | |
2323 | { | |
2324 | /* | |
2325 | * Make sure we have data before we read... | |
2326 | */ | |
2327 | ||
2328 | if (!http_wait(http, 10000, 0)) | |
2329 | { | |
b86bc4cf | 2330 | #ifdef WIN32 |
2331 | http->error = WSAETIMEDOUT; | |
2332 | #else | |
411affcf | 2333 | http->error = ETIMEDOUT; |
b86bc4cf | 2334 | #endif /* WIN32 */ |
2335 | ||
411affcf | 2336 | return (-1); |
2337 | } | |
2338 | } | |
2339 | ||
2340 | return (recv(http->fd, buf, size, 0)); | |
2341 | } | |
2342 | ||
2343 | ||
2344 | /* | |
2345 | * 'http_bio_write()' - Write data for OpenSSL. | |
2346 | */ | |
2347 | ||
2348 | static int /* O - Bytes written */ | |
2349 | http_bio_write(BIO *h, /* I - BIO data */ | |
2350 | const char *buf, /* I - Buffer to write */ | |
2351 | int num) /* I - Number of bytes to write */ | |
2352 | { | |
2353 | return (send(((http_t *)h->ptr)->fd, buf, num, 0)); | |
2354 | } | |
2355 | #endif /* HAVE_SSL && HAVE_LIBSSL */ | |
2356 | ||
2357 | ||
ef416fc2 | 2358 | /* |
2359 | * 'http_field()' - Return the field index for a field name. | |
2360 | */ | |
2361 | ||
2362 | static http_field_t /* O - Field index */ | |
2363 | http_field(const char *name) /* I - String name */ | |
2364 | { | |
2365 | int i; /* Looping var */ | |
2366 | ||
2367 | ||
2368 | for (i = 0; i < HTTP_FIELD_MAX; i ++) | |
2369 | if (strcasecmp(name, http_fields[i]) == 0) | |
2370 | return ((http_field_t)i); | |
2371 | ||
2372 | return (HTTP_FIELD_UNKNOWN); | |
2373 | } | |
2374 | ||
2375 | ||
2376 | #ifdef HAVE_SSL | |
2377 | /* | |
2378 | * 'http_read_ssl()' - Read from a SSL/TLS connection. | |
2379 | */ | |
2380 | ||
2381 | static int /* O - Bytes read */ | |
ecdc0628 | 2382 | http_read_ssl(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 2383 | char *buf, /* I - Buffer to store data */ |
2384 | int len) /* I - Length of buffer */ | |
2385 | { | |
2386 | # if defined(HAVE_LIBSSL) | |
2387 | return (SSL_read((SSL *)(http->tls), buf, len)); | |
2388 | ||
2389 | # elif defined(HAVE_GNUTLS) | |
2390 | return (gnutls_record_recv(((http_tls_t *)(http->tls))->session, buf, len)); | |
2391 | ||
2392 | # elif defined(HAVE_CDSASSL) | |
fa73b229 | 2393 | int result; /* Return value */ |
ef416fc2 | 2394 | OSStatus error; /* Error info */ |
2395 | size_t processed; /* Number of bytes processed */ | |
2396 | ||
2397 | ||
89d46774 | 2398 | error = SSLRead(((http_tls_t *)http->tls)->session, buf, len, &processed); |
ef416fc2 | 2399 | |
fa73b229 | 2400 | switch (error) |
ef416fc2 | 2401 | { |
fa73b229 | 2402 | case 0 : |
2403 | result = (int)processed; | |
2404 | break; | |
2405 | case errSSLClosedGraceful : | |
2406 | result = 0; | |
2407 | break; | |
2408 | case errSSLWouldBlock : | |
b423cd4c | 2409 | if (processed) |
2410 | result = (int)processed; | |
2411 | else | |
2412 | { | |
2413 | result = -1; | |
2414 | errno = EINTR; | |
2415 | } | |
fa73b229 | 2416 | break; |
2417 | default : | |
2418 | errno = EPIPE; | |
2419 | result = -1; | |
2420 | break; | |
ef416fc2 | 2421 | } |
fa73b229 | 2422 | |
2423 | return (result); | |
ef416fc2 | 2424 | # endif /* HAVE_LIBSSL */ |
2425 | } | |
2426 | #endif /* HAVE_SSL */ | |
2427 | ||
2428 | ||
2429 | /* | |
2430 | * 'http_send()' - Send a request with all fields and the trailing blank line. | |
2431 | */ | |
2432 | ||
2433 | static int /* O - 0 on success, non-zero on error */ | |
ecdc0628 | 2434 | http_send(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 2435 | http_state_t request, /* I - Request code */ |
2436 | const char *uri) /* I - URI */ | |
2437 | { | |
2438 | int i; /* Looping var */ | |
2439 | char *ptr, /* Pointer in buffer */ | |
2440 | buf[1024]; /* Encoded URI buffer */ | |
2441 | static const char * const codes[] = | |
2442 | { /* Request code strings */ | |
2443 | NULL, | |
2444 | "OPTIONS", | |
2445 | "GET", | |
2446 | NULL, | |
2447 | "HEAD", | |
2448 | "POST", | |
2449 | NULL, | |
2450 | NULL, | |
2451 | "PUT", | |
2452 | NULL, | |
2453 | "DELETE", | |
2454 | "TRACE", | |
2455 | "CLOSE" | |
2456 | }; | |
2457 | static const char hex[] = "0123456789ABCDEF"; | |
2458 | /* Hex digits */ | |
2459 | ||
2460 | ||
2461 | DEBUG_printf(("http_send(http=%p, request=HTTP_%s, uri=\"%s\")\n", | |
2462 | http, codes[request], uri)); | |
2463 | ||
2464 | if (http == NULL || uri == NULL) | |
2465 | return (-1); | |
2466 | ||
2467 | /* | |
2468 | * Set the User-Agent field if it isn't already... | |
2469 | */ | |
2470 | ||
2471 | if (!http->fields[HTTP_FIELD_USER_AGENT][0]) | |
2472 | httpSetField(http, HTTP_FIELD_USER_AGENT, CUPS_MINIMAL); | |
2473 | ||
2474 | /* | |
2475 | * Encode the URI as needed... | |
2476 | */ | |
2477 | ||
2478 | for (ptr = buf; *uri != '\0' && ptr < (buf + sizeof(buf) - 1); uri ++) | |
2479 | if (*uri <= ' ' || *uri >= 127) | |
2480 | { | |
2481 | if (ptr < (buf + sizeof(buf) - 1)) | |
2482 | *ptr ++ = '%'; | |
2483 | if (ptr < (buf + sizeof(buf) - 1)) | |
2484 | *ptr ++ = hex[(*uri >> 4) & 15]; | |
2485 | if (ptr < (buf + sizeof(buf) - 1)) | |
2486 | *ptr ++ = hex[*uri & 15]; | |
2487 | } | |
2488 | else | |
2489 | *ptr ++ = *uri; | |
2490 | ||
2491 | *ptr = '\0'; | |
2492 | ||
2493 | /* | |
2494 | * See if we had an error the last time around; if so, reconnect... | |
2495 | */ | |
2496 | ||
2497 | if (http->status == HTTP_ERROR || http->status >= HTTP_BAD_REQUEST) | |
fa73b229 | 2498 | if (httpReconnect(http)) |
2499 | return (-1); | |
ef416fc2 | 2500 | |
d09495fa | 2501 | /* |
2502 | * Flush any written data that is pending... | |
2503 | */ | |
2504 | ||
2505 | if (http->wused) | |
2506 | httpFlushWrite(http); | |
2507 | ||
ef416fc2 | 2508 | /* |
2509 | * Send the request header... | |
2510 | */ | |
2511 | ||
d09495fa | 2512 | http->state = request; |
2513 | http->data_encoding = HTTP_ENCODE_FIELDS; | |
2514 | ||
ef416fc2 | 2515 | if (request == HTTP_POST || request == HTTP_PUT) |
2516 | http->state ++; | |
2517 | ||
2518 | http->status = HTTP_CONTINUE; | |
2519 | ||
2520 | #ifdef HAVE_SSL | |
2521 | if (http->encryption == HTTP_ENCRYPT_REQUIRED && !http->tls) | |
2522 | { | |
2523 | httpSetField(http, HTTP_FIELD_CONNECTION, "Upgrade"); | |
2524 | httpSetField(http, HTTP_FIELD_UPGRADE, "TLS/1.0,SSL/2.0,SSL/3.0"); | |
2525 | } | |
2526 | #endif /* HAVE_SSL */ | |
2527 | ||
2528 | if (httpPrintf(http, "%s %s HTTP/1.1\r\n", codes[request], buf) < 1) | |
2529 | { | |
2530 | http->status = HTTP_ERROR; | |
2531 | return (-1); | |
2532 | } | |
2533 | ||
2534 | for (i = 0; i < HTTP_FIELD_MAX; i ++) | |
2535 | if (http->fields[i][0] != '\0') | |
2536 | { | |
f7deaa1a | 2537 | DEBUG_printf(("%s: %s\n", http_fields[i], httpGetField(http, i))); |
ef416fc2 | 2538 | |
f7deaa1a | 2539 | if (httpPrintf(http, "%s: %s\r\n", http_fields[i], |
2540 | httpGetField(http, i)) < 1) | |
ef416fc2 | 2541 | { |
2542 | http->status = HTTP_ERROR; | |
2543 | return (-1); | |
2544 | } | |
2545 | } | |
2546 | ||
2547 | if (http->cookie) | |
2548 | if (httpPrintf(http, "Cookie: $Version=0; %s\r\n", http->cookie) < 1) | |
2549 | { | |
2550 | http->status = HTTP_ERROR; | |
2551 | return (-1); | |
2552 | } | |
2553 | ||
b423cd4c | 2554 | if (http->expect == HTTP_CONTINUE && |
2555 | (http->state == HTTP_POST_RECV || http->state == HTTP_PUT_RECV)) | |
2556 | if (httpPrintf(http, "Expect: 100-continue\r\n") < 1) | |
2557 | { | |
2558 | http->status = HTTP_ERROR; | |
2559 | return (-1); | |
2560 | } | |
2561 | ||
ef416fc2 | 2562 | if (httpPrintf(http, "\r\n") < 1) |
2563 | { | |
2564 | http->status = HTTP_ERROR; | |
2565 | return (-1); | |
2566 | } | |
2567 | ||
d09495fa | 2568 | httpFlushWrite(http); |
ef416fc2 | 2569 | httpGetLength2(http); |
2570 | httpClearFields(http); | |
2571 | ||
f7deaa1a | 2572 | /* |
b94498cf | 2573 | * The Kerberos and AuthRef authentication strings can only be used once... |
f7deaa1a | 2574 | */ |
2575 | ||
bc44d920 | 2576 | if (http->field_authorization && http->authstring && |
b94498cf | 2577 | (!strncmp(http->authstring, "Negotiate", 9) || |
2578 | !strncmp(http->authstring, "AuthRef", 7))) | |
f7deaa1a | 2579 | { |
2580 | http->_authstring[0] = '\0'; | |
2581 | ||
2582 | if (http->authstring != http->_authstring) | |
2583 | free(http->authstring); | |
2584 | ||
2585 | http->authstring = http->_authstring; | |
2586 | } | |
2587 | ||
ef416fc2 | 2588 | return (0); |
2589 | } | |
2590 | ||
2591 | ||
2592 | #ifdef HAVE_SSL | |
2593 | /* | |
2594 | * 'http_setup_ssl()' - Set up SSL/TLS support on a connection. | |
2595 | */ | |
2596 | ||
2597 | static int /* O - Status of connection */ | |
ecdc0628 | 2598 | http_setup_ssl(http_t *http) /* I - HTTP connection */ |
ef416fc2 | 2599 | { |
2600 | # ifdef HAVE_LIBSSL | |
89d46774 | 2601 | SSL_CTX *context; /* Context for encryption */ |
2602 | SSL *conn; /* Connection for encryption */ | |
411affcf | 2603 | BIO *bio; /* BIO data */ |
ef416fc2 | 2604 | # elif defined(HAVE_GNUTLS) |
89d46774 | 2605 | http_tls_t *conn; /* TLS session object */ |
ef416fc2 | 2606 | gnutls_certificate_client_credentials *credentials; |
89d46774 | 2607 | /* TLS credentials */ |
ef416fc2 | 2608 | # elif defined(HAVE_CDSASSL) |
89d46774 | 2609 | OSStatus error; /* Error code */ |
2610 | http_tls_t *conn; /* CDSA connection information */ | |
ef416fc2 | 2611 | # endif /* HAVE_LIBSSL */ |
2612 | ||
2613 | ||
2614 | DEBUG_printf(("http_setup_ssl(http=%p)\n", http)); | |
2615 | ||
2616 | # ifdef HAVE_LIBSSL | |
2617 | context = SSL_CTX_new(SSLv23_client_method()); | |
2618 | ||
2619 | SSL_CTX_set_options(context, SSL_OP_NO_SSLv2); /* Only use SSLv3 or TLS */ | |
2620 | ||
411affcf | 2621 | bio = BIO_new(_httpBIOMethods()); |
2622 | BIO_ctrl(bio, BIO_C_SET_FILE_PTR, 0, (char *)http); | |
2623 | ||
ef416fc2 | 2624 | conn = SSL_new(context); |
411affcf | 2625 | SSL_set_bio(conn, bio, bio); |
ef416fc2 | 2626 | |
ef416fc2 | 2627 | if (SSL_connect(conn) != 1) |
2628 | { | |
2629 | # ifdef DEBUG | |
2630 | unsigned long error; /* Error code */ | |
2631 | ||
2632 | while ((error = ERR_get_error()) != 0) | |
2633 | printf("http_setup_ssl: %s\n", ERR_error_string(error, NULL)); | |
2634 | # endif /* DEBUG */ | |
2635 | ||
2636 | SSL_CTX_free(context); | |
2637 | SSL_free(conn); | |
2638 | ||
2639 | # ifdef WIN32 | |
2640 | http->error = WSAGetLastError(); | |
2641 | # else | |
2642 | http->error = errno; | |
2643 | # endif /* WIN32 */ | |
2644 | http->status = HTTP_ERROR; | |
2645 | ||
2646 | return (HTTP_ERROR); | |
2647 | } | |
2648 | ||
2649 | # elif defined(HAVE_GNUTLS) | |
89d46774 | 2650 | if ((conn = (http_tls_t *)malloc(sizeof(http_tls_t))) == NULL) |
ef416fc2 | 2651 | { |
2652 | http->error = errno; | |
2653 | http->status = HTTP_ERROR; | |
2654 | ||
2655 | return (-1); | |
2656 | } | |
2657 | ||
2658 | credentials = (gnutls_certificate_client_credentials *) | |
2659 | malloc(sizeof(gnutls_certificate_client_credentials)); | |
2660 | if (credentials == NULL) | |
2661 | { | |
2662 | free(conn); | |
2663 | ||
2664 | http->error = errno; | |
2665 | http->status = HTTP_ERROR; | |
2666 | ||
2667 | return (-1); | |
2668 | } | |
2669 | ||
2670 | gnutls_certificate_allocate_credentials(credentials); | |
2671 | ||
2672 | gnutls_init(&(conn->session), GNUTLS_CLIENT); | |
2673 | gnutls_set_default_priority(conn->session); | |
2674 | gnutls_credentials_set(conn->session, GNUTLS_CRD_CERTIFICATE, *credentials); | |
411affcf | 2675 | gnutls_transport_set_ptr(conn->session, (gnutls_transport_ptr)http); |
2676 | gnutls_transport_set_pull_function(conn->session, _httpReadGNUTLS); | |
2677 | gnutls_transport_set_push_function(conn->session, _httpWriteGNUTLS); | |
ef416fc2 | 2678 | |
2679 | if ((gnutls_handshake(conn->session)) != GNUTLS_E_SUCCESS) | |
2680 | { | |
2681 | http->error = errno; | |
2682 | http->status = HTTP_ERROR; | |
2683 | ||
2684 | return (-1); | |
2685 | } | |
2686 | ||
2687 | conn->credentials = credentials; | |
2688 | ||
2689 | # elif defined(HAVE_CDSASSL) | |
89d46774 | 2690 | conn = (http_tls_t *)calloc(1, sizeof(http_tls_t)); |
e53920b9 | 2691 | |
89d46774 | 2692 | if (conn == NULL) |
2693 | return (-1); | |
e53920b9 | 2694 | |
89d46774 | 2695 | if ((error = SSLNewContext(false, &conn->session))) |
e53920b9 | 2696 | { |
89d46774 | 2697 | http->error = error; |
2698 | http->status = HTTP_ERROR; | |
e53920b9 | 2699 | |
89d46774 | 2700 | free(conn); |
2701 | return (-1); | |
e53920b9 | 2702 | } |
ef416fc2 | 2703 | |
89d46774 | 2704 | /* |
2705 | * Use a union to resolve warnings about int/pointer size mismatches... | |
2706 | */ | |
2707 | ||
411affcf | 2708 | error = SSLSetConnection(conn->session, http); |
89d46774 | 2709 | |
2710 | if (!error) | |
2711 | error = SSLSetIOFuncs(conn->session, _httpReadCDSA, _httpWriteCDSA); | |
2712 | ||
ef416fc2 | 2713 | if (!error) |
89d46774 | 2714 | error = SSLSetAllowsExpiredCerts(conn->session, true); |
ef416fc2 | 2715 | |
2716 | if (!error) | |
89d46774 | 2717 | error = SSLSetAllowsAnyRoot(conn->session, true); |
ef416fc2 | 2718 | |
ed486911 | 2719 | if (!error) |
2720 | error = SSLSetProtocolVersionEnabled(conn->session, kSSLProtocol2, false); | |
2721 | ||
ef416fc2 | 2722 | if (!error) |
b423cd4c | 2723 | { |
89d46774 | 2724 | while ((error = SSLHandshake(conn->session)) == errSSLWouldBlock) |
b423cd4c | 2725 | usleep(1000); |
2726 | } | |
ef416fc2 | 2727 | |
89d46774 | 2728 | if (error) |
ef416fc2 | 2729 | { |
2730 | http->error = error; | |
2731 | http->status = HTTP_ERROR; | |
2732 | ||
89d46774 | 2733 | SSLDisposeContext(conn->session); |
ef416fc2 | 2734 | |
89d46774 | 2735 | free(conn); |
ef416fc2 | 2736 | |
2737 | return (-1); | |
2738 | } | |
2739 | # endif /* HAVE_CDSASSL */ | |
2740 | ||
2741 | http->tls = conn; | |
2742 | return (0); | |
2743 | } | |
2744 | #endif /* HAVE_SSL */ | |
2745 | ||
2746 | ||
2747 | #ifdef HAVE_SSL | |
2748 | /* | |
2749 | * 'http_shutdown_ssl()' - Shut down SSL/TLS on a connection. | |
2750 | */ | |
2751 | ||
2752 | static void | |
89d46774 | 2753 | http_shutdown_ssl(http_t *http) /* I - HTTP connection */ |
ef416fc2 | 2754 | { |
2755 | # ifdef HAVE_LIBSSL | |
89d46774 | 2756 | SSL_CTX *context; /* Context for encryption */ |
2757 | SSL *conn; /* Connection for encryption */ | |
ef416fc2 | 2758 | |
2759 | ||
2760 | conn = (SSL *)(http->tls); | |
2761 | context = SSL_get_SSL_CTX(conn); | |
2762 | ||
2763 | SSL_shutdown(conn); | |
2764 | SSL_CTX_free(context); | |
2765 | SSL_free(conn); | |
2766 | ||
2767 | # elif defined(HAVE_GNUTLS) | |
89d46774 | 2768 | http_tls_t *conn; /* Encryption session */ |
ef416fc2 | 2769 | gnutls_certificate_client_credentials *credentials; |
89d46774 | 2770 | /* TLS credentials */ |
ef416fc2 | 2771 | |
2772 | ||
2773 | conn = (http_tls_t *)(http->tls); | |
2774 | credentials = (gnutls_certificate_client_credentials *)(conn->credentials); | |
2775 | ||
2776 | gnutls_bye(conn->session, GNUTLS_SHUT_RDWR); | |
2777 | gnutls_deinit(conn->session); | |
2778 | gnutls_certificate_free_credentials(*credentials); | |
2779 | free(credentials); | |
2780 | free(conn); | |
2781 | ||
2782 | # elif defined(HAVE_CDSASSL) | |
89d46774 | 2783 | http_tls_t *conn; /* CDSA connection information */ |
2784 | ||
2785 | ||
2786 | conn = (http_tls_t *)(http->tls); | |
2787 | ||
2788 | while (SSLClose(conn->session) == errSSLWouldBlock) | |
b423cd4c | 2789 | usleep(1000); |
2790 | ||
89d46774 | 2791 | SSLDisposeContext(conn->session); |
2792 | ||
2793 | if (conn->certsArray) | |
2794 | CFRelease(conn->certsArray); | |
2795 | ||
2796 | free(conn); | |
ef416fc2 | 2797 | # endif /* HAVE_LIBSSL */ |
2798 | ||
2799 | http->tls = NULL; | |
2800 | } | |
2801 | #endif /* HAVE_SSL */ | |
2802 | ||
2803 | ||
2804 | #ifdef HAVE_SSL | |
2805 | /* | |
2806 | * 'http_upgrade()' - Force upgrade to TLS encryption. | |
2807 | */ | |
2808 | ||
89d46774 | 2809 | static int /* O - Status of connection */ |
2810 | http_upgrade(http_t *http) /* I - HTTP connection */ | |
ef416fc2 | 2811 | { |
89d46774 | 2812 | int ret; /* Return value */ |
2813 | http_t myhttp; /* Local copy of HTTP data */ | |
ef416fc2 | 2814 | |
2815 | ||
2816 | DEBUG_printf(("http_upgrade(%p)\n", http)); | |
2817 | ||
2818 | /* | |
2819 | * Copy the HTTP data to a local variable so we can do the OPTIONS | |
2820 | * request without interfering with the existing request data... | |
2821 | */ | |
2822 | ||
2823 | memcpy(&myhttp, http, sizeof(myhttp)); | |
2824 | ||
2825 | /* | |
2826 | * Send an OPTIONS request to the server, requiring SSL or TLS | |
2827 | * encryption on the link... | |
2828 | */ | |
2829 | ||
f7deaa1a | 2830 | http->field_authorization = NULL; /* Don't free the auth string */ |
2831 | ||
b86bc4cf | 2832 | httpClearFields(http); |
2833 | httpSetField(http, HTTP_FIELD_CONNECTION, "upgrade"); | |
2834 | httpSetField(http, HTTP_FIELD_UPGRADE, "TLS/1.0, SSL/2.0, SSL/3.0"); | |
ef416fc2 | 2835 | |
b86bc4cf | 2836 | if ((ret = httpOptions(http, "*")) == 0) |
ef416fc2 | 2837 | { |
2838 | /* | |
2839 | * Wait for the secure connection... | |
2840 | */ | |
2841 | ||
b86bc4cf | 2842 | while (httpUpdate(http) == HTTP_CONTINUE); |
ef416fc2 | 2843 | } |
2844 | ||
b86bc4cf | 2845 | httpFlush(http); |
ef416fc2 | 2846 | |
2847 | /* | |
b86bc4cf | 2848 | * Restore the HTTP request data... |
ef416fc2 | 2849 | */ |
2850 | ||
b86bc4cf | 2851 | memcpy(http->fields, myhttp.fields, sizeof(http->fields)); |
f7deaa1a | 2852 | http->data_encoding = myhttp.data_encoding; |
2853 | http->data_remaining = myhttp.data_remaining; | |
2854 | http->_data_remaining = myhttp._data_remaining; | |
2855 | http->expect = myhttp.expect; | |
2856 | http->field_authorization = myhttp.field_authorization; | |
bc44d920 | 2857 | http->digest_tries = myhttp.digest_tries; |
ef416fc2 | 2858 | |
2859 | /* | |
2860 | * See if we actually went secure... | |
2861 | */ | |
2862 | ||
2863 | if (!http->tls) | |
2864 | { | |
2865 | /* | |
2866 | * Server does not support HTTP upgrade... | |
2867 | */ | |
2868 | ||
2869 | DEBUG_puts("Server does not support HTTP upgrade!"); | |
2870 | ||
2871 | # ifdef WIN32 | |
2872 | closesocket(http->fd); | |
2873 | # else | |
2874 | close(http->fd); | |
2875 | # endif | |
2876 | ||
2877 | http->fd = -1; | |
2878 | ||
2879 | return (-1); | |
2880 | } | |
2881 | else | |
2882 | return (ret); | |
2883 | } | |
2884 | #endif /* HAVE_SSL */ | |
2885 | ||
2886 | ||
2887 | /* | |
2888 | * 'http_wait()' - Wait for data available on a connection. | |
2889 | */ | |
2890 | ||
2891 | static int /* O - 1 if data is available, 0 otherwise */ | |
ecdc0628 | 2892 | http_wait(http_t *http, /* I - HTTP connection */ |
411affcf | 2893 | int msec, /* I - Milliseconds to wait */ |
2894 | int usessl) /* I - Use SSL context? */ | |
ef416fc2 | 2895 | { |
f7deaa1a | 2896 | #ifdef HAVE_POLL |
2897 | struct pollfd pfd; /* Polled file descriptor */ | |
2898 | #else | |
2899 | fd_set input_set; /* select() input set */ | |
ef416fc2 | 2900 | struct timeval timeout; /* Timeout */ |
f7deaa1a | 2901 | #endif /* HAVE_POLL */ |
2902 | int nfds; /* Result from select()/poll() */ | |
ef416fc2 | 2903 | |
2904 | ||
2905 | DEBUG_printf(("http_wait(http=%p, msec=%d)\n", http, msec)); | |
2906 | ||
fa73b229 | 2907 | if (http->fd < 0) |
2908 | return (0); | |
2909 | ||
ef416fc2 | 2910 | /* |
2911 | * Check the SSL/TLS buffers for data first... | |
2912 | */ | |
2913 | ||
2914 | #ifdef HAVE_SSL | |
411affcf | 2915 | if (http->tls && usessl) |
ef416fc2 | 2916 | { |
2917 | # ifdef HAVE_LIBSSL | |
2918 | if (SSL_pending((SSL *)(http->tls))) | |
2919 | return (1); | |
2920 | # elif defined(HAVE_GNUTLS) | |
2921 | if (gnutls_record_check_pending(((http_tls_t *)(http->tls))->session)) | |
2922 | return (1); | |
2923 | # elif defined(HAVE_CDSASSL) | |
2924 | size_t bytes; /* Bytes that are available */ | |
2925 | ||
89d46774 | 2926 | if (!SSLGetBufferedReadSize(((http_tls_t *)http->tls)->session, &bytes) && bytes > 0) |
ef416fc2 | 2927 | return (1); |
2928 | # endif /* HAVE_LIBSSL */ | |
2929 | } | |
2930 | #endif /* HAVE_SSL */ | |
2931 | ||
2932 | /* | |
f7deaa1a | 2933 | * Then try doing a select() or poll() to poll the socket... |
ef416fc2 | 2934 | */ |
2935 | ||
f7deaa1a | 2936 | #ifdef HAVE_POLL |
2937 | pfd.fd = http->fd; | |
2938 | pfd.events = POLLIN; | |
ef416fc2 | 2939 | |
f7deaa1a | 2940 | while ((nfds = poll(&pfd, 1, msec)) < 0 && errno == EINTR); |
ef416fc2 | 2941 | |
f7deaa1a | 2942 | #else |
ef416fc2 | 2943 | do |
2944 | { | |
f7deaa1a | 2945 | FD_ZERO(&input_set); |
2946 | FD_SET(http->fd, &input_set); | |
ef416fc2 | 2947 | |
89d46774 | 2948 | DEBUG_printf(("http_wait: msec=%d, http->fd=%d\n", msec, http->fd)); |
2949 | ||
ef416fc2 | 2950 | if (msec >= 0) |
2951 | { | |
2952 | timeout.tv_sec = msec / 1000; | |
2953 | timeout.tv_usec = (msec % 1000) * 1000; | |
2954 | ||
f7deaa1a | 2955 | nfds = select(http->fd + 1, &input_set, NULL, NULL, &timeout); |
ef416fc2 | 2956 | } |
2957 | else | |
f7deaa1a | 2958 | nfds = select(http->fd + 1, &input_set, NULL, NULL, NULL); |
89d46774 | 2959 | |
2960 | DEBUG_printf(("http_wait: select() returned %d...\n", nfds)); | |
ef416fc2 | 2961 | } |
f7deaa1a | 2962 | # ifdef WIN32 |
ef416fc2 | 2963 | while (nfds < 0 && WSAGetLastError() == WSAEINTR); |
f7deaa1a | 2964 | # else |
ef416fc2 | 2965 | while (nfds < 0 && errno == EINTR); |
f7deaa1a | 2966 | # endif /* WIN32 */ |
2967 | #endif /* HAVE_POLL */ | |
ef416fc2 | 2968 | |
89d46774 | 2969 | DEBUG_printf(("http_wait: returning with nfds=%d...\n", nfds)); |
2970 | ||
ef416fc2 | 2971 | return (nfds > 0); |
2972 | } | |
2973 | ||
2974 | ||
2975 | /* | |
2976 | * 'http_write()' - Write a buffer to a HTTP connection. | |
2977 | */ | |
2978 | ||
2979 | static int /* O - Number of bytes written */ | |
ecdc0628 | 2980 | http_write(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 2981 | const char *buffer, /* I - Buffer for data */ |
2982 | int length) /* I - Number of bytes to write */ | |
2983 | { | |
2984 | int tbytes, /* Total bytes sent */ | |
2985 | bytes; /* Bytes sent */ | |
2986 | ||
2987 | ||
2988 | tbytes = 0; | |
2989 | ||
2990 | while (length > 0) | |
2991 | { | |
2992 | #ifdef HAVE_SSL | |
2993 | if (http->tls) | |
2994 | bytes = http_write_ssl(http, buffer, length); | |
2995 | else | |
2996 | #endif /* HAVE_SSL */ | |
2997 | bytes = send(http->fd, buffer, length, 0); | |
2998 | ||
2999 | if (bytes < 0) | |
3000 | { | |
3001 | #ifdef WIN32 | |
3002 | if (WSAGetLastError() != http->error) | |
3003 | { | |
3004 | http->error = WSAGetLastError(); | |
3005 | continue; | |
3006 | } | |
3007 | #else | |
3008 | if (errno == EINTR) | |
3009 | continue; | |
3010 | else if (errno != http->error && errno != ECONNRESET) | |
3011 | { | |
3012 | http->error = errno; | |
3013 | continue; | |
3014 | } | |
3015 | #endif /* WIN32 */ | |
3016 | ||
3017 | DEBUG_puts("http_write: error writing data...\n"); | |
3018 | ||
3019 | return (-1); | |
3020 | } | |
3021 | ||
3022 | buffer += bytes; | |
3023 | tbytes += bytes; | |
3024 | length -= bytes; | |
3025 | } | |
3026 | ||
3027 | #ifdef DEBUG | |
3028 | { | |
3029 | int i, j, ch; | |
3030 | printf("http_write: wrote %d bytes: \n", tbytes); | |
3031 | for (i = 0, buffer -= tbytes; i < tbytes; i += 16) | |
3032 | { | |
3033 | printf(" "); | |
3034 | ||
3035 | for (j = 0; j < 16 && (i + j) < tbytes; j ++) | |
3036 | printf(" %02X", buffer[i + j] & 255); | |
3037 | ||
3038 | while (j < 16) | |
3039 | { | |
3040 | printf(" "); | |
3041 | j ++; | |
3042 | } | |
3043 | ||
3044 | printf(" "); | |
3045 | for (j = 0; j < 16 && (i + j) < tbytes; j ++) | |
3046 | { | |
3047 | ch = buffer[i + j] & 255; | |
3048 | ||
3049 | if (ch < ' ' || ch == 127) | |
3050 | ch = '.'; | |
3051 | ||
3052 | putchar(ch); | |
3053 | } | |
3054 | putchar('\n'); | |
3055 | } | |
3056 | } | |
3057 | #endif /* DEBUG */ | |
3058 | ||
3059 | return (tbytes); | |
3060 | } | |
3061 | ||
3062 | ||
3063 | /* | |
3064 | * 'http_write_chunk()' - Write a chunked buffer. | |
3065 | */ | |
3066 | ||
3067 | static int /* O - Number bytes written */ | |
ecdc0628 | 3068 | http_write_chunk(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 3069 | const char *buffer, /* I - Buffer to write */ |
3070 | int length) /* I - Length of buffer */ | |
3071 | { | |
3072 | char header[255]; /* Chunk header */ | |
3073 | int bytes; /* Bytes written */ | |
3074 | ||
3075 | DEBUG_printf(("http_write_chunk(http=%p, buffer=%p, length=%d)\n", | |
3076 | http, buffer, length)); | |
3077 | ||
3078 | /* | |
3079 | * Write the chunk header, data, and trailer. | |
3080 | */ | |
3081 | ||
3082 | sprintf(header, "%x\r\n", length); | |
b86bc4cf | 3083 | if (http_write(http, header, (int)strlen(header)) < 0) |
ef416fc2 | 3084 | { |
3085 | DEBUG_puts(" http_write of length failed!"); | |
3086 | return (-1); | |
3087 | } | |
3088 | ||
3089 | if ((bytes = http_write(http, buffer, length)) < 0) | |
3090 | { | |
3091 | DEBUG_puts(" http_write of buffer failed!"); | |
3092 | return (-1); | |
3093 | } | |
3094 | ||
3095 | if (http_write(http, "\r\n", 2) < 0) | |
3096 | { | |
3097 | DEBUG_puts(" http_write of CR LF failed!"); | |
3098 | return (-1); | |
3099 | } | |
3100 | ||
3101 | return (bytes); | |
3102 | } | |
3103 | ||
3104 | ||
3105 | #ifdef HAVE_SSL | |
3106 | /* | |
3107 | * 'http_write_ssl()' - Write to a SSL/TLS connection. | |
3108 | */ | |
3109 | ||
3110 | static int /* O - Bytes written */ | |
ecdc0628 | 3111 | http_write_ssl(http_t *http, /* I - HTTP connection */ |
ef416fc2 | 3112 | const char *buf, /* I - Buffer holding data */ |
3113 | int len) /* I - Length of buffer */ | |
3114 | { | |
3115 | # if defined(HAVE_LIBSSL) | |
3116 | return (SSL_write((SSL *)(http->tls), buf, len)); | |
3117 | ||
3118 | # elif defined(HAVE_GNUTLS) | |
3119 | return (gnutls_record_send(((http_tls_t *)(http->tls))->session, buf, len)); | |
3120 | # elif defined(HAVE_CDSASSL) | |
fa73b229 | 3121 | int result; /* Return value */ |
ef416fc2 | 3122 | OSStatus error; /* Error info */ |
3123 | size_t processed; /* Number of bytes processed */ | |
3124 | ||
3125 | ||
89d46774 | 3126 | error = SSLWrite(((http_tls_t *)http->tls)->session, buf, len, &processed); |
ef416fc2 | 3127 | |
fa73b229 | 3128 | switch (error) |
ef416fc2 | 3129 | { |
fa73b229 | 3130 | case 0 : |
3131 | result = (int)processed; | |
3132 | break; | |
3133 | case errSSLClosedGraceful : | |
3134 | result = 0; | |
3135 | break; | |
3136 | case errSSLWouldBlock : | |
b423cd4c | 3137 | if (processed) |
3138 | result = (int)processed; | |
3139 | else | |
3140 | { | |
3141 | result = -1; | |
3142 | errno = EINTR; | |
3143 | } | |
fa73b229 | 3144 | break; |
3145 | default : | |
3146 | errno = EPIPE; | |
3147 | result = -1; | |
3148 | break; | |
ef416fc2 | 3149 | } |
fa73b229 | 3150 | |
3151 | return (result); | |
ef416fc2 | 3152 | # endif /* HAVE_LIBSSL */ |
3153 | } | |
3154 | #endif /* HAVE_SSL */ | |
3155 | ||
3156 | ||
3157 | /* | |
bc44d920 | 3158 | * End of "$Id: http.c 6649 2007-07-11 21:46:42Z mike $". |
ef416fc2 | 3159 | */ |