2 * "$Id: request.c 5555 2006-05-20 13:35:58Z mike $"
4 * IPP utilities for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 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__ */
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 ippDelete()
55 * after receiving a valid IPP response.
58 ipp_t
* /* O - Response data */
59 cupsDoFileRequest(http_t
*http
, /* I - HTTP connection to server */
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 NULL for none */
64 ipp_t
*response
; /* IPP response data */
65 size_t length
; /* Content-Length value */
66 http_status_t status
; /* Status of HTTP request */
67 int got_status
; /* Did we get the status? */
68 ipp_state_t state
; /* State of IPP processing */
69 FILE *file
; /* File to send */
70 struct stat fileinfo
; /* File information */
71 int bytes
; /* Number of bytes read/written */
72 char buffer
[32768]; /* Output buffer */
73 http_status_t expect
; /* Expect: header to use */
76 DEBUG_printf(("cupsDoFileRequest(%p, %p, \'%s\', \'%s\')\n",
77 http
, request
, resource
? resource
: "(null)",
78 filename
? filename
: "(null)"));
80 if (http
== NULL
|| request
== NULL
|| resource
== NULL
)
85 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
91 * See if we have a file to send...
96 if (stat(filename
, &fileinfo
))
99 * Can't get file information!
102 _cupsSetError(errno
== ENOENT
? IPP_NOT_FOUND
: IPP_NOT_AUTHORIZED
,
111 if (fileinfo
.st_mode
& _S_IFDIR
)
113 if (S_ISDIR(fileinfo
.st_mode
))
117 * Can't send a directory...
122 _cupsSetError(IPP_NOT_POSSIBLE
, strerror(EISDIR
));
127 if ((file
= fopen(filename
, "rb")) == NULL
)
133 _cupsSetError(errno
== ENOENT
? IPP_NOT_FOUND
: IPP_NOT_AUTHORIZED
,
145 * Loop until we can send the request without authorization problems.
150 expect
= HTTP_CONTINUE
;
152 while (response
== NULL
)
154 DEBUG_puts("cupsDoFileRequest: setup...");
157 * Setup the HTTP variables needed...
160 length
= ippLength(request
);
162 length
+= fileinfo
.st_size
;
164 httpClearFields(http
);
165 httpSetLength(http
, length
);
166 httpSetField(http
, HTTP_FIELD_CONTENT_TYPE
, "application/ipp");
167 httpSetField(http
, HTTP_FIELD_AUTHORIZATION
, http
->authstring
);
168 httpSetExpect(http
, expect
);
170 DEBUG_printf(("cupsDoFileRequest: authstring=\"%s\"\n", http
->authstring
));
176 DEBUG_puts("cupsDoFileRequest: post...");
178 if (httpPost(http
, resource
))
180 if (httpReconnect(http
))
190 * Send the IPP data...
193 DEBUG_puts("cupsDoFileRequest: ipp write...");
195 request
->state
= IPP_IDLE
;
196 status
= HTTP_CONTINUE
;
199 while ((state
= ippWrite(http
, request
)) != IPP_DATA
)
200 if (state
== IPP_ERROR
)
202 else if (httpCheck(http
))
206 if ((status
= httpUpdate(http
)) != HTTP_CONTINUE
)
213 * Wait up to 1 second to get the 100-continue response...
216 if (httpWait(http
, 1000))
217 status
= httpUpdate(http
);
219 else if (httpCheck(http
))
220 status
= httpUpdate(http
);
222 if (status
== HTTP_CONTINUE
&& state
== IPP_DATA
&& filename
)
224 DEBUG_puts("cupsDoFileRequest: file write...");
232 while ((bytes
= (int)fread(buffer
, 1, sizeof(buffer
), file
)) > 0)
236 if ((status
= httpUpdate(http
)) != HTTP_CONTINUE
)
240 if (httpWrite2(http
, buffer
, bytes
) < bytes
)
246 * Get the server's return status...
249 DEBUG_puts("cupsDoFileRequest: update...");
251 while (status
== HTTP_CONTINUE
)
252 status
= httpUpdate(http
);
254 DEBUG_printf(("cupsDoFileRequest: status = %d\n", status
));
256 if (status
== HTTP_UNAUTHORIZED
)
258 DEBUG_puts("cupsDoFileRequest: unauthorized...");
261 * Flush any error message...
267 * See if we can do authentication...
270 if (cupsDoAuthentication(http
, "POST", resource
))
273 if (httpReconnect(http
))
281 else if (status
== HTTP_ERROR
)
283 DEBUG_printf(("cupsDoFileRequest: http->error=%d (%s)\n", http
->error
,
284 strerror(http
->error
)));
287 if (http
->error
!= WSAENETDOWN
&& http
->error
!= WSAENETUNREACH
&&
288 http
->error
!= ETIMEDOUT
)
290 if (http
->error
!= ENETDOWN
&& http
->error
!= ENETUNREACH
&&
291 http
->error
!= ETIMEDOUT
)
298 else if (status
== HTTP_UPGRADE_REQUIRED
)
300 /* Flush any error message... */
304 if (httpReconnect(http
))
310 /* Upgrade with encryption... */
311 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
313 /* Try again, this time with encryption enabled... */
316 #endif /* HAVE_SSL */
317 else if (status
== HTTP_EXPECTATION_FAILED
)
320 * Don't try using the Expect: header the next time around...
323 expect
= (http_status_t
)0;
325 else if (status
!= HTTP_OK
)
327 DEBUG_printf(("cupsDoFileRequest: error %d...\n", status
));
330 * Flush any error message...
339 * Read the response...
342 DEBUG_puts("cupsDoFileRequest: response...");
346 while ((state
= ippRead(http
, response
)) != IPP_DATA
)
347 if (state
== IPP_ERROR
)
350 * Delete the response...
353 DEBUG_puts("IPP read error!");
357 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, strerror(errno
));
365 * Close the file if needed...
368 if (filename
!= NULL
)
372 * Flush any remaining data...
378 * Delete the original request and return the response...
385 ipp_attribute_t
*attr
; /* status-message attribute */
388 attr
= ippFindAttribute(response
, "status-message", IPP_TAG_TEXT
);
390 _cupsSetError(response
->request
.status
.status_code
,
391 attr
? attr
->values
[0].string
.text
:
392 ippErrorString(response
->request
.status
.status_code
));
394 else if (status
!= HTTP_OK
)
398 case HTTP_NOT_FOUND
:
399 _cupsSetError(IPP_NOT_FOUND
, httpStatus(status
));
402 case HTTP_UNAUTHORIZED
:
403 _cupsSetError(IPP_NOT_AUTHORIZED
, httpStatus(status
));
406 case HTTP_FORBIDDEN
:
407 _cupsSetError(IPP_FORBIDDEN
, httpStatus(status
));
410 case HTTP_BAD_REQUEST
:
411 _cupsSetError(IPP_BAD_REQUEST
, httpStatus(status
));
414 case HTTP_REQUEST_TOO_LARGE
:
415 _cupsSetError(IPP_REQUEST_VALUE
, httpStatus(status
));
418 case HTTP_NOT_IMPLEMENTED
:
419 _cupsSetError(IPP_OPERATION_NOT_SUPPORTED
, httpStatus(status
));
422 case HTTP_NOT_SUPPORTED
:
423 _cupsSetError(IPP_VERSION_NOT_SUPPORTED
, httpStatus(status
));
427 DEBUG_printf(("HTTP error %d mapped to IPP_SERVICE_UNAVAILABLE!\n",
429 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, httpStatus(status
));
439 * 'cupsDoRequest()' - Do an IPP request.
441 * This function sends the IPP request to the specified server, retrying
442 * and authenticating as necessary. The request is freed with ippDelete()
443 * after receiving a valid IPP response.
446 ipp_t
* /* O - Response data */
447 cupsDoRequest(http_t
*http
, /* I - HTTP connection to server */
448 ipp_t
*request
, /* I - IPP request */
449 const char *resource
) /* I - HTTP resource for POST */
451 return (cupsDoFileRequest(http
, request
, resource
, NULL
));
456 * '_cupsSetError()' - Set the last IPP status code and status-message.
460 _cupsSetError(ipp_status_t status
, /* I - IPP status code */
461 const char *message
) /* I - status-message value */
463 _cups_globals_t
*cg
; /* Global data */
467 cg
->last_error
= status
;
469 if (cg
->last_status_message
)
471 free(cg
->last_status_message
);
473 cg
->last_status_message
= NULL
;
477 cg
->last_status_message
= strdup(message
);
482 * End of "$Id: request.c 5555 2006-05-20 13:35:58Z mike $".