]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/request.c
Merge changes from CUPS 1.4svn-r7696.
[thirdparty/cups.git] / cups / request.c
1 /*
2 * "$Id: request.c 7460 2008-04-16 02:19:54Z mike $"
3 *
4 * IPP utilities for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products.
8 *
9 * These coded instructions, statements, and computer programs are the
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/".
14 *
15 * This file is subject to the Apple OS-Developed Software exception.
16 *
17 * Contents:
18 *
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.
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__ */
45 #ifndef O_BINARY
46 # define O_BINARY 0
47 #endif /* O_BINARY */
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
54 * and authenticating as necessary. The request is freed with @link ippDelete@
55 * after receiving a valid IPP response.
56 */
57
58 ipp_t * /* O - Response data */
59 cupsDoFileRequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
60 ipp_t *request, /* I - IPP request */
61 const char *resource, /* I - HTTP resource for POST */
62 const char *filename) /* I - File to send or @code NULL@ for none */
63 {
64 ipp_t *response; /* IPP response data */
65 int infile; /* Input file */
66
67
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
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,
83 strerror(errno));
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
118 ipp_t * /* O - Response data */
119 cupsDoIORequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
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 */
124 {
125 ipp_t *response = NULL; /* IPP response data */
126 size_t length = 0; /* Content-Length value */
127 http_status_t status; /* Status of HTTP request */
128 struct stat fileinfo; /* File information */
129 int bytes; /* Number of bytes read/written */
130 char buffer[32768]; /* Output buffer */
131
132
133 DEBUG_printf(("cupsDoIORequest(http=%p, request=%p(%s), resource=\"%s\", "
134 "infile=%d, outfile=%d)\n", http, request,
135 request ? ippOpString(request->request.op.operation_id) : "?",
136 resource ? resource : "(null)", infile, outfile));
137
138 /*
139 * Range check input...
140 */
141
142 if (!request || !resource)
143 {
144 ippDelete(request);
145
146 _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL));
147
148 return (NULL);
149 }
150
151 /*
152 * Get the default connection as needed...
153 */
154
155 if (!http)
156 if ((http = _cupsConnect()) == NULL)
157 return (NULL);
158
159 /*
160 * See if we have a file to send...
161 */
162
163 if (infile >= 0)
164 {
165 if (fstat(infile, &fileinfo))
166 {
167 /*
168 * Can't get file information!
169 */
170
171 _cupsSetError(errno == EBADF ? IPP_NOT_FOUND : IPP_NOT_AUTHORIZED,
172 strerror(errno));
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
191 _cupsSetError(IPP_NOT_POSSIBLE, strerror(EISDIR));
192
193 return (NULL);
194 }
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
258 if (status == HTTP_FORBIDDEN)
259 break;
260
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
302 ipp_t * /* O - Response data */
303 cupsDoRequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
304 ipp_t *request, /* I - IPP request */
305 const char *resource) /* I - HTTP resource for POST */
306 {
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
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
326 ipp_t * /* O - Response or @code NULL@ on HTTP error */
327 cupsGetResponse(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
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
335 DEBUG_printf(("cupsGetReponse(http=%p, resource=\"%s\")\n", http,
336 resource ? resource : "(null)"));
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
368 DEBUG_puts("cupsGetResponse: Update loop...");
369
370 while ((status = httpUpdate(http)) == HTTP_CONTINUE)
371 /* Do nothing but update */;
372
373 DEBUG_printf(("cupsGetResponse: status=%d\n", status));
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
393 DEBUG_puts("cupsGetResponse: IPP read error!");
394
395 ippDelete(response);
396 response = NULL;
397
398 _cupsSetError(IPP_SERVICE_UNAVAILABLE, strerror(errno));
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
419 int auth_result;
420
421 DEBUG_puts("cupsGetResponse: Need authorization...");
422
423 if ((auth_result = cupsDoAuthentication(http, "POST", resource)) == 0)
424 httpReconnect(http);
425 else if (auth_result < 0)
426 http->status = status = HTTP_FORBIDDEN;
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
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
455 _cupsSetError(response->request.status.status_code,
456 attr ? attr->values[0].string.text :
457 ippErrorString(response->request.status.status_code));
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
475 ssize_t /* O - Bytes read, 0 on EOF, -1 on error */
476 cupsReadResponseData(
477 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
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
485 DEBUG_printf(("cupsReadResponseData(http=%p, buffer=%p, "
486 "length=" CUPS_LLFMT ")\n", http, buffer, CUPS_LLCAST length));
487
488 if (!http)
489 {
490 _cups_globals_t *cg = _cupsGlobals();
491 /* Pointer to library globals */
492
493 if ((http = cg->http) == NULL)
494 {
495 _cupsSetError(IPP_INTERNAL_ERROR, "No active connection");
496 return (-1);
497 }
498 }
499
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
522 http_status_t /* O - Initial HTTP status */
523 cupsSendRequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
524 ipp_t *request, /* I - IPP request */
525 const char *resource, /* I - Resource path */
526 size_t length) /* I - Length of data to follow or @code CUPS_LENGTH_VARIABLE@ */
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
534 DEBUG_printf(("cupsSendRequest(http=%p, request=%p(%s), resource=\"%s\", "
535 "length=" CUPS_LLFMT ")\n", http, request,
536 request ? ippOpString(request->request.op.operation_id) : "?",
537 resource ? resource : "(null)", CUPS_LLCAST length));
538
539 /*
540 * Range check input...
541 */
542
543 if (!request || !resource)
544 {
545 _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL));
546
547 return (HTTP_ERROR);
548 }
549
550 /*
551 * Get the default connection as needed...
552 */
553
554 if (!http)
555 if ((http = _cupsConnect()) == NULL)
556 return (HTTP_SERVICE_UNAVAILABLE);
557
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))
568 return (HTTP_ERROR);
569 #endif /* HAVE_SSL */
570
571 /*
572 * Loop until we can send the request without authorization problems.
573 */
574
575 status = HTTP_ERROR;
576 expect = HTTP_CONTINUE;
577
578 for (;;)
579 {
580 DEBUG_puts("cupsSendRequest: Setup...");
581
582 /*
583 * Setup the HTTP variables needed...
584 */
585
586 httpClearFields(http);
587 httpSetLength(http, length);
588 httpSetField(http, HTTP_FIELD_CONTENT_TYPE, "application/ipp");
589 httpSetField(http, HTTP_FIELD_AUTHORIZATION, http->authstring);
590 httpSetExpect(http, expect);
591
592 DEBUG_printf(("cupsSendRequest: authstring=\"%s\"\n", http->authstring));
593
594 /*
595 * Try the request...
596 */
597
598 DEBUG_puts("cupsSendRequest: Sending HTTP POST...");
599
600 if (httpPost(http, resource))
601 {
602 if (httpReconnect(http))
603 return (HTTP_ERROR);
604 else
605 continue;
606 }
607
608 /*
609 * Send the IPP data...
610 */
611
612 DEBUG_puts("cupsSendRequest: Writing IPP request...");
613
614 request->state = IPP_IDLE;
615 status = HTTP_CONTINUE;
616 got_status = 0;
617
618 while ((state = ippWrite(http, request)) != IPP_DATA)
619 if (state == IPP_ERROR)
620 break;
621 else if (httpCheck(http))
622 {
623 got_status = 1;
624
625 if ((status = httpUpdate(http)) != HTTP_CONTINUE)
626 break;
627 }
628
629 /*
630 * Wait up to 1 second to get the 100-continue response as needed...
631 */
632
633 if (!got_status && expect == HTTP_CONTINUE)
634 {
635 DEBUG_puts("cupsSendRequest: Waiting for 100-continue...");
636
637 if (httpWait(http, 1000))
638 status = httpUpdate(http);
639 else
640 status = HTTP_EXPECTATION_FAILED;
641 }
642 else if (httpCheck(http))
643 status = httpUpdate(http);
644
645 DEBUG_printf(("cupsSendRequest: status=%d\n", status));
646
647 /*
648 * Process the current HTTP status...
649 */
650
651 switch (status)
652 {
653 case HTTP_ERROR :
654 case HTTP_CONTINUE :
655 case HTTP_OK :
656 return (status);
657
658 case HTTP_UNAUTHORIZED :
659 if (!cupsDoAuthentication(http, "POST", resource))
660 if (httpReconnect(http))
661 return (HTTP_ERROR);
662
663 return (status);
664
665 #ifdef HAVE_SSL
666 case HTTP_UPGRADE_REQUIRED :
667 /*
668 * Flush any error message, reconnect, and then upgrade with
669 * encryption...
670 */
671
672 if (httpReconnect(http))
673 return (HTTP_ERROR);
674
675 httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
676
677 return (status);
678 #endif /* HAVE_SSL */
679
680 case HTTP_EXPECTATION_FAILED :
681 /*
682 * Don't try using the Expect: header the next time around...
683 */
684
685 expect = (http_status_t)0;
686 break;
687
688 default :
689 /*
690 * Some other error...
691 */
692
693 return (status);
694 }
695 }
696 }
697
698
699 /*
700 * 'cupsWriteRequestData()' - Write additional data after an IPP request.
701 *
702 * This function is used after @link cupsSendRequest@ to provide a PPD and
703 * after @link cupsStartDocument@ to provide a document file.
704 *
705 * @since CUPS 1.4@
706 */
707
708 http_status_t /* O - @code HTTP_CONTINUE@ if OK or HTTP status on error */
709 cupsWriteRequestData(
710 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
711 const char *buffer, /* I - Bytes to write */
712 size_t length) /* I - Number of bytes to write */
713 {
714 /*
715 * Get the default connection as needed...
716 */
717
718 DEBUG_printf(("cupsWriteRequestData(http=%p, buffer=%p, "
719 "length=" CUPS_LLFMT ")\n", http, buffer, CUPS_LLCAST length));
720
721 if (!http)
722 {
723 _cups_globals_t *cg = _cupsGlobals();
724 /* Pointer to library globals */
725
726 if ((http = cg->http) == NULL)
727 {
728 _cupsSetError(IPP_INTERNAL_ERROR, "No active connection");
729 return (HTTP_ERROR);
730 }
731 }
732
733 /*
734 * Then write to the HTTP connection...
735 */
736
737 if (httpWrite2(http, buffer, length) < 0)
738 return (HTTP_ERROR);
739
740 /*
741 * Finally, check if we have any pending data from the server...
742 */
743
744 if (httpCheck(http))
745 return (httpUpdate(http));
746 else
747 return (HTTP_CONTINUE);
748 }
749
750
751 /*
752 * '_cupsSetError()' - Set the last IPP status code and status-message.
753 */
754
755 void
756 _cupsSetError(ipp_status_t status, /* I - IPP status code */
757 const char *message) /* I - status-message value */
758 {
759 _cups_globals_t *cg; /* Global data */
760
761
762 cg = _cupsGlobals();
763 cg->last_error = status;
764
765 if (cg->last_status_message)
766 {
767 free(cg->last_status_message);
768
769 cg->last_status_message = NULL;
770 }
771
772 if (message)
773 cg->last_status_message = strdup(message);
774
775 DEBUG_printf(("_cupsSetError: last_error=%s, last_status_message=\"%s\"\n",
776 ippErrorString(cg->last_error), message ? message : ""));
777 }
778
779
780 /*
781 * '_cupsSetHTTPError()' - Set the last error using the HTTP status.
782 */
783
784 void
785 _cupsSetHTTPError(http_status_t status) /* I - HTTP status code */
786 {
787 switch (status)
788 {
789 case HTTP_NOT_FOUND :
790 _cupsSetError(IPP_NOT_FOUND, httpStatus(status));
791 break;
792
793 case HTTP_UNAUTHORIZED :
794 _cupsSetError(IPP_NOT_AUTHORIZED, httpStatus(status));
795 break;
796
797 case HTTP_FORBIDDEN :
798 _cupsSetError(IPP_FORBIDDEN, httpStatus(status));
799 break;
800
801 case HTTP_BAD_REQUEST :
802 _cupsSetError(IPP_BAD_REQUEST, httpStatus(status));
803 break;
804
805 case HTTP_REQUEST_TOO_LARGE :
806 _cupsSetError(IPP_REQUEST_VALUE, httpStatus(status));
807 break;
808
809 case HTTP_NOT_IMPLEMENTED :
810 _cupsSetError(IPP_OPERATION_NOT_SUPPORTED, httpStatus(status));
811 break;
812
813 case HTTP_NOT_SUPPORTED :
814 _cupsSetError(IPP_VERSION_NOT_SUPPORTED, httpStatus(status));
815 break;
816
817 default :
818 DEBUG_printf(("HTTP error %d mapped to IPP_SERVICE_UNAVAILABLE!\n",
819 status));
820 _cupsSetError(IPP_SERVICE_UNAVAILABLE, httpStatus(status));
821 break;
822 }
823 }
824
825
826 /*
827 * End of "$Id: request.c 7460 2008-04-16 02:19:54Z mike $".
828 */