2 * "$Id: request.c 5147 2006-02-22 16:37:44Z 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 ipp_state_t state
; /* State of IPP processing */
68 FILE *file
; /* File to send */
69 struct stat fileinfo
; /* File information */
70 int bytes
; /* Number of bytes read/written */
71 char buffer
[65536]; /* Output buffer */
74 DEBUG_printf(("cupsDoFileRequest(%p, %p, \'%s\', \'%s\')\n",
75 http
, request
, resource
? resource
: "(null)",
76 filename
? filename
: "(null)"));
78 if (http
== NULL
|| request
== NULL
|| resource
== NULL
)
83 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
89 * See if we have a file to send...
94 if (stat(filename
, &fileinfo
))
97 * Can't get file information!
100 _cupsSetError(errno
== ENOENT
? IPP_NOT_FOUND
: IPP_NOT_AUTHORIZED
,
109 if (fileinfo
.st_mode
& _S_IFDIR
)
111 if (S_ISDIR(fileinfo
.st_mode
))
115 * Can't send a directory...
120 _cupsSetError(IPP_NOT_POSSIBLE
, NULL
);
125 if ((file
= fopen(filename
, "rb")) == NULL
)
131 _cupsSetError(errno
== ENOENT
? IPP_NOT_FOUND
: IPP_NOT_AUTHORIZED
,
143 * Loop until we can send the request without authorization problems.
149 while (response
== NULL
)
151 DEBUG_puts("cupsDoFileRequest: setup...");
154 * Setup the HTTP variables needed...
157 length
= ippLength(request
);
159 length
+= fileinfo
.st_size
;
161 httpClearFields(http
);
162 httpSetLength(http
, length
);
163 httpSetField(http
, HTTP_FIELD_CONTENT_TYPE
, "application/ipp");
164 httpSetField(http
, HTTP_FIELD_AUTHORIZATION
, http
->authstring
);
165 httpSetExpect(http
, HTTP_CONTINUE
);
167 DEBUG_printf(("cupsDoFileRequest: authstring=\"%s\"\n", http
->authstring
));
173 DEBUG_puts("cupsDoFileRequest: post...");
175 if (httpPost(http
, resource
))
177 if (httpReconnect(http
))
187 * Wait up to 1 second for a 100-continue response...
190 if (httpWait(http
, 1000))
191 status
= httpUpdate(http
);
193 status
= HTTP_CONTINUE
;
195 if (status
== HTTP_CONTINUE
)
198 * Send the IPP data...
201 DEBUG_puts("cupsDoFileRequest: ipp write...");
203 request
->state
= IPP_IDLE
;
205 while ((state
= ippWrite(http
, request
)) != IPP_DATA
)
206 if (state
== IPP_ERROR
)
208 else if (httpCheck(http
))
210 if ((status
= httpUpdate(http
)) != HTTP_CONTINUE
)
214 if (state
== IPP_DATA
&& filename
)
216 DEBUG_puts("cupsDoFileRequest: file write...");
224 while ((bytes
= (int)fread(buffer
, 1, sizeof(buffer
), file
)) > 0)
228 if ((status
= httpUpdate(http
)) != HTTP_CONTINUE
)
232 if (httpWrite2(http
, buffer
, bytes
) < bytes
)
239 * Get the server's return status...
242 DEBUG_puts("cupsDoFileRequest: update...");
244 while (status
== HTTP_CONTINUE
)
245 status
= httpUpdate(http
);
247 DEBUG_printf(("cupsDoFileRequest: status = %d\n", status
));
249 if (status
== HTTP_UNAUTHORIZED
)
251 DEBUG_puts("cupsDoFileRequest: unauthorized...");
254 * Flush any error message...
260 * See if we can do authentication...
263 if (cupsDoAuthentication(http
, "POST", resource
))
266 if (httpReconnect(http
))
274 else if (status
== HTTP_ERROR
)
277 if (http
->error
!= WSAENETDOWN
&& http
->error
!= WSAENETUNREACH
)
279 if (http
->error
!= ENETDOWN
&& http
->error
!= ENETUNREACH
)
286 else if (status
== HTTP_UPGRADE_REQUIRED
)
288 /* Flush any error message... */
292 if (httpReconnect(http
))
298 /* Upgrade with encryption... */
299 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
301 /* Try again, this time with encryption enabled... */
304 #endif /* HAVE_SSL */
305 else if (status
!= HTTP_OK
)
307 DEBUG_printf(("cupsDoFileRequest: error %d...\n", status
));
310 * Flush any error message...
319 * Read the response...
322 DEBUG_puts("cupsDoFileRequest: response...");
326 if (ippRead(http
, response
) == IPP_ERROR
)
329 * Delete the response...
332 DEBUG_puts("IPP read error!");
336 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, strerror(errno
));
344 * Close the file if needed...
347 if (filename
!= NULL
)
351 * Flush any remaining data...
357 * Delete the original request and return the response...
364 ipp_attribute_t
*attr
; /* status-message attribute */
367 attr
= ippFindAttribute(response
, "status-message", IPP_TAG_TEXT
);
369 _cupsSetError(response
->request
.status
.status_code
,
370 attr
? attr
->values
[0].string
.text
:
371 ippErrorString(response
->request
.status
.status_code
));
373 else if (status
!= HTTP_OK
)
377 case HTTP_NOT_FOUND
:
378 _cupsSetError(IPP_NOT_FOUND
, httpStatus(status
));
381 case HTTP_UNAUTHORIZED
:
382 _cupsSetError(IPP_NOT_AUTHORIZED
, httpStatus(status
));
385 case HTTP_FORBIDDEN
:
386 _cupsSetError(IPP_FORBIDDEN
, httpStatus(status
));
389 case HTTP_BAD_REQUEST
:
390 _cupsSetError(IPP_BAD_REQUEST
, httpStatus(status
));
393 case HTTP_REQUEST_TOO_LARGE
:
394 _cupsSetError(IPP_REQUEST_VALUE
, httpStatus(status
));
397 case HTTP_NOT_IMPLEMENTED
:
398 _cupsSetError(IPP_OPERATION_NOT_SUPPORTED
, httpStatus(status
));
401 case HTTP_NOT_SUPPORTED
:
402 _cupsSetError(IPP_VERSION_NOT_SUPPORTED
, httpStatus(status
));
406 DEBUG_printf(("HTTP error %d mapped to IPP_SERVICE_UNAVAILABLE!\n",
408 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, httpStatus(status
));
418 * 'cupsDoRequest()' - Do an IPP request.
420 * This function sends the IPP request to the specified server, retrying
421 * and authenticating as necessary. The request is freed with ippDelete()
422 * after receiving a valid IPP response.
425 ipp_t
* /* O - Response data */
426 cupsDoRequest(http_t
*http
, /* I - HTTP connection to server */
427 ipp_t
*request
, /* I - IPP request */
428 const char *resource
) /* I - HTTP resource for POST */
430 return (cupsDoFileRequest(http
, request
, resource
, NULL
));
435 * '_cupsSetError()' - Set the last IPP status code and status-message.
439 _cupsSetError(ipp_status_t status
, /* I - IPP status code */
440 const char *message
) /* I - status-message value */
442 _cups_globals_t
*cg
; /* Global data */
446 cg
->last_error
= status
;
448 if (cg
->last_status_message
)
450 free(cg
->last_status_message
);
452 cg
->last_status_message
= NULL
;
456 cg
->last_status_message
= strdup(message
);
461 * End of "$Id: request.c 5147 2006-02-22 16:37:44Z mike $".