]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/request.c
Merge changes from CUPS 1.5svn-r9105.
[thirdparty/cups.git] / cups / request.c
CommitLineData
ecdc0628 1/*
b19ccc9e 2 * "$Id: request.c 7946 2008-09-16 23:27:54Z mike $"
ecdc0628 3 *
71e16022 4 * IPP utilities for CUPS.
ecdc0628 5 *
5a6b583a 6 * Copyright 2007-2010 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.
3d052e43
MS
25 * cupsReadResponseData() - Read additional data after the IPP response.
26 * cupsSendRequest() - Send an IPP request.
27 * cupsWriteRequestData() - Write additional data after an IPP request.
6d2f911b 28 * _cupsConnect() - Get the default server connection...
3d052e43
MS
29 * _cupsSetError() - Set the last IPP status code and status-message.
30 * _cupsSetHTTPError() - Set the last error using the HTTP status.
ecdc0628 31 */
32
33/*
34 * Include necessary headers...
35 */
36
71e16022 37#include "cups-private.h"
ecdc0628 38#include <fcntl.h>
39#include <sys/stat.h>
40#if defined(WIN32) || defined(__EMX__)
41# include <io.h>
42#else
43# include <unistd.h>
44#endif /* WIN32 || __EMX__ */
b94498cf 45#ifndef O_BINARY
46# define O_BINARY 0
47#endif /* O_BINARY */
ecdc0628 48
49
50/*
51 * 'cupsDoFileRequest()' - Do an IPP request with a file.
52 *
53 * This function sends the IPP request to the specified server, retrying
568fa3fa 54 * and authenticating as necessary. The request is freed with @link ippDelete@
ecdc0628 55 * after receiving a valid IPP response.
56 */
57
58ipp_t * /* O - Response data */
568fa3fa 59cupsDoFileRequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
ecdc0628 60 ipp_t *request, /* I - IPP request */
61 const char *resource, /* I - HTTP resource for POST */
568fa3fa 62 const char *filename) /* I - File to send or @code NULL@ for none */
b94498cf 63{
64 ipp_t *response; /* IPP response data */
65 int infile; /* Input file */
66
67
a603edef 68 DEBUG_printf(("cupsDoFileRequest(http=%p, request=%p(%s), resource=\"%s\", "
e07d4801 69 "filename=\"%s\")", http, request,
a603edef 70 request ? ippOpString(request->request.op.operation_id) : "?",
e07d4801 71 resource, filename));
a603edef 72
b94498cf 73 if (filename)
74 {
75 if ((infile = open(filename, O_RDONLY | O_BINARY)) < 0)
76 {
77 /*
78 * Can't get file information!
79 */
80
81 _cupsSetError(errno == ENOENT ? IPP_NOT_FOUND : IPP_NOT_AUTHORIZED,
749b1e90 82 NULL, 0);
b94498cf 83
84 ippDelete(request);
85
86 return (NULL);
87 }
88 }
89 else
90 infile = -1;
91
92 response = cupsDoIORequest(http, request, resource, infile, -1);
93
94 if (infile >= 0)
95 close(infile);
96
97 return (response);
98}
99
100
101/*
102 * 'cupsDoIORequest()' - Do an IPP request with file descriptors.
103 *
104 * This function sends the IPP request to the specified server, retrying
105 * and authenticating as necessary. The request is freed with ippDelete()
106 * after receiving a valid IPP response.
107 *
108 * If "infile" is a valid file descriptor, cupsDoIORequest() copies
109 * all of the data from the file after the IPP request message.
110 *
111 * If "outfile" is a valid file descriptor, cupsDoIORequest() copies
112 * all of the data after the IPP response message to the file.
113 *
426c6a59 114 * @since CUPS 1.3/Mac OS X 10.5@
b94498cf 115 */
116
117ipp_t * /* O - Response data */
568fa3fa 118cupsDoIORequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
b94498cf 119 ipp_t *request, /* I - IPP request */
120 const char *resource, /* I - HTTP resource for POST */
121 int infile, /* I - File to read from or -1 for none */
122 int outfile) /* I - File to write to or -1 for none */
ecdc0628 123{
3d052e43
MS
124 ipp_t *response = NULL; /* IPP response data */
125 size_t length = 0; /* Content-Length value */
ecdc0628 126 http_status_t status; /* Status of HTTP request */
ecdc0628 127 struct stat fileinfo; /* File information */
128 int bytes; /* Number of bytes read/written */
a74454a7 129 char buffer[32768]; /* Output buffer */
ecdc0628 130
131
a603edef 132 DEBUG_printf(("cupsDoIORequest(http=%p, request=%p(%s), resource=\"%s\", "
e07d4801 133 "infile=%d, outfile=%d)", http, request,
a603edef 134 request ? ippOpString(request->request.op.operation_id) : "?",
e07d4801 135 resource, infile, outfile));
3d052e43
MS
136
137 /*
138 * Range check input...
139 */
ecdc0628 140
3d052e43 141 if (!request || !resource)
ecdc0628 142 {
3d052e43 143 ippDelete(request);
ecdc0628 144
749b1e90 145 _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
ecdc0628 146
147 return (NULL);
148 }
149
3d052e43
MS
150 /*
151 * Get the default connection as needed...
152 */
153
154 if (!http)
a603edef
MS
155 if ((http = _cupsConnect()) == NULL)
156 return (NULL);
3d052e43 157
ecdc0628 158 /*
159 * See if we have a file to send...
160 */
161
b94498cf 162 if (infile >= 0)
ecdc0628 163 {
b94498cf 164 if (fstat(infile, &fileinfo))
ecdc0628 165 {
166 /*
167 * Can't get file information!
168 */
169
3d052e43 170 _cupsSetError(errno == EBADF ? IPP_NOT_FOUND : IPP_NOT_AUTHORIZED,
749b1e90 171 NULL, 0);
ecdc0628 172
173 ippDelete(request);
174
175 return (NULL);
176 }
177
178#ifdef WIN32
179 if (fileinfo.st_mode & _S_IFDIR)
180#else
181 if (S_ISDIR(fileinfo.st_mode))
182#endif /* WIN32 */
183 {
184 /*
185 * Can't send a directory...
186 */
187
188 ippDelete(request);
189
749b1e90 190 _cupsSetError(IPP_NOT_POSSIBLE, strerror(EISDIR), 0);
ecdc0628 191
192 return (NULL);
193 }
3d052e43
MS
194
195#ifndef WIN32
196 if (!S_ISREG(fileinfo.st_mode))
197 length = 0; /* Chunk when piping */
198 else
199#endif /* !WIN32 */
200 length = ippLength(request) + fileinfo.st_size;
201 }
202 else
203 length = ippLength(request);
204
e07d4801 205 DEBUG_printf(("2cupsDoIORequest: Request length=%ld, total length=%ld",
52f6f666
MS
206 (long)ippLength(request), (long)length));
207
b9faaae1
MS
208 /*
209 * Clear any "Local" authentication data since it is probably stale...
210 */
211
212 if (http->authstring && !strncmp(http->authstring, "Local ", 6))
213 httpSetAuthString(http, NULL, NULL);
214
3d052e43
MS
215 /*
216 * Loop until we can send the request without authorization problems.
217 */
218
219 while (response == NULL)
220 {
e07d4801 221 DEBUG_puts("2cupsDoIORequest: setup...");
3d052e43
MS
222
223 /*
224 * Send the request...
225 */
226
227 status = cupsSendRequest(http, request, resource, length);
228
e07d4801 229 DEBUG_printf(("2cupsDoIORequest: status=%d", status));
3d052e43
MS
230
231 if (status == HTTP_CONTINUE && request->state == IPP_DATA && infile >= 0)
232 {
e07d4801 233 DEBUG_puts("2cupsDoIORequest: file write...");
3d052e43
MS
234
235 /*
236 * Send the file with the request...
237 */
238
239#ifndef WIN32
240 if (S_ISREG(fileinfo.st_mode))
241#endif /* WIN32 */
242 lseek(infile, 0, SEEK_SET);
243
244 while ((bytes = (int)read(infile, buffer, sizeof(buffer))) > 0)
245 {
246 if (httpCheck(http))
247 {
248 if ((status = httpUpdate(http)) != HTTP_CONTINUE)
249 break;
250 }
251
252 if (httpWrite2(http, buffer, bytes) < bytes)
253 break;
254 }
255 }
256
257 /*
258 * Get the server's response...
259 */
260
261 if (status == HTTP_CONTINUE || status == HTTP_OK)
262 {
263 response = cupsGetResponse(http, resource);
264 status = http->status;
265 }
5a6b583a
MS
266 else
267 httpFlush(http);
3d052e43 268
f11a948a
MS
269 DEBUG_printf(("2cupsDoIORequest: status=%d", status));
270
5a6b583a
MS
271 if (status == HTTP_ERROR ||
272 (status >= HTTP_BAD_REQUEST && status != HTTP_UNAUTHORIZED &&
273 status != HTTP_UPGRADE_REQUIRED))
ee571f26
MS
274 {
275 _cupsSetHTTPError(status);
568fa3fa 276 break;
ee571f26 277 }
568fa3fa 278
3d052e43
MS
279 if (response)
280 {
281 if (outfile >= 0)
282 {
283 /*
284 * Write trailing data to file...
285 */
286
287 while ((bytes = (int)httpRead2(http, buffer, sizeof(buffer))) > 0)
288 if (write(outfile, buffer, bytes) < bytes)
289 break;
290 }
291 else
292 {
293 /*
294 * Flush any remaining data...
295 */
296
297 httpFlush(http);
298 }
299 }
300 }
301
302 /*
303 * Delete the original request and return the response...
304 */
6d2f911b 305
3d052e43
MS
306 ippDelete(request);
307
308 return (response);
309}
310
311
312/*
313 * 'cupsDoRequest()' - Do an IPP request.
314 *
315 * This function sends the IPP request to the specified server, retrying
316 * and authenticating as necessary. The request is freed with ippDelete()
317 * after receiving a valid IPP response.
318 */
319
320ipp_t * /* O - Response data */
568fa3fa 321cupsDoRequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
3d052e43
MS
322 ipp_t *request, /* I - IPP request */
323 const char *resource) /* I - HTTP resource for POST */
324{
e07d4801 325 DEBUG_printf(("cupsDoRequest(http=%p, request=%p(%s), resource=\"%s\")",
a603edef
MS
326 http, request,
327 request ? ippOpString(request->request.op.operation_id) : "?",
e07d4801 328 resource));
a603edef 329
b19ccc9e 330 return (cupsDoIORequest(http, request, resource, -1, -1));
3d052e43
MS
331}
332
333
334/*
335 * 'cupsGetResponse()' - Get a response to an IPP request.
336 *
337 * Use this function to get the response for an IPP request sent using
338 * cupsSendDocument() or cupsSendRequest(). For requests that return
339 * additional data, use httpRead() after getting a successful response.
340 *
178cb736 341 * @since CUPS 1.4/Mac OS X 10.6@
3d052e43
MS
342 */
343
568fa3fa
MS
344ipp_t * /* O - Response or @code NULL@ on HTTP error */
345cupsGetResponse(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
3d052e43
MS
346 const char *resource) /* I - HTTP resource for POST */
347{
348 http_status_t status; /* HTTP status */
349 ipp_state_t state; /* IPP read state */
350 ipp_t *response = NULL; /* IPP response */
351
352
e07d4801 353 DEBUG_printf(("cupsGetResponse(http=%p, resource=\"%s\")", http, resource));
3d052e43
MS
354
355 /*
356 * Connect to the default server as needed...
357 */
358
359 if (!http)
360 http = _cupsConnect();
361
362 if (!http || (http->state != HTTP_POST_RECV && http->state != HTTP_POST_SEND))
363 return (NULL);
364
365 /*
366 * Check for an unfinished chunked request...
367 */
368
369 if (http->data_encoding == HTTP_ENCODE_CHUNKED)
370 {
371 /*
372 * Send a 0-length chunk to finish off the request...
373 */
374
e07d4801 375 DEBUG_puts("2cupsGetResponse: Finishing chunked POST...");
3d052e43
MS
376
377 if (httpWrite2(http, "", 0) < 0)
378 return (NULL);
379 }
380
381 /*
382 * Wait for a response from the server...
383 */
384
e07d4801 385 DEBUG_printf(("2cupsGetResponse: Update loop, http->status=%d...",
1ff0402e 386 http->status));
3d052e43 387
1ff0402e
MS
388 status = http->status;
389 while (status == HTTP_CONTINUE)
390 status = httpUpdate(http);
3d052e43 391
e07d4801 392 DEBUG_printf(("2cupsGetResponse: status=%d", status));
3d052e43
MS
393
394 if (status == HTTP_OK)
395 {
396 /*
397 * Get the IPP response...
398 */
399
400 response = ippNew();
401
402 while ((state = ippRead(http, response)) != IPP_DATA)
403 if (state == IPP_ERROR)
404 break;
405
406 if (state == IPP_ERROR)
407 {
408 /*
409 * Delete the response...
410 */
411
e07d4801 412 DEBUG_puts("1cupsGetResponse: IPP read error!");
3d052e43
MS
413
414 ippDelete(response);
415 response = NULL;
416
749b1e90 417 _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
3d052e43
MS
418 }
419 }
420 else if (status != HTTP_ERROR)
421 {
422 /*
423 * Flush any error message...
424 */
425
426 httpFlush(http);
427
428 /*
429 * Then handle encryption and authentication...
430 */
431
432 if (status == HTTP_UNAUTHORIZED)
433 {
434 /*
435 * See if we can do authentication...
436 */
437
e07d4801 438 DEBUG_puts("2cupsGetResponse: Need authorization...");
568fa3fa 439
e07d4801 440 if (!cupsDoAuthentication(http, "POST", resource))
3d052e43 441 httpReconnect(http);
f11a948a
MS
442 else
443 status = HTTP_AUTHORIZATION_CANCELED;
3d052e43
MS
444 }
445
446#ifdef HAVE_SSL
447 else if (status == HTTP_UPGRADE_REQUIRED)
448 {
449 /*
450 * Force a reconnect with encryption...
451 */
452
e07d4801 453 DEBUG_puts("2cupsGetResponse: Need encryption...");
3d052e43
MS
454
455 if (!httpReconnect(http))
456 httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
457 }
458#endif /* HAVE_SSL */
459 }
460
461 if (response)
462 {
463 ipp_attribute_t *attr; /* status-message attribute */
464
465
466 attr = ippFindAttribute(response, "status-message", IPP_TAG_TEXT);
467
e07d4801 468 DEBUG_printf(("1cupsGetResponse: status-code=%s, status-message=\"%s\"",
a603edef
MS
469 ippErrorString(response->request.status.status_code),
470 attr ? attr->values[0].string.text : ""));
471
3d052e43 472 _cupsSetError(response->request.status.status_code,
749b1e90
MS
473 attr ? attr->values[0].string.text :
474 ippErrorString(response->request.status.status_code), 0);
3d052e43
MS
475 }
476 else if (status != HTTP_OK)
477 _cupsSetHTTPError(status);
478
479 return (response);
480}
481
482
6d2f911b
MS
483/*
484 * 'cupsLastError()' - Return the last IPP status code.
485 */
486
487ipp_status_t /* O - IPP status code from last request */
488cupsLastError(void)
489{
490 return (_cupsGlobals()->last_error);
491}
492
493
494/*
495 * 'cupsLastErrorString()' - Return the last IPP status-message.
496 *
497 * @since CUPS 1.2/Mac OS X 10.5@
498 */
499
500const char * /* O - status-message text from last request */
501cupsLastErrorString(void)
502{
503 return (_cupsGlobals()->last_status_message);
504}
505
506
3d052e43
MS
507/*
508 * 'cupsReadResponseData()' - Read additional data after the IPP response.
509 *
510 * This function is used after cupsGetResponse() to read the PPD or document
511 * files for CUPS_GET_PPD and CUPS_GET_DOCUMENT requests, respectively.
512 *
178cb736 513 * @since CUPS 1.4/Mac OS X 10.6@
3d052e43
MS
514 */
515
516ssize_t /* O - Bytes read, 0 on EOF, -1 on error */
517cupsReadResponseData(
568fa3fa 518 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
3d052e43
MS
519 char *buffer, /* I - Buffer to use */
520 size_t length) /* I - Number of bytes to read */
521{
522 /*
523 * Get the default connection as needed...
524 */
525
a603edef 526 DEBUG_printf(("cupsReadResponseData(http=%p, buffer=%p, "
e07d4801 527 "length=" CUPS_LLFMT ")", http, buffer, CUPS_LLCAST length));
a603edef 528
3d052e43
MS
529 if (!http)
530 {
531 _cups_globals_t *cg = _cupsGlobals();
532 /* Pointer to library globals */
533
534 if ((http = cg->http) == NULL)
535 {
749b1e90 536 _cupsSetError(IPP_INTERNAL_ERROR, _("No active connection"), 1);
3d052e43
MS
537 return (-1);
538 }
ecdc0628 539 }
ecdc0628 540
3d052e43
MS
541 /*
542 * Then read from the HTTP connection...
543 */
544
545 return (httpRead2(http, buffer, length));
546}
547
548
549/*
550 * 'cupsSendRequest()' - Send an IPP request.
551 *
552 * Use httpWrite() to write any additional data (document, PPD file, etc.)
553 * for the request, cupsGetResponse() to get the IPP response, and httpRead()
6d2f911b 554 * to read any additional data following the response. Only one request can be
3d052e43
MS
555 * sent/queued at a time.
556 *
557 * Unlike cupsDoFileRequest(), cupsDoIORequest(), and cupsDoRequest(), the
558 * request is not freed.
559 *
178cb736 560 * @since CUPS 1.4/Mac OS X 10.6@
3d052e43
MS
561 */
562
563http_status_t /* O - Initial HTTP status */
568fa3fa 564cupsSendRequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
3d052e43
MS
565 ipp_t *request, /* I - IPP request */
566 const char *resource, /* I - Resource path */
ae71f5de 567 size_t length) /* I - Length of data to follow or @code CUPS_LENGTH_VARIABLE@ */
3d052e43
MS
568{
569 http_status_t status; /* Status of HTTP request */
570 int got_status; /* Did we get the status? */
571 ipp_state_t state; /* State of IPP processing */
572 http_status_t expect; /* Expect: header to use */
573
574
a603edef 575 DEBUG_printf(("cupsSendRequest(http=%p, request=%p(%s), resource=\"%s\", "
e07d4801 576 "length=" CUPS_LLFMT ")", http, request,
a603edef 577 request ? ippOpString(request->request.op.operation_id) : "?",
e07d4801 578 resource, CUPS_LLCAST length));
3d052e43
MS
579
580 /*
581 * Range check input...
582 */
583
584 if (!request || !resource)
585 {
749b1e90 586 _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
3d052e43
MS
587
588 return (HTTP_ERROR);
589 }
590
591 /*
592 * Get the default connection as needed...
593 */
594
595 if (!http)
a603edef
MS
596 if ((http = _cupsConnect()) == NULL)
597 return (HTTP_SERVICE_UNAVAILABLE);
3d052e43 598
7594b224 599#ifdef HAVE_SSL
600 /*
601 * See if we have an auth-info attribute and are communicating over
602 * a non-local link. If so, encrypt the link so that we can pass
603 * the authentication information securely...
604 */
605
606 if (ippFindAttribute(request, "auth-info", IPP_TAG_TEXT) &&
607 !httpAddrLocalhost(http->hostaddr) && !http->tls &&
608 httpEncryption(http, HTTP_ENCRYPT_REQUIRED))
b19ccc9e
MS
609 {
610 _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
ee571f26 611 return (HTTP_SERVICE_UNAVAILABLE);
b19ccc9e 612 }
7594b224 613#endif /* HAVE_SSL */
614
1ff0402e
MS
615 /*
616 * Reconnect if the last response had a "Connection: close"...
617 */
618
619 if (!strcasecmp(http->fields[HTTP_FIELD_CONNECTION], "close"))
620 if (httpReconnect(http))
b19ccc9e
MS
621 {
622 _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
1ff0402e 623 return (HTTP_SERVICE_UNAVAILABLE);
b19ccc9e 624 }
1ff0402e 625
ecdc0628 626 /*
627 * Loop until we can send the request without authorization problems.
628 */
629
3d052e43 630 expect = HTTP_CONTINUE;
ecdc0628 631
3d052e43 632 for (;;)
ecdc0628 633 {
e07d4801 634 DEBUG_puts("2cupsSendRequest: Setup...");
ecdc0628 635
636 /*
637 * Setup the HTTP variables needed...
638 */
639
ecdc0628 640 httpClearFields(http);
641 httpSetLength(http, length);
642 httpSetField(http, HTTP_FIELD_CONTENT_TYPE, "application/ipp");
643 httpSetField(http, HTTP_FIELD_AUTHORIZATION, http->authstring);
f301802f 644 httpSetExpect(http, expect);
ecdc0628 645
e07d4801 646 DEBUG_printf(("2cupsSendRequest: authstring=\"%s\"", http->authstring));
ecdc0628 647
648 /*
649 * Try the request...
650 */
651
e07d4801 652 DEBUG_puts("2cupsSendRequest: Sending HTTP POST...");
ecdc0628 653
654 if (httpPost(http, resource))
655 {
656 if (httpReconnect(http))
b19ccc9e
MS
657 {
658 _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
ee571f26 659 return (HTTP_SERVICE_UNAVAILABLE);
b19ccc9e 660 }
ecdc0628 661 else
662 continue;
663 }
664
665 /*
d6ae789d 666 * Send the IPP data...
ecdc0628 667 */
668
e07d4801 669 DEBUG_puts("2cupsSendRequest: Writing IPP request...");
b423cd4c 670
d6ae789d 671 request->state = IPP_IDLE;
672 status = HTTP_CONTINUE;
673 got_status = 0;
674
675 while ((state = ippWrite(http, request)) != IPP_DATA)
676 if (state == IPP_ERROR)
677 break;
678 else if (httpCheck(http))
679 {
680 got_status = 1;
681
682 if ((status = httpUpdate(http)) != HTTP_CONTINUE)
683 break;
684 }
685
3d052e43
MS
686 /*
687 * Wait up to 1 second to get the 100-continue response as needed...
688 */
ecdc0628 689
3d052e43
MS
690 if (!got_status && expect == HTTP_CONTINUE)
691 {
e07d4801 692 DEBUG_puts("2cupsSendRequest: Waiting for 100-continue...");
a603edef 693
d6ae789d 694 if (httpWait(http, 1000))
695 status = httpUpdate(http);
696 }
697 else if (httpCheck(http))
698 status = httpUpdate(http);
ecdc0628 699
e07d4801 700 DEBUG_printf(("2cupsSendRequest: status=%d", status));
a603edef 701
ecdc0628 702 /*
3d052e43 703 * Process the current HTTP status...
ecdc0628 704 */
705
ba55dc12
MS
706 if (status >= HTTP_BAD_REQUEST)
707 httpFlush(http);
708
3d052e43 709 switch (status)
ecdc0628 710 {
3d052e43
MS
711 case HTTP_ERROR :
712 case HTTP_CONTINUE :
713 case HTTP_OK :
714 return (status);
ecdc0628 715
3d052e43 716 case HTTP_UNAUTHORIZED :
ba55dc12
MS
717 if (cupsDoAuthentication(http, "POST", resource))
718 return (HTTP_AUTHORIZATION_CANCELED);
719
720 if (httpReconnect(http))
f11a948a 721 {
ba55dc12
MS
722 _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
723 return (HTTP_SERVICE_UNAVAILABLE);
f11a948a 724 }
ba55dc12 725 break;
89d46774 726
ecdc0628 727#ifdef HAVE_SSL
3d052e43
MS
728 case HTTP_UPGRADE_REQUIRED :
729 /*
730 * Flush any error message, reconnect, and then upgrade with
731 * encryption...
732 */
ecdc0628 733
3d052e43 734 if (httpReconnect(http))
b19ccc9e
MS
735 {
736 _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
737 return (HTTP_SERVICE_UNAVAILABLE);
738 }
ecdc0628 739
ba55dc12
MS
740 if (httpEncryption(http, HTTP_ENCRYPT_REQUIRED))
741 {
742 _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
743 return (HTTP_SERVICE_UNAVAILABLE);
744 }
745 break;
ecdc0628 746#endif /* HAVE_SSL */
f301802f 747
3d052e43
MS
748 case HTTP_EXPECTATION_FAILED :
749 /*
750 * Don't try using the Expect: header the next time around...
751 */
ecdc0628 752
3d052e43 753 expect = (http_status_t)0;
52f6f666
MS
754
755 if (httpReconnect(http))
756 {
757 _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
758 return (HTTP_SERVICE_UNAVAILABLE);
759 }
a603edef 760 break;
ecdc0628 761
3d052e43
MS
762 default :
763 /*
764 * Some other error...
765 */
ecdc0628 766
3d052e43 767 return (status);
b94498cf 768 }
769 }
3d052e43
MS
770}
771
772
773/*
774 * 'cupsWriteRequestData()' - Write additional data after an IPP request.
775 *
568fa3fa
MS
776 * This function is used after @link cupsSendRequest@ to provide a PPD and
777 * after @link cupsStartDocument@ to provide a document file.
3d052e43 778 *
178cb736 779 * @since CUPS 1.4/Mac OS X 10.6@
3d052e43 780 */
ecdc0628 781
568fa3fa 782http_status_t /* O - @code HTTP_CONTINUE@ if OK or HTTP status on error */
3d052e43 783cupsWriteRequestData(
568fa3fa 784 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
3d052e43
MS
785 const char *buffer, /* I - Bytes to write */
786 size_t length) /* I - Number of bytes to write */
787{
38e73f87
MS
788 int wused; /* Previous bytes in buffer */
789
790
ecdc0628 791 /*
3d052e43 792 * Get the default connection as needed...
ecdc0628 793 */
ecdc0628 794
a603edef 795 DEBUG_printf(("cupsWriteRequestData(http=%p, buffer=%p, "
e07d4801 796 "length=" CUPS_LLFMT ")", http, buffer, CUPS_LLCAST length));
a603edef 797
3d052e43 798 if (!http)
ecdc0628 799 {
3d052e43
MS
800 _cups_globals_t *cg = _cupsGlobals();
801 /* Pointer to library globals */
ecdc0628 802
3d052e43
MS
803 if ((http = cg->http) == NULL)
804 {
749b1e90 805 _cupsSetError(IPP_INTERNAL_ERROR, _("No active connection"), 1);
3d052e43
MS
806 return (HTTP_ERROR);
807 }
ecdc0628 808 }
ecdc0628 809
3d052e43
MS
810 /*
811 * Then write to the HTTP connection...
812 */
ecdc0628 813
38e73f87
MS
814 wused = http->wused;
815
3d052e43
MS
816 if (httpWrite2(http, buffer, length) < 0)
817 return (HTTP_ERROR);
ecdc0628 818
3d052e43
MS
819 /*
820 * Finally, check if we have any pending data from the server...
821 */
ecdc0628 822
f11a948a 823 if (length >= HTTP_MAX_BUFFER ||
38e73f87
MS
824 http->wused < wused ||
825 (wused > 0 && http->wused == length))
826 {
827 /*
828 * We've written something to the server, so check for response data...
829 */
830
831 if (_httpWait(http, 0, 1))
ba55dc12
MS
832 {
833 http_status_t status; /* Status from httpUpdate */
834
835 if ((status = httpUpdate(http)) >= HTTP_BAD_REQUEST)
836 httpFlush(http);
837
838 return (status);
839 }
38e73f87
MS
840 }
841
842 return (HTTP_CONTINUE);
ecdc0628 843}
844
845
6d2f911b
MS
846/*
847 * '_cupsConnect()' - Get the default server connection...
848 */
849
850http_t * /* O - HTTP connection */
851_cupsConnect(void)
852{
853 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
854
855
856 /*
857 * See if we are connected to the same server...
858 */
859
860 if (cg->http)
861 {
862 /*
863 * Compare the connection hostname, port, and encryption settings to
864 * the cached defaults; these were initialized the first time we
865 * connected...
866 */
867
868 if (strcmp(cg->http->hostname, cg->server) ||
869 cg->ipp_port != _httpAddrPort(cg->http->hostaddr) ||
870 (cg->http->encryption != cg->encryption &&
871 cg->http->encryption == HTTP_ENCRYPT_NEVER))
872 {
873 /*
874 * Need to close the current connection because something has changed...
875 */
876
877 httpClose(cg->http);
878 cg->http = NULL;
879 }
880 }
881
882 /*
883 * (Re)connect as needed...
884 */
885
886 if (!cg->http)
887 {
888 if ((cg->http = httpConnectEncrypt(cupsServer(), ippPort(),
889 cupsEncryption())) == NULL)
890 {
891 if (errno)
892 _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
893 else
894 _cupsSetError(IPP_SERVICE_UNAVAILABLE,
895 _("Unable to connect to host."), 1);
896 }
897 }
898
899 /*
900 * Return the cached connection...
901 */
902
903 return (cg->http);
904}
905
906
ecdc0628 907/*
908 * '_cupsSetError()' - Set the last IPP status code and status-message.
909 */
910
911void
912_cupsSetError(ipp_status_t status, /* I - IPP status code */
749b1e90
MS
913 const char *message, /* I - status-message value */
914 int localize) /* I - Localize the message? */
ecdc0628 915{
916 _cups_globals_t *cg; /* Global data */
917
918
749b1e90
MS
919 if (!message && errno)
920 {
921 message = strerror(errno);
922 localize = 0;
923 }
924
ecdc0628 925 cg = _cupsGlobals();
926 cg->last_error = status;
927
928 if (cg->last_status_message)
929 {
749b1e90 930 _cupsStrFree(cg->last_status_message);
ecdc0628 931
932 cg->last_status_message = NULL;
933 }
934
935 if (message)
749b1e90
MS
936 {
937 if (localize)
938 {
939 /*
940 * Get the message catalog...
941 */
942
943 if (!cg->lang_default)
944 cg->lang_default = cupsLangDefault();
945
946 cg->last_status_message = _cupsStrAlloc(_cupsLangString(cg->lang_default,
947 message));
948 }
949 else
950 cg->last_status_message = _cupsStrAlloc(message);
951 }
a603edef 952
e07d4801
MS
953 DEBUG_printf(("4_cupsSetError: last_error=%s, last_status_message=\"%s\"",
954 ippErrorString(cg->last_error), cg->last_status_message));
ecdc0628 955}
956
957
958/*
355e94dc
MS
959 * '_cupsSetHTTPError()' - Set the last error using the HTTP status.
960 */
961
962void
963_cupsSetHTTPError(http_status_t status) /* I - HTTP status code */
964{
965 switch (status)
966 {
967 case HTTP_NOT_FOUND :
749b1e90 968 _cupsSetError(IPP_NOT_FOUND, httpStatus(status), 0);
355e94dc
MS
969 break;
970
971 case HTTP_UNAUTHORIZED :
f11a948a 972 case HTTP_AUTHORIZATION_CANCELED :
749b1e90 973 _cupsSetError(IPP_NOT_AUTHORIZED, httpStatus(status), 0);
355e94dc
MS
974 break;
975
976 case HTTP_FORBIDDEN :
749b1e90 977 _cupsSetError(IPP_FORBIDDEN, httpStatus(status), 0);
355e94dc
MS
978 break;
979
980 case HTTP_BAD_REQUEST :
749b1e90 981 _cupsSetError(IPP_BAD_REQUEST, httpStatus(status), 0);
355e94dc
MS
982 break;
983
984 case HTTP_REQUEST_TOO_LARGE :
749b1e90 985 _cupsSetError(IPP_REQUEST_VALUE, httpStatus(status), 0);
355e94dc
MS
986 break;
987
988 case HTTP_NOT_IMPLEMENTED :
749b1e90 989 _cupsSetError(IPP_OPERATION_NOT_SUPPORTED, httpStatus(status), 0);
355e94dc
MS
990 break;
991
992 case HTTP_NOT_SUPPORTED :
749b1e90 993 _cupsSetError(IPP_VERSION_NOT_SUPPORTED, httpStatus(status), 0);
355e94dc
MS
994 break;
995
996 default :
e07d4801
MS
997 DEBUG_printf(("4_cupsSetHTTPError: HTTP error %d mapped to "
998 "IPP_SERVICE_UNAVAILABLE!", status));
749b1e90 999 _cupsSetError(IPP_SERVICE_UNAVAILABLE, httpStatus(status), 0);
355e94dc
MS
1000 break;
1001 }
1002}
1003
1004
1005/*
b19ccc9e 1006 * End of "$Id: request.c 7946 2008-09-16 23:27:54Z mike $".
ecdc0628 1007 */