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