]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/request.c
Merge changes from CUPS 1.4svn-r8329.
[thirdparty/cups.git] / cups / request.c
CommitLineData
ecdc0628 1/*
b19ccc9e 2 * "$Id: request.c 7946 2008-09-16 23:27:54Z mike $"
ecdc0628 3 *
4 * IPP utilities for the Common UNIX Printing System (CUPS).
5 *
745129be 6 * Copyright 2007-2009 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 *
426c6a59 115 * @since CUPS 1.3/Mac OS X 10.5@
b94498cf 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
52f6f666
MS
206 DEBUG_printf(("cupsDoIORequest: Request length=%ld, total length=%ld",
207 (long)ippLength(request), (long)length));
208
3d052e43
MS
209 /*
210 * Loop until we can send the request without authorization problems.
211 */
212
213 while (response == NULL)
214 {
b19ccc9e 215 DEBUG_puts("cupsDoIORequest: setup...");
3d052e43
MS
216
217 /*
218 * Send the request...
219 */
220
221 status = cupsSendRequest(http, request, resource, length);
222
b19ccc9e 223 DEBUG_printf(("cupsDoIORequest: status=%d\n", status));
3d052e43
MS
224
225 if (status == HTTP_CONTINUE && request->state == IPP_DATA && infile >= 0)
226 {
b19ccc9e 227 DEBUG_puts("cupsDoIORequest: file write...");
3d052e43
MS
228
229 /*
230 * Send the file with the request...
231 */
232
233#ifndef WIN32
234 if (S_ISREG(fileinfo.st_mode))
235#endif /* WIN32 */
236 lseek(infile, 0, SEEK_SET);
237
238 while ((bytes = (int)read(infile, buffer, sizeof(buffer))) > 0)
239 {
240 if (httpCheck(http))
241 {
242 if ((status = httpUpdate(http)) != HTTP_CONTINUE)
243 break;
244 }
245
246 if (httpWrite2(http, buffer, bytes) < bytes)
247 break;
248 }
249 }
250
251 /*
252 * Get the server's response...
253 */
254
255 if (status == HTTP_CONTINUE || status == HTTP_OK)
256 {
257 response = cupsGetResponse(http, resource);
258 status = http->status;
259 }
260
ee571f26
MS
261 if (status == HTTP_FORBIDDEN || status == HTTP_ERROR ||
262 status == HTTP_SERVICE_UNAVAILABLE)
263 {
264 _cupsSetHTTPError(status);
568fa3fa 265 break;
ee571f26 266 }
568fa3fa 267
3d052e43
MS
268 if (response)
269 {
270 if (outfile >= 0)
271 {
272 /*
273 * Write trailing data to file...
274 */
275
276 while ((bytes = (int)httpRead2(http, buffer, sizeof(buffer))) > 0)
277 if (write(outfile, buffer, bytes) < bytes)
278 break;
279 }
280 else
281 {
282 /*
283 * Flush any remaining data...
284 */
285
286 httpFlush(http);
287 }
288 }
289 }
290
291 /*
292 * Delete the original request and return the response...
293 */
294
295 ippDelete(request);
296
297 return (response);
298}
299
300
301/*
302 * 'cupsDoRequest()' - Do an IPP request.
303 *
304 * This function sends the IPP request to the specified server, retrying
305 * and authenticating as necessary. The request is freed with ippDelete()
306 * after receiving a valid IPP response.
307 */
308
309ipp_t * /* O - Response data */
568fa3fa 310cupsDoRequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
3d052e43
MS
311 ipp_t *request, /* I - IPP request */
312 const char *resource) /* I - HTTP resource for POST */
313{
a603edef
MS
314 DEBUG_printf(("cupsDoRequest(http=%p, request=%p(%s), resource=\"%s\")\n",
315 http, request,
316 request ? ippOpString(request->request.op.operation_id) : "?",
317 resource ? resource : "(null)"));
318
b19ccc9e 319 return (cupsDoIORequest(http, request, resource, -1, -1));
3d052e43
MS
320}
321
322
323/*
324 * 'cupsGetResponse()' - Get a response to an IPP request.
325 *
326 * Use this function to get the response for an IPP request sent using
327 * cupsSendDocument() or cupsSendRequest(). For requests that return
328 * additional data, use httpRead() after getting a successful response.
329 *
330 * @since CUPS 1.4@
331 */
332
568fa3fa
MS
333ipp_t * /* O - Response or @code NULL@ on HTTP error */
334cupsGetResponse(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
3d052e43
MS
335 const char *resource) /* I - HTTP resource for POST */
336{
337 http_status_t status; /* HTTP status */
338 ipp_state_t state; /* IPP read state */
339 ipp_t *response = NULL; /* IPP response */
340
341
a603edef
MS
342 DEBUG_printf(("cupsGetReponse(http=%p, resource=\"%s\")\n", http,
343 resource ? resource : "(null)"));
3d052e43
MS
344
345 /*
346 * Connect to the default server as needed...
347 */
348
349 if (!http)
350 http = _cupsConnect();
351
352 if (!http || (http->state != HTTP_POST_RECV && http->state != HTTP_POST_SEND))
353 return (NULL);
354
355 /*
356 * Check for an unfinished chunked request...
357 */
358
359 if (http->data_encoding == HTTP_ENCODE_CHUNKED)
360 {
361 /*
362 * Send a 0-length chunk to finish off the request...
363 */
364
365 DEBUG_puts("cupsGetResponse: Finishing chunked POST...");
366
367 if (httpWrite2(http, "", 0) < 0)
368 return (NULL);
369 }
370
371 /*
372 * Wait for a response from the server...
373 */
374
1ff0402e
MS
375 DEBUG_printf(("cupsGetResponse: Update loop, http->status=%d...\n",
376 http->status));
3d052e43 377
1ff0402e
MS
378 status = http->status;
379 while (status == HTTP_CONTINUE)
380 status = httpUpdate(http);
3d052e43 381
a603edef 382 DEBUG_printf(("cupsGetResponse: status=%d\n", status));
3d052e43
MS
383
384 if (status == HTTP_OK)
385 {
386 /*
387 * Get the IPP response...
388 */
389
390 response = ippNew();
391
392 while ((state = ippRead(http, response)) != IPP_DATA)
393 if (state == IPP_ERROR)
394 break;
395
396 if (state == IPP_ERROR)
397 {
398 /*
399 * Delete the response...
400 */
401
a603edef 402 DEBUG_puts("cupsGetResponse: IPP read error!");
3d052e43
MS
403
404 ippDelete(response);
405 response = NULL;
406
749b1e90 407 _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
3d052e43
MS
408 }
409 }
410 else if (status != HTTP_ERROR)
411 {
412 /*
413 * Flush any error message...
414 */
415
416 httpFlush(http);
417
418 /*
419 * Then handle encryption and authentication...
420 */
421
422 if (status == HTTP_UNAUTHORIZED)
423 {
424 /*
425 * See if we can do authentication...
426 */
427
568fa3fa
MS
428 int auth_result;
429
3d052e43
MS
430 DEBUG_puts("cupsGetResponse: Need authorization...");
431
ae71f5de 432 if ((auth_result = cupsDoAuthentication(http, "POST", resource)) == 0)
3d052e43 433 httpReconnect(http);
568fa3fa
MS
434 else if (auth_result < 0)
435 http->status = status = HTTP_FORBIDDEN;
3d052e43
MS
436 }
437
438#ifdef HAVE_SSL
439 else if (status == HTTP_UPGRADE_REQUIRED)
440 {
441 /*
442 * Force a reconnect with encryption...
443 */
444
445 DEBUG_puts("cupsGetResponse: Need encryption...");
446
447 if (!httpReconnect(http))
448 httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
449 }
450#endif /* HAVE_SSL */
451 }
452
453 if (response)
454 {
455 ipp_attribute_t *attr; /* status-message attribute */
456
457
458 attr = ippFindAttribute(response, "status-message", IPP_TAG_TEXT);
459
a603edef
MS
460 DEBUG_printf(("cupsGetResponse: status-code=%s, status-message=\"%s\"\n",
461 ippErrorString(response->request.status.status_code),
462 attr ? attr->values[0].string.text : ""));
463
3d052e43 464 _cupsSetError(response->request.status.status_code,
749b1e90
MS
465 attr ? attr->values[0].string.text :
466 ippErrorString(response->request.status.status_code), 0);
3d052e43
MS
467 }
468 else if (status != HTTP_OK)
469 _cupsSetHTTPError(status);
470
471 return (response);
472}
473
474
475/*
476 * 'cupsReadResponseData()' - Read additional data after the IPP response.
477 *
478 * This function is used after cupsGetResponse() to read the PPD or document
479 * files for CUPS_GET_PPD and CUPS_GET_DOCUMENT requests, respectively.
480 *
481 * @since CUPS 1.4@
482 */
483
484ssize_t /* O - Bytes read, 0 on EOF, -1 on error */
485cupsReadResponseData(
568fa3fa 486 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
3d052e43
MS
487 char *buffer, /* I - Buffer to use */
488 size_t length) /* I - Number of bytes to read */
489{
490 /*
491 * Get the default connection as needed...
492 */
493
a603edef
MS
494 DEBUG_printf(("cupsReadResponseData(http=%p, buffer=%p, "
495 "length=" CUPS_LLFMT ")\n", http, buffer, CUPS_LLCAST length));
496
3d052e43
MS
497 if (!http)
498 {
499 _cups_globals_t *cg = _cupsGlobals();
500 /* Pointer to library globals */
501
502 if ((http = cg->http) == NULL)
503 {
749b1e90 504 _cupsSetError(IPP_INTERNAL_ERROR, _("No active connection"), 1);
3d052e43
MS
505 return (-1);
506 }
ecdc0628 507 }
ecdc0628 508
3d052e43
MS
509 /*
510 * Then read from the HTTP connection...
511 */
512
513 return (httpRead2(http, buffer, length));
514}
515
516
517/*
518 * 'cupsSendRequest()' - Send an IPP request.
519 *
520 * Use httpWrite() to write any additional data (document, PPD file, etc.)
521 * for the request, cupsGetResponse() to get the IPP response, and httpRead()
522 * to read any additional data following the response. Only one request can be
523 * sent/queued at a time.
524 *
525 * Unlike cupsDoFileRequest(), cupsDoIORequest(), and cupsDoRequest(), the
526 * request is not freed.
527 *
528 * @since CUPS 1.4@
529 */
530
531http_status_t /* O - Initial HTTP status */
568fa3fa 532cupsSendRequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
3d052e43
MS
533 ipp_t *request, /* I - IPP request */
534 const char *resource, /* I - Resource path */
ae71f5de 535 size_t length) /* I - Length of data to follow or @code CUPS_LENGTH_VARIABLE@ */
3d052e43
MS
536{
537 http_status_t status; /* Status of HTTP request */
538 int got_status; /* Did we get the status? */
539 ipp_state_t state; /* State of IPP processing */
540 http_status_t expect; /* Expect: header to use */
541
542
a603edef 543 DEBUG_printf(("cupsSendRequest(http=%p, request=%p(%s), resource=\"%s\", "
3d052e43 544 "length=" CUPS_LLFMT ")\n", http, request,
a603edef 545 request ? ippOpString(request->request.op.operation_id) : "?",
3d052e43
MS
546 resource ? resource : "(null)", CUPS_LLCAST length));
547
548 /*
549 * Range check input...
550 */
551
552 if (!request || !resource)
553 {
749b1e90 554 _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
3d052e43
MS
555
556 return (HTTP_ERROR);
557 }
558
559 /*
560 * Get the default connection as needed...
561 */
562
563 if (!http)
a603edef
MS
564 if ((http = _cupsConnect()) == NULL)
565 return (HTTP_SERVICE_UNAVAILABLE);
3d052e43 566
7594b224 567#ifdef HAVE_SSL
568 /*
569 * See if we have an auth-info attribute and are communicating over
570 * a non-local link. If so, encrypt the link so that we can pass
571 * the authentication information securely...
572 */
573
574 if (ippFindAttribute(request, "auth-info", IPP_TAG_TEXT) &&
575 !httpAddrLocalhost(http->hostaddr) && !http->tls &&
576 httpEncryption(http, HTTP_ENCRYPT_REQUIRED))
b19ccc9e
MS
577 {
578 _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
ee571f26 579 return (HTTP_SERVICE_UNAVAILABLE);
b19ccc9e 580 }
7594b224 581#endif /* HAVE_SSL */
582
1ff0402e
MS
583 /*
584 * Reconnect if the last response had a "Connection: close"...
585 */
586
587 if (!strcasecmp(http->fields[HTTP_FIELD_CONNECTION], "close"))
588 if (httpReconnect(http))
b19ccc9e
MS
589 {
590 _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
1ff0402e 591 return (HTTP_SERVICE_UNAVAILABLE);
b19ccc9e 592 }
1ff0402e 593
745129be
MS
594 /*
595 * Clear any "Local" authentication data since it is probably stale...
596 */
597
598 if (http->authstring && !strncmp(http->authstring, "Local ", 6))
599 httpSetAuthString(http, NULL, NULL);
600
ecdc0628 601 /*
602 * Loop until we can send the request without authorization problems.
603 */
604
3d052e43 605 expect = HTTP_CONTINUE;
ecdc0628 606
3d052e43 607 for (;;)
ecdc0628 608 {
a603edef 609 DEBUG_puts("cupsSendRequest: Setup...");
ecdc0628 610
611 /*
612 * Setup the HTTP variables needed...
613 */
614
ecdc0628 615 httpClearFields(http);
616 httpSetLength(http, length);
617 httpSetField(http, HTTP_FIELD_CONTENT_TYPE, "application/ipp");
618 httpSetField(http, HTTP_FIELD_AUTHORIZATION, http->authstring);
f301802f 619 httpSetExpect(http, expect);
ecdc0628 620
3d052e43 621 DEBUG_printf(("cupsSendRequest: authstring=\"%s\"\n", http->authstring));
ecdc0628 622
623 /*
624 * Try the request...
625 */
626
a603edef 627 DEBUG_puts("cupsSendRequest: Sending HTTP POST...");
ecdc0628 628
629 if (httpPost(http, resource))
630 {
631 if (httpReconnect(http))
b19ccc9e
MS
632 {
633 _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
ee571f26 634 return (HTTP_SERVICE_UNAVAILABLE);
b19ccc9e 635 }
ecdc0628 636 else
637 continue;
638 }
639
640 /*
d6ae789d 641 * Send the IPP data...
ecdc0628 642 */
643
a603edef 644 DEBUG_puts("cupsSendRequest: Writing IPP request...");
b423cd4c 645
d6ae789d 646 request->state = IPP_IDLE;
647 status = HTTP_CONTINUE;
648 got_status = 0;
649
650 while ((state = ippWrite(http, request)) != IPP_DATA)
651 if (state == IPP_ERROR)
652 break;
653 else if (httpCheck(http))
654 {
655 got_status = 1;
656
657 if ((status = httpUpdate(http)) != HTTP_CONTINUE)
658 break;
659 }
660
3d052e43
MS
661 /*
662 * Wait up to 1 second to get the 100-continue response as needed...
663 */
ecdc0628 664
3d052e43
MS
665 if (!got_status && expect == HTTP_CONTINUE)
666 {
a603edef
MS
667 DEBUG_puts("cupsSendRequest: Waiting for 100-continue...");
668
d6ae789d 669 if (httpWait(http, 1000))
670 status = httpUpdate(http);
671 }
672 else if (httpCheck(http))
673 status = httpUpdate(http);
ecdc0628 674
a603edef
MS
675 DEBUG_printf(("cupsSendRequest: status=%d\n", status));
676
ecdc0628 677 /*
3d052e43 678 * Process the current HTTP status...
ecdc0628 679 */
680
3d052e43 681 switch (status)
ecdc0628 682 {
3d052e43
MS
683 case HTTP_ERROR :
684 case HTTP_CONTINUE :
685 case HTTP_OK :
686 return (status);
ecdc0628 687
3d052e43
MS
688 case HTTP_UNAUTHORIZED :
689 if (!cupsDoAuthentication(http, "POST", resource))
690 if (httpReconnect(http))
b19ccc9e
MS
691 {
692 _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
693 return (HTTP_SERVICE_UNAVAILABLE);
694 }
ecdc0628 695
3d052e43 696 return (status);
89d46774 697
ecdc0628 698#ifdef HAVE_SSL
3d052e43
MS
699 case HTTP_UPGRADE_REQUIRED :
700 /*
701 * Flush any error message, reconnect, and then upgrade with
702 * encryption...
703 */
ecdc0628 704
3d052e43 705 if (httpReconnect(http))
b19ccc9e
MS
706 {
707 _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
708 return (HTTP_SERVICE_UNAVAILABLE);
709 }
ecdc0628 710
3d052e43 711 httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
ecdc0628 712
3d052e43 713 return (status);
ecdc0628 714#endif /* HAVE_SSL */
f301802f 715
3d052e43
MS
716 case HTTP_EXPECTATION_FAILED :
717 /*
718 * Don't try using the Expect: header the next time around...
719 */
ecdc0628 720
3d052e43 721 expect = (http_status_t)0;
52f6f666
MS
722
723 if (httpReconnect(http))
724 {
725 _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
726 return (HTTP_SERVICE_UNAVAILABLE);
727 }
a603edef 728 break;
ecdc0628 729
3d052e43
MS
730 default :
731 /*
732 * Some other error...
733 */
ecdc0628 734
3d052e43 735 return (status);
b94498cf 736 }
737 }
3d052e43
MS
738}
739
740
741/*
742 * 'cupsWriteRequestData()' - Write additional data after an IPP request.
743 *
568fa3fa
MS
744 * This function is used after @link cupsSendRequest@ to provide a PPD and
745 * after @link cupsStartDocument@ to provide a document file.
3d052e43
MS
746 *
747 * @since CUPS 1.4@
748 */
ecdc0628 749
568fa3fa 750http_status_t /* O - @code HTTP_CONTINUE@ if OK or HTTP status on error */
3d052e43 751cupsWriteRequestData(
568fa3fa 752 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
3d052e43
MS
753 const char *buffer, /* I - Bytes to write */
754 size_t length) /* I - Number of bytes to write */
755{
ecdc0628 756 /*
3d052e43 757 * Get the default connection as needed...
ecdc0628 758 */
ecdc0628 759
a603edef
MS
760 DEBUG_printf(("cupsWriteRequestData(http=%p, buffer=%p, "
761 "length=" CUPS_LLFMT ")\n", http, buffer, CUPS_LLCAST length));
762
3d052e43 763 if (!http)
ecdc0628 764 {
3d052e43
MS
765 _cups_globals_t *cg = _cupsGlobals();
766 /* Pointer to library globals */
ecdc0628 767
3d052e43
MS
768 if ((http = cg->http) == NULL)
769 {
749b1e90 770 _cupsSetError(IPP_INTERNAL_ERROR, _("No active connection"), 1);
3d052e43
MS
771 return (HTTP_ERROR);
772 }
ecdc0628 773 }
ecdc0628 774
3d052e43
MS
775 /*
776 * Then write to the HTTP connection...
777 */
ecdc0628 778
3d052e43
MS
779 if (httpWrite2(http, buffer, length) < 0)
780 return (HTTP_ERROR);
ecdc0628 781
3d052e43
MS
782 /*
783 * Finally, check if we have any pending data from the server...
784 */
ecdc0628 785
3d052e43
MS
786 if (httpCheck(http))
787 return (httpUpdate(http));
788 else
789 return (HTTP_CONTINUE);
ecdc0628 790}
791
792
793/*
794 * '_cupsSetError()' - Set the last IPP status code and status-message.
795 */
796
797void
798_cupsSetError(ipp_status_t status, /* I - IPP status code */
749b1e90
MS
799 const char *message, /* I - status-message value */
800 int localize) /* I - Localize the message? */
ecdc0628 801{
802 _cups_globals_t *cg; /* Global data */
803
804
749b1e90
MS
805 if (!message && errno)
806 {
807 message = strerror(errno);
808 localize = 0;
809 }
810
ecdc0628 811 cg = _cupsGlobals();
812 cg->last_error = status;
813
814 if (cg->last_status_message)
815 {
749b1e90 816 _cupsStrFree(cg->last_status_message);
ecdc0628 817
818 cg->last_status_message = NULL;
819 }
820
821 if (message)
749b1e90
MS
822 {
823 if (localize)
824 {
825 /*
826 * Get the message catalog...
827 */
828
829 if (!cg->lang_default)
830 cg->lang_default = cupsLangDefault();
831
832 cg->last_status_message = _cupsStrAlloc(_cupsLangString(cg->lang_default,
833 message));
834 }
835 else
836 cg->last_status_message = _cupsStrAlloc(message);
837 }
a603edef
MS
838
839 DEBUG_printf(("_cupsSetError: last_error=%s, last_status_message=\"%s\"\n",
749b1e90
MS
840 ippErrorString(cg->last_error),
841 cg->last_status_message ? cg->last_status_message : ""));
ecdc0628 842}
843
844
845/*
355e94dc
MS
846 * '_cupsSetHTTPError()' - Set the last error using the HTTP status.
847 */
848
849void
850_cupsSetHTTPError(http_status_t status) /* I - HTTP status code */
851{
852 switch (status)
853 {
854 case HTTP_NOT_FOUND :
749b1e90 855 _cupsSetError(IPP_NOT_FOUND, httpStatus(status), 0);
355e94dc
MS
856 break;
857
858 case HTTP_UNAUTHORIZED :
749b1e90 859 _cupsSetError(IPP_NOT_AUTHORIZED, httpStatus(status), 0);
355e94dc
MS
860 break;
861
862 case HTTP_FORBIDDEN :
749b1e90 863 _cupsSetError(IPP_FORBIDDEN, httpStatus(status), 0);
355e94dc
MS
864 break;
865
866 case HTTP_BAD_REQUEST :
749b1e90 867 _cupsSetError(IPP_BAD_REQUEST, httpStatus(status), 0);
355e94dc
MS
868 break;
869
870 case HTTP_REQUEST_TOO_LARGE :
749b1e90 871 _cupsSetError(IPP_REQUEST_VALUE, httpStatus(status), 0);
355e94dc
MS
872 break;
873
874 case HTTP_NOT_IMPLEMENTED :
749b1e90 875 _cupsSetError(IPP_OPERATION_NOT_SUPPORTED, httpStatus(status), 0);
355e94dc
MS
876 break;
877
878 case HTTP_NOT_SUPPORTED :
749b1e90 879 _cupsSetError(IPP_VERSION_NOT_SUPPORTED, httpStatus(status), 0);
355e94dc
MS
880 break;
881
882 default :
883 DEBUG_printf(("HTTP error %d mapped to IPP_SERVICE_UNAVAILABLE!\n",
884 status));
749b1e90 885 _cupsSetError(IPP_SERVICE_UNAVAILABLE, httpStatus(status), 0);
355e94dc
MS
886 break;
887 }
888}
889
890
891/*
b19ccc9e 892 * End of "$Id: request.c 7946 2008-09-16 23:27:54Z mike $".
ecdc0628 893 */