2 * "$Id: request.c 6879 2007-08-29 20:26:50Z 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 */
70 if ((infile
= open(filename
, O_RDONLY
| O_BINARY
)) < 0)
73 * Can't get file information!
76 _cupsSetError(errno
== ENOENT
? IPP_NOT_FOUND
: IPP_NOT_AUTHORIZED
,
87 response
= cupsDoIORequest(http
, request
, resource
, infile
, -1);
97 * 'cupsDoIORequest()' - Do an IPP request with file descriptors.
99 * This function sends the IPP request to the specified server, retrying
100 * and authenticating as necessary. The request is freed with ippDelete()
101 * after receiving a valid IPP response.
103 * If "infile" is a valid file descriptor, cupsDoIORequest() copies
104 * all of the data from the file after the IPP request message.
106 * If "outfile" is a valid file descriptor, cupsDoIORequest() copies
107 * all of the data after the IPP response message to the file.
112 ipp_t
* /* O - Response data */
113 cupsDoIORequest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
114 ipp_t
*request
, /* I - IPP request */
115 const char *resource
, /* I - HTTP resource for POST */
116 int infile
, /* I - File to read from or -1 for none */
117 int outfile
) /* I - File to write to or -1 for none */
119 ipp_t
*response
= NULL
; /* IPP response data */
120 size_t length
= 0; /* Content-Length value */
121 http_status_t status
; /* Status of HTTP request */
122 struct stat fileinfo
; /* File information */
123 int bytes
; /* Number of bytes read/written */
124 char buffer
[32768]; /* Output buffer */
127 DEBUG_printf(("cupsDoIORequest(http=%p, request=%p, resource=\"%s\""
128 "infile=%d, outfile=%d)\n", http
, request
,
129 resource
? resource
: "(null)", infile
, outfile
));
132 * Range check input...
135 if (!request
|| !resource
)
139 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
));
145 * Get the default connection as needed...
149 http
= _cupsConnect();
152 * See if we have a file to send...
157 if (fstat(infile
, &fileinfo
))
160 * Can't get file information!
163 _cupsSetError(errno
== EBADF
? IPP_NOT_FOUND
: IPP_NOT_AUTHORIZED
,
172 if (fileinfo
.st_mode
& _S_IFDIR
)
174 if (S_ISDIR(fileinfo
.st_mode
))
178 * Can't send a directory...
183 _cupsSetError(IPP_NOT_POSSIBLE
, strerror(EISDIR
));
189 if (!S_ISREG(fileinfo
.st_mode
))
190 length
= 0; /* Chunk when piping */
193 length
= ippLength(request
) + fileinfo
.st_size
;
196 length
= ippLength(request
);
199 * Loop until we can send the request without authorization problems.
202 while (response
== NULL
)
204 DEBUG_puts("cupsDoFileRequest: setup...");
207 * Send the request...
210 status
= cupsSendRequest(http
, request
, resource
, length
);
212 DEBUG_printf(("cupsDoFileRequest: status=%d\n", status
));
214 if (status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
&& infile
>= 0)
216 DEBUG_puts("cupsDoFileRequest: file write...");
219 * Send the file with the request...
223 if (S_ISREG(fileinfo
.st_mode
))
225 lseek(infile
, 0, SEEK_SET
);
227 while ((bytes
= (int)read(infile
, buffer
, sizeof(buffer
))) > 0)
231 if ((status
= httpUpdate(http
)) != HTTP_CONTINUE
)
235 if (httpWrite2(http
, buffer
, bytes
) < bytes
)
241 * Get the server's response...
244 if (status
== HTTP_CONTINUE
|| status
== HTTP_OK
)
246 response
= cupsGetResponse(http
, resource
);
247 status
= http
->status
;
250 if (status
== HTTP_FORBIDDEN
)
258 * Write trailing data to file...
261 while ((bytes
= (int)httpRead2(http
, buffer
, sizeof(buffer
))) > 0)
262 if (write(outfile
, buffer
, bytes
) < bytes
)
268 * Flush any remaining data...
277 * Delete the original request and return the response...
287 * 'cupsDoRequest()' - Do an IPP request.
289 * This function sends the IPP request to the specified server, retrying
290 * and authenticating as necessary. The request is freed with ippDelete()
291 * after receiving a valid IPP response.
294 ipp_t
* /* O - Response data */
295 cupsDoRequest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
296 ipp_t
*request
, /* I - IPP request */
297 const char *resource
) /* I - HTTP resource for POST */
299 return (cupsDoFileRequest(http
, request
, resource
, NULL
));
304 * 'cupsGetResponse()' - Get a response to an IPP request.
306 * Use this function to get the response for an IPP request sent using
307 * cupsSendDocument() or cupsSendRequest(). For requests that return
308 * additional data, use httpRead() after getting a successful response.
313 ipp_t
* /* O - Response or @code NULL@ on HTTP error */
314 cupsGetResponse(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
315 const char *resource
) /* I - HTTP resource for POST */
317 http_status_t status
; /* HTTP status */
318 ipp_state_t state
; /* IPP read state */
319 ipp_t
*response
= NULL
; /* IPP response */
322 DEBUG_printf(("cupsGetReponse(http=%p)\n", http
));
325 * Connect to the default server as needed...
329 http
= _cupsConnect();
331 if (!http
|| (http
->state
!= HTTP_POST_RECV
&& http
->state
!= HTTP_POST_SEND
))
335 * Check for an unfinished chunked request...
338 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
341 * Send a 0-length chunk to finish off the request...
344 DEBUG_puts("cupsGetResponse: Finishing chunked POST...");
346 if (httpWrite2(http
, "", 0) < 0)
351 * Wait for a response from the server...
354 DEBUG_puts("cupsGetResponse: update...");
356 while ((status
= httpUpdate(http
)) == HTTP_CONTINUE
)
357 /* Do nothing but update */;
359 DEBUG_printf(("cupsGetResponse: status = %d\n", status
));
361 if (status
== HTTP_OK
)
364 * Get the IPP response...
369 while ((state
= ippRead(http
, response
)) != IPP_DATA
)
370 if (state
== IPP_ERROR
)
373 if (state
== IPP_ERROR
)
376 * Delete the response...
379 DEBUG_puts("IPP read error!");
384 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, strerror(errno
));
387 else if (status
!= HTTP_ERROR
)
390 * Flush any error message...
396 * Then handle encryption and authentication...
399 if (status
== HTTP_UNAUTHORIZED
)
402 * See if we can do authentication...
407 DEBUG_puts("cupsGetResponse: Need authorization...");
409 if ((auth_result
=cupsDoAuthentication(http
, "POST", resource
)) == 0)
411 else if (auth_result
< 0)
412 http
->status
= status
= HTTP_FORBIDDEN
;
416 else if (status
== HTTP_UPGRADE_REQUIRED
)
419 * Force a reconnect with encryption...
422 DEBUG_puts("cupsGetResponse: Need encryption...");
424 if (!httpReconnect(http
))
425 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
427 #endif /* HAVE_SSL */
432 ipp_attribute_t
*attr
; /* status-message attribute */
435 attr
= ippFindAttribute(response
, "status-message", IPP_TAG_TEXT
);
437 _cupsSetError(response
->request
.status
.status_code
,
438 attr
? attr
->values
[0].string
.text
:
439 ippErrorString(response
->request
.status
.status_code
));
441 else if (status
!= HTTP_OK
)
442 _cupsSetHTTPError(status
);
449 * 'cupsReadResponseData()' - Read additional data after the IPP response.
451 * This function is used after cupsGetResponse() to read the PPD or document
452 * files for CUPS_GET_PPD and CUPS_GET_DOCUMENT requests, respectively.
457 ssize_t
/* O - Bytes read, 0 on EOF, -1 on error */
458 cupsReadResponseData(
459 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
460 char *buffer
, /* I - Buffer to use */
461 size_t length
) /* I - Number of bytes to read */
464 * Get the default connection as needed...
469 _cups_globals_t
*cg
= _cupsGlobals();
470 /* Pointer to library globals */
472 if ((http
= cg
->http
) == NULL
)
474 _cupsSetError(IPP_INTERNAL_ERROR
, "No active connection");
480 * Then read from the HTTP connection...
483 return (httpRead2(http
, buffer
, length
));
488 * 'cupsSendRequest()' - Send an IPP request.
490 * Use httpWrite() to write any additional data (document, PPD file, etc.)
491 * for the request, cupsGetResponse() to get the IPP response, and httpRead()
492 * to read any additional data following the response. Only one request can be
493 * sent/queued at a time.
495 * Unlike cupsDoFileRequest(), cupsDoIORequest(), and cupsDoRequest(), the
496 * request is not freed.
501 http_status_t
/* O - Initial HTTP status */
502 cupsSendRequest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
503 ipp_t
*request
, /* I - IPP request */
504 const char *resource
, /* I - Resource path */
505 size_t length
) /* I - Length of data to follow or CUPS_LENGTH_VARIABLE */
507 http_status_t status
; /* Status of HTTP request */
508 int got_status
; /* Did we get the status? */
509 ipp_state_t state
; /* State of IPP processing */
510 http_status_t expect
; /* Expect: header to use */
513 DEBUG_printf(("cupsSendRequest(http=%p, request=%p, resource=\"%s\", "
514 "length=" CUPS_LLFMT
")\n", http
, request
,
515 resource
? resource
: "(null)", CUPS_LLCAST length
));
518 * Range check input...
521 if (!request
|| !resource
)
523 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
));
529 * Get the default connection as needed...
533 http
= _cupsConnect();
537 * See if we have an auth-info attribute and are communicating over
538 * a non-local link. If so, encrypt the link so that we can pass
539 * the authentication information securely...
542 if (ippFindAttribute(request
, "auth-info", IPP_TAG_TEXT
) &&
543 !httpAddrLocalhost(http
->hostaddr
) && !http
->tls
&&
544 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
))
546 #endif /* HAVE_SSL */
549 * Loop until we can send the request without authorization problems.
553 expect
= HTTP_CONTINUE
;
557 DEBUG_puts("cupsSendRequest: setup...");
560 * Setup the HTTP variables needed...
563 httpClearFields(http
);
564 httpSetLength(http
, length
);
565 httpSetField(http
, HTTP_FIELD_CONTENT_TYPE
, "application/ipp");
566 httpSetField(http
, HTTP_FIELD_AUTHORIZATION
, http
->authstring
);
567 httpSetExpect(http
, expect
);
569 DEBUG_printf(("cupsSendRequest: authstring=\"%s\"\n", http
->authstring
));
575 DEBUG_puts("cupsSendRequest: post...");
577 if (httpPost(http
, resource
))
579 if (httpReconnect(http
))
586 * Send the IPP data...
589 DEBUG_puts("cupsSendRequest: ipp write...");
591 request
->state
= IPP_IDLE
;
592 status
= HTTP_CONTINUE
;
595 while ((state
= ippWrite(http
, request
)) != IPP_DATA
)
596 if (state
== IPP_ERROR
)
598 else if (httpCheck(http
))
602 if ((status
= httpUpdate(http
)) != HTTP_CONTINUE
)
607 * Wait up to 1 second to get the 100-continue response as needed...
610 if (!got_status
&& expect
== HTTP_CONTINUE
)
612 if (httpWait(http
, 1000))
613 status
= httpUpdate(http
);
615 else if (httpCheck(http
))
616 status
= httpUpdate(http
);
619 * Process the current HTTP status...
629 case HTTP_UNAUTHORIZED
:
630 if (!cupsDoAuthentication(http
, "POST", resource
))
631 if (httpReconnect(http
))
637 case HTTP_UPGRADE_REQUIRED
:
639 * Flush any error message, reconnect, and then upgrade with
643 if (httpReconnect(http
))
646 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
649 #endif /* HAVE_SSL */
651 case HTTP_EXPECTATION_FAILED
:
653 * Don't try using the Expect: header the next time around...
656 expect
= (http_status_t
)0;
660 * Some other error...
670 * 'cupsWriteRequestData()' - Write additional data after an IPP request.
672 * This function is used after @link cupsSendRequest@ to provide a PPD and
673 * after @link cupsStartDocument@ to provide a document file.
678 http_status_t
/* O - @code HTTP_CONTINUE@ if OK or HTTP status on error */
679 cupsWriteRequestData(
680 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
681 const char *buffer
, /* I - Bytes to write */
682 size_t length
) /* I - Number of bytes to write */
685 * Get the default connection as needed...
690 _cups_globals_t
*cg
= _cupsGlobals();
691 /* Pointer to library globals */
693 if ((http
= cg
->http
) == NULL
)
695 _cupsSetError(IPP_INTERNAL_ERROR
, "No active connection");
701 * Then write to the HTTP connection...
704 if (httpWrite2(http
, buffer
, length
) < 0)
708 * Finally, check if we have any pending data from the server...
712 return (httpUpdate(http
));
714 return (HTTP_CONTINUE
);
719 * '_cupsSetError()' - Set the last IPP status code and status-message.
723 _cupsSetError(ipp_status_t status
, /* I - IPP status code */
724 const char *message
) /* I - status-message value */
726 _cups_globals_t
*cg
; /* Global data */
730 cg
->last_error
= status
;
732 if (cg
->last_status_message
)
734 free(cg
->last_status_message
);
736 cg
->last_status_message
= NULL
;
740 cg
->last_status_message
= strdup(message
);
745 * '_cupsSetHTTPError()' - Set the last error using the HTTP status.
749 _cupsSetHTTPError(http_status_t status
) /* I - HTTP status code */
753 case HTTP_NOT_FOUND
:
754 _cupsSetError(IPP_NOT_FOUND
, httpStatus(status
));
757 case HTTP_UNAUTHORIZED
:
758 _cupsSetError(IPP_NOT_AUTHORIZED
, httpStatus(status
));
761 case HTTP_FORBIDDEN
:
762 _cupsSetError(IPP_FORBIDDEN
, httpStatus(status
));
765 case HTTP_BAD_REQUEST
:
766 _cupsSetError(IPP_BAD_REQUEST
, httpStatus(status
));
769 case HTTP_REQUEST_TOO_LARGE
:
770 _cupsSetError(IPP_REQUEST_VALUE
, httpStatus(status
));
773 case HTTP_NOT_IMPLEMENTED
:
774 _cupsSetError(IPP_OPERATION_NOT_SUPPORTED
, httpStatus(status
));
777 case HTTP_NOT_SUPPORTED
:
778 _cupsSetError(IPP_VERSION_NOT_SUPPORTED
, httpStatus(status
));
782 DEBUG_printf(("HTTP error %d mapped to IPP_SERVICE_UNAVAILABLE!\n",
784 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, httpStatus(status
));
791 * End of "$Id: request.c 6879 2007-08-29 20:26:50Z mike $".