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