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 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 * _cupsSetError() - Set the last IPP status code and status-message.
23 * _cupsSetHTTPError() - Set the last error using the HTTP status.
27 * Include necessary headers...
36 #if defined(WIN32) || defined(__EMX__)
40 #endif /* WIN32 || __EMX__ */
47 * 'cupsDoFileRequest()' - Do an IPP request with a file.
49 * This function sends the IPP request to the specified server, retrying
50 * and authenticating as necessary. The request is freed with ippDelete()
51 * after receiving a valid IPP response.
54 ipp_t
* /* O - Response data */
55 cupsDoFileRequest(http_t
*http
, /* I - HTTP connection to server */
56 ipp_t
*request
, /* I - IPP request */
57 const char *resource
, /* I - HTTP resource for POST */
58 const char *filename
) /* I - File to send or NULL for none */
60 ipp_t
*response
; /* IPP response data */
61 int infile
; /* Input file */
66 if ((infile
= open(filename
, O_RDONLY
| O_BINARY
)) < 0)
69 * Can't get file information!
72 _cupsSetError(errno
== ENOENT
? IPP_NOT_FOUND
: IPP_NOT_AUTHORIZED
,
83 response
= cupsDoIORequest(http
, request
, resource
, infile
, -1);
93 * 'cupsDoIORequest()' - Do an IPP request with file descriptors.
95 * This function sends the IPP request to the specified server, retrying
96 * and authenticating as necessary. The request is freed with ippDelete()
97 * after receiving a valid IPP response.
99 * If "infile" is a valid file descriptor, cupsDoIORequest() copies
100 * all of the data from the file after the IPP request message.
102 * If "outfile" is a valid file descriptor, cupsDoIORequest() copies
103 * all of the data after the IPP response message to the file.
108 ipp_t
* /* O - Response data */
109 cupsDoIORequest(http_t
*http
, /* I - HTTP connection to server */
110 ipp_t
*request
, /* I - IPP request */
111 const char *resource
, /* I - HTTP resource for POST */
112 int infile
, /* I - File to read from or -1 for none */
113 int outfile
) /* I - File to write to or -1 for none */
115 ipp_t
*response
; /* IPP response data */
116 size_t length
; /* Content-Length value */
117 http_status_t status
; /* Status of HTTP request */
118 int got_status
; /* Did we get the status? */
119 ipp_state_t state
; /* State of IPP processing */
120 struct stat fileinfo
; /* File information */
121 int bytes
; /* Number of bytes read/written */
122 char buffer
[32768]; /* Output buffer */
123 http_status_t expect
; /* Expect: header to use */
126 DEBUG_printf(("cupsDoFileRequest(%p, %p, \'%s\', \'%s\')\n",
127 http
, request
, resource
? resource
: "(null)",
128 filename
? filename
: "(null)"));
130 if (http
== NULL
|| request
== NULL
|| resource
== NULL
)
135 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
141 * See if we have a file to send...
146 if (fstat(infile
, &fileinfo
))
149 * Can't get file information!
152 _cupsSetError(errno
== ENOENT
? IPP_NOT_FOUND
: IPP_NOT_AUTHORIZED
,
161 if (fileinfo
.st_mode
& _S_IFDIR
)
163 if (S_ISDIR(fileinfo
.st_mode
))
167 * Can't send a directory...
172 _cupsSetError(IPP_NOT_POSSIBLE
, strerror(EISDIR
));
180 * See if we have an auth-info attribute and are communicating over
181 * a non-local link. If so, encrypt the link so that we can pass
182 * the authentication information securely...
185 if (ippFindAttribute(request
, "auth-info", IPP_TAG_TEXT
) &&
186 !httpAddrLocalhost(http
->hostaddr
) && !http
->tls
&&
187 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
))
189 #endif /* HAVE_SSL */
192 * Loop until we can send the request without authorization problems.
197 expect
= HTTP_CONTINUE
;
199 while (response
== NULL
)
201 DEBUG_puts("cupsDoFileRequest: setup...");
204 * Setup the HTTP variables needed...
207 length
= ippLength(request
);
211 if (!S_ISREG(fileinfo
.st_mode
))
212 length
= 0; /* Chunk when piping */
215 length
+= fileinfo
.st_size
;
218 httpClearFields(http
);
219 httpSetLength(http
, length
);
220 httpSetField(http
, HTTP_FIELD_CONTENT_TYPE
, "application/ipp");
221 httpSetField(http
, HTTP_FIELD_AUTHORIZATION
, http
->authstring
);
222 httpSetExpect(http
, expect
);
224 DEBUG_printf(("cupsDoFileRequest: authstring=\"%s\"\n", http
->authstring
));
230 DEBUG_puts("cupsDoFileRequest: post...");
232 if (httpPost(http
, resource
))
234 if (httpReconnect(http
))
244 * Send the IPP data...
247 DEBUG_puts("cupsDoFileRequest: ipp write...");
249 request
->state
= IPP_IDLE
;
250 status
= HTTP_CONTINUE
;
253 while ((state
= ippWrite(http
, request
)) != IPP_DATA
)
254 if (state
== IPP_ERROR
)
256 else if (httpCheck(http
))
260 if ((status
= httpUpdate(http
)) != HTTP_CONTINUE
)
267 * Wait up to 1 second to get the 100-continue response...
270 if (httpWait(http
, 1000))
271 status
= httpUpdate(http
);
273 else if (httpCheck(http
))
274 status
= httpUpdate(http
);
276 if (status
== HTTP_CONTINUE
&& state
== IPP_DATA
&& infile
>= 0)
278 DEBUG_puts("cupsDoFileRequest: file write...");
285 if (S_ISREG(fileinfo
.st_mode
))
287 lseek(infile
, 0, SEEK_SET
);
289 while ((bytes
= (int)read(infile
, buffer
, sizeof(buffer
))) > 0)
293 if ((status
= httpUpdate(http
)) != HTTP_CONTINUE
)
297 if (httpWrite2(http
, buffer
, bytes
) < bytes
)
303 * Get the server's return status...
306 DEBUG_puts("cupsDoFileRequest: update...");
308 while (status
== HTTP_CONTINUE
)
309 status
= httpUpdate(http
);
311 DEBUG_printf(("cupsDoFileRequest: status = %d\n", status
));
313 if (status
== HTTP_UNAUTHORIZED
)
315 DEBUG_puts("cupsDoFileRequest: unauthorized...");
318 * Flush any error message...
324 * See if we can do authentication...
327 if (cupsDoAuthentication(http
, "POST", resource
))
330 if (httpReconnect(http
))
338 else if (status
== HTTP_ERROR
)
340 DEBUG_printf(("cupsDoFileRequest: http->error=%d (%s)\n", http
->error
,
341 strerror(http
->error
)));
344 if (http
->error
!= WSAENETDOWN
&& http
->error
!= WSAENETUNREACH
&&
345 http
->error
!= WSAETIMEDOUT
)
347 if (http
->error
!= ENETDOWN
&& http
->error
!= ENETUNREACH
&&
348 http
->error
!= ETIMEDOUT
)
355 else if (status
== HTTP_UPGRADE_REQUIRED
)
357 /* Flush any error message... */
361 if (httpReconnect(http
))
367 /* Upgrade with encryption... */
368 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
370 /* Try again, this time with encryption enabled... */
373 #endif /* HAVE_SSL */
374 else if (status
== HTTP_EXPECTATION_FAILED
)
377 * Don't try using the Expect: header the next time around...
380 expect
= (http_status_t
)0;
382 else if (status
!= HTTP_OK
)
384 DEBUG_printf(("cupsDoFileRequest: error %d...\n", status
));
387 * Flush any error message...
396 * Read the response...
399 DEBUG_puts("cupsDoFileRequest: response...");
403 while ((state
= ippRead(http
, response
)) != IPP_DATA
)
404 if (state
== IPP_ERROR
)
407 if (state
== IPP_ERROR
)
410 * Delete the response...
413 DEBUG_puts("IPP read error!");
417 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, strerror(errno
));
421 else if (outfile
>= 0)
424 * Write trailing data to file...
427 while ((bytes
= (int)httpRead2(http
, buffer
, sizeof(buffer
))) > 0)
428 if (write(outfile
, buffer
, bytes
) < bytes
)
434 * Flush any remaining data...
443 * Delete the original request and return the response...
450 ipp_attribute_t
*attr
; /* status-message attribute */
453 attr
= ippFindAttribute(response
, "status-message", IPP_TAG_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 * 'cupsDoRequest()' - Do an IPP request.
469 * This function sends the IPP request to the specified server, retrying
470 * and authenticating as necessary. The request is freed with ippDelete()
471 * after receiving a valid IPP response.
474 ipp_t
* /* O - Response data */
475 cupsDoRequest(http_t
*http
, /* I - HTTP connection to server */
476 ipp_t
*request
, /* I - IPP request */
477 const char *resource
) /* I - HTTP resource for POST */
479 return (cupsDoFileRequest(http
, request
, resource
, NULL
));
484 * '_cupsSetError()' - Set the last IPP status code and status-message.
488 _cupsSetError(ipp_status_t status
, /* I - IPP status code */
489 const char *message
) /* I - status-message value */
491 _cups_globals_t
*cg
; /* Global data */
495 cg
->last_error
= status
;
497 if (cg
->last_status_message
)
499 free(cg
->last_status_message
);
501 cg
->last_status_message
= NULL
;
505 cg
->last_status_message
= strdup(message
);
510 * '_cupsSetHTTPError()' - Set the last error using the HTTP status.
514 _cupsSetHTTPError(http_status_t status
) /* I - HTTP status code */
518 case HTTP_NOT_FOUND
:
519 _cupsSetError(IPP_NOT_FOUND
, httpStatus(status
));
522 case HTTP_UNAUTHORIZED
:
523 _cupsSetError(IPP_NOT_AUTHORIZED
, httpStatus(status
));
526 case HTTP_FORBIDDEN
:
527 _cupsSetError(IPP_FORBIDDEN
, httpStatus(status
));
530 case HTTP_BAD_REQUEST
:
531 _cupsSetError(IPP_BAD_REQUEST
, httpStatus(status
));
534 case HTTP_REQUEST_TOO_LARGE
:
535 _cupsSetError(IPP_REQUEST_VALUE
, httpStatus(status
));
538 case HTTP_NOT_IMPLEMENTED
:
539 _cupsSetError(IPP_OPERATION_NOT_SUPPORTED
, httpStatus(status
));
542 case HTTP_NOT_SUPPORTED
:
543 _cupsSetError(IPP_VERSION_NOT_SUPPORTED
, httpStatus(status
));
547 DEBUG_printf(("HTTP error %d mapped to IPP_SERVICE_UNAVAILABLE!\n",
549 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, httpStatus(status
));
556 * End of "$Id: request.c 6879 2007-08-29 20:26:50Z mike $".