2 * "$Id: request.c 7460 2008-04-16 02:19: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
);
207 * Loop until we can send the request without authorization problems.
210 while (response
== NULL
)
212 DEBUG_puts("cupsDoFileRequest: setup...");
215 * Send the request...
218 status
= cupsSendRequest(http
, request
, resource
, length
);
220 DEBUG_printf(("cupsDoFileRequest: status=%d\n", status
));
222 if (status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
&& infile
>= 0)
224 DEBUG_puts("cupsDoFileRequest: file write...");
227 * Send the file with the request...
231 if (S_ISREG(fileinfo
.st_mode
))
233 lseek(infile
, 0, SEEK_SET
);
235 while ((bytes
= (int)read(infile
, buffer
, sizeof(buffer
))) > 0)
239 if ((status
= httpUpdate(http
)) != HTTP_CONTINUE
)
243 if (httpWrite2(http
, buffer
, bytes
) < bytes
)
249 * Get the server's response...
252 if (status
== HTTP_CONTINUE
|| status
== HTTP_OK
)
254 response
= cupsGetResponse(http
, resource
);
255 status
= http
->status
;
258 if (status
== HTTP_FORBIDDEN
|| status
== HTTP_ERROR
||
259 status
== HTTP_SERVICE_UNAVAILABLE
)
261 _cupsSetHTTPError(status
);
270 * Write trailing data to file...
273 while ((bytes
= (int)httpRead2(http
, buffer
, sizeof(buffer
))) > 0)
274 if (write(outfile
, buffer
, bytes
) < bytes
)
280 * Flush any remaining data...
289 * Delete the original request and return the response...
299 * 'cupsDoRequest()' - Do an IPP request.
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.
306 ipp_t
* /* O - Response data */
307 cupsDoRequest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
308 ipp_t
*request
, /* I - IPP request */
309 const char *resource
) /* I - HTTP resource for POST */
311 DEBUG_printf(("cupsDoRequest(http=%p, request=%p(%s), resource=\"%s\")\n",
313 request
? ippOpString(request
->request
.op
.operation_id
) : "?",
314 resource
? resource
: "(null)"));
316 return (cupsDoFileRequest(http
, request
, resource
, NULL
));
321 * 'cupsGetResponse()' - Get a response to an IPP request.
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.
330 ipp_t
* /* O - Response or @code NULL@ on HTTP error */
331 cupsGetResponse(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
332 const char *resource
) /* I - HTTP resource for POST */
334 http_status_t status
; /* HTTP status */
335 ipp_state_t state
; /* IPP read state */
336 ipp_t
*response
= NULL
; /* IPP response */
339 DEBUG_printf(("cupsGetReponse(http=%p, resource=\"%s\")\n", http
,
340 resource
? resource
: "(null)"));
343 * Connect to the default server as needed...
347 http
= _cupsConnect();
349 if (!http
|| (http
->state
!= HTTP_POST_RECV
&& http
->state
!= HTTP_POST_SEND
))
353 * Check for an unfinished chunked request...
356 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
359 * Send a 0-length chunk to finish off the request...
362 DEBUG_puts("cupsGetResponse: Finishing chunked POST...");
364 if (httpWrite2(http
, "", 0) < 0)
369 * Wait for a response from the server...
372 DEBUG_printf(("cupsGetResponse: Update loop, http->status=%d...\n",
375 status
= http
->status
;
376 while (status
== HTTP_CONTINUE
)
377 status
= httpUpdate(http
);
379 DEBUG_printf(("cupsGetResponse: status=%d\n", status
));
381 if (status
== HTTP_OK
)
384 * Get the IPP response...
389 while ((state
= ippRead(http
, response
)) != IPP_DATA
)
390 if (state
== IPP_ERROR
)
393 if (state
== IPP_ERROR
)
396 * Delete the response...
399 DEBUG_puts("cupsGetResponse: IPP read error!");
404 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, NULL
, 0);
407 else if (status
!= HTTP_ERROR
)
410 * Flush any error message...
416 * Then handle encryption and authentication...
419 if (status
== HTTP_UNAUTHORIZED
)
422 * See if we can do authentication...
427 DEBUG_puts("cupsGetResponse: Need authorization...");
429 if ((auth_result
= cupsDoAuthentication(http
, "POST", resource
)) == 0)
431 else if (auth_result
< 0)
432 http
->status
= status
= HTTP_FORBIDDEN
;
436 else if (status
== HTTP_UPGRADE_REQUIRED
)
439 * Force a reconnect with encryption...
442 DEBUG_puts("cupsGetResponse: Need encryption...");
444 if (!httpReconnect(http
))
445 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
447 #endif /* HAVE_SSL */
452 ipp_attribute_t
*attr
; /* status-message attribute */
455 attr
= ippFindAttribute(response
, "status-message", IPP_TAG_TEXT
);
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
: ""));
461 _cupsSetError(response
->request
.status
.status_code
,
462 attr
? attr
->values
[0].string
.text
:
463 ippErrorString(response
->request
.status
.status_code
), 0);
465 else if (status
!= HTTP_OK
)
466 _cupsSetHTTPError(status
);
473 * 'cupsReadResponseData()' - Read additional data after the IPP response.
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.
481 ssize_t
/* O - Bytes read, 0 on EOF, -1 on error */
482 cupsReadResponseData(
483 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
484 char *buffer
, /* I - Buffer to use */
485 size_t length
) /* I - Number of bytes to read */
488 * Get the default connection as needed...
491 DEBUG_printf(("cupsReadResponseData(http=%p, buffer=%p, "
492 "length=" CUPS_LLFMT
")\n", http
, buffer
, CUPS_LLCAST length
));
496 _cups_globals_t
*cg
= _cupsGlobals();
497 /* Pointer to library globals */
499 if ((http
= cg
->http
) == NULL
)
501 _cupsSetError(IPP_INTERNAL_ERROR
, _("No active connection"), 1);
507 * Then read from the HTTP connection...
510 return (httpRead2(http
, buffer
, length
));
515 * 'cupsSendRequest()' - Send an IPP request.
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.
522 * Unlike cupsDoFileRequest(), cupsDoIORequest(), and cupsDoRequest(), the
523 * request is not freed.
528 http_status_t
/* O - Initial HTTP status */
529 cupsSendRequest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
530 ipp_t
*request
, /* I - IPP request */
531 const char *resource
, /* I - Resource path */
532 size_t length
) /* I - Length of data to follow or @code CUPS_LENGTH_VARIABLE@ */
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 */
540 DEBUG_printf(("cupsSendRequest(http=%p, request=%p(%s), resource=\"%s\", "
541 "length=" CUPS_LLFMT
")\n", http
, request
,
542 request
? ippOpString(request
->request
.op
.operation_id
) : "?",
543 resource
? resource
: "(null)", CUPS_LLCAST length
));
546 * Range check input...
549 if (!request
|| !resource
)
551 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
557 * Get the default connection as needed...
561 if ((http
= _cupsConnect()) == NULL
)
562 return (HTTP_SERVICE_UNAVAILABLE
);
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...
571 if (ippFindAttribute(request
, "auth-info", IPP_TAG_TEXT
) &&
572 !httpAddrLocalhost(http
->hostaddr
) && !http
->tls
&&
573 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
))
574 return (HTTP_SERVICE_UNAVAILABLE
);
575 #endif /* HAVE_SSL */
578 * Reconnect if the last response had a "Connection: close"...
581 if (!strcasecmp(http
->fields
[HTTP_FIELD_CONNECTION
], "close"))
582 if (httpReconnect(http
))
583 return (HTTP_SERVICE_UNAVAILABLE
);
586 * Loop until we can send the request without authorization problems.
589 expect
= HTTP_CONTINUE
;
593 DEBUG_puts("cupsSendRequest: Setup...");
596 * Setup the HTTP variables needed...
599 httpClearFields(http
);
600 httpSetLength(http
, length
);
601 httpSetField(http
, HTTP_FIELD_CONTENT_TYPE
, "application/ipp");
602 httpSetField(http
, HTTP_FIELD_AUTHORIZATION
, http
->authstring
);
603 httpSetExpect(http
, expect
);
605 DEBUG_printf(("cupsSendRequest: authstring=\"%s\"\n", http
->authstring
));
611 DEBUG_puts("cupsSendRequest: Sending HTTP POST...");
613 if (httpPost(http
, resource
))
615 if (httpReconnect(http
))
616 return (HTTP_SERVICE_UNAVAILABLE
);
622 * Send the IPP data...
625 DEBUG_puts("cupsSendRequest: Writing IPP request...");
627 request
->state
= IPP_IDLE
;
628 status
= HTTP_CONTINUE
;
631 while ((state
= ippWrite(http
, request
)) != IPP_DATA
)
632 if (state
== IPP_ERROR
)
634 else if (httpCheck(http
))
638 if ((status
= httpUpdate(http
)) != HTTP_CONTINUE
)
643 * Wait up to 1 second to get the 100-continue response as needed...
646 if (!got_status
&& expect
== HTTP_CONTINUE
)
648 DEBUG_puts("cupsSendRequest: Waiting for 100-continue...");
650 if (httpWait(http
, 1000))
651 status
= httpUpdate(http
);
653 status
= HTTP_EXPECTATION_FAILED
;
655 else if (httpCheck(http
))
656 status
= httpUpdate(http
);
658 DEBUG_printf(("cupsSendRequest: status=%d\n", status
));
661 * Process the current HTTP status...
671 case HTTP_UNAUTHORIZED
:
672 if (!cupsDoAuthentication(http
, "POST", resource
))
673 if (httpReconnect(http
))
679 case HTTP_UPGRADE_REQUIRED
:
681 * Flush any error message, reconnect, and then upgrade with
685 if (httpReconnect(http
))
688 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
691 #endif /* HAVE_SSL */
693 case HTTP_EXPECTATION_FAILED
:
695 * Don't try using the Expect: header the next time around...
698 expect
= (http_status_t
)0;
703 * Some other error...
713 * 'cupsWriteRequestData()' - Write additional data after an IPP request.
715 * This function is used after @link cupsSendRequest@ to provide a PPD and
716 * after @link cupsStartDocument@ to provide a document file.
721 http_status_t
/* O - @code HTTP_CONTINUE@ if OK or HTTP status on error */
722 cupsWriteRequestData(
723 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
724 const char *buffer
, /* I - Bytes to write */
725 size_t length
) /* I - Number of bytes to write */
728 * Get the default connection as needed...
731 DEBUG_printf(("cupsWriteRequestData(http=%p, buffer=%p, "
732 "length=" CUPS_LLFMT
")\n", http
, buffer
, CUPS_LLCAST length
));
736 _cups_globals_t
*cg
= _cupsGlobals();
737 /* Pointer to library globals */
739 if ((http
= cg
->http
) == NULL
)
741 _cupsSetError(IPP_INTERNAL_ERROR
, _("No active connection"), 1);
747 * Then write to the HTTP connection...
750 if (httpWrite2(http
, buffer
, length
) < 0)
754 * Finally, check if we have any pending data from the server...
758 return (httpUpdate(http
));
760 return (HTTP_CONTINUE
);
765 * '_cupsSetError()' - Set the last IPP status code and status-message.
769 _cupsSetError(ipp_status_t status
, /* I - IPP status code */
770 const char *message
, /* I - status-message value */
771 int localize
) /* I - Localize the message? */
773 _cups_globals_t
*cg
; /* Global data */
776 if (!message
&& errno
)
778 message
= strerror(errno
);
783 cg
->last_error
= status
;
785 if (cg
->last_status_message
)
787 _cupsStrFree(cg
->last_status_message
);
789 cg
->last_status_message
= NULL
;
797 * Get the message catalog...
800 if (!cg
->lang_default
)
801 cg
->lang_default
= cupsLangDefault();
803 cg
->last_status_message
= _cupsStrAlloc(_cupsLangString(cg
->lang_default
,
807 cg
->last_status_message
= _cupsStrAlloc(message
);
810 DEBUG_printf(("_cupsSetError: last_error=%s, last_status_message=\"%s\"\n",
811 ippErrorString(cg
->last_error
),
812 cg
->last_status_message
? cg
->last_status_message
: ""));
817 * '_cupsSetHTTPError()' - Set the last error using the HTTP status.
821 _cupsSetHTTPError(http_status_t status
) /* I - HTTP status code */
825 case HTTP_NOT_FOUND
:
826 _cupsSetError(IPP_NOT_FOUND
, httpStatus(status
), 0);
829 case HTTP_UNAUTHORIZED
:
830 _cupsSetError(IPP_NOT_AUTHORIZED
, httpStatus(status
), 0);
833 case HTTP_FORBIDDEN
:
834 _cupsSetError(IPP_FORBIDDEN
, httpStatus(status
), 0);
837 case HTTP_BAD_REQUEST
:
838 _cupsSetError(IPP_BAD_REQUEST
, httpStatus(status
), 0);
841 case HTTP_REQUEST_TOO_LARGE
:
842 _cupsSetError(IPP_REQUEST_VALUE
, httpStatus(status
), 0);
845 case HTTP_NOT_IMPLEMENTED
:
846 _cupsSetError(IPP_OPERATION_NOT_SUPPORTED
, httpStatus(status
), 0);
849 case HTTP_NOT_SUPPORTED
:
850 _cupsSetError(IPP_VERSION_NOT_SUPPORTED
, httpStatus(status
), 0);
854 DEBUG_printf(("HTTP error %d mapped to IPP_SERVICE_UNAVAILABLE!\n",
856 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, httpStatus(status
), 0);
863 * End of "$Id: request.c 7460 2008-04-16 02:19:54Z mike $".