]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/request.c
Merge changes from CUPS 1.4svn-r7817.
[thirdparty/cups.git] / cups / request.c
CommitLineData
ecdc0628 1/*
75bd9771 2 * "$Id: request.c 7460 2008-04-16 02:19:54Z mike $"
ecdc0628 3 *
4 * IPP utilities for the Common UNIX Printing System (CUPS).
5 *
3d052e43 6 * Copyright 2007-2008 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.
23 * cupsReadResponseData() - Read additional data after the IPP response.
24 * cupsSendRequest() - Send an IPP request.
25 * cupsWriteRequestData() - Write additional data after an IPP request.
26 * _cupsSetError() - Set the last IPP status code and status-message.
27 * _cupsSetHTTPError() - Set the last error using the HTTP status.
ecdc0628 28 */
29
30/*
31 * Include necessary headers...
32 */
33
34#include "globals.h"
35#include "debug.h"
36#include <stdlib.h>
37#include <errno.h>
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
MS
68 DEBUG_printf(("cupsDoFileRequest(http=%p, request=%p(%s), resource=\"%s\", "
69 "filename=\"%s\")\n", http, request,
70 request ? ippOpString(request->request.op.operation_id) : "?",
71 resource ? resource : "(null)",
72 filename ? filename : "(null)"));
73
b94498cf 74 if (filename)
75 {
76 if ((infile = open(filename, O_RDONLY | O_BINARY)) < 0)
77 {
78 /*
79 * Can't get file information!
80 */
81
82 _cupsSetError(errno == ENOENT ? IPP_NOT_FOUND : IPP_NOT_AUTHORIZED,
749b1e90 83 NULL, 0);
b94498cf 84
85 ippDelete(request);
86
87 return (NULL);
88 }
89 }
90 else
91 infile = -1;
92
93 response = cupsDoIORequest(http, request, resource, infile, -1);
94
95 if (infile >= 0)
96 close(infile);
97
98 return (response);
99}
100
101
102/*
103 * 'cupsDoIORequest()' - Do an IPP request with file descriptors.
104 *
105 * This function sends the IPP request to the specified server, retrying
106 * and authenticating as necessary. The request is freed with ippDelete()
107 * after receiving a valid IPP response.
108 *
109 * If "infile" is a valid file descriptor, cupsDoIORequest() copies
110 * all of the data from the file after the IPP request message.
111 *
112 * If "outfile" is a valid file descriptor, cupsDoIORequest() copies
113 * all of the data after the IPP response message to the file.
114 *
115 * @since CUPS 1.3@
116 */
117
118ipp_t * /* O - Response data */
568fa3fa 119cupsDoIORequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
b94498cf 120 ipp_t *request, /* I - IPP request */
121 const char *resource, /* I - HTTP resource for POST */
122 int infile, /* I - File to read from or -1 for none */
123 int outfile) /* I - File to write to or -1 for none */
ecdc0628 124{
3d052e43
MS
125 ipp_t *response = NULL; /* IPP response data */
126 size_t length = 0; /* Content-Length value */
ecdc0628 127 http_status_t status; /* Status of HTTP request */
ecdc0628 128 struct stat fileinfo; /* File information */
129 int bytes; /* Number of bytes read/written */
a74454a7 130 char buffer[32768]; /* Output buffer */
ecdc0628 131
132
a603edef 133 DEBUG_printf(("cupsDoIORequest(http=%p, request=%p(%s), resource=\"%s\", "
3d052e43 134 "infile=%d, outfile=%d)\n", http, request,
a603edef 135 request ? ippOpString(request->request.op.operation_id) : "?",
3d052e43
MS
136 resource ? resource : "(null)", infile, outfile));
137
138 /*
139 * Range check input...
140 */
ecdc0628 141
3d052e43 142 if (!request || !resource)
ecdc0628 143 {
3d052e43 144 ippDelete(request);
ecdc0628 145
749b1e90 146 _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
ecdc0628 147
148 return (NULL);
149 }
150
3d052e43
MS
151 /*
152 * Get the default connection as needed...
153 */
154
155 if (!http)
a603edef
MS
156 if ((http = _cupsConnect()) == NULL)
157 return (NULL);
3d052e43 158
ecdc0628 159 /*
160 * See if we have a file to send...
161 */
162
b94498cf 163 if (infile >= 0)
ecdc0628 164 {
b94498cf 165 if (fstat(infile, &fileinfo))
ecdc0628 166 {
167 /*
168 * Can't get file information!
169 */
170
3d052e43 171 _cupsSetError(errno == EBADF ? IPP_NOT_FOUND : IPP_NOT_AUTHORIZED,
749b1e90 172 NULL, 0);
ecdc0628 173
174 ippDelete(request);
175
176 return (NULL);
177 }
178
179#ifdef WIN32
180 if (fileinfo.st_mode & _S_IFDIR)
181#else
182 if (S_ISDIR(fileinfo.st_mode))
183#endif /* WIN32 */
184 {
185 /*
186 * Can't send a directory...
187 */
188
189 ippDelete(request);
190
749b1e90 191 _cupsSetError(IPP_NOT_POSSIBLE, strerror(EISDIR), 0);
ecdc0628 192
193 return (NULL);
194 }
3d052e43
MS
195
196#ifndef WIN32
197 if (!S_ISREG(fileinfo.st_mode))
198 length = 0; /* Chunk when piping */
199 else
200#endif /* !WIN32 */
201 length = ippLength(request) + fileinfo.st_size;
202 }
203 else
204 length = ippLength(request);
205
206 /*
207 * Loop until we can send the request without authorization problems.
208 */
209
210 while (response == NULL)
211 {
212 DEBUG_puts("cupsDoFileRequest: setup...");
213
214 /*
215 * Send the request...
216 */
217
218 status = cupsSendRequest(http, request, resource, length);
219
220 DEBUG_printf(("cupsDoFileRequest: status=%d\n", status));
221
222 if (status == HTTP_CONTINUE && request->state == IPP_DATA && infile >= 0)
223 {
224 DEBUG_puts("cupsDoFileRequest: file write...");
225
226 /*
227 * Send the file with the request...
228 */
229
230#ifndef WIN32
231 if (S_ISREG(fileinfo.st_mode))
232#endif /* WIN32 */
233 lseek(infile, 0, SEEK_SET);
234
235 while ((bytes = (int)read(infile, buffer, sizeof(buffer))) > 0)
236 {
237 if (httpCheck(http))
238 {
239 if ((status = httpUpdate(http)) != HTTP_CONTINUE)
240 break;
241 }
242
243 if (httpWrite2(http, buffer, bytes) < bytes)
244 break;
245 }
246 }
247
248 /*
249 * Get the server's response...
250 */
251
252 if (status == HTTP_CONTINUE || status == HTTP_OK)
253 {
254 response = cupsGetResponse(http, resource);
255 status = http->status;
256 }
257
568fa3fa
MS
258 if (status == HTTP_FORBIDDEN)
259 break;
260
3d052e43
MS
261 if (response)
262 {
263 if (outfile >= 0)
264 {
265 /*
266 * Write trailing data to file...
267 */
268
269 while ((bytes = (int)httpRead2(http, buffer, sizeof(buffer))) > 0)
270 if (write(outfile, buffer, bytes) < bytes)
271 break;
272 }
273 else
274 {
275 /*
276 * Flush any remaining data...
277 */
278
279 httpFlush(http);
280 }
281 }
282 }
283
284 /*
285 * Delete the original request and return the response...
286 */
287
288 ippDelete(request);
289
290 return (response);
291}
292
293
294/*
295 * 'cupsDoRequest()' - Do an IPP request.
296 *
297 * This function sends the IPP request to the specified server, retrying
298 * and authenticating as necessary. The request is freed with ippDelete()
299 * after receiving a valid IPP response.
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{
a603edef
MS
307 DEBUG_printf(("cupsDoRequest(http=%p, request=%p(%s), resource=\"%s\")\n",
308 http, request,
309 request ? ippOpString(request->request.op.operation_id) : "?",
310 resource ? resource : "(null)"));
311
3d052e43
MS
312 return (cupsDoFileRequest(http, request, resource, NULL));
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
320 * cupsSendDocument() or cupsSendRequest(). For requests that return
321 * additional data, use httpRead() after getting a successful response.
322 *
323 * @since CUPS 1.4@
324 */
325
568fa3fa
MS
326ipp_t * /* O - Response or @code NULL@ on HTTP error */
327cupsGetResponse(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
3d052e43
MS
328 const char *resource) /* I - HTTP resource for POST */
329{
330 http_status_t status; /* HTTP status */
331 ipp_state_t state; /* IPP read state */
332 ipp_t *response = NULL; /* IPP response */
333
334
a603edef
MS
335 DEBUG_printf(("cupsGetReponse(http=%p, resource=\"%s\")\n", http,
336 resource ? resource : "(null)"));
3d052e43
MS
337
338 /*
339 * Connect to the default server as needed...
340 */
341
342 if (!http)
343 http = _cupsConnect();
344
345 if (!http || (http->state != HTTP_POST_RECV && http->state != HTTP_POST_SEND))
346 return (NULL);
347
348 /*
349 * Check for an unfinished chunked request...
350 */
351
352 if (http->data_encoding == HTTP_ENCODE_CHUNKED)
353 {
354 /*
355 * Send a 0-length chunk to finish off the request...
356 */
357
358 DEBUG_puts("cupsGetResponse: Finishing chunked POST...");
359
360 if (httpWrite2(http, "", 0) < 0)
361 return (NULL);
362 }
363
364 /*
365 * Wait for a response from the server...
366 */
367
a603edef 368 DEBUG_puts("cupsGetResponse: Update loop...");
3d052e43
MS
369
370 while ((status = httpUpdate(http)) == HTTP_CONTINUE)
371 /* Do nothing but update */;
372
a603edef 373 DEBUG_printf(("cupsGetResponse: status=%d\n", status));
3d052e43
MS
374
375 if (status == HTTP_OK)
376 {
377 /*
378 * Get the IPP response...
379 */
380
381 response = ippNew();
382
383 while ((state = ippRead(http, response)) != IPP_DATA)
384 if (state == IPP_ERROR)
385 break;
386
387 if (state == IPP_ERROR)
388 {
389 /*
390 * Delete the response...
391 */
392
a603edef 393 DEBUG_puts("cupsGetResponse: IPP read error!");
3d052e43
MS
394
395 ippDelete(response);
396 response = NULL;
397
749b1e90 398 _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
3d052e43
MS
399 }
400 }
401 else if (status != HTTP_ERROR)
402 {
403 /*
404 * Flush any error message...
405 */
406
407 httpFlush(http);
408
409 /*
410 * Then handle encryption and authentication...
411 */
412
413 if (status == HTTP_UNAUTHORIZED)
414 {
415 /*
416 * See if we can do authentication...
417 */
418
568fa3fa
MS
419 int auth_result;
420
3d052e43
MS
421 DEBUG_puts("cupsGetResponse: Need authorization...");
422
ae71f5de 423 if ((auth_result = cupsDoAuthentication(http, "POST", resource)) == 0)
3d052e43 424 httpReconnect(http);
568fa3fa
MS
425 else if (auth_result < 0)
426 http->status = status = HTTP_FORBIDDEN;
3d052e43
MS
427 }
428
429#ifdef HAVE_SSL
430 else if (status == HTTP_UPGRADE_REQUIRED)
431 {
432 /*
433 * Force a reconnect with encryption...
434 */
435
436 DEBUG_puts("cupsGetResponse: Need encryption...");
437
438 if (!httpReconnect(http))
439 httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
440 }
441#endif /* HAVE_SSL */
442 }
443
444 if (response)
445 {
446 ipp_attribute_t *attr; /* status-message attribute */
447
448
449 attr = ippFindAttribute(response, "status-message", IPP_TAG_TEXT);
450
a603edef
MS
451 DEBUG_printf(("cupsGetResponse: status-code=%s, status-message=\"%s\"\n",
452 ippErrorString(response->request.status.status_code),
453 attr ? attr->values[0].string.text : ""));
454
3d052e43 455 _cupsSetError(response->request.status.status_code,
749b1e90
MS
456 attr ? attr->values[0].string.text :
457 ippErrorString(response->request.status.status_code), 0);
3d052e43
MS
458 }
459 else if (status != HTTP_OK)
460 _cupsSetHTTPError(status);
461
462 return (response);
463}
464
465
466/*
467 * 'cupsReadResponseData()' - Read additional data after the IPP response.
468 *
469 * This function is used after cupsGetResponse() to read the PPD or document
470 * files for CUPS_GET_PPD and CUPS_GET_DOCUMENT requests, respectively.
471 *
472 * @since CUPS 1.4@
473 */
474
475ssize_t /* O - Bytes read, 0 on EOF, -1 on error */
476cupsReadResponseData(
568fa3fa 477 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
3d052e43
MS
478 char *buffer, /* I - Buffer to use */
479 size_t length) /* I - Number of bytes to read */
480{
481 /*
482 * Get the default connection as needed...
483 */
484
a603edef
MS
485 DEBUG_printf(("cupsReadResponseData(http=%p, buffer=%p, "
486 "length=" CUPS_LLFMT ")\n", http, buffer, CUPS_LLCAST length));
487
3d052e43
MS
488 if (!http)
489 {
490 _cups_globals_t *cg = _cupsGlobals();
491 /* Pointer to library globals */
492
493 if ((http = cg->http) == NULL)
494 {
749b1e90 495 _cupsSetError(IPP_INTERNAL_ERROR, _("No active connection"), 1);
3d052e43
MS
496 return (-1);
497 }
ecdc0628 498 }
ecdc0628 499
3d052e43
MS
500 /*
501 * Then read from the HTTP connection...
502 */
503
504 return (httpRead2(http, buffer, length));
505}
506
507
508/*
509 * 'cupsSendRequest()' - Send an IPP request.
510 *
511 * Use httpWrite() to write any additional data (document, PPD file, etc.)
512 * for the request, cupsGetResponse() to get the IPP response, and httpRead()
513 * to read any additional data following the response. Only one request can be
514 * sent/queued at a time.
515 *
516 * Unlike cupsDoFileRequest(), cupsDoIORequest(), and cupsDoRequest(), the
517 * request is not freed.
518 *
519 * @since CUPS 1.4@
520 */
521
522http_status_t /* O - Initial HTTP status */
568fa3fa 523cupsSendRequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
3d052e43
MS
524 ipp_t *request, /* I - IPP request */
525 const char *resource, /* I - Resource path */
ae71f5de 526 size_t length) /* I - Length of data to follow or @code CUPS_LENGTH_VARIABLE@ */
3d052e43
MS
527{
528 http_status_t status; /* Status of HTTP request */
529 int got_status; /* Did we get the status? */
530 ipp_state_t state; /* State of IPP processing */
531 http_status_t expect; /* Expect: header to use */
532
533
a603edef 534 DEBUG_printf(("cupsSendRequest(http=%p, request=%p(%s), resource=\"%s\", "
3d052e43 535 "length=" CUPS_LLFMT ")\n", http, request,
a603edef 536 request ? ippOpString(request->request.op.operation_id) : "?",
3d052e43
MS
537 resource ? resource : "(null)", CUPS_LLCAST length));
538
539 /*
540 * Range check input...
541 */
542
543 if (!request || !resource)
544 {
749b1e90 545 _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
3d052e43
MS
546
547 return (HTTP_ERROR);
548 }
549
550 /*
551 * Get the default connection as needed...
552 */
553
554 if (!http)
a603edef
MS
555 if ((http = _cupsConnect()) == NULL)
556 return (HTTP_SERVICE_UNAVAILABLE);
3d052e43 557
7594b224 558#ifdef HAVE_SSL
559 /*
560 * See if we have an auth-info attribute and are communicating over
561 * a non-local link. If so, encrypt the link so that we can pass
562 * the authentication information securely...
563 */
564
565 if (ippFindAttribute(request, "auth-info", IPP_TAG_TEXT) &&
566 !httpAddrLocalhost(http->hostaddr) && !http->tls &&
567 httpEncryption(http, HTTP_ENCRYPT_REQUIRED))
3d052e43 568 return (HTTP_ERROR);
7594b224 569#endif /* HAVE_SSL */
570
ecdc0628 571 /*
572 * Loop until we can send the request without authorization problems.
573 */
574
3d052e43 575 expect = HTTP_CONTINUE;
ecdc0628 576
3d052e43 577 for (;;)
ecdc0628 578 {
a603edef 579 DEBUG_puts("cupsSendRequest: Setup...");
ecdc0628 580
581 /*
582 * Setup the HTTP variables needed...
583 */
584
ecdc0628 585 httpClearFields(http);
586 httpSetLength(http, length);
587 httpSetField(http, HTTP_FIELD_CONTENT_TYPE, "application/ipp");
588 httpSetField(http, HTTP_FIELD_AUTHORIZATION, http->authstring);
f301802f 589 httpSetExpect(http, expect);
ecdc0628 590
3d052e43 591 DEBUG_printf(("cupsSendRequest: authstring=\"%s\"\n", http->authstring));
ecdc0628 592
593 /*
594 * Try the request...
595 */
596
a603edef 597 DEBUG_puts("cupsSendRequest: Sending HTTP POST...");
ecdc0628 598
599 if (httpPost(http, resource))
600 {
601 if (httpReconnect(http))
3d052e43 602 return (HTTP_ERROR);
ecdc0628 603 else
604 continue;
605 }
606
607 /*
d6ae789d 608 * Send the IPP data...
ecdc0628 609 */
610
a603edef 611 DEBUG_puts("cupsSendRequest: Writing IPP request...");
b423cd4c 612
d6ae789d 613 request->state = IPP_IDLE;
614 status = HTTP_CONTINUE;
615 got_status = 0;
616
617 while ((state = ippWrite(http, request)) != IPP_DATA)
618 if (state == IPP_ERROR)
619 break;
620 else if (httpCheck(http))
621 {
622 got_status = 1;
623
624 if ((status = httpUpdate(http)) != HTTP_CONTINUE)
625 break;
626 }
627
3d052e43
MS
628 /*
629 * Wait up to 1 second to get the 100-continue response as needed...
630 */
ecdc0628 631
3d052e43
MS
632 if (!got_status && expect == HTTP_CONTINUE)
633 {
a603edef
MS
634 DEBUG_puts("cupsSendRequest: Waiting for 100-continue...");
635
d6ae789d 636 if (httpWait(http, 1000))
637 status = httpUpdate(http);
a603edef
MS
638 else
639 status = HTTP_EXPECTATION_FAILED;
d6ae789d 640 }
641 else if (httpCheck(http))
642 status = httpUpdate(http);
ecdc0628 643
a603edef
MS
644 DEBUG_printf(("cupsSendRequest: status=%d\n", status));
645
ecdc0628 646 /*
3d052e43 647 * Process the current HTTP status...
ecdc0628 648 */
649
3d052e43 650 switch (status)
ecdc0628 651 {
3d052e43
MS
652 case HTTP_ERROR :
653 case HTTP_CONTINUE :
654 case HTTP_OK :
655 return (status);
ecdc0628 656
3d052e43
MS
657 case HTTP_UNAUTHORIZED :
658 if (!cupsDoAuthentication(http, "POST", resource))
659 if (httpReconnect(http))
660 return (HTTP_ERROR);
ecdc0628 661
3d052e43 662 return (status);
89d46774 663
ecdc0628 664#ifdef HAVE_SSL
3d052e43
MS
665 case HTTP_UPGRADE_REQUIRED :
666 /*
667 * Flush any error message, reconnect, and then upgrade with
668 * encryption...
669 */
ecdc0628 670
3d052e43
MS
671 if (httpReconnect(http))
672 return (HTTP_ERROR);
ecdc0628 673
3d052e43 674 httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
ecdc0628 675
3d052e43 676 return (status);
ecdc0628 677#endif /* HAVE_SSL */
f301802f 678
3d052e43
MS
679 case HTTP_EXPECTATION_FAILED :
680 /*
681 * Don't try using the Expect: header the next time around...
682 */
ecdc0628 683
3d052e43 684 expect = (http_status_t)0;
a603edef 685 break;
ecdc0628 686
3d052e43
MS
687 default :
688 /*
689 * Some other error...
690 */
ecdc0628 691
3d052e43 692 return (status);
b94498cf 693 }
694 }
3d052e43
MS
695}
696
697
698/*
699 * 'cupsWriteRequestData()' - Write additional data after an IPP request.
700 *
568fa3fa
MS
701 * This function is used after @link cupsSendRequest@ to provide a PPD and
702 * after @link cupsStartDocument@ to provide a document file.
3d052e43
MS
703 *
704 * @since CUPS 1.4@
705 */
ecdc0628 706
568fa3fa 707http_status_t /* O - @code HTTP_CONTINUE@ if OK or HTTP status on error */
3d052e43 708cupsWriteRequestData(
568fa3fa 709 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
3d052e43
MS
710 const char *buffer, /* I - Bytes to write */
711 size_t length) /* I - Number of bytes to write */
712{
ecdc0628 713 /*
3d052e43 714 * Get the default connection as needed...
ecdc0628 715 */
ecdc0628 716
a603edef
MS
717 DEBUG_printf(("cupsWriteRequestData(http=%p, buffer=%p, "
718 "length=" CUPS_LLFMT ")\n", http, buffer, CUPS_LLCAST length));
719
3d052e43 720 if (!http)
ecdc0628 721 {
3d052e43
MS
722 _cups_globals_t *cg = _cupsGlobals();
723 /* Pointer to library globals */
ecdc0628 724
3d052e43
MS
725 if ((http = cg->http) == NULL)
726 {
749b1e90 727 _cupsSetError(IPP_INTERNAL_ERROR, _("No active connection"), 1);
3d052e43
MS
728 return (HTTP_ERROR);
729 }
ecdc0628 730 }
ecdc0628 731
3d052e43
MS
732 /*
733 * Then write to the HTTP connection...
734 */
ecdc0628 735
3d052e43
MS
736 if (httpWrite2(http, buffer, length) < 0)
737 return (HTTP_ERROR);
ecdc0628 738
3d052e43
MS
739 /*
740 * Finally, check if we have any pending data from the server...
741 */
ecdc0628 742
3d052e43
MS
743 if (httpCheck(http))
744 return (httpUpdate(http));
745 else
746 return (HTTP_CONTINUE);
ecdc0628 747}
748
749
750/*
751 * '_cupsSetError()' - Set the last IPP status code and status-message.
752 */
753
754void
755_cupsSetError(ipp_status_t status, /* I - IPP status code */
749b1e90
MS
756 const char *message, /* I - status-message value */
757 int localize) /* I - Localize the message? */
ecdc0628 758{
759 _cups_globals_t *cg; /* Global data */
760
761
749b1e90
MS
762 if (!message && errno)
763 {
764 message = strerror(errno);
765 localize = 0;
766 }
767
ecdc0628 768 cg = _cupsGlobals();
769 cg->last_error = status;
770
771 if (cg->last_status_message)
772 {
749b1e90 773 _cupsStrFree(cg->last_status_message);
ecdc0628 774
775 cg->last_status_message = NULL;
776 }
777
778 if (message)
749b1e90
MS
779 {
780 if (localize)
781 {
782 /*
783 * Get the message catalog...
784 */
785
786 if (!cg->lang_default)
787 cg->lang_default = cupsLangDefault();
788
789 cg->last_status_message = _cupsStrAlloc(_cupsLangString(cg->lang_default,
790 message));
791 }
792 else
793 cg->last_status_message = _cupsStrAlloc(message);
794 }
a603edef
MS
795
796 DEBUG_printf(("_cupsSetError: last_error=%s, last_status_message=\"%s\"\n",
749b1e90
MS
797 ippErrorString(cg->last_error),
798 cg->last_status_message ? cg->last_status_message : ""));
ecdc0628 799}
800
801
802/*
355e94dc
MS
803 * '_cupsSetHTTPError()' - Set the last error using the HTTP status.
804 */
805
806void
807_cupsSetHTTPError(http_status_t status) /* I - HTTP status code */
808{
809 switch (status)
810 {
811 case HTTP_NOT_FOUND :
749b1e90 812 _cupsSetError(IPP_NOT_FOUND, httpStatus(status), 0);
355e94dc
MS
813 break;
814
815 case HTTP_UNAUTHORIZED :
749b1e90 816 _cupsSetError(IPP_NOT_AUTHORIZED, httpStatus(status), 0);
355e94dc
MS
817 break;
818
819 case HTTP_FORBIDDEN :
749b1e90 820 _cupsSetError(IPP_FORBIDDEN, httpStatus(status), 0);
355e94dc
MS
821 break;
822
823 case HTTP_BAD_REQUEST :
749b1e90 824 _cupsSetError(IPP_BAD_REQUEST, httpStatus(status), 0);
355e94dc
MS
825 break;
826
827 case HTTP_REQUEST_TOO_LARGE :
749b1e90 828 _cupsSetError(IPP_REQUEST_VALUE, httpStatus(status), 0);
355e94dc
MS
829 break;
830
831 case HTTP_NOT_IMPLEMENTED :
749b1e90 832 _cupsSetError(IPP_OPERATION_NOT_SUPPORTED, httpStatus(status), 0);
355e94dc
MS
833 break;
834
835 case HTTP_NOT_SUPPORTED :
749b1e90 836 _cupsSetError(IPP_VERSION_NOT_SUPPORTED, httpStatus(status), 0);
355e94dc
MS
837 break;
838
839 default :
840 DEBUG_printf(("HTTP error %d mapped to IPP_SERVICE_UNAVAILABLE!\n",
841 status));
749b1e90 842 _cupsSetError(IPP_SERVICE_UNAVAILABLE, httpStatus(status), 0);
355e94dc
MS
843 break;
844 }
845}
846
847
848/*
75bd9771 849 * End of "$Id: request.c 7460 2008-04-16 02:19:54Z mike $".
ecdc0628 850 */