2 * "$Id: request.c 6506 2007-05-03 18:12:35Z mike $"
4 * IPP utilities for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2007 by Easy Software Products.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * cupsDoFileRequest() - Do an IPP request with a file.
29 * cupsDoRequest() - Do an IPP request.
30 * _cupsSetError() - Set the last IPP status code and status-message.
34 * Include necessary headers...
43 #if defined(WIN32) || defined(__EMX__)
47 #endif /* WIN32 || __EMX__ */
54 * 'cupsDoFileRequest()' - Do an IPP request with a file.
56 * This function sends the IPP request to the specified server, retrying
57 * and authenticating as necessary. The request is freed with ippDelete()
58 * after receiving a valid IPP response.
61 ipp_t
* /* O - Response data */
62 cupsDoFileRequest(http_t
*http
, /* I - HTTP connection to server */
63 ipp_t
*request
, /* I - IPP request */
64 const char *resource
, /* I - HTTP resource for POST */
65 const char *filename
) /* I - File to send or NULL for none */
67 ipp_t
*response
; /* IPP response data */
68 int infile
; /* Input file */
73 if ((infile
= open(filename
, O_RDONLY
| O_BINARY
)) < 0)
76 * Can't get file information!
79 _cupsSetError(errno
== ENOENT
? IPP_NOT_FOUND
: IPP_NOT_AUTHORIZED
,
90 response
= cupsDoIORequest(http
, request
, resource
, infile
, -1);
100 * 'cupsDoIORequest()' - Do an IPP request with file descriptors.
102 * This function sends the IPP request to the specified server, retrying
103 * and authenticating as necessary. The request is freed with ippDelete()
104 * after receiving a valid IPP response.
106 * If "infile" is a valid file descriptor, cupsDoIORequest() copies
107 * all of the data from the file after the IPP request message.
109 * If "outfile" is a valid file descriptor, cupsDoIORequest() copies
110 * all of the data after the IPP response message to the file.
115 ipp_t
* /* O - Response data */
116 cupsDoIORequest(http_t
*http
, /* I - HTTP connection to server */
117 ipp_t
*request
, /* I - IPP request */
118 const char *resource
, /* I - HTTP resource for POST */
119 int infile
, /* I - File to read from or -1 for none */
120 int outfile
) /* I - File to write to or -1 for none */
122 ipp_t
*response
; /* IPP response data */
123 size_t length
; /* Content-Length value */
124 http_status_t status
; /* Status of HTTP request */
125 int got_status
; /* Did we get the status? */
126 ipp_state_t state
; /* State of IPP processing */
127 struct stat fileinfo
; /* File information */
128 int bytes
; /* Number of bytes read/written */
129 char buffer
[32768]; /* Output buffer */
130 http_status_t expect
; /* Expect: header to use */
133 DEBUG_printf(("cupsDoFileRequest(%p, %p, \'%s\', \'%s\')\n",
134 http
, request
, resource
? resource
: "(null)",
135 filename
? filename
: "(null)"));
137 if (http
== NULL
|| request
== NULL
|| resource
== NULL
)
142 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
148 * See if we have a file to send...
153 if (fstat(infile
, &fileinfo
))
156 * Can't get file information!
159 _cupsSetError(errno
== ENOENT
? IPP_NOT_FOUND
: IPP_NOT_AUTHORIZED
,
168 if (fileinfo
.st_mode
& _S_IFDIR
)
170 if (S_ISDIR(fileinfo
.st_mode
))
174 * Can't send a directory...
179 _cupsSetError(IPP_NOT_POSSIBLE
, strerror(EISDIR
));
187 * See if we have an auth-info attribute and are communicating over
188 * a non-local link. If so, encrypt the link so that we can pass
189 * the authentication information securely...
192 if (ippFindAttribute(request
, "auth-info", IPP_TAG_TEXT
) &&
193 !httpAddrLocalhost(http
->hostaddr
) && !http
->tls
&&
194 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
))
196 #endif /* HAVE_SSL */
199 * Loop until we can send the request without authorization problems.
204 expect
= HTTP_CONTINUE
;
206 while (response
== NULL
)
208 DEBUG_puts("cupsDoFileRequest: setup...");
211 * Setup the HTTP variables needed...
214 length
= ippLength(request
);
218 if (!S_ISREG(fileinfo
.st_mode
))
219 length
= 0; /* Chunk when piping */
222 length
+= fileinfo
.st_size
;
225 httpClearFields(http
);
226 httpSetLength(http
, length
);
227 httpSetField(http
, HTTP_FIELD_CONTENT_TYPE
, "application/ipp");
228 httpSetField(http
, HTTP_FIELD_AUTHORIZATION
, http
->authstring
);
229 httpSetExpect(http
, expect
);
231 DEBUG_printf(("cupsDoFileRequest: authstring=\"%s\"\n", http
->authstring
));
237 DEBUG_puts("cupsDoFileRequest: post...");
239 if (httpPost(http
, resource
))
241 if (httpReconnect(http
))
251 * Send the IPP data...
254 DEBUG_puts("cupsDoFileRequest: ipp write...");
256 request
->state
= IPP_IDLE
;
257 status
= HTTP_CONTINUE
;
260 while ((state
= ippWrite(http
, request
)) != IPP_DATA
)
261 if (state
== IPP_ERROR
)
263 else if (httpCheck(http
))
267 if ((status
= httpUpdate(http
)) != HTTP_CONTINUE
)
274 * Wait up to 1 second to get the 100-continue response...
277 if (httpWait(http
, 1000))
278 status
= httpUpdate(http
);
280 else if (httpCheck(http
))
281 status
= httpUpdate(http
);
283 if (status
== HTTP_CONTINUE
&& state
== IPP_DATA
&& infile
>= 0)
285 DEBUG_puts("cupsDoFileRequest: file write...");
292 if (S_ISREG(fileinfo
.st_mode
))
294 lseek(infile
, 0, SEEK_SET
);
296 while ((bytes
= (int)read(infile
, buffer
, sizeof(buffer
))) > 0)
300 if ((status
= httpUpdate(http
)) != HTTP_CONTINUE
)
304 if (httpWrite2(http
, buffer
, bytes
) < bytes
)
310 * Get the server's return status...
313 DEBUG_puts("cupsDoFileRequest: update...");
315 while (status
== HTTP_CONTINUE
)
316 status
= httpUpdate(http
);
318 DEBUG_printf(("cupsDoFileRequest: status = %d\n", status
));
320 if (status
== HTTP_UNAUTHORIZED
)
322 DEBUG_puts("cupsDoFileRequest: unauthorized...");
325 * Flush any error message...
331 * See if we can do authentication...
334 if (cupsDoAuthentication(http
, "POST", resource
))
337 if (httpReconnect(http
))
345 else if (status
== HTTP_ERROR
)
347 DEBUG_printf(("cupsDoFileRequest: http->error=%d (%s)\n", http
->error
,
348 strerror(http
->error
)));
351 if (http
->error
!= WSAENETDOWN
&& http
->error
!= WSAENETUNREACH
&&
352 http
->error
!= WSAETIMEDOUT
)
354 if (http
->error
!= ENETDOWN
&& http
->error
!= ENETUNREACH
&&
355 http
->error
!= ETIMEDOUT
)
362 else if (status
== HTTP_UPGRADE_REQUIRED
)
364 /* Flush any error message... */
368 if (httpReconnect(http
))
374 /* Upgrade with encryption... */
375 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
377 /* Try again, this time with encryption enabled... */
380 #endif /* HAVE_SSL */
381 else if (status
== HTTP_EXPECTATION_FAILED
)
384 * Don't try using the Expect: header the next time around...
387 expect
= (http_status_t
)0;
389 else if (status
!= HTTP_OK
)
391 DEBUG_printf(("cupsDoFileRequest: error %d...\n", status
));
394 * Flush any error message...
403 * Read the response...
406 DEBUG_puts("cupsDoFileRequest: response...");
410 while ((state
= ippRead(http
, response
)) != IPP_DATA
)
411 if (state
== IPP_ERROR
)
414 if (state
== IPP_ERROR
)
417 * Delete the response...
420 DEBUG_puts("IPP read error!");
424 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, strerror(errno
));
428 else if (outfile
>= 0)
431 * Write trailing data to file...
434 while ((bytes
= (int)httpRead2(http
, buffer
, sizeof(buffer
))) > 0)
435 if (write(outfile
, buffer
, bytes
) < bytes
)
441 * Flush any remaining data...
450 * Delete the original request and return the response...
457 ipp_attribute_t
*attr
; /* status-message attribute */
460 attr
= ippFindAttribute(response
, "status-message", IPP_TAG_TEXT
);
462 _cupsSetError(response
->request
.status
.status_code
,
463 attr
? attr
->values
[0].string
.text
:
464 ippErrorString(response
->request
.status
.status_code
));
466 else if (status
!= HTTP_OK
)
470 case HTTP_NOT_FOUND
:
471 _cupsSetError(IPP_NOT_FOUND
, httpStatus(status
));
474 case HTTP_UNAUTHORIZED
:
475 _cupsSetError(IPP_NOT_AUTHORIZED
, httpStatus(status
));
478 case HTTP_FORBIDDEN
:
479 _cupsSetError(IPP_FORBIDDEN
, httpStatus(status
));
482 case HTTP_BAD_REQUEST
:
483 _cupsSetError(IPP_BAD_REQUEST
, httpStatus(status
));
486 case HTTP_REQUEST_TOO_LARGE
:
487 _cupsSetError(IPP_REQUEST_VALUE
, httpStatus(status
));
490 case HTTP_NOT_IMPLEMENTED
:
491 _cupsSetError(IPP_OPERATION_NOT_SUPPORTED
, httpStatus(status
));
494 case HTTP_NOT_SUPPORTED
:
495 _cupsSetError(IPP_VERSION_NOT_SUPPORTED
, httpStatus(status
));
499 DEBUG_printf(("HTTP error %d mapped to IPP_SERVICE_UNAVAILABLE!\n",
501 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, httpStatus(status
));
511 * 'cupsDoRequest()' - Do an IPP request.
513 * This function sends the IPP request to the specified server, retrying
514 * and authenticating as necessary. The request is freed with ippDelete()
515 * after receiving a valid IPP response.
518 ipp_t
* /* O - Response data */
519 cupsDoRequest(http_t
*http
, /* I - HTTP connection to server */
520 ipp_t
*request
, /* I - IPP request */
521 const char *resource
) /* I - HTTP resource for POST */
523 return (cupsDoFileRequest(http
, request
, resource
, NULL
));
528 * '_cupsSetError()' - Set the last IPP status code and status-message.
532 _cupsSetError(ipp_status_t status
, /* I - IPP status code */
533 const char *message
) /* I - status-message value */
535 _cups_globals_t
*cg
; /* Global data */
539 cg
->last_error
= status
;
541 if (cg
->last_status_message
)
543 free(cg
->last_status_message
);
545 cg
->last_status_message
= NULL
;
549 cg
->last_status_message
= strdup(message
);
554 * End of "$Id: request.c 6506 2007-05-03 18:12:35Z mike $".