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-2009 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\")", http
, request
,
70 request
? ippOpString(request
->request
.op
.operation_id
) : "?",
75 if ((infile
= open(filename
, O_RDONLY
| O_BINARY
)) < 0)
78 * Can't get file information!
81 _cupsSetError(errno
== ENOENT
? IPP_NOT_FOUND
: IPP_NOT_AUTHORIZED
,
92 response
= cupsDoIORequest(http
, request
, resource
, infile
, -1);
102 * 'cupsDoIORequest()' - Do an IPP request with file descriptors.
104 * This function sends the IPP request to the specified server, retrying
105 * and authenticating as necessary. The request is freed with ippDelete()
106 * after receiving a valid IPP response.
108 * If "infile" is a valid file descriptor, cupsDoIORequest() copies
109 * all of the data from the file after the IPP request message.
111 * If "outfile" is a valid file descriptor, cupsDoIORequest() copies
112 * all of the data after the IPP response message to the file.
114 * @since CUPS 1.3/Mac OS X 10.5@
117 ipp_t
* /* O - Response data */
118 cupsDoIORequest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
119 ipp_t
*request
, /* I - IPP request */
120 const char *resource
, /* I - HTTP resource for POST */
121 int infile
, /* I - File to read from or -1 for none */
122 int outfile
) /* I - File to write to or -1 for none */
124 ipp_t
*response
= NULL
; /* IPP response data */
125 size_t length
= 0; /* Content-Length value */
126 http_status_t status
; /* Status of HTTP request */
127 struct stat fileinfo
; /* File information */
128 int bytes
; /* Number of bytes read/written */
129 char buffer
[32768]; /* Output buffer */
132 DEBUG_printf(("cupsDoIORequest(http=%p, request=%p(%s), resource=\"%s\", "
133 "infile=%d, outfile=%d)", http
, request
,
134 request
? ippOpString(request
->request
.op
.operation_id
) : "?",
135 resource
, infile
, outfile
));
138 * Range check input...
141 if (!request
|| !resource
)
145 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
151 * Get the default connection as needed...
155 if ((http
= _cupsConnect()) == NULL
)
159 * See if we have a file to send...
164 if (fstat(infile
, &fileinfo
))
167 * Can't get file information!
170 _cupsSetError(errno
== EBADF
? IPP_NOT_FOUND
: IPP_NOT_AUTHORIZED
,
179 if (fileinfo
.st_mode
& _S_IFDIR
)
181 if (S_ISDIR(fileinfo
.st_mode
))
185 * Can't send a directory...
190 _cupsSetError(IPP_NOT_POSSIBLE
, strerror(EISDIR
), 0);
196 if (!S_ISREG(fileinfo
.st_mode
))
197 length
= 0; /* Chunk when piping */
200 length
= ippLength(request
) + fileinfo
.st_size
;
203 length
= ippLength(request
);
205 DEBUG_printf(("2cupsDoIORequest: Request length=%ld, total length=%ld",
206 (long)ippLength(request
), (long)length
));
209 * Clear any "Local" authentication data since it is probably stale...
212 if (http
->authstring
&& !strncmp(http
->authstring
, "Local ", 6))
213 httpSetAuthString(http
, NULL
, NULL
);
216 * Loop until we can send the request without authorization problems.
219 while (response
== NULL
)
221 DEBUG_puts("2cupsDoIORequest: setup...");
224 * Send the request...
227 status
= cupsSendRequest(http
, request
, resource
, length
);
229 DEBUG_printf(("2cupsDoIORequest: status=%d", status
));
231 if (status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
&& infile
>= 0)
233 DEBUG_puts("2cupsDoIORequest: file write...");
236 * Send the file with the request...
240 if (S_ISREG(fileinfo
.st_mode
))
242 lseek(infile
, 0, SEEK_SET
);
244 while ((bytes
= (int)read(infile
, buffer
, sizeof(buffer
))) > 0)
248 if ((status
= httpUpdate(http
)) != HTTP_CONTINUE
)
252 if (httpWrite2(http
, buffer
, bytes
) < bytes
)
258 * Get the server's response...
261 if (status
== HTTP_CONTINUE
|| status
== HTTP_OK
)
263 response
= cupsGetResponse(http
, resource
);
264 status
= http
->status
;
267 DEBUG_printf(("2cupsDoIORequest: status=%d", status
));
269 if (status
>= HTTP_BAD_REQUEST
&&
270 status
!= HTTP_UNAUTHORIZED
&&
271 status
!= HTTP_UPGRADE_REQUIRED
)
274 _cupsSetHTTPError(status
);
283 * Write trailing data to file...
286 while ((bytes
= (int)httpRead2(http
, buffer
, sizeof(buffer
))) > 0)
287 if (write(outfile
, buffer
, bytes
) < bytes
)
293 * Flush any remaining data...
302 * Delete the original request and return the response...
312 * 'cupsDoRequest()' - Do an IPP request.
314 * This function sends the IPP request to the specified server, retrying
315 * and authenticating as necessary. The request is freed with ippDelete()
316 * after receiving a valid IPP response.
319 ipp_t
* /* O - Response data */
320 cupsDoRequest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
321 ipp_t
*request
, /* I - IPP request */
322 const char *resource
) /* I - HTTP resource for POST */
324 DEBUG_printf(("cupsDoRequest(http=%p, request=%p(%s), resource=\"%s\")",
326 request
? ippOpString(request
->request
.op
.operation_id
) : "?",
329 return (cupsDoIORequest(http
, request
, resource
, -1, -1));
334 * 'cupsGetResponse()' - Get a response to an IPP request.
336 * Use this function to get the response for an IPP request sent using
337 * cupsSendDocument() or cupsSendRequest(). For requests that return
338 * additional data, use httpRead() after getting a successful response.
340 * @since CUPS 1.4/Mac OS X 10.6@
343 ipp_t
* /* O - Response or @code NULL@ on HTTP error */
344 cupsGetResponse(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
345 const char *resource
) /* I - HTTP resource for POST */
347 http_status_t status
; /* HTTP status */
348 ipp_state_t state
; /* IPP read state */
349 ipp_t
*response
= NULL
; /* IPP response */
352 DEBUG_printf(("cupsGetResponse(http=%p, resource=\"%s\")", http
, resource
));
355 * Connect to the default server as needed...
359 http
= _cupsConnect();
361 if (!http
|| (http
->state
!= HTTP_POST_RECV
&& http
->state
!= HTTP_POST_SEND
))
365 * Check for an unfinished chunked request...
368 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
371 * Send a 0-length chunk to finish off the request...
374 DEBUG_puts("2cupsGetResponse: Finishing chunked POST...");
376 if (httpWrite2(http
, "", 0) < 0)
381 * Wait for a response from the server...
384 DEBUG_printf(("2cupsGetResponse: Update loop, http->status=%d...",
387 status
= http
->status
;
388 while (status
== HTTP_CONTINUE
)
389 status
= httpUpdate(http
);
391 DEBUG_printf(("2cupsGetResponse: status=%d", status
));
393 if (status
== HTTP_OK
)
396 * Get the IPP response...
401 while ((state
= ippRead(http
, response
)) != IPP_DATA
)
402 if (state
== IPP_ERROR
)
405 if (state
== IPP_ERROR
)
408 * Delete the response...
411 DEBUG_puts("1cupsGetResponse: IPP read error!");
416 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, NULL
, 0);
419 else if (status
!= HTTP_ERROR
)
422 * Flush any error message...
428 * Then handle encryption and authentication...
431 if (status
== HTTP_UNAUTHORIZED
)
434 * See if we can do authentication...
437 DEBUG_puts("2cupsGetResponse: Need authorization...");
439 if (!cupsDoAuthentication(http
, "POST", resource
))
442 status
= HTTP_AUTHORIZATION_CANCELED
;
446 else if (status
== HTTP_UPGRADE_REQUIRED
)
449 * Force a reconnect with encryption...
452 DEBUG_puts("2cupsGetResponse: Need encryption...");
454 if (!httpReconnect(http
))
455 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
457 #endif /* HAVE_SSL */
462 ipp_attribute_t
*attr
; /* status-message attribute */
465 attr
= ippFindAttribute(response
, "status-message", IPP_TAG_TEXT
);
467 DEBUG_printf(("1cupsGetResponse: status-code=%s, status-message=\"%s\"",
468 ippErrorString(response
->request
.status
.status_code
),
469 attr
? attr
->values
[0].string
.text
: ""));
471 _cupsSetError(response
->request
.status
.status_code
,
472 attr
? attr
->values
[0].string
.text
:
473 ippErrorString(response
->request
.status
.status_code
), 0);
475 else if (status
!= HTTP_OK
)
476 _cupsSetHTTPError(status
);
483 * 'cupsReadResponseData()' - Read additional data after the IPP response.
485 * This function is used after cupsGetResponse() to read the PPD or document
486 * files for CUPS_GET_PPD and CUPS_GET_DOCUMENT requests, respectively.
488 * @since CUPS 1.4/Mac OS X 10.6@
491 ssize_t
/* O - Bytes read, 0 on EOF, -1 on error */
492 cupsReadResponseData(
493 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
494 char *buffer
, /* I - Buffer to use */
495 size_t length
) /* I - Number of bytes to read */
498 * Get the default connection as needed...
501 DEBUG_printf(("cupsReadResponseData(http=%p, buffer=%p, "
502 "length=" CUPS_LLFMT
")", http
, buffer
, CUPS_LLCAST length
));
506 _cups_globals_t
*cg
= _cupsGlobals();
507 /* Pointer to library globals */
509 if ((http
= cg
->http
) == NULL
)
511 _cupsSetError(IPP_INTERNAL_ERROR
, _("No active connection"), 1);
517 * Then read from the HTTP connection...
520 return (httpRead2(http
, buffer
, length
));
525 * 'cupsSendRequest()' - Send an IPP request.
527 * Use httpWrite() to write any additional data (document, PPD file, etc.)
528 * for the request, cupsGetResponse() to get the IPP response, and httpRead()
529 * to read any additional data following the response. Only one request can be
530 * sent/queued at a time.
532 * Unlike cupsDoFileRequest(), cupsDoIORequest(), and cupsDoRequest(), the
533 * request is not freed.
535 * @since CUPS 1.4/Mac OS X 10.6@
538 http_status_t
/* O - Initial HTTP status */
539 cupsSendRequest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
540 ipp_t
*request
, /* I - IPP request */
541 const char *resource
, /* I - Resource path */
542 size_t length
) /* I - Length of data to follow or @code CUPS_LENGTH_VARIABLE@ */
544 http_status_t status
; /* Status of HTTP request */
545 int got_status
; /* Did we get the status? */
546 ipp_state_t state
; /* State of IPP processing */
547 http_status_t expect
; /* Expect: header to use */
550 DEBUG_printf(("cupsSendRequest(http=%p, request=%p(%s), resource=\"%s\", "
551 "length=" CUPS_LLFMT
")", http
, request
,
552 request
? ippOpString(request
->request
.op
.operation_id
) : "?",
553 resource
, CUPS_LLCAST length
));
556 * Range check input...
559 if (!request
|| !resource
)
561 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
567 * Get the default connection as needed...
571 if ((http
= _cupsConnect()) == NULL
)
572 return (HTTP_SERVICE_UNAVAILABLE
);
576 * See if we have an auth-info attribute and are communicating over
577 * a non-local link. If so, encrypt the link so that we can pass
578 * the authentication information securely...
581 if (ippFindAttribute(request
, "auth-info", IPP_TAG_TEXT
) &&
582 !httpAddrLocalhost(http
->hostaddr
) && !http
->tls
&&
583 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
))
585 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, NULL
, 0);
586 return (HTTP_SERVICE_UNAVAILABLE
);
588 #endif /* HAVE_SSL */
591 * Reconnect if the last response had a "Connection: close"...
594 if (!strcasecmp(http
->fields
[HTTP_FIELD_CONNECTION
], "close"))
595 if (httpReconnect(http
))
597 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, NULL
, 0);
598 return (HTTP_SERVICE_UNAVAILABLE
);
602 * Loop until we can send the request without authorization problems.
605 expect
= HTTP_CONTINUE
;
609 DEBUG_puts("2cupsSendRequest: Setup...");
612 * Setup the HTTP variables needed...
615 httpClearFields(http
);
616 httpSetLength(http
, length
);
617 httpSetField(http
, HTTP_FIELD_CONTENT_TYPE
, "application/ipp");
618 httpSetField(http
, HTTP_FIELD_AUTHORIZATION
, http
->authstring
);
619 httpSetExpect(http
, expect
);
621 DEBUG_printf(("2cupsSendRequest: authstring=\"%s\"", http
->authstring
));
627 DEBUG_puts("2cupsSendRequest: Sending HTTP POST...");
629 if (httpPost(http
, resource
))
631 if (httpReconnect(http
))
633 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, NULL
, 0);
634 return (HTTP_SERVICE_UNAVAILABLE
);
641 * Send the IPP data...
644 DEBUG_puts("2cupsSendRequest: Writing IPP request...");
646 request
->state
= IPP_IDLE
;
647 status
= HTTP_CONTINUE
;
650 while ((state
= ippWrite(http
, request
)) != IPP_DATA
)
651 if (state
== IPP_ERROR
)
653 else if (httpCheck(http
))
657 if ((status
= httpUpdate(http
)) != HTTP_CONTINUE
)
662 * Wait up to 1 second to get the 100-continue response as needed...
665 if (!got_status
&& expect
== HTTP_CONTINUE
)
667 DEBUG_puts("2cupsSendRequest: Waiting for 100-continue...");
669 if (httpWait(http
, 1000))
670 status
= httpUpdate(http
);
672 else if (httpCheck(http
))
673 status
= httpUpdate(http
);
675 DEBUG_printf(("2cupsSendRequest: status=%d", status
));
678 * Process the current HTTP status...
688 case HTTP_UNAUTHORIZED
:
689 if (!cupsDoAuthentication(http
, "POST", resource
))
691 if (httpReconnect(http
))
693 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, NULL
, 0);
694 return (HTTP_SERVICE_UNAVAILABLE
);
698 status
= HTTP_AUTHORIZATION_CANCELED
;
703 case HTTP_UPGRADE_REQUIRED
:
705 * Flush any error message, reconnect, and then upgrade with
709 if (httpReconnect(http
))
711 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, NULL
, 0);
712 return (HTTP_SERVICE_UNAVAILABLE
);
715 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
718 #endif /* HAVE_SSL */
720 case HTTP_EXPECTATION_FAILED
:
722 * Don't try using the Expect: header the next time around...
725 expect
= (http_status_t
)0;
727 if (httpReconnect(http
))
729 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, NULL
, 0);
730 return (HTTP_SERVICE_UNAVAILABLE
);
736 * Some other error...
746 * 'cupsWriteRequestData()' - Write additional data after an IPP request.
748 * This function is used after @link cupsSendRequest@ to provide a PPD and
749 * after @link cupsStartDocument@ to provide a document file.
751 * @since CUPS 1.4/Mac OS X 10.6@
754 http_status_t
/* O - @code HTTP_CONTINUE@ if OK or HTTP status on error */
755 cupsWriteRequestData(
756 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
757 const char *buffer
, /* I - Bytes to write */
758 size_t length
) /* I - Number of bytes to write */
760 int wused
; /* Previous bytes in buffer */
764 * Get the default connection as needed...
767 DEBUG_printf(("cupsWriteRequestData(http=%p, buffer=%p, "
768 "length=" CUPS_LLFMT
")", http
, buffer
, CUPS_LLCAST length
));
772 _cups_globals_t
*cg
= _cupsGlobals();
773 /* Pointer to library globals */
775 if ((http
= cg
->http
) == NULL
)
777 _cupsSetError(IPP_INTERNAL_ERROR
, _("No active connection"), 1);
783 * Then write to the HTTP connection...
788 if (httpWrite2(http
, buffer
, length
) < 0)
792 * Finally, check if we have any pending data from the server...
795 if (length
>= HTTP_MAX_BUFFER
||
796 http
->wused
< wused
||
797 (wused
> 0 && http
->wused
== length
))
800 * We've written something to the server, so check for response data...
803 if (_httpWait(http
, 0, 1))
804 return (httpUpdate(http
));
807 return (HTTP_CONTINUE
);
812 * '_cupsSetError()' - Set the last IPP status code and status-message.
816 _cupsSetError(ipp_status_t status
, /* I - IPP status code */
817 const char *message
, /* I - status-message value */
818 int localize
) /* I - Localize the message? */
820 _cups_globals_t
*cg
; /* Global data */
823 if (!message
&& errno
)
825 message
= strerror(errno
);
830 cg
->last_error
= status
;
832 if (cg
->last_status_message
)
834 _cupsStrFree(cg
->last_status_message
);
836 cg
->last_status_message
= NULL
;
844 * Get the message catalog...
847 if (!cg
->lang_default
)
848 cg
->lang_default
= cupsLangDefault();
850 cg
->last_status_message
= _cupsStrAlloc(_cupsLangString(cg
->lang_default
,
854 cg
->last_status_message
= _cupsStrAlloc(message
);
857 DEBUG_printf(("4_cupsSetError: last_error=%s, last_status_message=\"%s\"",
858 ippErrorString(cg
->last_error
), cg
->last_status_message
));
863 * '_cupsSetHTTPError()' - Set the last error using the HTTP status.
867 _cupsSetHTTPError(http_status_t status
) /* I - HTTP status code */
871 case HTTP_NOT_FOUND
:
872 _cupsSetError(IPP_NOT_FOUND
, httpStatus(status
), 0);
875 case HTTP_UNAUTHORIZED
:
876 case HTTP_AUTHORIZATION_CANCELED
:
877 _cupsSetError(IPP_NOT_AUTHORIZED
, httpStatus(status
), 0);
880 case HTTP_FORBIDDEN
:
881 _cupsSetError(IPP_FORBIDDEN
, httpStatus(status
), 0);
884 case HTTP_BAD_REQUEST
:
885 _cupsSetError(IPP_BAD_REQUEST
, httpStatus(status
), 0);
888 case HTTP_REQUEST_TOO_LARGE
:
889 _cupsSetError(IPP_REQUEST_VALUE
, httpStatus(status
), 0);
892 case HTTP_NOT_IMPLEMENTED
:
893 _cupsSetError(IPP_OPERATION_NOT_SUPPORTED
, httpStatus(status
), 0);
896 case HTTP_NOT_SUPPORTED
:
897 _cupsSetError(IPP_VERSION_NOT_SUPPORTED
, httpStatus(status
), 0);
901 DEBUG_printf(("4_cupsSetHTTPError: HTTP error %d mapped to "
902 "IPP_SERVICE_UNAVAILABLE!", status
));
903 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, httpStatus(status
), 0);
910 * End of "$Id: request.c 7946 2008-09-16 23:27:54Z mike $".