2 * "$Id: request.c 6712 2007-07-24 00:13:05Z mike $"
4 * IPP utilities for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 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 * cupsDoRequest() - Do an IPP request.
21 * _cupsSetError() - Set the last IPP status code and status-message.
22 * _cupsSetHTTPError() - Set the last error using the HTTP status.
26 * Include necessary headers...
35 #if defined(WIN32) || defined(__EMX__)
39 #endif /* WIN32 || __EMX__ */
46 * 'cupsDoFileRequest()' - Do an IPP request with a file.
48 * This function sends the IPP request to the specified server, retrying
49 * and authenticating as necessary. The request is freed with ippDelete()
50 * after receiving a valid IPP response.
53 ipp_t
* /* O - Response data */
54 cupsDoFileRequest(http_t
*http
, /* I - HTTP connection to server */
55 ipp_t
*request
, /* I - IPP request */
56 const char *resource
, /* I - HTTP resource for POST */
57 const char *filename
) /* I - File to send or NULL for none */
59 ipp_t
*response
; /* IPP response data */
60 int infile
; /* Input file */
65 if ((infile
= open(filename
, O_RDONLY
| O_BINARY
)) < 0)
68 * Can't get file information!
71 _cupsSetError(errno
== ENOENT
? IPP_NOT_FOUND
: IPP_NOT_AUTHORIZED
,
82 response
= cupsDoIORequest(http
, request
, resource
, infile
, -1);
92 * 'cupsDoIORequest()' - Do an IPP request with file descriptors.
94 * This function sends the IPP request to the specified server, retrying
95 * and authenticating as necessary. The request is freed with ippDelete()
96 * after receiving a valid IPP response.
98 * If "infile" is a valid file descriptor, cupsDoIORequest() copies
99 * all of the data from the file after the IPP request message.
101 * If "outfile" is a valid file descriptor, cupsDoIORequest() copies
102 * all of the data after the IPP response message to the file.
107 ipp_t
* /* O - Response data */
108 cupsDoIORequest(http_t
*http
, /* I - HTTP connection to server */
109 ipp_t
*request
, /* I - IPP request */
110 const char *resource
, /* I - HTTP resource for POST */
111 int infile
, /* I - File to read from or -1 for none */
112 int outfile
) /* I - File to write to or -1 for none */
114 ipp_t
*response
; /* IPP response data */
115 size_t length
; /* Content-Length value */
116 http_status_t status
; /* Status of HTTP request */
117 int got_status
; /* Did we get the status? */
118 ipp_state_t state
; /* State of IPP processing */
119 struct stat fileinfo
; /* File information */
120 int bytes
; /* Number of bytes read/written */
121 char buffer
[32768]; /* Output buffer */
122 http_status_t expect
; /* Expect: header to use */
125 DEBUG_printf(("cupsDoFileRequest(%p, %p, \'%s\', \'%s\')\n",
126 http
, request
, resource
? resource
: "(null)",
127 filename
? filename
: "(null)"));
129 if (http
== NULL
|| request
== NULL
|| resource
== NULL
)
134 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
140 * See if we have a file to send...
145 if (fstat(infile
, &fileinfo
))
148 * Can't get file information!
151 _cupsSetError(errno
== ENOENT
? IPP_NOT_FOUND
: IPP_NOT_AUTHORIZED
,
160 if (fileinfo
.st_mode
& _S_IFDIR
)
162 if (S_ISDIR(fileinfo
.st_mode
))
166 * Can't send a directory...
171 _cupsSetError(IPP_NOT_POSSIBLE
, strerror(EISDIR
));
179 * See if we have an auth-info attribute and are communicating over
180 * a non-local link. If so, encrypt the link so that we can pass
181 * the authentication information securely...
184 if (ippFindAttribute(request
, "auth-info", IPP_TAG_TEXT
) &&
185 !httpAddrLocalhost(http
->hostaddr
) && !http
->tls
&&
186 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
))
188 #endif /* HAVE_SSL */
191 * Loop until we can send the request without authorization problems.
196 expect
= HTTP_CONTINUE
;
198 while (response
== NULL
)
200 DEBUG_puts("cupsDoFileRequest: setup...");
203 * Setup the HTTP variables needed...
206 length
= ippLength(request
);
210 if (!S_ISREG(fileinfo
.st_mode
))
211 length
= 0; /* Chunk when piping */
214 length
+= fileinfo
.st_size
;
217 httpClearFields(http
);
218 httpSetLength(http
, length
);
219 httpSetField(http
, HTTP_FIELD_CONTENT_TYPE
, "application/ipp");
220 httpSetField(http
, HTTP_FIELD_AUTHORIZATION
, http
->authstring
);
221 httpSetExpect(http
, expect
);
223 DEBUG_printf(("cupsDoFileRequest: authstring=\"%s\"\n", http
->authstring
));
229 DEBUG_puts("cupsDoFileRequest: post...");
231 if (httpPost(http
, resource
))
233 if (httpReconnect(http
))
243 * Send the IPP data...
246 DEBUG_puts("cupsDoFileRequest: ipp write...");
248 request
->state
= IPP_IDLE
;
249 status
= HTTP_CONTINUE
;
252 while ((state
= ippWrite(http
, request
)) != IPP_DATA
)
253 if (state
== IPP_ERROR
)
255 else if (httpCheck(http
))
259 if ((status
= httpUpdate(http
)) != HTTP_CONTINUE
)
266 * Wait up to 1 second to get the 100-continue response...
269 if (httpWait(http
, 1000))
270 status
= httpUpdate(http
);
272 else if (httpCheck(http
))
273 status
= httpUpdate(http
);
275 if (status
== HTTP_CONTINUE
&& state
== IPP_DATA
&& infile
>= 0)
277 DEBUG_puts("cupsDoFileRequest: file write...");
284 if (S_ISREG(fileinfo
.st_mode
))
286 lseek(infile
, 0, SEEK_SET
);
288 while ((bytes
= (int)read(infile
, buffer
, sizeof(buffer
))) > 0)
292 if ((status
= httpUpdate(http
)) != HTTP_CONTINUE
)
296 if (httpWrite2(http
, buffer
, bytes
) < bytes
)
302 * Get the server's return status...
305 DEBUG_puts("cupsDoFileRequest: update...");
307 while (status
== HTTP_CONTINUE
)
308 status
= httpUpdate(http
);
310 DEBUG_printf(("cupsDoFileRequest: status = %d\n", status
));
312 if (status
== HTTP_UNAUTHORIZED
)
314 DEBUG_puts("cupsDoFileRequest: unauthorized...");
317 * Flush any error message...
323 * See if we can do authentication...
326 if (cupsDoAuthentication(http
, "POST", resource
))
329 if (httpReconnect(http
))
337 else if (status
== HTTP_ERROR
)
339 DEBUG_printf(("cupsDoFileRequest: http->error=%d (%s)\n", http
->error
,
340 strerror(http
->error
)));
343 if (http
->error
!= WSAENETDOWN
&& http
->error
!= WSAENETUNREACH
&&
344 http
->error
!= WSAETIMEDOUT
)
346 if (http
->error
!= ENETDOWN
&& http
->error
!= ENETUNREACH
&&
347 http
->error
!= ETIMEDOUT
)
354 else if (status
== HTTP_UPGRADE_REQUIRED
)
356 /* Flush any error message... */
360 if (httpReconnect(http
))
366 /* Upgrade with encryption... */
367 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
369 /* Try again, this time with encryption enabled... */
372 #endif /* HAVE_SSL */
373 else if (status
== HTTP_EXPECTATION_FAILED
)
376 * Don't try using the Expect: header the next time around...
379 expect
= (http_status_t
)0;
381 else if (status
!= HTTP_OK
)
383 DEBUG_printf(("cupsDoFileRequest: error %d...\n", status
));
386 * Flush any error message...
395 * Read the response...
398 DEBUG_puts("cupsDoFileRequest: response...");
402 while ((state
= ippRead(http
, response
)) != IPP_DATA
)
403 if (state
== IPP_ERROR
)
406 if (state
== IPP_ERROR
)
409 * Delete the response...
412 DEBUG_puts("IPP read error!");
416 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, strerror(errno
));
420 else if (outfile
>= 0)
423 * Write trailing data to file...
426 while ((bytes
= (int)httpRead2(http
, buffer
, sizeof(buffer
))) > 0)
427 if (write(outfile
, buffer
, bytes
) < bytes
)
433 * Flush any remaining data...
442 * Delete the original request and return the response...
449 ipp_attribute_t
*attr
; /* status-message attribute */
452 attr
= ippFindAttribute(response
, "status-message", IPP_TAG_TEXT
);
454 _cupsSetError(response
->request
.status
.status_code
,
455 attr
? attr
->values
[0].string
.text
:
456 ippErrorString(response
->request
.status
.status_code
));
458 else if (status
!= HTTP_OK
)
459 _cupsSetHTTPError(status
);
466 * 'cupsDoRequest()' - Do an IPP request.
468 * This function sends the IPP request to the specified server, retrying
469 * and authenticating as necessary. The request is freed with ippDelete()
470 * after receiving a valid IPP response.
473 ipp_t
* /* O - Response data */
474 cupsDoRequest(http_t
*http
, /* I - HTTP connection to server */
475 ipp_t
*request
, /* I - IPP request */
476 const char *resource
) /* I - HTTP resource for POST */
478 return (cupsDoFileRequest(http
, request
, resource
, NULL
));
483 * '_cupsSetError()' - Set the last IPP status code and status-message.
487 _cupsSetError(ipp_status_t status
, /* I - IPP status code */
488 const char *message
) /* I - status-message value */
490 _cups_globals_t
*cg
; /* Global data */
494 cg
->last_error
= status
;
496 if (cg
->last_status_message
)
498 free(cg
->last_status_message
);
500 cg
->last_status_message
= NULL
;
504 cg
->last_status_message
= strdup(message
);
509 * '_cupsSetHTTPError()' - Set the last error using the HTTP status.
513 _cupsSetHTTPError(http_status_t status
) /* I - HTTP status code */
517 case HTTP_NOT_FOUND
:
518 _cupsSetError(IPP_NOT_FOUND
, httpStatus(status
));
521 case HTTP_UNAUTHORIZED
:
522 _cupsSetError(IPP_NOT_AUTHORIZED
, httpStatus(status
));
525 case HTTP_FORBIDDEN
:
526 _cupsSetError(IPP_FORBIDDEN
, httpStatus(status
));
529 case HTTP_BAD_REQUEST
:
530 _cupsSetError(IPP_BAD_REQUEST
, httpStatus(status
));
533 case HTTP_REQUEST_TOO_LARGE
:
534 _cupsSetError(IPP_REQUEST_VALUE
, httpStatus(status
));
537 case HTTP_NOT_IMPLEMENTED
:
538 _cupsSetError(IPP_OPERATION_NOT_SUPPORTED
, httpStatus(status
));
541 case HTTP_NOT_SUPPORTED
:
542 _cupsSetError(IPP_VERSION_NOT_SUPPORTED
, httpStatus(status
));
546 DEBUG_printf(("HTTP error %d mapped to IPP_SERVICE_UNAVAILABLE!\n",
548 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, httpStatus(status
));
555 * End of "$Id: request.c 6712 2007-07-24 00:13:05Z mike $".