]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/request.c
Full sweep of all Clang warnings, plus some bug fixes for incorrect memcpy usage.
[thirdparty/cups.git] / cups / request.c
CommitLineData
ecdc0628 1/*
f2d18633 2 * "$Id$"
ecdc0628 3 *
7e86f2f6 4 * IPP utilities for CUPS.
ecdc0628 5 *
7e86f2f6
MS
6 * Copyright 2007-2014 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products.
ecdc0628 8 *
7e86f2f6
MS
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
ecdc0628 14 *
7e86f2f6 15 * This file is subject to the Apple OS-Developed Software exception.
ecdc0628 16 */
17
18/*
19 * Include necessary headers...
20 */
21
71e16022 22#include "cups-private.h"
ecdc0628 23#include <fcntl.h>
24#include <sys/stat.h>
25#if defined(WIN32) || defined(__EMX__)
26# include <io.h>
27#else
28# include <unistd.h>
29#endif /* WIN32 || __EMX__ */
b94498cf 30#ifndef O_BINARY
31# define O_BINARY 0
32#endif /* O_BINARY */
db8b865d
MS
33#ifndef MSG_DONTWAIT
34# define MSG_DONTWAIT 0
35#endif /* !MSG_DONTWAIT */
ecdc0628 36
37
38/*
39 * 'cupsDoFileRequest()' - Do an IPP request with a file.
40 *
5a9febac
MS
41 * This function sends the IPP request and attached file to the specified
42 * server, retrying and authenticating as necessary. The request is freed with
43 * @link ippDelete@.
ecdc0628 44 */
45
46ipp_t * /* O - Response data */
568fa3fa 47cupsDoFileRequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
ecdc0628 48 ipp_t *request, /* I - IPP request */
49 const char *resource, /* I - HTTP resource for POST */
568fa3fa 50 const char *filename) /* I - File to send or @code NULL@ for none */
b94498cf 51{
52 ipp_t *response; /* IPP response data */
53 int infile; /* Input file */
54
55
a603edef 56 DEBUG_printf(("cupsDoFileRequest(http=%p, request=%p(%s), resource=\"%s\", "
e07d4801 57 "filename=\"%s\")", http, request,
a603edef 58 request ? ippOpString(request->request.op.operation_id) : "?",
e07d4801 59 resource, filename));
a603edef 60
b94498cf 61 if (filename)
62 {
63 if ((infile = open(filename, O_RDONLY | O_BINARY)) < 0)
64 {
65 /*
66 * Can't get file information!
67 */
68
cb7f98ee 69 _cupsSetError(errno == ENOENT ? IPP_STATUS_ERROR_NOT_FOUND : IPP_STATUS_ERROR_NOT_AUTHORIZED,
749b1e90 70 NULL, 0);
b94498cf 71
72 ippDelete(request);
73
74 return (NULL);
75 }
76 }
77 else
78 infile = -1;
79
80 response = cupsDoIORequest(http, request, resource, infile, -1);
81
82 if (infile >= 0)
83 close(infile);
84
85 return (response);
86}
87
88
89/*
90 * 'cupsDoIORequest()' - Do an IPP request with file descriptors.
91 *
5a9febac
MS
92 * This function sends the IPP request with the optional input file "infile" to
93 * the specified server, retrying and authenticating as necessary. The request
94 * is freed with @link ippDelete@.
b94498cf 95 *
5a9febac 96 * If "infile" is a valid file descriptor, @code cupsDoIORequest@ copies
b94498cf 97 * all of the data from the file after the IPP request message.
98 *
5a9febac 99 * If "outfile" is a valid file descriptor, @code cupsDoIORequest@ copies
b94498cf 100 * all of the data after the IPP response message to the file.
101 *
f3c17241 102 * @since CUPS 1.3/OS X 10.5@
b94498cf 103 */
104
105ipp_t * /* O - Response data */
568fa3fa 106cupsDoIORequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
b94498cf 107 ipp_t *request, /* I - IPP request */
108 const char *resource, /* I - HTTP resource for POST */
109 int infile, /* I - File to read from or -1 for none */
110 int outfile) /* I - File to write to or -1 for none */
ecdc0628 111{
3d052e43
MS
112 ipp_t *response = NULL; /* IPP response data */
113 size_t length = 0; /* Content-Length value */
ecdc0628 114 http_status_t status; /* Status of HTTP request */
ecdc0628 115 struct stat fileinfo; /* File information */
7e86f2f6 116 ssize_t bytes; /* Number of bytes read/written */
a74454a7 117 char buffer[32768]; /* Output buffer */
ecdc0628 118
119
a603edef 120 DEBUG_printf(("cupsDoIORequest(http=%p, request=%p(%s), resource=\"%s\", "
e07d4801 121 "infile=%d, outfile=%d)", http, request,
a603edef 122 request ? ippOpString(request->request.op.operation_id) : "?",
e07d4801 123 resource, infile, outfile));
3d052e43
MS
124
125 /*
126 * Range check input...
127 */
ecdc0628 128
3d052e43 129 if (!request || !resource)
ecdc0628 130 {
3d052e43 131 ippDelete(request);
ecdc0628 132
cb7f98ee 133 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
ecdc0628 134
135 return (NULL);
136 }
137
3d052e43
MS
138 /*
139 * Get the default connection as needed...
140 */
141
142 if (!http)
a603edef 143 if ((http = _cupsConnect()) == NULL)
e60ec91f
MS
144 {
145 ippDelete(request);
146
a603edef 147 return (NULL);
e60ec91f 148 }
3d052e43 149
ecdc0628 150 /*
151 * See if we have a file to send...
152 */
153
b94498cf 154 if (infile >= 0)
ecdc0628 155 {
b94498cf 156 if (fstat(infile, &fileinfo))
ecdc0628 157 {
158 /*
159 * Can't get file information!
160 */
161
cb7f98ee 162 _cupsSetError(errno == EBADF ? IPP_STATUS_ERROR_NOT_FOUND : IPP_STATUS_ERROR_NOT_AUTHORIZED,
749b1e90 163 NULL, 0);
ecdc0628 164
165 ippDelete(request);
166
167 return (NULL);
168 }
169
170#ifdef WIN32
171 if (fileinfo.st_mode & _S_IFDIR)
172#else
173 if (S_ISDIR(fileinfo.st_mode))
174#endif /* WIN32 */
175 {
176 /*
177 * Can't send a directory...
178 */
179
180 ippDelete(request);
181
cb7f98ee 182 _cupsSetError(IPP_STATUS_ERROR_NOT_POSSIBLE, strerror(EISDIR), 0);
ecdc0628 183
184 return (NULL);
185 }
3d052e43
MS
186
187#ifndef WIN32
188 if (!S_ISREG(fileinfo.st_mode))
189 length = 0; /* Chunk when piping */
190 else
191#endif /* !WIN32 */
7e86f2f6 192 length = ippLength(request) + (size_t)fileinfo.st_size;
3d052e43
MS
193 }
194 else
195 length = ippLength(request);
196
e07d4801 197 DEBUG_printf(("2cupsDoIORequest: Request length=%ld, total length=%ld",
52f6f666
MS
198 (long)ippLength(request), (long)length));
199
b9faaae1
MS
200 /*
201 * Clear any "Local" authentication data since it is probably stale...
202 */
203
204 if (http->authstring && !strncmp(http->authstring, "Local ", 6))
205 httpSetAuthString(http, NULL, NULL);
206
3d052e43
MS
207 /*
208 * Loop until we can send the request without authorization problems.
209 */
210
211 while (response == NULL)
212 {
e07d4801 213 DEBUG_puts("2cupsDoIORequest: setup...");
3d052e43
MS
214
215 /*
216 * Send the request...
217 */
218
219 status = cupsSendRequest(http, request, resource, length);
220
e07d4801 221 DEBUG_printf(("2cupsDoIORequest: status=%d", status));
3d052e43 222
cb7f98ee 223 if (status == HTTP_STATUS_CONTINUE && request->state == IPP_STATE_DATA && infile >= 0)
3d052e43 224 {
e07d4801 225 DEBUG_puts("2cupsDoIORequest: file write...");
3d052e43
MS
226
227 /*
228 * Send the file with the request...
229 */
230
231#ifndef WIN32
232 if (S_ISREG(fileinfo.st_mode))
233#endif /* WIN32 */
234 lseek(infile, 0, SEEK_SET);
235
7e86f2f6 236 while ((bytes = read(infile, buffer, sizeof(buffer))) > 0)
3d052e43 237 {
7e86f2f6 238 if ((status = cupsWriteRequestData(http, buffer, (size_t)bytes))
cb7f98ee 239 != HTTP_STATUS_CONTINUE)
83e08001 240 break;
3d052e43
MS
241 }
242 }
243
244 /*
245 * Get the server's response...
246 */
247
cb7f98ee 248 if (status != HTTP_STATUS_ERROR)
3d052e43
MS
249 {
250 response = cupsGetResponse(http, resource);
83e08001 251 status = httpGetStatus(http);
3d052e43
MS
252 }
253
f11a948a
MS
254 DEBUG_printf(("2cupsDoIORequest: status=%d", status));
255
cb7f98ee
MS
256 if (status == HTTP_STATUS_ERROR ||
257 (status >= HTTP_STATUS_BAD_REQUEST && status != HTTP_STATUS_UNAUTHORIZED &&
258 status != HTTP_STATUS_UPGRADE_REQUIRED))
ee571f26
MS
259 {
260 _cupsSetHTTPError(status);
568fa3fa 261 break;
ee571f26 262 }
568fa3fa 263
85dda01c 264 if (response && outfile >= 0)
3d052e43 265 {
85dda01c
MS
266 /*
267 * Write trailing data to file...
268 */
3d052e43 269
7e86f2f6
MS
270 while ((bytes = httpRead2(http, buffer, sizeof(buffer))) > 0)
271 if (write(outfile, buffer, (size_t)bytes) < bytes)
85dda01c
MS
272 break;
273 }
3d052e43 274
cb7f98ee 275 if (http->state != HTTP_STATE_WAITING)
85dda01c
MS
276 {
277 /*
278 * Flush any remaining data...
279 */
280
281 httpFlush(http);
3d052e43
MS
282 }
283 }
284
285 /*
286 * Delete the original request and return the response...
287 */
6d2f911b 288
3d052e43
MS
289 ippDelete(request);
290
291 return (response);
292}
293
294
295/*
296 * 'cupsDoRequest()' - Do an IPP request.
297 *
298 * This function sends the IPP request to the specified server, retrying
5a9febac 299 * and authenticating as necessary. The request is freed with @link ippDelete@.
3d052e43
MS
300 */
301
302ipp_t * /* O - Response data */
568fa3fa 303cupsDoRequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
3d052e43
MS
304 ipp_t *request, /* I - IPP request */
305 const char *resource) /* I - HTTP resource for POST */
306{
e07d4801 307 DEBUG_printf(("cupsDoRequest(http=%p, request=%p(%s), resource=\"%s\")",
a603edef
MS
308 http, request,
309 request ? ippOpString(request->request.op.operation_id) : "?",
e07d4801 310 resource));
a603edef 311
b19ccc9e 312 return (cupsDoIORequest(http, request, resource, -1, -1));
3d052e43
MS
313}
314
315
316/*
317 * 'cupsGetResponse()' - Get a response to an IPP request.
318 *
319 * Use this function to get the response for an IPP request sent using
5a9febac
MS
320 * @link cupsSendRequest@. For requests that return additional data, use
321 * @link cupsReadResponseData@ after getting a successful response,
322 * otherwise call @link httpFlush@ to complete the response processing.
3d052e43 323 *
f3c17241 324 * @since CUPS 1.4/OS X 10.6@
3d052e43
MS
325 */
326
568fa3fa
MS
327ipp_t * /* O - Response or @code NULL@ on HTTP error */
328cupsGetResponse(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
3d052e43
MS
329 const char *resource) /* I - HTTP resource for POST */
330{
331 http_status_t status; /* HTTP status */
332 ipp_state_t state; /* IPP read state */
333 ipp_t *response = NULL; /* IPP response */
334
335
e07d4801 336 DEBUG_printf(("cupsGetResponse(http=%p, resource=\"%s\")", http, resource));
3d052e43
MS
337
338 /*
339 * Connect to the default server as needed...
340 */
341
342 if (!http)
343 http = _cupsConnect();
344
6961465f
MS
345 if (!http || (http->state != HTTP_STATE_POST_RECV &&
346 http->state != HTTP_STATE_POST_SEND))
3d052e43
MS
347 return (NULL);
348
349 /*
350 * Check for an unfinished chunked request...
351 */
352
cb7f98ee 353 if (http->data_encoding == HTTP_ENCODING_CHUNKED)
3d052e43
MS
354 {
355 /*
356 * Send a 0-length chunk to finish off the request...
357 */
358
e07d4801 359 DEBUG_puts("2cupsGetResponse: Finishing chunked POST...");
3d052e43
MS
360
361 if (httpWrite2(http, "", 0) < 0)
362 return (NULL);
363 }
364
365 /*
366 * Wait for a response from the server...
367 */
368
e07d4801 369 DEBUG_printf(("2cupsGetResponse: Update loop, http->status=%d...",
1ff0402e 370 http->status));
3d052e43 371
e60ec91f
MS
372 do
373 {
1ff0402e 374 status = httpUpdate(http);
e60ec91f 375 }
a469f8a5 376 while (status == HTTP_STATUS_CONTINUE);
3d052e43 377
e07d4801 378 DEBUG_printf(("2cupsGetResponse: status=%d", status));
3d052e43 379
cb7f98ee 380 if (status == HTTP_STATUS_OK)
3d052e43
MS
381 {
382 /*
383 * Get the IPP response...
384 */
385
386 response = ippNew();
387
cb7f98ee
MS
388 while ((state = ippRead(http, response)) != IPP_STATE_DATA)
389 if (state == IPP_STATE_ERROR)
3d052e43
MS
390 break;
391
cb7f98ee 392 if (state == IPP_STATE_ERROR)
3d052e43
MS
393 {
394 /*
f14324a7 395 * Flush remaining data and delete the response...
3d052e43
MS
396 */
397
e07d4801 398 DEBUG_puts("1cupsGetResponse: IPP read error!");
3d052e43 399
f14324a7
MS
400 httpFlush(http);
401
3d052e43
MS
402 ippDelete(response);
403 response = NULL;
404
cb7f98ee 405 http->status = status = HTTP_STATUS_ERROR;
a29fd7dd 406 http->error = EINVAL;
3d052e43
MS
407 }
408 }
cb7f98ee 409 else if (status != HTTP_STATUS_ERROR)
3d052e43
MS
410 {
411 /*
412 * Flush any error message...
413 */
414
415 httpFlush(http);
416
417 /*
418 * Then handle encryption and authentication...
419 */
420
cb7f98ee 421 if (status == HTTP_STATUS_UNAUTHORIZED)
3d052e43
MS
422 {
423 /*
424 * See if we can do authentication...
425 */
426
e07d4801 427 DEBUG_puts("2cupsGetResponse: Need authorization...");
568fa3fa 428
e07d4801 429 if (!cupsDoAuthentication(http, "POST", resource))
cb7f98ee 430 httpReconnect2(http, 30000, NULL);
f11a948a 431 else
cb7f98ee 432 http->status = status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED;
3d052e43
MS
433 }
434
435#ifdef HAVE_SSL
cb7f98ee 436 else if (status == HTTP_STATUS_UPGRADE_REQUIRED)
3d052e43
MS
437 {
438 /*
439 * Force a reconnect with encryption...
440 */
441
e07d4801 442 DEBUG_puts("2cupsGetResponse: Need encryption...");
3d052e43 443
cb7f98ee
MS
444 if (!httpReconnect2(http, 30000, NULL))
445 httpEncryption(http, HTTP_ENCRYPTION_REQUIRED);
3d052e43
MS
446 }
447#endif /* HAVE_SSL */
448 }
449
450 if (response)
451 {
452 ipp_attribute_t *attr; /* status-message attribute */
453
454
455 attr = ippFindAttribute(response, "status-message", IPP_TAG_TEXT);
456
e07d4801 457 DEBUG_printf(("1cupsGetResponse: status-code=%s, status-message=\"%s\"",
a603edef
MS
458 ippErrorString(response->request.status.status_code),
459 attr ? attr->values[0].string.text : ""));
460
3d052e43 461 _cupsSetError(response->request.status.status_code,
749b1e90
MS
462 attr ? attr->values[0].string.text :
463 ippErrorString(response->request.status.status_code), 0);
3d052e43 464 }
3d052e43
MS
465
466 return (response);
467}
468
469
6d2f911b 470/*
5a9febac
MS
471 * 'cupsLastError()' - Return the last IPP status code received on the current
472 * thread.
6d2f911b
MS
473 */
474
475ipp_status_t /* O - IPP status code from last request */
476cupsLastError(void)
477{
478 return (_cupsGlobals()->last_error);
479}
480
481
482/*
5a9febac
MS
483 * 'cupsLastErrorString()' - Return the last IPP status-message received on the
484 * current thread.
6d2f911b 485 *
f3c17241 486 * @since CUPS 1.2/OS X 10.5@
6d2f911b
MS
487 */
488
489const char * /* O - status-message text from last request */
490cupsLastErrorString(void)
491{
492 return (_cupsGlobals()->last_status_message);
493}
494
495
f14324a7
MS
496/*
497 * '_cupsNextDelay()' - Return the next retry delay value.
498 *
499 * This function currently returns the Fibonacci sequence 1 1 2 3 5 8.
500 *
501 * Pass 0 for the current delay value to initialize the sequence.
502 */
503
504int /* O - Next delay value */
505_cupsNextDelay(int current, /* I - Current delay value or 0 */
506 int *previous) /* IO - Previous delay value */
507{
508 int next; /* Next delay value */
509
510
511 if (current > 0)
512 {
513 next = (current + *previous) % 12;
514 *previous = next < current ? 0 : current;
515 }
516 else
517 {
518 next = 1;
519 *previous = 0;
520 }
521
522 return (next);
523}
524
525
3d052e43
MS
526/*
527 * 'cupsReadResponseData()' - Read additional data after the IPP response.
528 *
5a9febac
MS
529 * This function is used after @link cupsGetResponse@ to read the PPD or document
530 * files from @code CUPS_GET_PPD@ and @code CUPS_GET_DOCUMENT@ requests,
531 * respectively.
3d052e43 532 *
f3c17241 533 * @since CUPS 1.4/OS X 10.6@
3d052e43
MS
534 */
535
536ssize_t /* O - Bytes read, 0 on EOF, -1 on error */
537cupsReadResponseData(
568fa3fa 538 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
3d052e43
MS
539 char *buffer, /* I - Buffer to use */
540 size_t length) /* I - Number of bytes to read */
541{
542 /*
543 * Get the default connection as needed...
544 */
545
a603edef 546 DEBUG_printf(("cupsReadResponseData(http=%p, buffer=%p, "
e07d4801 547 "length=" CUPS_LLFMT ")", http, buffer, CUPS_LLCAST length));
a603edef 548
3d052e43
MS
549 if (!http)
550 {
551 _cups_globals_t *cg = _cupsGlobals();
552 /* Pointer to library globals */
553
554 if ((http = cg->http) == NULL)
555 {
cb7f98ee 556 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No active connection"), 1);
3d052e43
MS
557 return (-1);
558 }
ecdc0628 559 }
ecdc0628 560
3d052e43
MS
561 /*
562 * Then read from the HTTP connection...
563 */
564
565 return (httpRead2(http, buffer, length));
566}
567
568
569/*
570 * 'cupsSendRequest()' - Send an IPP request.
571 *
5a9febac
MS
572 * Use @link cupsWriteRequestData@ to write any additional data (document, PPD
573 * file, etc.) for the request, @link cupsGetResponse@ to get the IPP response,
574 * and @link cupsReadResponseData@ to read any additional data following the
575 * response. Only one request can be sent/queued at a time per @code http_t@
576 * connection.
577 *
cb7f98ee 578 * Returns the initial HTTP status code, which will be @code HTTP_STATUS_CONTINUE@
5a9febac 579 * on a successful send of the request.
3d052e43 580 *
5a9febac
MS
581 * Note: Unlike @link cupsDoFileRequest@, @link cupsDoIORequest@, and
582 * @link cupsDoRequest@, the request is NOT freed with @link ippDelete@.
3d052e43 583 *
f3c17241 584 * @since CUPS 1.4/OS X 10.6@
3d052e43
MS
585 */
586
587http_status_t /* O - Initial HTTP status */
568fa3fa 588cupsSendRequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
3d052e43
MS
589 ipp_t *request, /* I - IPP request */
590 const char *resource, /* I - Resource path */
ae71f5de 591 size_t length) /* I - Length of data to follow or @code CUPS_LENGTH_VARIABLE@ */
3d052e43 592{
a469f8a5
MS
593 http_status_t status; /* Status of HTTP request */
594 int got_status; /* Did we get the status? */
595 ipp_state_t state; /* State of IPP processing */
596 http_status_t expect; /* Expect: header to use */
3d052e43
MS
597
598
a603edef 599 DEBUG_printf(("cupsSendRequest(http=%p, request=%p(%s), resource=\"%s\", "
e07d4801 600 "length=" CUPS_LLFMT ")", http, request,
a603edef 601 request ? ippOpString(request->request.op.operation_id) : "?",
e07d4801 602 resource, CUPS_LLCAST length));
3d052e43
MS
603
604 /*
605 * Range check input...
606 */
607
608 if (!request || !resource)
609 {
cb7f98ee 610 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
3d052e43 611
cb7f98ee 612 return (HTTP_STATUS_ERROR);
3d052e43
MS
613 }
614
615 /*
616 * Get the default connection as needed...
617 */
618
619 if (!http)
a603edef 620 if ((http = _cupsConnect()) == NULL)
cb7f98ee 621 return (HTTP_STATUS_SERVICE_UNAVAILABLE);
3d052e43 622
a4845881
MS
623 /*
624 * If the prior request was not flushed out, do so now...
625 */
626
cb7f98ee
MS
627 if (http->state == HTTP_STATE_GET_SEND ||
628 http->state == HTTP_STATE_POST_SEND)
10ddcf65
MS
629 {
630 DEBUG_puts("2cupsSendRequest: Flush prior response.");
a4845881 631 httpFlush(http);
10ddcf65 632 }
cb7f98ee 633 else if (http->state != HTTP_STATE_WAITING)
a4845881 634 {
82cc1f9a
MS
635 DEBUG_printf(("1cupsSendRequest: Unknown HTTP state (%d), "
636 "reconnecting.", http->state));
cb7f98ee
MS
637 if (httpReconnect2(http, 30000, NULL))
638 return (HTTP_STATUS_ERROR);
a4845881
MS
639 }
640
7594b224 641#ifdef HAVE_SSL
642 /*
643 * See if we have an auth-info attribute and are communicating over
644 * a non-local link. If so, encrypt the link so that we can pass
645 * the authentication information securely...
646 */
647
648 if (ippFindAttribute(request, "auth-info", IPP_TAG_TEXT) &&
649 !httpAddrLocalhost(http->hostaddr) && !http->tls &&
cb7f98ee 650 httpEncryption(http, HTTP_ENCRYPTION_REQUIRED))
b19ccc9e 651 {
10ddcf65 652 DEBUG_puts("1cupsSendRequest: Unable to encrypt connection.");
cb7f98ee 653 return (HTTP_STATUS_SERVICE_UNAVAILABLE);
b19ccc9e 654 }
7594b224 655#endif /* HAVE_SSL */
656
1ff0402e
MS
657 /*
658 * Reconnect if the last response had a "Connection: close"...
659 */
660
88f9aafc 661 if (!_cups_strcasecmp(http->fields[HTTP_FIELD_CONNECTION], "close"))
10ddcf65
MS
662 {
663 DEBUG_puts("2cupsSendRequest: Connection: close");
664 httpClearFields(http);
cb7f98ee 665 if (httpReconnect2(http, 30000, NULL))
b19ccc9e 666 {
10ddcf65 667 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
cb7f98ee 668 return (HTTP_STATUS_SERVICE_UNAVAILABLE);
b19ccc9e 669 }
10ddcf65 670 }
1ff0402e 671
ecdc0628 672 /*
673 * Loop until we can send the request without authorization problems.
674 */
675
cb7f98ee 676 expect = HTTP_STATUS_CONTINUE;
ecdc0628 677
3d052e43 678 for (;;)
ecdc0628 679 {
e07d4801 680 DEBUG_puts("2cupsSendRequest: Setup...");
ecdc0628 681
682 /*
683 * Setup the HTTP variables needed...
684 */
685
ecdc0628 686 httpClearFields(http);
0268488e 687 httpSetExpect(http, expect);
ecdc0628 688 httpSetField(http, HTTP_FIELD_CONTENT_TYPE, "application/ipp");
0268488e
MS
689 httpSetLength(http, length);
690
691#ifdef HAVE_GSSAPI
692 if (http->authstring && !strncmp(http->authstring, "Negotiate", 9))
693 {
694 /*
695 * Do not use cached Kerberos credentials since they will look like a
696 * "replay" attack...
697 */
698
eac3a0a0 699 _cupsSetNegotiateAuthString(http, "POST", resource);
0268488e 700 }
0268488e 701#endif /* HAVE_GSSAPI */
f14324a7 702
ecdc0628 703 httpSetField(http, HTTP_FIELD_AUTHORIZATION, http->authstring);
704
e07d4801 705 DEBUG_printf(("2cupsSendRequest: authstring=\"%s\"", http->authstring));
ecdc0628 706
707 /*
708 * Try the request...
709 */
710
e07d4801 711 DEBUG_puts("2cupsSendRequest: Sending HTTP POST...");
ecdc0628 712
713 if (httpPost(http, resource))
714 {
10ddcf65 715 DEBUG_puts("2cupsSendRequest: POST failed, reconnecting.");
cb7f98ee 716 if (httpReconnect2(http, 30000, NULL))
b19ccc9e 717 {
10ddcf65 718 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
cb7f98ee 719 return (HTTP_STATUS_SERVICE_UNAVAILABLE);
b19ccc9e 720 }
ecdc0628 721 else
722 continue;
723 }
724
725 /*
d6ae789d 726 * Send the IPP data...
ecdc0628 727 */
728
e07d4801 729 DEBUG_puts("2cupsSendRequest: Writing IPP request...");
b423cd4c 730
cb7f98ee
MS
731 request->state = IPP_STATE_IDLE;
732 status = HTTP_STATUS_CONTINUE;
d6ae789d 733 got_status = 0;
734
cb7f98ee
MS
735 while ((state = ippWrite(http, request)) != IPP_STATE_DATA)
736 if (state == IPP_STATE_ERROR)
d6ae789d 737 break;
738 else if (httpCheck(http))
739 {
740 got_status = 1;
741
e60ec91f 742 _httpUpdate(http, &status);
cb7f98ee 743 if (status >= HTTP_STATUS_MULTIPLE_CHOICES)
d6ae789d 744 break;
745 }
746
cb7f98ee 747 if (state == IPP_STATE_ERROR)
22c9029b 748 {
10ddcf65
MS
749 DEBUG_puts("1cupsSendRequest: Unable to send IPP request.");
750
cb7f98ee
MS
751 http->status = HTTP_STATUS_ERROR;
752 http->state = HTTP_STATE_WAITING;
22c9029b 753
cb7f98ee 754 return (HTTP_STATUS_ERROR);
22c9029b
MS
755 }
756
3d052e43
MS
757 /*
758 * Wait up to 1 second to get the 100-continue response as needed...
759 */
ecdc0628 760
e60ec91f 761 if (!got_status)
3d052e43 762 {
cb7f98ee 763 if (expect == HTTP_STATUS_CONTINUE)
e60ec91f
MS
764 {
765 DEBUG_puts("2cupsSendRequest: Waiting for 100-continue...");
a603edef 766
e60ec91f
MS
767 if (httpWait(http, 1000))
768 _httpUpdate(http, &status);
769 }
770 else if (httpCheck(http))
771 _httpUpdate(http, &status);
d6ae789d 772 }
ecdc0628 773
e07d4801 774 DEBUG_printf(("2cupsSendRequest: status=%d", status));
a603edef 775
ecdc0628 776 /*
3d052e43 777 * Process the current HTTP status...
ecdc0628 778 */
779
a469f8a5 780 if (status >= HTTP_STATUS_MULTIPLE_CHOICES)
83e08001 781 {
a469f8a5
MS
782 int temp_status; /* Temporary status */
783
83e08001
MS
784 _cupsSetHTTPError(status);
785
786 do
787 {
a469f8a5 788 temp_status = httpUpdate(http);
83e08001 789 }
a469f8a5
MS
790 while (temp_status != HTTP_STATUS_ERROR &&
791 http->state == HTTP_STATE_POST_RECV);
83e08001 792
ba55dc12 793 httpFlush(http);
83e08001 794 }
ba55dc12 795
3d052e43 796 switch (status)
ecdc0628 797 {
a469f8a5
MS
798 case HTTP_STATUS_CONTINUE :
799 case HTTP_STATUS_OK :
800 case HTTP_STATUS_ERROR :
10ddcf65 801 DEBUG_printf(("1cupsSendRequest: Returning %d.", status));
3d052e43 802 return (status);
ecdc0628 803
a469f8a5 804 case HTTP_STATUS_UNAUTHORIZED :
ba55dc12 805 if (cupsDoAuthentication(http, "POST", resource))
10ddcf65 806 {
cb7f98ee
MS
807 DEBUG_puts("1cupsSendRequest: Returning HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED.");
808 return (HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED);
10ddcf65
MS
809 }
810
cb7f98ee 811 DEBUG_puts("2cupsSendRequest: Reconnecting after HTTP_STATUS_UNAUTHORIZED.");
ba55dc12 812
cb7f98ee 813 if (httpReconnect2(http, 30000, NULL))
f11a948a 814 {
10ddcf65 815 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
cb7f98ee 816 return (HTTP_STATUS_SERVICE_UNAVAILABLE);
f11a948a 817 }
ba55dc12 818 break;
89d46774 819
ecdc0628 820#ifdef HAVE_SSL
a469f8a5 821 case HTTP_STATUS_UPGRADE_REQUIRED :
3d052e43
MS
822 /*
823 * Flush any error message, reconnect, and then upgrade with
824 * encryption...
825 */
ecdc0628 826
10ddcf65 827 DEBUG_puts("2cupsSendRequest: Reconnecting after "
cb7f98ee 828 "HTTP_STATUS_UPGRADE_REQUIRED.");
10ddcf65 829
cb7f98ee 830 if (httpReconnect2(http, 30000, NULL))
b19ccc9e 831 {
10ddcf65 832 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
cb7f98ee 833 return (HTTP_STATUS_SERVICE_UNAVAILABLE);
b19ccc9e 834 }
ecdc0628 835
10ddcf65 836 DEBUG_puts("2cupsSendRequest: Upgrading to TLS.");
cb7f98ee 837 if (httpEncryption(http, HTTP_ENCRYPTION_REQUIRED))
ba55dc12 838 {
10ddcf65 839 DEBUG_puts("1cupsSendRequest: Unable to encrypt connection.");
cb7f98ee 840 return (HTTP_STATUS_SERVICE_UNAVAILABLE);
ba55dc12
MS
841 }
842 break;
ecdc0628 843#endif /* HAVE_SSL */
f301802f 844
a469f8a5 845 case HTTP_STATUS_EXPECTATION_FAILED :
3d052e43
MS
846 /*
847 * Don't try using the Expect: header the next time around...
848 */
ecdc0628 849
3d052e43 850 expect = (http_status_t)0;
52f6f666 851
10ddcf65
MS
852 DEBUG_puts("2cupsSendRequest: Reconnecting after "
853 "HTTP_EXPECTATION_FAILED.");
854
cb7f98ee 855 if (httpReconnect2(http, 30000, NULL))
52f6f666 856 {
10ddcf65 857 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
cb7f98ee 858 return (HTTP_STATUS_SERVICE_UNAVAILABLE);
52f6f666 859 }
a603edef 860 break;
ecdc0628 861
3d052e43
MS
862 default :
863 /*
864 * Some other error...
865 */
ecdc0628 866
3d052e43 867 return (status);
b94498cf 868 }
869 }
3d052e43
MS
870}
871
872
873/*
874 * 'cupsWriteRequestData()' - Write additional data after an IPP request.
875 *
568fa3fa
MS
876 * This function is used after @link cupsSendRequest@ to provide a PPD and
877 * after @link cupsStartDocument@ to provide a document file.
3d052e43 878 *
f3c17241 879 * @since CUPS 1.4/OS X 10.6@
3d052e43 880 */
ecdc0628 881
cb7f98ee 882http_status_t /* O - @code HTTP_STATUS_CONTINUE@ if OK or HTTP status on error */
3d052e43 883cupsWriteRequestData(
568fa3fa 884 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
3d052e43
MS
885 const char *buffer, /* I - Bytes to write */
886 size_t length) /* I - Number of bytes to write */
887{
38e73f87
MS
888 int wused; /* Previous bytes in buffer */
889
890
ecdc0628 891 /*
3d052e43 892 * Get the default connection as needed...
ecdc0628 893 */
ecdc0628 894
a603edef 895 DEBUG_printf(("cupsWriteRequestData(http=%p, buffer=%p, "
e07d4801 896 "length=" CUPS_LLFMT ")", http, buffer, CUPS_LLCAST length));
a603edef 897
3d052e43 898 if (!http)
ecdc0628 899 {
3d052e43
MS
900 _cups_globals_t *cg = _cupsGlobals();
901 /* Pointer to library globals */
ecdc0628 902
3d052e43
MS
903 if ((http = cg->http) == NULL)
904 {
cb7f98ee
MS
905 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No active connection"), 1);
906 DEBUG_puts("1cupsWriteRequestData: Returning HTTP_STATUS_ERROR.");
907 return (HTTP_STATUS_ERROR);
3d052e43 908 }
ecdc0628 909 }
ecdc0628 910
3d052e43
MS
911 /*
912 * Then write to the HTTP connection...
913 */
ecdc0628 914
38e73f87
MS
915 wused = http->wused;
916
3d052e43 917 if (httpWrite2(http, buffer, length) < 0)
10d09e33 918 {
cb7f98ee
MS
919 DEBUG_puts("1cupsWriteRequestData: Returning HTTP_STATUS_ERROR.");
920 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(http->error), 0);
921 return (HTTP_STATUS_ERROR);
10d09e33 922 }
ecdc0628 923
3d052e43
MS
924 /*
925 * Finally, check if we have any pending data from the server...
926 */
ecdc0628 927
f11a948a 928 if (length >= HTTP_MAX_BUFFER ||
38e73f87 929 http->wused < wused ||
7e86f2f6 930 (wused > 0 && (size_t)http->wused == length))
38e73f87
MS
931 {
932 /*
933 * We've written something to the server, so check for response data...
934 */
935
936 if (_httpWait(http, 0, 1))
ba55dc12 937 {
e60ec91f 938 http_status_t status; /* Status from _httpUpdate */
ba55dc12 939
e60ec91f 940 _httpUpdate(http, &status);
cb7f98ee 941 if (status >= HTTP_STATUS_MULTIPLE_CHOICES)
10d09e33
MS
942 {
943 _cupsSetHTTPError(status);
83e08001
MS
944
945 do
946 {
947 status = httpUpdate(http);
948 }
cb7f98ee 949 while (status != HTTP_STATUS_ERROR && http->state == HTTP_STATE_POST_RECV);
83e08001 950
ba55dc12 951 httpFlush(http);
10d09e33 952 }
ba55dc12 953
10d09e33 954 DEBUG_printf(("1cupsWriteRequestData: Returning %d.\n", status));
ba55dc12
MS
955 return (status);
956 }
38e73f87
MS
957 }
958
cb7f98ee
MS
959 DEBUG_puts("1cupsWriteRequestData: Returning HTTP_STATUS_CONTINUE.");
960 return (HTTP_STATUS_CONTINUE);
ecdc0628 961}
962
963
6d2f911b
MS
964/*
965 * '_cupsConnect()' - Get the default server connection...
966 */
967
968http_t * /* O - HTTP connection */
969_cupsConnect(void)
970{
971 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
972
973
974 /*
975 * See if we are connected to the same server...
976 */
977
978 if (cg->http)
979 {
980 /*
981 * Compare the connection hostname, port, and encryption settings to
982 * the cached defaults; these were initialized the first time we
983 * connected...
984 */
985
986 if (strcmp(cg->http->hostname, cg->server) ||
a469f8a5 987 cg->ipp_port != httpAddrPort(cg->http->hostaddr) ||
6d2f911b 988 (cg->http->encryption != cg->encryption &&
cb7f98ee 989 cg->http->encryption == HTTP_ENCRYPTION_NEVER))
6d2f911b
MS
990 {
991 /*
992 * Need to close the current connection because something has changed...
993 */
994
995 httpClose(cg->http);
996 cg->http = NULL;
997 }
0fa6c7fa
MS
998 else
999 {
1000 /*
1001 * Same server, see if the connection is still established...
1002 */
1003
80360a5e
MS
1004 char ch; /* Connection check byte */
1005 ssize_t n; /* Number of bytes */
0fa6c7fa 1006
db8b865d 1007#ifdef WIN32
80360a5e
MS
1008 if ((n = recv(cg->http->fd, &ch, 1, MSG_PEEK)) == 0 ||
1009 (n < 0 && WSAGetLastError() != WSAEWOULDBLOCK))
db8b865d 1010#else
80360a5e
MS
1011 if ((n = recv(cg->http->fd, &ch, 1, MSG_PEEK | MSG_DONTWAIT)) == 0 ||
1012 (n < 0 && errno != EWOULDBLOCK))
db8b865d 1013#endif /* WIN32 */
0fa6c7fa
MS
1014 {
1015 /*
1016 * Nope, close the connection...
1017 */
1018
1019 httpClose(cg->http);
1020 cg->http = NULL;
1021 }
1022 }
6d2f911b
MS
1023 }
1024
1025 /*
1026 * (Re)connect as needed...
1027 */
1028
1029 if (!cg->http)
1030 {
cb7f98ee
MS
1031 if ((cg->http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC,
1032 cupsEncryption(), 1, 30000, NULL)) == NULL)
6d2f911b
MS
1033 {
1034 if (errno)
cb7f98ee 1035 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE, NULL, 0);
6d2f911b 1036 else
cb7f98ee 1037 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE,
6d2f911b
MS
1038 _("Unable to connect to host."), 1);
1039 }
1040 }
1041
1042 /*
1043 * Return the cached connection...
1044 */
1045
1046 return (cg->http);
1047}
1048
1049
ecdc0628 1050/*
1051 * '_cupsSetError()' - Set the last IPP status code and status-message.
1052 */
1053
1054void
1055_cupsSetError(ipp_status_t status, /* I - IPP status code */
749b1e90
MS
1056 const char *message, /* I - status-message value */
1057 int localize) /* I - Localize the message? */
ecdc0628 1058{
1059 _cups_globals_t *cg; /* Global data */
1060
1061
749b1e90
MS
1062 if (!message && errno)
1063 {
1064 message = strerror(errno);
1065 localize = 0;
1066 }
1067
ecdc0628 1068 cg = _cupsGlobals();
1069 cg->last_error = status;
1070
1071 if (cg->last_status_message)
1072 {
749b1e90 1073 _cupsStrFree(cg->last_status_message);
ecdc0628 1074
1075 cg->last_status_message = NULL;
1076 }
1077
1078 if (message)
749b1e90
MS
1079 {
1080 if (localize)
1081 {
1082 /*
1083 * Get the message catalog...
1084 */
1085
1086 if (!cg->lang_default)
1087 cg->lang_default = cupsLangDefault();
1088
1089 cg->last_status_message = _cupsStrAlloc(_cupsLangString(cg->lang_default,
1090 message));
1091 }
1092 else
1093 cg->last_status_message = _cupsStrAlloc(message);
1094 }
a603edef 1095
e07d4801
MS
1096 DEBUG_printf(("4_cupsSetError: last_error=%s, last_status_message=\"%s\"",
1097 ippErrorString(cg->last_error), cg->last_status_message));
ecdc0628 1098}
1099
1100
1101/*
355e94dc
MS
1102 * '_cupsSetHTTPError()' - Set the last error using the HTTP status.
1103 */
1104
1105void
1106_cupsSetHTTPError(http_status_t status) /* I - HTTP status code */
1107{
1108 switch (status)
1109 {
cb7f98ee
MS
1110 case HTTP_STATUS_NOT_FOUND :
1111 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND, httpStatus(status), 0);
355e94dc
MS
1112 break;
1113
cb7f98ee
MS
1114 case HTTP_STATUS_UNAUTHORIZED :
1115 _cupsSetError(IPP_STATUS_ERROR_NOT_AUTHENTICATED, httpStatus(status), 0);
355e94dc
MS
1116 break;
1117
cb7f98ee
MS
1118 case HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED :
1119 _cupsSetError(IPP_STATUS_ERROR_CUPS_AUTHENTICATION_CANCELED, httpStatus(status), 0);
7cf5915e
MS
1120 break;
1121
cb7f98ee
MS
1122 case HTTP_STATUS_FORBIDDEN :
1123 _cupsSetError(IPP_STATUS_ERROR_FORBIDDEN, httpStatus(status), 0);
355e94dc
MS
1124 break;
1125
cb7f98ee
MS
1126 case HTTP_STATUS_BAD_REQUEST :
1127 _cupsSetError(IPP_STATUS_ERROR_BAD_REQUEST, httpStatus(status), 0);
355e94dc
MS
1128 break;
1129
cb7f98ee
MS
1130 case HTTP_STATUS_REQUEST_TOO_LARGE :
1131 _cupsSetError(IPP_STATUS_ERROR_REQUEST_VALUE, httpStatus(status), 0);
355e94dc
MS
1132 break;
1133
cb7f98ee
MS
1134 case HTTP_STATUS_NOT_IMPLEMENTED :
1135 _cupsSetError(IPP_STATUS_ERROR_OPERATION_NOT_SUPPORTED, httpStatus(status), 0);
355e94dc
MS
1136 break;
1137
cb7f98ee
MS
1138 case HTTP_STATUS_NOT_SUPPORTED :
1139 _cupsSetError(IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED, httpStatus(status), 0);
355e94dc
MS
1140 break;
1141
cb7f98ee
MS
1142 case HTTP_STATUS_UPGRADE_REQUIRED :
1143 _cupsSetError(IPP_STATUS_ERROR_CUPS_UPGRADE_REQUIRED, httpStatus(status), 0);
7cf5915e
MS
1144 break;
1145
cb7f98ee
MS
1146 case HTTP_STATUS_CUPS_PKI_ERROR :
1147 _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI, httpStatus(status), 0);
7cf5915e
MS
1148 break;
1149
cb7f98ee
MS
1150 case HTTP_STATUS_ERROR :
1151 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
10d09e33
MS
1152 break;
1153
355e94dc 1154 default :
e07d4801 1155 DEBUG_printf(("4_cupsSetHTTPError: HTTP error %d mapped to "
cb7f98ee
MS
1156 "IPP_STATUS_ERROR_SERVICE_UNAVAILABLE!", status));
1157 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE, httpStatus(status), 0);
355e94dc
MS
1158 break;
1159 }
1160}
1161
1162
1163/*
f2d18633 1164 * End of "$Id$".
ecdc0628 1165 */