2 * "$Id: request.c 7946 2008-09-16 23:27:54Z mike $"
4 * IPP utilities for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products.
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/".
15 * This file is subject to the Apple OS-Developed Software exception.
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.
31 * Include necessary headers...
40 #if defined(WIN32) || defined(__EMX__)
44 #endif /* WIN32 || __EMX__ */
51 * 'cupsDoFileRequest()' - Do an IPP request with a file.
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.
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 */
64 ipp_t
*response
; /* IPP response data */
65 int infile
; /* Input file */
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)"));
76 if ((infile
= open(filename
, O_RDONLY
| O_BINARY
)) < 0)
79 * Can't get file information!
82 _cupsSetError(errno
== ENOENT
? IPP_NOT_FOUND
: IPP_NOT_AUTHORIZED
,
93 response
= cupsDoIORequest(http
, request
, resource
, infile
, -1);
103 * 'cupsDoIORequest()' - Do an IPP request with file descriptors.
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.
109 * If "infile" is a valid file descriptor, cupsDoIORequest() copies
110 * all of the data from the file after the IPP request message.
112 * If "outfile" is a valid file descriptor, cupsDoIORequest() copies
113 * all of the data after the IPP response message to the file.
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 */
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 */
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
));
139 * Range check input...
142 if (!request
|| !resource
)
146 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
152 * Get the default connection as needed...
156 if ((http
= _cupsConnect()) == NULL
)
160 * See if we have a file to send...
165 if (fstat(infile
, &fileinfo
))
168 * Can't get file information!
171 _cupsSetError(errno
== EBADF
? IPP_NOT_FOUND
: IPP_NOT_AUTHORIZED
,
180 if (fileinfo
.st_mode
& _S_IFDIR
)
182 if (S_ISDIR(fileinfo
.st_mode
))
186 * Can't send a directory...
191 _cupsSetError(IPP_NOT_POSSIBLE
, strerror(EISDIR
), 0);
197 if (!S_ISREG(fileinfo
.st_mode
))
198 length
= 0; /* Chunk when piping */
201 length
= ippLength(request
) + fileinfo
.st_size
;
204 length
= ippLength(request
);
206 DEBUG_printf(("cupsDoIORequest: Request length=%ld, total length=%ld",
207 (long)ippLength(request
), (long)length
));
210 * Loop until we can send the request without authorization problems.
213 while (response
== NULL
)
215 DEBUG_puts("cupsDoIORequest: setup...");
218 * Send the request...
221 status
= cupsSendRequest(http
, request
, resource
, length
);
223 DEBUG_printf(("cupsDoIORequest: status=%d\n", status
));
225 if (status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
&& infile
>= 0)
227 DEBUG_puts("cupsDoIORequest: file write...");
230 * Send the file with the request...
234 if (S_ISREG(fileinfo
.st_mode
))
236 lseek(infile
, 0, SEEK_SET
);
238 while ((bytes
= (int)read(infile
, buffer
, sizeof(buffer
))) > 0)
242 if ((status
= httpUpdate(http
)) != HTTP_CONTINUE
)
246 if (httpWrite2(http
, buffer
, bytes
) < bytes
)
252 * Get the server's response...
255 if (status
== HTTP_CONTINUE
|| status
== HTTP_OK
)
257 response
= cupsGetResponse(http
, resource
);
258 status
= http
->status
;
261 if (status
== HTTP_FORBIDDEN
|| status
== HTTP_ERROR
||
262 status
== HTTP_SERVICE_UNAVAILABLE
)
264 _cupsSetHTTPError(status
);
273 * Write trailing data to file...
276 while ((bytes
= (int)httpRead2(http
, buffer
, sizeof(buffer
))) > 0)
277 if (write(outfile
, buffer
, bytes
) < bytes
)
283 * Flush any remaining data...
292 * Delete the original request and return the response...
302 * 'cupsDoRequest()' - Do an IPP request.
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.
309 ipp_t
* /* O - Response data */
310 cupsDoRequest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
311 ipp_t
*request
, /* I - IPP request */
312 const char *resource
) /* I - HTTP resource for POST */
314 DEBUG_printf(("cupsDoRequest(http=%p, request=%p(%s), resource=\"%s\")\n",
316 request
? ippOpString(request
->request
.op
.operation_id
) : "?",
317 resource
? resource
: "(null)"));
319 return (cupsDoIORequest(http
, request
, resource
, -1, -1));
324 * 'cupsGetResponse()' - Get a response to an IPP request.
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.
333 ipp_t
* /* O - Response or @code NULL@ on HTTP error */
334 cupsGetResponse(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
335 const char *resource
) /* I - HTTP resource for POST */
337 http_status_t status
; /* HTTP status */
338 ipp_state_t state
; /* IPP read state */
339 ipp_t
*response
= NULL
; /* IPP response */
342 DEBUG_printf(("cupsGetReponse(http=%p, resource=\"%s\")\n", http
,
343 resource
? resource
: "(null)"));
346 * Connect to the default server as needed...
350 http
= _cupsConnect();
352 if (!http
|| (http
->state
!= HTTP_POST_RECV
&& http
->state
!= HTTP_POST_SEND
))
356 * Check for an unfinished chunked request...
359 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
362 * Send a 0-length chunk to finish off the request...
365 DEBUG_puts("cupsGetResponse: Finishing chunked POST...");
367 if (httpWrite2(http
, "", 0) < 0)
372 * Wait for a response from the server...
375 DEBUG_printf(("cupsGetResponse: Update loop, http->status=%d...\n",
378 status
= http
->status
;
379 while (status
== HTTP_CONTINUE
)
380 status
= httpUpdate(http
);
382 DEBUG_printf(("cupsGetResponse: status=%d\n", status
));
384 if (status
== HTTP_OK
)
387 * Get the IPP response...
392 while ((state
= ippRead(http
, response
)) != IPP_DATA
)
393 if (state
== IPP_ERROR
)
396 if (state
== IPP_ERROR
)
399 * Delete the response...
402 DEBUG_puts("cupsGetResponse: IPP read error!");
407 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, NULL
, 0);
410 else if (status
!= HTTP_ERROR
)
413 * Flush any error message...
419 * Then handle encryption and authentication...
422 if (status
== HTTP_UNAUTHORIZED
)
425 * See if we can do authentication...
430 DEBUG_puts("cupsGetResponse: Need authorization...");
432 if ((auth_result
= cupsDoAuthentication(http
, "POST", resource
)) == 0)
434 else if (auth_result
< 0)
435 http
->status
= status
= HTTP_FORBIDDEN
;
439 else if (status
== HTTP_UPGRADE_REQUIRED
)
442 * Force a reconnect with encryption...
445 DEBUG_puts("cupsGetResponse: Need encryption...");
447 if (!httpReconnect(http
))
448 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
450 #endif /* HAVE_SSL */
455 ipp_attribute_t
*attr
; /* status-message attribute */
458 attr
= ippFindAttribute(response
, "status-message", IPP_TAG_TEXT
);
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
: ""));
464 _cupsSetError(response
->request
.status
.status_code
,
465 attr
? attr
->values
[0].string
.text
:
466 ippErrorString(response
->request
.status
.status_code
), 0);
468 else if (status
!= HTTP_OK
)
469 _cupsSetHTTPError(status
);
476 * 'cupsReadResponseData()' - Read additional data after the IPP response.
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.
484 ssize_t
/* O - Bytes read, 0 on EOF, -1 on error */
485 cupsReadResponseData(
486 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
487 char *buffer
, /* I - Buffer to use */
488 size_t length
) /* I - Number of bytes to read */
491 * Get the default connection as needed...
494 DEBUG_printf(("cupsReadResponseData(http=%p, buffer=%p, "
495 "length=" CUPS_LLFMT
")\n", http
, buffer
, CUPS_LLCAST length
));
499 _cups_globals_t
*cg
= _cupsGlobals();
500 /* Pointer to library globals */
502 if ((http
= cg
->http
) == NULL
)
504 _cupsSetError(IPP_INTERNAL_ERROR
, _("No active connection"), 1);
510 * Then read from the HTTP connection...
513 return (httpRead2(http
, buffer
, length
));
518 * 'cupsSendRequest()' - Send an IPP request.
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.
525 * Unlike cupsDoFileRequest(), cupsDoIORequest(), and cupsDoRequest(), the
526 * request is not freed.
531 http_status_t
/* O - Initial HTTP status */
532 cupsSendRequest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
533 ipp_t
*request
, /* I - IPP request */
534 const char *resource
, /* I - Resource path */
535 size_t length
) /* I - Length of data to follow or @code CUPS_LENGTH_VARIABLE@ */
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 */
543 DEBUG_printf(("cupsSendRequest(http=%p, request=%p(%s), resource=\"%s\", "
544 "length=" CUPS_LLFMT
")\n", http
, request
,
545 request
? ippOpString(request
->request
.op
.operation_id
) : "?",
546 resource
? resource
: "(null)", CUPS_LLCAST length
));
549 * Range check input...
552 if (!request
|| !resource
)
554 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
560 * Get the default connection as needed...
564 if ((http
= _cupsConnect()) == NULL
)
565 return (HTTP_SERVICE_UNAVAILABLE
);
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...
574 if (ippFindAttribute(request
, "auth-info", IPP_TAG_TEXT
) &&
575 !httpAddrLocalhost(http
->hostaddr
) && !http
->tls
&&
576 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
))
578 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, NULL
, 0);
579 return (HTTP_SERVICE_UNAVAILABLE
);
581 #endif /* HAVE_SSL */
584 * Reconnect if the last response had a "Connection: close"...
587 if (!strcasecmp(http
->fields
[HTTP_FIELD_CONNECTION
], "close"))
588 if (httpReconnect(http
))
590 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, NULL
, 0);
591 return (HTTP_SERVICE_UNAVAILABLE
);
595 * Loop until we can send the request without authorization problems.
598 expect
= HTTP_CONTINUE
;
602 DEBUG_puts("cupsSendRequest: Setup...");
605 * Setup the HTTP variables needed...
608 httpClearFields(http
);
609 httpSetLength(http
, length
);
610 httpSetField(http
, HTTP_FIELD_CONTENT_TYPE
, "application/ipp");
611 httpSetField(http
, HTTP_FIELD_AUTHORIZATION
, http
->authstring
);
612 httpSetExpect(http
, expect
);
614 DEBUG_printf(("cupsSendRequest: authstring=\"%s\"\n", http
->authstring
));
620 DEBUG_puts("cupsSendRequest: Sending HTTP POST...");
622 if (httpPost(http
, resource
))
624 if (httpReconnect(http
))
626 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, NULL
, 0);
627 return (HTTP_SERVICE_UNAVAILABLE
);
634 * Send the IPP data...
637 DEBUG_puts("cupsSendRequest: Writing IPP request...");
639 request
->state
= IPP_IDLE
;
640 status
= HTTP_CONTINUE
;
643 while ((state
= ippWrite(http
, request
)) != IPP_DATA
)
644 if (state
== IPP_ERROR
)
646 else if (httpCheck(http
))
650 if ((status
= httpUpdate(http
)) != HTTP_CONTINUE
)
655 * Wait up to 1 second to get the 100-continue response as needed...
658 if (!got_status
&& expect
== HTTP_CONTINUE
)
660 DEBUG_puts("cupsSendRequest: Waiting for 100-continue...");
662 if (httpWait(http
, 1000))
663 status
= httpUpdate(http
);
665 else if (httpCheck(http
))
666 status
= httpUpdate(http
);
668 DEBUG_printf(("cupsSendRequest: status=%d\n", status
));
671 * Process the current HTTP status...
681 case HTTP_UNAUTHORIZED
:
682 if (!cupsDoAuthentication(http
, "POST", resource
))
683 if (httpReconnect(http
))
685 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, NULL
, 0);
686 return (HTTP_SERVICE_UNAVAILABLE
);
692 case HTTP_UPGRADE_REQUIRED
:
694 * Flush any error message, reconnect, and then upgrade with
698 if (httpReconnect(http
))
700 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, NULL
, 0);
701 return (HTTP_SERVICE_UNAVAILABLE
);
704 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
707 #endif /* HAVE_SSL */
709 case HTTP_EXPECTATION_FAILED
:
711 * Don't try using the Expect: header the next time around...
714 expect
= (http_status_t
)0;
716 if (httpReconnect(http
))
718 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, NULL
, 0);
719 return (HTTP_SERVICE_UNAVAILABLE
);
725 * Some other error...
735 * 'cupsWriteRequestData()' - Write additional data after an IPP request.
737 * This function is used after @link cupsSendRequest@ to provide a PPD and
738 * after @link cupsStartDocument@ to provide a document file.
743 http_status_t
/* O - @code HTTP_CONTINUE@ if OK or HTTP status on error */
744 cupsWriteRequestData(
745 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
746 const char *buffer
, /* I - Bytes to write */
747 size_t length
) /* I - Number of bytes to write */
750 * Get the default connection as needed...
753 DEBUG_printf(("cupsWriteRequestData(http=%p, buffer=%p, "
754 "length=" CUPS_LLFMT
")\n", http
, buffer
, CUPS_LLCAST length
));
758 _cups_globals_t
*cg
= _cupsGlobals();
759 /* Pointer to library globals */
761 if ((http
= cg
->http
) == NULL
)
763 _cupsSetError(IPP_INTERNAL_ERROR
, _("No active connection"), 1);
769 * Then write to the HTTP connection...
772 if (httpWrite2(http
, buffer
, length
) < 0)
776 * Finally, check if we have any pending data from the server...
780 return (httpUpdate(http
));
782 return (HTTP_CONTINUE
);
787 * '_cupsSetError()' - Set the last IPP status code and status-message.
791 _cupsSetError(ipp_status_t status
, /* I - IPP status code */
792 const char *message
, /* I - status-message value */
793 int localize
) /* I - Localize the message? */
795 _cups_globals_t
*cg
; /* Global data */
798 if (!message
&& errno
)
800 message
= strerror(errno
);
805 cg
->last_error
= status
;
807 if (cg
->last_status_message
)
809 _cupsStrFree(cg
->last_status_message
);
811 cg
->last_status_message
= NULL
;
819 * Get the message catalog...
822 if (!cg
->lang_default
)
823 cg
->lang_default
= cupsLangDefault();
825 cg
->last_status_message
= _cupsStrAlloc(_cupsLangString(cg
->lang_default
,
829 cg
->last_status_message
= _cupsStrAlloc(message
);
832 DEBUG_printf(("_cupsSetError: last_error=%s, last_status_message=\"%s\"\n",
833 ippErrorString(cg
->last_error
),
834 cg
->last_status_message
? cg
->last_status_message
: ""));
839 * '_cupsSetHTTPError()' - Set the last error using the HTTP status.
843 _cupsSetHTTPError(http_status_t status
) /* I - HTTP status code */
847 case HTTP_NOT_FOUND
:
848 _cupsSetError(IPP_NOT_FOUND
, httpStatus(status
), 0);
851 case HTTP_UNAUTHORIZED
:
852 _cupsSetError(IPP_NOT_AUTHORIZED
, httpStatus(status
), 0);
855 case HTTP_FORBIDDEN
:
856 _cupsSetError(IPP_FORBIDDEN
, httpStatus(status
), 0);
859 case HTTP_BAD_REQUEST
:
860 _cupsSetError(IPP_BAD_REQUEST
, httpStatus(status
), 0);
863 case HTTP_REQUEST_TOO_LARGE
:
864 _cupsSetError(IPP_REQUEST_VALUE
, httpStatus(status
), 0);
867 case HTTP_NOT_IMPLEMENTED
:
868 _cupsSetError(IPP_OPERATION_NOT_SUPPORTED
, httpStatus(status
), 0);
871 case HTTP_NOT_SUPPORTED
:
872 _cupsSetError(IPP_VERSION_NOT_SUPPORTED
, httpStatus(status
), 0);
876 DEBUG_printf(("HTTP error %d mapped to IPP_SERVICE_UNAVAILABLE!\n",
878 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, httpStatus(status
), 0);
885 * End of "$Id: request.c 7946 2008-09-16 23:27:54Z mike $".