2 * "$Id: request.c 6649 2007-07-11 21:46:42Z 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.
25 * Include necessary headers...
34 #if defined(WIN32) || defined(__EMX__)
38 #endif /* WIN32 || __EMX__ */
45 * 'cupsDoFileRequest()' - Do an IPP request with a file.
47 * This function sends the IPP request to the specified server, retrying
48 * and authenticating as necessary. The request is freed with ippDelete()
49 * after receiving a valid IPP response.
52 ipp_t
* /* O - Response data */
53 cupsDoFileRequest(http_t
*http
, /* I - HTTP connection to server */
54 ipp_t
*request
, /* I - IPP request */
55 const char *resource
, /* I - HTTP resource for POST */
56 const char *filename
) /* I - File to send or NULL for none */
58 ipp_t
*response
; /* IPP response data */
59 int infile
; /* Input file */
64 if ((infile
= open(filename
, O_RDONLY
| O_BINARY
)) < 0)
67 * Can't get file information!
70 _cupsSetError(errno
== ENOENT
? IPP_NOT_FOUND
: IPP_NOT_AUTHORIZED
,
81 response
= cupsDoIORequest(http
, request
, resource
, infile
, -1);
91 * 'cupsDoIORequest()' - Do an IPP request with file descriptors.
93 * This function sends the IPP request to the specified server, retrying
94 * and authenticating as necessary. The request is freed with ippDelete()
95 * after receiving a valid IPP response.
97 * If "infile" is a valid file descriptor, cupsDoIORequest() copies
98 * all of the data from the file after the IPP request message.
100 * If "outfile" is a valid file descriptor, cupsDoIORequest() copies
101 * all of the data after the IPP response message to the file.
106 ipp_t
* /* O - Response data */
107 cupsDoIORequest(http_t
*http
, /* I - HTTP connection to server */
108 ipp_t
*request
, /* I - IPP request */
109 const char *resource
, /* I - HTTP resource for POST */
110 int infile
, /* I - File to read from or -1 for none */
111 int outfile
) /* I - File to write to or -1 for none */
113 ipp_t
*response
; /* IPP response data */
114 size_t length
; /* Content-Length value */
115 http_status_t status
; /* Status of HTTP request */
116 int got_status
; /* Did we get the status? */
117 ipp_state_t state
; /* State of IPP processing */
118 struct stat fileinfo
; /* File information */
119 int bytes
; /* Number of bytes read/written */
120 char buffer
[32768]; /* Output buffer */
121 http_status_t expect
; /* Expect: header to use */
124 DEBUG_printf(("cupsDoFileRequest(%p, %p, \'%s\', \'%s\')\n",
125 http
, request
, resource
? resource
: "(null)",
126 filename
? filename
: "(null)"));
128 if (http
== NULL
|| request
== NULL
|| resource
== NULL
)
133 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
139 * See if we have a file to send...
144 if (fstat(infile
, &fileinfo
))
147 * Can't get file information!
150 _cupsSetError(errno
== ENOENT
? IPP_NOT_FOUND
: IPP_NOT_AUTHORIZED
,
159 if (fileinfo
.st_mode
& _S_IFDIR
)
161 if (S_ISDIR(fileinfo
.st_mode
))
165 * Can't send a directory...
170 _cupsSetError(IPP_NOT_POSSIBLE
, strerror(EISDIR
));
178 * See if we have an auth-info attribute and are communicating over
179 * a non-local link. If so, encrypt the link so that we can pass
180 * the authentication information securely...
183 if (ippFindAttribute(request
, "auth-info", IPP_TAG_TEXT
) &&
184 !httpAddrLocalhost(http
->hostaddr
) && !http
->tls
&&
185 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
))
187 #endif /* HAVE_SSL */
190 * Loop until we can send the request without authorization problems.
195 expect
= HTTP_CONTINUE
;
197 while (response
== NULL
)
199 DEBUG_puts("cupsDoFileRequest: setup...");
202 * Setup the HTTP variables needed...
205 length
= ippLength(request
);
209 if (!S_ISREG(fileinfo
.st_mode
))
210 length
= 0; /* Chunk when piping */
213 length
+= fileinfo
.st_size
;
216 httpClearFields(http
);
217 httpSetLength(http
, length
);
218 httpSetField(http
, HTTP_FIELD_CONTENT_TYPE
, "application/ipp");
219 httpSetField(http
, HTTP_FIELD_AUTHORIZATION
, http
->authstring
);
220 httpSetExpect(http
, expect
);
222 DEBUG_printf(("cupsDoFileRequest: authstring=\"%s\"\n", http
->authstring
));
228 DEBUG_puts("cupsDoFileRequest: post...");
230 if (httpPost(http
, resource
))
232 if (httpReconnect(http
))
242 * Send the IPP data...
245 DEBUG_puts("cupsDoFileRequest: ipp write...");
247 request
->state
= IPP_IDLE
;
248 status
= HTTP_CONTINUE
;
251 while ((state
= ippWrite(http
, request
)) != IPP_DATA
)
252 if (state
== IPP_ERROR
)
254 else if (httpCheck(http
))
258 if ((status
= httpUpdate(http
)) != HTTP_CONTINUE
)
265 * Wait up to 1 second to get the 100-continue response...
268 if (httpWait(http
, 1000))
269 status
= httpUpdate(http
);
271 else if (httpCheck(http
))
272 status
= httpUpdate(http
);
274 if (status
== HTTP_CONTINUE
&& state
== IPP_DATA
&& infile
>= 0)
276 DEBUG_puts("cupsDoFileRequest: file write...");
283 if (S_ISREG(fileinfo
.st_mode
))
285 lseek(infile
, 0, SEEK_SET
);
287 while ((bytes
= (int)read(infile
, buffer
, sizeof(buffer
))) > 0)
291 if ((status
= httpUpdate(http
)) != HTTP_CONTINUE
)
295 if (httpWrite2(http
, buffer
, bytes
) < bytes
)
301 * Get the server's return status...
304 DEBUG_puts("cupsDoFileRequest: update...");
306 while (status
== HTTP_CONTINUE
)
307 status
= httpUpdate(http
);
309 DEBUG_printf(("cupsDoFileRequest: status = %d\n", status
));
311 if (status
== HTTP_UNAUTHORIZED
)
313 DEBUG_puts("cupsDoFileRequest: unauthorized...");
316 * Flush any error message...
322 * See if we can do authentication...
325 if (cupsDoAuthentication(http
, "POST", resource
))
328 if (httpReconnect(http
))
336 else if (status
== HTTP_ERROR
)
338 DEBUG_printf(("cupsDoFileRequest: http->error=%d (%s)\n", http
->error
,
339 strerror(http
->error
)));
342 if (http
->error
!= WSAENETDOWN
&& http
->error
!= WSAENETUNREACH
&&
343 http
->error
!= WSAETIMEDOUT
)
345 if (http
->error
!= ENETDOWN
&& http
->error
!= ENETUNREACH
&&
346 http
->error
!= ETIMEDOUT
)
353 else if (status
== HTTP_UPGRADE_REQUIRED
)
355 /* Flush any error message... */
359 if (httpReconnect(http
))
365 /* Upgrade with encryption... */
366 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
368 /* Try again, this time with encryption enabled... */
371 #endif /* HAVE_SSL */
372 else if (status
== HTTP_EXPECTATION_FAILED
)
375 * Don't try using the Expect: header the next time around...
378 expect
= (http_status_t
)0;
380 else if (status
!= HTTP_OK
)
382 DEBUG_printf(("cupsDoFileRequest: error %d...\n", status
));
385 * Flush any error message...
394 * Read the response...
397 DEBUG_puts("cupsDoFileRequest: 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("IPP read error!");
415 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, strerror(errno
));
419 else if (outfile
>= 0)
422 * Write trailing data to file...
425 while ((bytes
= (int)httpRead2(http
, buffer
, sizeof(buffer
))) > 0)
426 if (write(outfile
, buffer
, bytes
) < bytes
)
432 * Flush any remaining data...
441 * Delete the original request and return the response...
448 ipp_attribute_t
*attr
; /* status-message attribute */
451 attr
= ippFindAttribute(response
, "status-message", IPP_TAG_TEXT
);
453 _cupsSetError(response
->request
.status
.status_code
,
454 attr
? attr
->values
[0].string
.text
:
455 ippErrorString(response
->request
.status
.status_code
));
457 else if (status
!= HTTP_OK
)
461 case HTTP_NOT_FOUND
:
462 _cupsSetError(IPP_NOT_FOUND
, httpStatus(status
));
465 case HTTP_UNAUTHORIZED
:
466 _cupsSetError(IPP_NOT_AUTHORIZED
, httpStatus(status
));
469 case HTTP_FORBIDDEN
:
470 _cupsSetError(IPP_FORBIDDEN
, httpStatus(status
));
473 case HTTP_BAD_REQUEST
:
474 _cupsSetError(IPP_BAD_REQUEST
, httpStatus(status
));
477 case HTTP_REQUEST_TOO_LARGE
:
478 _cupsSetError(IPP_REQUEST_VALUE
, httpStatus(status
));
481 case HTTP_NOT_IMPLEMENTED
:
482 _cupsSetError(IPP_OPERATION_NOT_SUPPORTED
, httpStatus(status
));
485 case HTTP_NOT_SUPPORTED
:
486 _cupsSetError(IPP_VERSION_NOT_SUPPORTED
, httpStatus(status
));
490 DEBUG_printf(("HTTP error %d mapped to IPP_SERVICE_UNAVAILABLE!\n",
492 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, httpStatus(status
));
502 * 'cupsDoRequest()' - Do an IPP request.
504 * This function sends the IPP request to the specified server, retrying
505 * and authenticating as necessary. The request is freed with ippDelete()
506 * after receiving a valid IPP response.
509 ipp_t
* /* O - Response data */
510 cupsDoRequest(http_t
*http
, /* I - HTTP connection to server */
511 ipp_t
*request
, /* I - IPP request */
512 const char *resource
) /* I - HTTP resource for POST */
514 return (cupsDoFileRequest(http
, request
, resource
, NULL
));
519 * '_cupsSetError()' - Set the last IPP status code and status-message.
523 _cupsSetError(ipp_status_t status
, /* I - IPP status code */
524 const char *message
) /* I - status-message value */
526 _cups_globals_t
*cg
; /* Global data */
530 cg
->last_error
= status
;
532 if (cg
->last_status_message
)
534 free(cg
->last_status_message
);
536 cg
->last_status_message
= NULL
;
540 cg
->last_status_message
= strdup(message
);
545 * End of "$Id: request.c 6649 2007-07-11 21:46:42Z mike $".