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 */
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
));
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
));
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
)
266 * Write trailing data to file...
269 while ((bytes
= (int)httpRead2(http
, buffer
, sizeof(buffer
))) > 0)
270 if (write(outfile
, buffer
, bytes
) < bytes
)
276 * Flush any remaining data...
285 * Delete the original request and return the response...
295 * 'cupsDoRequest()' - Do an IPP request.
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.
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 */
307 DEBUG_printf(("cupsDoRequest(http=%p, request=%p(%s), resource=\"%s\")\n",
309 request
? ippOpString(request
->request
.op
.operation_id
) : "?",
310 resource
? resource
: "(null)"));
312 return (cupsDoFileRequest(http
, request
, resource
, NULL
));
317 * 'cupsGetResponse()' - Get a response to an IPP request.
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.
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 */
330 http_status_t status
; /* HTTP status */
331 ipp_state_t state
; /* IPP read state */
332 ipp_t
*response
= NULL
; /* IPP response */
335 DEBUG_printf(("cupsGetReponse(http=%p, resource=\"%s\")\n", http
,
336 resource
? resource
: "(null)"));
339 * Connect to the default server as needed...
343 http
= _cupsConnect();
345 if (!http
|| (http
->state
!= HTTP_POST_RECV
&& http
->state
!= HTTP_POST_SEND
))
349 * Check for an unfinished chunked request...
352 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
355 * Send a 0-length chunk to finish off the request...
358 DEBUG_puts("cupsGetResponse: Finishing chunked POST...");
360 if (httpWrite2(http
, "", 0) < 0)
365 * Wait for a response from the server...
368 DEBUG_puts("cupsGetResponse: Update loop...");
370 while ((status
= httpUpdate(http
)) == HTTP_CONTINUE
)
371 /* Do nothing but update */;
373 DEBUG_printf(("cupsGetResponse: status=%d\n", status
));
375 if (status
== HTTP_OK
)
378 * Get the IPP response...
383 while ((state
= ippRead(http
, response
)) != IPP_DATA
)
384 if (state
== IPP_ERROR
)
387 if (state
== IPP_ERROR
)
390 * Delete the response...
393 DEBUG_puts("cupsGetResponse: IPP read error!");
398 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, strerror(errno
));
401 else if (status
!= HTTP_ERROR
)
404 * Flush any error message...
410 * Then handle encryption and authentication...
413 if (status
== HTTP_UNAUTHORIZED
)
416 * See if we can do authentication...
421 DEBUG_puts("cupsGetResponse: Need authorization...");
423 if ((auth_result
= cupsDoAuthentication(http
, "POST", resource
)) == 0)
425 else if (auth_result
< 0)
426 http
->status
= status
= HTTP_FORBIDDEN
;
430 else if (status
== HTTP_UPGRADE_REQUIRED
)
433 * Force a reconnect with encryption...
436 DEBUG_puts("cupsGetResponse: Need encryption...");
438 if (!httpReconnect(http
))
439 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
441 #endif /* HAVE_SSL */
446 ipp_attribute_t
*attr
; /* status-message attribute */
449 attr
= ippFindAttribute(response
, "status-message", IPP_TAG_TEXT
);
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
: ""));
455 _cupsSetError(response
->request
.status
.status_code
,
456 attr
? attr
->values
[0].string
.text
:
457 ippErrorString(response
->request
.status
.status_code
));
459 else if (status
!= HTTP_OK
)
460 _cupsSetHTTPError(status
);
467 * 'cupsReadResponseData()' - Read additional data after the IPP response.
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.
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 */
482 * Get the default connection as needed...
485 DEBUG_printf(("cupsReadResponseData(http=%p, buffer=%p, "
486 "length=" CUPS_LLFMT
")\n", http
, buffer
, CUPS_LLCAST length
));
490 _cups_globals_t
*cg
= _cupsGlobals();
491 /* Pointer to library globals */
493 if ((http
= cg
->http
) == NULL
)
495 _cupsSetError(IPP_INTERNAL_ERROR
, "No active connection");
501 * Then read from the HTTP connection...
504 return (httpRead2(http
, buffer
, length
));
509 * 'cupsSendRequest()' - Send an IPP request.
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.
516 * Unlike cupsDoFileRequest(), cupsDoIORequest(), and cupsDoRequest(), the
517 * request is not freed.
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@ */
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 */
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
));
540 * Range check input...
543 if (!request
|| !resource
)
545 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
));
551 * Get the default connection as needed...
555 if ((http
= _cupsConnect()) == NULL
)
556 return (HTTP_SERVICE_UNAVAILABLE
);
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...
565 if (ippFindAttribute(request
, "auth-info", IPP_TAG_TEXT
) &&
566 !httpAddrLocalhost(http
->hostaddr
) && !http
->tls
&&
567 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
))
569 #endif /* HAVE_SSL */
572 * Loop until we can send the request without authorization problems.
576 expect
= HTTP_CONTINUE
;
580 DEBUG_puts("cupsSendRequest: Setup...");
583 * Setup the HTTP variables needed...
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
);
592 DEBUG_printf(("cupsSendRequest: authstring=\"%s\"\n", http
->authstring
));
598 DEBUG_puts("cupsSendRequest: Sending HTTP POST...");
600 if (httpPost(http
, resource
))
602 if (httpReconnect(http
))
609 * Send the IPP data...
612 DEBUG_puts("cupsSendRequest: Writing IPP request...");
614 request
->state
= IPP_IDLE
;
615 status
= HTTP_CONTINUE
;
618 while ((state
= ippWrite(http
, request
)) != IPP_DATA
)
619 if (state
== IPP_ERROR
)
621 else if (httpCheck(http
))
625 if ((status
= httpUpdate(http
)) != HTTP_CONTINUE
)
630 * Wait up to 1 second to get the 100-continue response as needed...
633 if (!got_status
&& expect
== HTTP_CONTINUE
)
635 DEBUG_puts("cupsSendRequest: Waiting for 100-continue...");
637 if (httpWait(http
, 1000))
638 status
= httpUpdate(http
);
640 status
= HTTP_EXPECTATION_FAILED
;
642 else if (httpCheck(http
))
643 status
= httpUpdate(http
);
645 DEBUG_printf(("cupsSendRequest: status=%d\n", status
));
648 * Process the current HTTP status...
658 case HTTP_UNAUTHORIZED
:
659 if (!cupsDoAuthentication(http
, "POST", resource
))
660 if (httpReconnect(http
))
666 case HTTP_UPGRADE_REQUIRED
:
668 * Flush any error message, reconnect, and then upgrade with
672 if (httpReconnect(http
))
675 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
678 #endif /* HAVE_SSL */
680 case HTTP_EXPECTATION_FAILED
:
682 * Don't try using the Expect: header the next time around...
685 expect
= (http_status_t
)0;
690 * Some other error...
700 * 'cupsWriteRequestData()' - Write additional data after an IPP request.
702 * This function is used after @link cupsSendRequest@ to provide a PPD and
703 * after @link cupsStartDocument@ to provide a document file.
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 */
715 * Get the default connection as needed...
718 DEBUG_printf(("cupsWriteRequestData(http=%p, buffer=%p, "
719 "length=" CUPS_LLFMT
")\n", http
, buffer
, CUPS_LLCAST length
));
723 _cups_globals_t
*cg
= _cupsGlobals();
724 /* Pointer to library globals */
726 if ((http
= cg
->http
) == NULL
)
728 _cupsSetError(IPP_INTERNAL_ERROR
, "No active connection");
734 * Then write to the HTTP connection...
737 if (httpWrite2(http
, buffer
, length
) < 0)
741 * Finally, check if we have any pending data from the server...
745 return (httpUpdate(http
));
747 return (HTTP_CONTINUE
);
752 * '_cupsSetError()' - Set the last IPP status code and status-message.
756 _cupsSetError(ipp_status_t status
, /* I - IPP status code */
757 const char *message
) /* I - status-message value */
759 _cups_globals_t
*cg
; /* Global data */
763 cg
->last_error
= status
;
765 if (cg
->last_status_message
)
767 free(cg
->last_status_message
);
769 cg
->last_status_message
= NULL
;
773 cg
->last_status_message
= strdup(message
);
775 DEBUG_printf(("_cupsSetError: last_error=%s, last_status_message=\"%s\"\n",
776 ippErrorString(cg
->last_error
), message
? message
: ""));
781 * '_cupsSetHTTPError()' - Set the last error using the HTTP status.
785 _cupsSetHTTPError(http_status_t status
) /* I - HTTP status code */
789 case HTTP_NOT_FOUND
:
790 _cupsSetError(IPP_NOT_FOUND
, httpStatus(status
));
793 case HTTP_UNAUTHORIZED
:
794 _cupsSetError(IPP_NOT_AUTHORIZED
, httpStatus(status
));
797 case HTTP_FORBIDDEN
:
798 _cupsSetError(IPP_FORBIDDEN
, httpStatus(status
));
801 case HTTP_BAD_REQUEST
:
802 _cupsSetError(IPP_BAD_REQUEST
, httpStatus(status
));
805 case HTTP_REQUEST_TOO_LARGE
:
806 _cupsSetError(IPP_REQUEST_VALUE
, httpStatus(status
));
809 case HTTP_NOT_IMPLEMENTED
:
810 _cupsSetError(IPP_OPERATION_NOT_SUPPORTED
, httpStatus(status
));
813 case HTTP_NOT_SUPPORTED
:
814 _cupsSetError(IPP_VERSION_NOT_SUPPORTED
, httpStatus(status
));
818 DEBUG_printf(("HTTP error %d mapped to IPP_SERVICE_UNAVAILABLE!\n",
820 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, httpStatus(status
));
827 * End of "$Id: request.c 6879 2007-08-29 20:26:50Z mike $".