]>
Commit | Line | Data |
---|---|---|
ecdc0628 | 1 | /* |
b19ccc9e | 2 | * "$Id: request.c 7946 2008-09-16 23:27:54Z mike $" |
ecdc0628 | 3 | * |
4 | * IPP utilities for the Common UNIX Printing System (CUPS). | |
5 | * | |
3d052e43 | 6 | * Copyright 2007-2008 by Apple Inc. |
b86bc4cf | 7 | * Copyright 1997-2007 by Easy Software Products. |
ecdc0628 | 8 | * |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 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/". | |
ecdc0628 | 14 | * |
15 | * This file is subject to the Apple OS-Developed Software exception. | |
16 | * | |
17 | * Contents: | |
18 | * | |
3d052e43 MS |
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 | * cupsGetResponse() - Get a response to an IPP request. | |
23 | * cupsReadResponseData() - Read additional data after the IPP response. | |
24 | * cupsSendRequest() - Send an IPP request. | |
25 | * cupsWriteRequestData() - Write additional data after an IPP request. | |
26 | * _cupsSetError() - Set the last IPP status code and status-message. | |
27 | * _cupsSetHTTPError() - Set the last error using the HTTP status. | |
ecdc0628 | 28 | */ |
29 | ||
30 | /* | |
31 | * Include necessary headers... | |
32 | */ | |
33 | ||
34 | #include "globals.h" | |
35 | #include "debug.h" | |
36 | #include <stdlib.h> | |
37 | #include <errno.h> | |
38 | #include <fcntl.h> | |
39 | #include <sys/stat.h> | |
40 | #if defined(WIN32) || defined(__EMX__) | |
41 | # include <io.h> | |
42 | #else | |
43 | # include <unistd.h> | |
44 | #endif /* WIN32 || __EMX__ */ | |
b94498cf | 45 | #ifndef O_BINARY |
46 | # define O_BINARY 0 | |
47 | #endif /* O_BINARY */ | |
ecdc0628 | 48 | |
49 | ||
50 | /* | |
51 | * 'cupsDoFileRequest()' - Do an IPP request with a file. | |
52 | * | |
53 | * This function sends the IPP request to the specified server, retrying | |
568fa3fa | 54 | * and authenticating as necessary. The request is freed with @link ippDelete@ |
ecdc0628 | 55 | * after receiving a valid IPP response. |
56 | */ | |
57 | ||
58 | ipp_t * /* O - Response data */ | |
568fa3fa | 59 | cupsDoFileRequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
ecdc0628 | 60 | ipp_t *request, /* I - IPP request */ |
61 | const char *resource, /* I - HTTP resource for POST */ | |
568fa3fa | 62 | const char *filename) /* I - File to send or @code NULL@ for none */ |
b94498cf | 63 | { |
64 | ipp_t *response; /* IPP response data */ | |
65 | int infile; /* Input file */ | |
66 | ||
67 | ||
a603edef MS |
68 | DEBUG_printf(("cupsDoFileRequest(http=%p, request=%p(%s), resource=\"%s\", " |
69 | "filename=\"%s\")\n", http, request, | |
70 | request ? ippOpString(request->request.op.operation_id) : "?", | |
71 | resource ? resource : "(null)", | |
72 | filename ? filename : "(null)")); | |
73 | ||
b94498cf | 74 | if (filename) |
75 | { | |
76 | if ((infile = open(filename, O_RDONLY | O_BINARY)) < 0) | |
77 | { | |
78 | /* | |
79 | * Can't get file information! | |
80 | */ | |
81 | ||
82 | _cupsSetError(errno == ENOENT ? IPP_NOT_FOUND : IPP_NOT_AUTHORIZED, | |
749b1e90 | 83 | NULL, 0); |
b94498cf | 84 | |
85 | ippDelete(request); | |
86 | ||
87 | return (NULL); | |
88 | } | |
89 | } | |
90 | else | |
91 | infile = -1; | |
92 | ||
93 | response = cupsDoIORequest(http, request, resource, infile, -1); | |
94 | ||
95 | if (infile >= 0) | |
96 | close(infile); | |
97 | ||
98 | return (response); | |
99 | } | |
100 | ||
101 | ||
102 | /* | |
103 | * 'cupsDoIORequest()' - Do an IPP request with file descriptors. | |
104 | * | |
105 | * This function sends the IPP request to the specified server, retrying | |
106 | * and authenticating as necessary. The request is freed with ippDelete() | |
107 | * after receiving a valid IPP response. | |
108 | * | |
109 | * If "infile" is a valid file descriptor, cupsDoIORequest() copies | |
110 | * all of the data from the file after the IPP request message. | |
111 | * | |
112 | * If "outfile" is a valid file descriptor, cupsDoIORequest() copies | |
113 | * all of the data after the IPP response message to the file. | |
114 | * | |
426c6a59 | 115 | * @since CUPS 1.3/Mac OS X 10.5@ |
b94498cf | 116 | */ |
117 | ||
118 | ipp_t * /* O - Response data */ | |
568fa3fa | 119 | cupsDoIORequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
b94498cf | 120 | ipp_t *request, /* I - IPP request */ |
121 | const char *resource, /* I - HTTP resource for POST */ | |
122 | int infile, /* I - File to read from or -1 for none */ | |
123 | int outfile) /* I - File to write to or -1 for none */ | |
ecdc0628 | 124 | { |
3d052e43 MS |
125 | ipp_t *response = NULL; /* IPP response data */ |
126 | size_t length = 0; /* Content-Length value */ | |
ecdc0628 | 127 | http_status_t status; /* Status of HTTP request */ |
ecdc0628 | 128 | struct stat fileinfo; /* File information */ |
129 | int bytes; /* Number of bytes read/written */ | |
a74454a7 | 130 | char buffer[32768]; /* Output buffer */ |
ecdc0628 | 131 | |
132 | ||
a603edef | 133 | DEBUG_printf(("cupsDoIORequest(http=%p, request=%p(%s), resource=\"%s\", " |
3d052e43 | 134 | "infile=%d, outfile=%d)\n", http, request, |
a603edef | 135 | request ? ippOpString(request->request.op.operation_id) : "?", |
3d052e43 MS |
136 | resource ? resource : "(null)", infile, outfile)); |
137 | ||
138 | /* | |
139 | * Range check input... | |
140 | */ | |
ecdc0628 | 141 | |
3d052e43 | 142 | if (!request || !resource) |
ecdc0628 | 143 | { |
3d052e43 | 144 | ippDelete(request); |
ecdc0628 | 145 | |
749b1e90 | 146 | _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); |
ecdc0628 | 147 | |
148 | return (NULL); | |
149 | } | |
150 | ||
3d052e43 MS |
151 | /* |
152 | * Get the default connection as needed... | |
153 | */ | |
154 | ||
155 | if (!http) | |
a603edef MS |
156 | if ((http = _cupsConnect()) == NULL) |
157 | return (NULL); | |
3d052e43 | 158 | |
ecdc0628 | 159 | /* |
160 | * See if we have a file to send... | |
161 | */ | |
162 | ||
b94498cf | 163 | if (infile >= 0) |
ecdc0628 | 164 | { |
b94498cf | 165 | if (fstat(infile, &fileinfo)) |
ecdc0628 | 166 | { |
167 | /* | |
168 | * Can't get file information! | |
169 | */ | |
170 | ||
3d052e43 | 171 | _cupsSetError(errno == EBADF ? IPP_NOT_FOUND : IPP_NOT_AUTHORIZED, |
749b1e90 | 172 | NULL, 0); |
ecdc0628 | 173 | |
174 | ippDelete(request); | |
175 | ||
176 | return (NULL); | |
177 | } | |
178 | ||
179 | #ifdef WIN32 | |
180 | if (fileinfo.st_mode & _S_IFDIR) | |
181 | #else | |
182 | if (S_ISDIR(fileinfo.st_mode)) | |
183 | #endif /* WIN32 */ | |
184 | { | |
185 | /* | |
186 | * Can't send a directory... | |
187 | */ | |
188 | ||
189 | ippDelete(request); | |
190 | ||
749b1e90 | 191 | _cupsSetError(IPP_NOT_POSSIBLE, strerror(EISDIR), 0); |
ecdc0628 | 192 | |
193 | return (NULL); | |
194 | } | |
3d052e43 MS |
195 | |
196 | #ifndef WIN32 | |
197 | if (!S_ISREG(fileinfo.st_mode)) | |
198 | length = 0; /* Chunk when piping */ | |
199 | else | |
200 | #endif /* !WIN32 */ | |
201 | length = ippLength(request) + fileinfo.st_size; | |
202 | } | |
203 | else | |
204 | length = ippLength(request); | |
205 | ||
52f6f666 MS |
206 | DEBUG_printf(("cupsDoIORequest: Request length=%ld, total length=%ld", |
207 | (long)ippLength(request), (long)length)); | |
208 | ||
3d052e43 MS |
209 | /* |
210 | * Loop until we can send the request without authorization problems. | |
211 | */ | |
212 | ||
213 | while (response == NULL) | |
214 | { | |
b19ccc9e | 215 | DEBUG_puts("cupsDoIORequest: setup..."); |
3d052e43 MS |
216 | |
217 | /* | |
218 | * Send the request... | |
219 | */ | |
220 | ||
221 | status = cupsSendRequest(http, request, resource, length); | |
222 | ||
b19ccc9e | 223 | DEBUG_printf(("cupsDoIORequest: status=%d\n", status)); |
3d052e43 MS |
224 | |
225 | if (status == HTTP_CONTINUE && request->state == IPP_DATA && infile >= 0) | |
226 | { | |
b19ccc9e | 227 | DEBUG_puts("cupsDoIORequest: file write..."); |
3d052e43 MS |
228 | |
229 | /* | |
230 | * Send the file with the request... | |
231 | */ | |
232 | ||
233 | #ifndef WIN32 | |
234 | if (S_ISREG(fileinfo.st_mode)) | |
235 | #endif /* WIN32 */ | |
236 | lseek(infile, 0, SEEK_SET); | |
237 | ||
238 | while ((bytes = (int)read(infile, buffer, sizeof(buffer))) > 0) | |
239 | { | |
240 | if (httpCheck(http)) | |
241 | { | |
242 | if ((status = httpUpdate(http)) != HTTP_CONTINUE) | |
243 | break; | |
244 | } | |
245 | ||
246 | if (httpWrite2(http, buffer, bytes) < bytes) | |
247 | break; | |
248 | } | |
249 | } | |
250 | ||
251 | /* | |
252 | * Get the server's response... | |
253 | */ | |
254 | ||
255 | if (status == HTTP_CONTINUE || status == HTTP_OK) | |
256 | { | |
257 | response = cupsGetResponse(http, resource); | |
258 | status = http->status; | |
259 | } | |
260 | ||
ee571f26 MS |
261 | if (status == HTTP_FORBIDDEN || status == HTTP_ERROR || |
262 | status == HTTP_SERVICE_UNAVAILABLE) | |
263 | { | |
264 | _cupsSetHTTPError(status); | |
568fa3fa | 265 | break; |
ee571f26 | 266 | } |
568fa3fa | 267 | |
3d052e43 MS |
268 | if (response) |
269 | { | |
270 | if (outfile >= 0) | |
271 | { | |
272 | /* | |
273 | * Write trailing data to file... | |
274 | */ | |
275 | ||
276 | while ((bytes = (int)httpRead2(http, buffer, sizeof(buffer))) > 0) | |
277 | if (write(outfile, buffer, bytes) < bytes) | |
278 | break; | |
279 | } | |
280 | else | |
281 | { | |
282 | /* | |
283 | * Flush any remaining data... | |
284 | */ | |
285 | ||
286 | httpFlush(http); | |
287 | } | |
288 | } | |
289 | } | |
290 | ||
291 | /* | |
292 | * Delete the original request and return the response... | |
293 | */ | |
294 | ||
295 | ippDelete(request); | |
296 | ||
297 | return (response); | |
298 | } | |
299 | ||
300 | ||
301 | /* | |
302 | * 'cupsDoRequest()' - Do an IPP request. | |
303 | * | |
304 | * This function sends the IPP request to the specified server, retrying | |
305 | * and authenticating as necessary. The request is freed with ippDelete() | |
306 | * after receiving a valid IPP response. | |
307 | */ | |
308 | ||
309 | ipp_t * /* O - Response data */ | |
568fa3fa | 310 | cupsDoRequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
3d052e43 MS |
311 | ipp_t *request, /* I - IPP request */ |
312 | const char *resource) /* I - HTTP resource for POST */ | |
313 | { | |
a603edef MS |
314 | DEBUG_printf(("cupsDoRequest(http=%p, request=%p(%s), resource=\"%s\")\n", |
315 | http, request, | |
316 | request ? ippOpString(request->request.op.operation_id) : "?", | |
317 | resource ? resource : "(null)")); | |
318 | ||
b19ccc9e | 319 | return (cupsDoIORequest(http, request, resource, -1, -1)); |
3d052e43 MS |
320 | } |
321 | ||
322 | ||
323 | /* | |
324 | * 'cupsGetResponse()' - Get a response to an IPP request. | |
325 | * | |
326 | * Use this function to get the response for an IPP request sent using | |
327 | * cupsSendDocument() or cupsSendRequest(). For requests that return | |
328 | * additional data, use httpRead() after getting a successful response. | |
329 | * | |
330 | * @since CUPS 1.4@ | |
331 | */ | |
332 | ||
568fa3fa MS |
333 | ipp_t * /* O - Response or @code NULL@ on HTTP error */ |
334 | cupsGetResponse(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ | |
3d052e43 MS |
335 | const char *resource) /* I - HTTP resource for POST */ |
336 | { | |
337 | http_status_t status; /* HTTP status */ | |
338 | ipp_state_t state; /* IPP read state */ | |
339 | ipp_t *response = NULL; /* IPP response */ | |
340 | ||
341 | ||
a603edef MS |
342 | DEBUG_printf(("cupsGetReponse(http=%p, resource=\"%s\")\n", http, |
343 | resource ? resource : "(null)")); | |
3d052e43 MS |
344 | |
345 | /* | |
346 | * Connect to the default server as needed... | |
347 | */ | |
348 | ||
349 | if (!http) | |
350 | http = _cupsConnect(); | |
351 | ||
352 | if (!http || (http->state != HTTP_POST_RECV && http->state != HTTP_POST_SEND)) | |
353 | return (NULL); | |
354 | ||
355 | /* | |
356 | * Check for an unfinished chunked request... | |
357 | */ | |
358 | ||
359 | if (http->data_encoding == HTTP_ENCODE_CHUNKED) | |
360 | { | |
361 | /* | |
362 | * Send a 0-length chunk to finish off the request... | |
363 | */ | |
364 | ||
365 | DEBUG_puts("cupsGetResponse: Finishing chunked POST..."); | |
366 | ||
367 | if (httpWrite2(http, "", 0) < 0) | |
368 | return (NULL); | |
369 | } | |
370 | ||
371 | /* | |
372 | * Wait for a response from the server... | |
373 | */ | |
374 | ||
1ff0402e MS |
375 | DEBUG_printf(("cupsGetResponse: Update loop, http->status=%d...\n", |
376 | http->status)); | |
3d052e43 | 377 | |
1ff0402e MS |
378 | status = http->status; |
379 | while (status == HTTP_CONTINUE) | |
380 | status = httpUpdate(http); | |
3d052e43 | 381 | |
a603edef | 382 | DEBUG_printf(("cupsGetResponse: status=%d\n", status)); |
3d052e43 MS |
383 | |
384 | if (status == HTTP_OK) | |
385 | { | |
386 | /* | |
387 | * Get the IPP response... | |
388 | */ | |
389 | ||
390 | response = ippNew(); | |
391 | ||
392 | while ((state = ippRead(http, response)) != IPP_DATA) | |
393 | if (state == IPP_ERROR) | |
394 | break; | |
395 | ||
396 | if (state == IPP_ERROR) | |
397 | { | |
398 | /* | |
399 | * Delete the response... | |
400 | */ | |
401 | ||
a603edef | 402 | DEBUG_puts("cupsGetResponse: IPP read error!"); |
3d052e43 MS |
403 | |
404 | ippDelete(response); | |
405 | response = NULL; | |
406 | ||
749b1e90 | 407 | _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0); |
3d052e43 MS |
408 | } |
409 | } | |
410 | else if (status != HTTP_ERROR) | |
411 | { | |
412 | /* | |
413 | * Flush any error message... | |
414 | */ | |
415 | ||
416 | httpFlush(http); | |
417 | ||
418 | /* | |
419 | * Then handle encryption and authentication... | |
420 | */ | |
421 | ||
422 | if (status == HTTP_UNAUTHORIZED) | |
423 | { | |
424 | /* | |
425 | * See if we can do authentication... | |
426 | */ | |
427 | ||
568fa3fa MS |
428 | int auth_result; |
429 | ||
3d052e43 MS |
430 | DEBUG_puts("cupsGetResponse: Need authorization..."); |
431 | ||
ae71f5de | 432 | if ((auth_result = cupsDoAuthentication(http, "POST", resource)) == 0) |
3d052e43 | 433 | httpReconnect(http); |
568fa3fa MS |
434 | else if (auth_result < 0) |
435 | http->status = status = HTTP_FORBIDDEN; | |
3d052e43 MS |
436 | } |
437 | ||
438 | #ifdef HAVE_SSL | |
439 | else if (status == HTTP_UPGRADE_REQUIRED) | |
440 | { | |
441 | /* | |
442 | * Force a reconnect with encryption... | |
443 | */ | |
444 | ||
445 | DEBUG_puts("cupsGetResponse: Need encryption..."); | |
446 | ||
447 | if (!httpReconnect(http)) | |
448 | httpEncryption(http, HTTP_ENCRYPT_REQUIRED); | |
449 | } | |
450 | #endif /* HAVE_SSL */ | |
451 | } | |
452 | ||
453 | if (response) | |
454 | { | |
455 | ipp_attribute_t *attr; /* status-message attribute */ | |
456 | ||
457 | ||
458 | attr = ippFindAttribute(response, "status-message", IPP_TAG_TEXT); | |
459 | ||
a603edef MS |
460 | DEBUG_printf(("cupsGetResponse: status-code=%s, status-message=\"%s\"\n", |
461 | ippErrorString(response->request.status.status_code), | |
462 | attr ? attr->values[0].string.text : "")); | |
463 | ||
3d052e43 | 464 | _cupsSetError(response->request.status.status_code, |
749b1e90 MS |
465 | attr ? attr->values[0].string.text : |
466 | ippErrorString(response->request.status.status_code), 0); | |
3d052e43 MS |
467 | } |
468 | else if (status != HTTP_OK) | |
469 | _cupsSetHTTPError(status); | |
470 | ||
471 | return (response); | |
472 | } | |
473 | ||
474 | ||
475 | /* | |
476 | * 'cupsReadResponseData()' - Read additional data after the IPP response. | |
477 | * | |
478 | * This function is used after cupsGetResponse() to read the PPD or document | |
479 | * files for CUPS_GET_PPD and CUPS_GET_DOCUMENT requests, respectively. | |
480 | * | |
481 | * @since CUPS 1.4@ | |
482 | */ | |
483 | ||
484 | ssize_t /* O - Bytes read, 0 on EOF, -1 on error */ | |
485 | cupsReadResponseData( | |
568fa3fa | 486 | http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
3d052e43 MS |
487 | char *buffer, /* I - Buffer to use */ |
488 | size_t length) /* I - Number of bytes to read */ | |
489 | { | |
490 | /* | |
491 | * Get the default connection as needed... | |
492 | */ | |
493 | ||
a603edef MS |
494 | DEBUG_printf(("cupsReadResponseData(http=%p, buffer=%p, " |
495 | "length=" CUPS_LLFMT ")\n", http, buffer, CUPS_LLCAST length)); | |
496 | ||
3d052e43 MS |
497 | if (!http) |
498 | { | |
499 | _cups_globals_t *cg = _cupsGlobals(); | |
500 | /* Pointer to library globals */ | |
501 | ||
502 | if ((http = cg->http) == NULL) | |
503 | { | |
749b1e90 | 504 | _cupsSetError(IPP_INTERNAL_ERROR, _("No active connection"), 1); |
3d052e43 MS |
505 | return (-1); |
506 | } | |
ecdc0628 | 507 | } |
ecdc0628 | 508 | |
3d052e43 MS |
509 | /* |
510 | * Then read from the HTTP connection... | |
511 | */ | |
512 | ||
513 | return (httpRead2(http, buffer, length)); | |
514 | } | |
515 | ||
516 | ||
517 | /* | |
518 | * 'cupsSendRequest()' - Send an IPP request. | |
519 | * | |
520 | * Use httpWrite() to write any additional data (document, PPD file, etc.) | |
521 | * for the request, cupsGetResponse() to get the IPP response, and httpRead() | |
522 | * to read any additional data following the response. Only one request can be | |
523 | * sent/queued at a time. | |
524 | * | |
525 | * Unlike cupsDoFileRequest(), cupsDoIORequest(), and cupsDoRequest(), the | |
526 | * request is not freed. | |
527 | * | |
528 | * @since CUPS 1.4@ | |
529 | */ | |
530 | ||
531 | http_status_t /* O - Initial HTTP status */ | |
568fa3fa | 532 | cupsSendRequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
3d052e43 MS |
533 | ipp_t *request, /* I - IPP request */ |
534 | const char *resource, /* I - Resource path */ | |
ae71f5de | 535 | size_t length) /* I - Length of data to follow or @code CUPS_LENGTH_VARIABLE@ */ |
3d052e43 MS |
536 | { |
537 | http_status_t status; /* Status of HTTP request */ | |
538 | int got_status; /* Did we get the status? */ | |
539 | ipp_state_t state; /* State of IPP processing */ | |
540 | http_status_t expect; /* Expect: header to use */ | |
541 | ||
542 | ||
a603edef | 543 | DEBUG_printf(("cupsSendRequest(http=%p, request=%p(%s), resource=\"%s\", " |
3d052e43 | 544 | "length=" CUPS_LLFMT ")\n", http, request, |
a603edef | 545 | request ? ippOpString(request->request.op.operation_id) : "?", |
3d052e43 MS |
546 | resource ? resource : "(null)", CUPS_LLCAST length)); |
547 | ||
548 | /* | |
549 | * Range check input... | |
550 | */ | |
551 | ||
552 | if (!request || !resource) | |
553 | { | |
749b1e90 | 554 | _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); |
3d052e43 MS |
555 | |
556 | return (HTTP_ERROR); | |
557 | } | |
558 | ||
559 | /* | |
560 | * Get the default connection as needed... | |
561 | */ | |
562 | ||
563 | if (!http) | |
a603edef MS |
564 | if ((http = _cupsConnect()) == NULL) |
565 | return (HTTP_SERVICE_UNAVAILABLE); | |
3d052e43 | 566 | |
7594b224 | 567 | #ifdef HAVE_SSL |
568 | /* | |
569 | * See if we have an auth-info attribute and are communicating over | |
570 | * a non-local link. If so, encrypt the link so that we can pass | |
571 | * the authentication information securely... | |
572 | */ | |
573 | ||
574 | if (ippFindAttribute(request, "auth-info", IPP_TAG_TEXT) && | |
575 | !httpAddrLocalhost(http->hostaddr) && !http->tls && | |
576 | httpEncryption(http, HTTP_ENCRYPT_REQUIRED)) | |
b19ccc9e MS |
577 | { |
578 | _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0); | |
ee571f26 | 579 | return (HTTP_SERVICE_UNAVAILABLE); |
b19ccc9e | 580 | } |
7594b224 | 581 | #endif /* HAVE_SSL */ |
582 | ||
1ff0402e MS |
583 | /* |
584 | * Reconnect if the last response had a "Connection: close"... | |
585 | */ | |
586 | ||
587 | if (!strcasecmp(http->fields[HTTP_FIELD_CONNECTION], "close")) | |
588 | if (httpReconnect(http)) | |
b19ccc9e MS |
589 | { |
590 | _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0); | |
1ff0402e | 591 | return (HTTP_SERVICE_UNAVAILABLE); |
b19ccc9e | 592 | } |
1ff0402e | 593 | |
ecdc0628 | 594 | /* |
595 | * Loop until we can send the request without authorization problems. | |
596 | */ | |
597 | ||
3d052e43 | 598 | expect = HTTP_CONTINUE; |
ecdc0628 | 599 | |
3d052e43 | 600 | for (;;) |
ecdc0628 | 601 | { |
a603edef | 602 | DEBUG_puts("cupsSendRequest: Setup..."); |
ecdc0628 | 603 | |
604 | /* | |
605 | * Setup the HTTP variables needed... | |
606 | */ | |
607 | ||
ecdc0628 | 608 | httpClearFields(http); |
609 | httpSetLength(http, length); | |
610 | httpSetField(http, HTTP_FIELD_CONTENT_TYPE, "application/ipp"); | |
611 | httpSetField(http, HTTP_FIELD_AUTHORIZATION, http->authstring); | |
f301802f | 612 | httpSetExpect(http, expect); |
ecdc0628 | 613 | |
3d052e43 | 614 | DEBUG_printf(("cupsSendRequest: authstring=\"%s\"\n", http->authstring)); |
ecdc0628 | 615 | |
616 | /* | |
617 | * Try the request... | |
618 | */ | |
619 | ||
a603edef | 620 | DEBUG_puts("cupsSendRequest: Sending HTTP POST..."); |
ecdc0628 | 621 | |
622 | if (httpPost(http, resource)) | |
623 | { | |
624 | if (httpReconnect(http)) | |
b19ccc9e MS |
625 | { |
626 | _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0); | |
ee571f26 | 627 | return (HTTP_SERVICE_UNAVAILABLE); |
b19ccc9e | 628 | } |
ecdc0628 | 629 | else |
630 | continue; | |
631 | } | |
632 | ||
633 | /* | |
d6ae789d | 634 | * Send the IPP data... |
ecdc0628 | 635 | */ |
636 | ||
a603edef | 637 | DEBUG_puts("cupsSendRequest: Writing IPP request..."); |
b423cd4c | 638 | |
d6ae789d | 639 | request->state = IPP_IDLE; |
640 | status = HTTP_CONTINUE; | |
641 | got_status = 0; | |
642 | ||
643 | while ((state = ippWrite(http, request)) != IPP_DATA) | |
644 | if (state == IPP_ERROR) | |
645 | break; | |
646 | else if (httpCheck(http)) | |
647 | { | |
648 | got_status = 1; | |
649 | ||
650 | if ((status = httpUpdate(http)) != HTTP_CONTINUE) | |
651 | break; | |
652 | } | |
653 | ||
3d052e43 MS |
654 | /* |
655 | * Wait up to 1 second to get the 100-continue response as needed... | |
656 | */ | |
ecdc0628 | 657 | |
3d052e43 MS |
658 | if (!got_status && expect == HTTP_CONTINUE) |
659 | { | |
a603edef MS |
660 | DEBUG_puts("cupsSendRequest: Waiting for 100-continue..."); |
661 | ||
d6ae789d | 662 | if (httpWait(http, 1000)) |
663 | status = httpUpdate(http); | |
664 | } | |
665 | else if (httpCheck(http)) | |
666 | status = httpUpdate(http); | |
ecdc0628 | 667 | |
a603edef MS |
668 | DEBUG_printf(("cupsSendRequest: status=%d\n", status)); |
669 | ||
ecdc0628 | 670 | /* |
3d052e43 | 671 | * Process the current HTTP status... |
ecdc0628 | 672 | */ |
673 | ||
3d052e43 | 674 | switch (status) |
ecdc0628 | 675 | { |
3d052e43 MS |
676 | case HTTP_ERROR : |
677 | case HTTP_CONTINUE : | |
678 | case HTTP_OK : | |
679 | return (status); | |
ecdc0628 | 680 | |
3d052e43 MS |
681 | case HTTP_UNAUTHORIZED : |
682 | if (!cupsDoAuthentication(http, "POST", resource)) | |
683 | if (httpReconnect(http)) | |
b19ccc9e MS |
684 | { |
685 | _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0); | |
686 | return (HTTP_SERVICE_UNAVAILABLE); | |
687 | } | |
ecdc0628 | 688 | |
3d052e43 | 689 | return (status); |
89d46774 | 690 | |
ecdc0628 | 691 | #ifdef HAVE_SSL |
3d052e43 MS |
692 | case HTTP_UPGRADE_REQUIRED : |
693 | /* | |
694 | * Flush any error message, reconnect, and then upgrade with | |
695 | * encryption... | |
696 | */ | |
ecdc0628 | 697 | |
3d052e43 | 698 | if (httpReconnect(http)) |
b19ccc9e MS |
699 | { |
700 | _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0); | |
701 | return (HTTP_SERVICE_UNAVAILABLE); | |
702 | } | |
ecdc0628 | 703 | |
3d052e43 | 704 | httpEncryption(http, HTTP_ENCRYPT_REQUIRED); |
ecdc0628 | 705 | |
3d052e43 | 706 | return (status); |
ecdc0628 | 707 | #endif /* HAVE_SSL */ |
f301802f | 708 | |
3d052e43 MS |
709 | case HTTP_EXPECTATION_FAILED : |
710 | /* | |
711 | * Don't try using the Expect: header the next time around... | |
712 | */ | |
ecdc0628 | 713 | |
3d052e43 | 714 | expect = (http_status_t)0; |
52f6f666 MS |
715 | |
716 | if (httpReconnect(http)) | |
717 | { | |
718 | _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0); | |
719 | return (HTTP_SERVICE_UNAVAILABLE); | |
720 | } | |
a603edef | 721 | break; |
ecdc0628 | 722 | |
3d052e43 MS |
723 | default : |
724 | /* | |
725 | * Some other error... | |
726 | */ | |
ecdc0628 | 727 | |
3d052e43 | 728 | return (status); |
b94498cf | 729 | } |
730 | } | |
3d052e43 MS |
731 | } |
732 | ||
733 | ||
734 | /* | |
735 | * 'cupsWriteRequestData()' - Write additional data after an IPP request. | |
736 | * | |
568fa3fa MS |
737 | * This function is used after @link cupsSendRequest@ to provide a PPD and |
738 | * after @link cupsStartDocument@ to provide a document file. | |
3d052e43 MS |
739 | * |
740 | * @since CUPS 1.4@ | |
741 | */ | |
ecdc0628 | 742 | |
568fa3fa | 743 | http_status_t /* O - @code HTTP_CONTINUE@ if OK or HTTP status on error */ |
3d052e43 | 744 | cupsWriteRequestData( |
568fa3fa | 745 | http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
3d052e43 MS |
746 | const char *buffer, /* I - Bytes to write */ |
747 | size_t length) /* I - Number of bytes to write */ | |
748 | { | |
ecdc0628 | 749 | /* |
3d052e43 | 750 | * Get the default connection as needed... |
ecdc0628 | 751 | */ |
ecdc0628 | 752 | |
a603edef MS |
753 | DEBUG_printf(("cupsWriteRequestData(http=%p, buffer=%p, " |
754 | "length=" CUPS_LLFMT ")\n", http, buffer, CUPS_LLCAST length)); | |
755 | ||
3d052e43 | 756 | if (!http) |
ecdc0628 | 757 | { |
3d052e43 MS |
758 | _cups_globals_t *cg = _cupsGlobals(); |
759 | /* Pointer to library globals */ | |
ecdc0628 | 760 | |
3d052e43 MS |
761 | if ((http = cg->http) == NULL) |
762 | { | |
749b1e90 | 763 | _cupsSetError(IPP_INTERNAL_ERROR, _("No active connection"), 1); |
3d052e43 MS |
764 | return (HTTP_ERROR); |
765 | } | |
ecdc0628 | 766 | } |
ecdc0628 | 767 | |
3d052e43 MS |
768 | /* |
769 | * Then write to the HTTP connection... | |
770 | */ | |
ecdc0628 | 771 | |
3d052e43 MS |
772 | if (httpWrite2(http, buffer, length) < 0) |
773 | return (HTTP_ERROR); | |
ecdc0628 | 774 | |
3d052e43 MS |
775 | /* |
776 | * Finally, check if we have any pending data from the server... | |
777 | */ | |
ecdc0628 | 778 | |
3d052e43 MS |
779 | if (httpCheck(http)) |
780 | return (httpUpdate(http)); | |
781 | else | |
782 | return (HTTP_CONTINUE); | |
ecdc0628 | 783 | } |
784 | ||
785 | ||
786 | /* | |
787 | * '_cupsSetError()' - Set the last IPP status code and status-message. | |
788 | */ | |
789 | ||
790 | void | |
791 | _cupsSetError(ipp_status_t status, /* I - IPP status code */ | |
749b1e90 MS |
792 | const char *message, /* I - status-message value */ |
793 | int localize) /* I - Localize the message? */ | |
ecdc0628 | 794 | { |
795 | _cups_globals_t *cg; /* Global data */ | |
796 | ||
797 | ||
749b1e90 MS |
798 | if (!message && errno) |
799 | { | |
800 | message = strerror(errno); | |
801 | localize = 0; | |
802 | } | |
803 | ||
ecdc0628 | 804 | cg = _cupsGlobals(); |
805 | cg->last_error = status; | |
806 | ||
807 | if (cg->last_status_message) | |
808 | { | |
749b1e90 | 809 | _cupsStrFree(cg->last_status_message); |
ecdc0628 | 810 | |
811 | cg->last_status_message = NULL; | |
812 | } | |
813 | ||
814 | if (message) | |
749b1e90 MS |
815 | { |
816 | if (localize) | |
817 | { | |
818 | /* | |
819 | * Get the message catalog... | |
820 | */ | |
821 | ||
822 | if (!cg->lang_default) | |
823 | cg->lang_default = cupsLangDefault(); | |
824 | ||
825 | cg->last_status_message = _cupsStrAlloc(_cupsLangString(cg->lang_default, | |
826 | message)); | |
827 | } | |
828 | else | |
829 | cg->last_status_message = _cupsStrAlloc(message); | |
830 | } | |
a603edef MS |
831 | |
832 | DEBUG_printf(("_cupsSetError: last_error=%s, last_status_message=\"%s\"\n", | |
749b1e90 MS |
833 | ippErrorString(cg->last_error), |
834 | cg->last_status_message ? cg->last_status_message : "")); | |
ecdc0628 | 835 | } |
836 | ||
837 | ||
838 | /* | |
355e94dc MS |
839 | * '_cupsSetHTTPError()' - Set the last error using the HTTP status. |
840 | */ | |
841 | ||
842 | void | |
843 | _cupsSetHTTPError(http_status_t status) /* I - HTTP status code */ | |
844 | { | |
845 | switch (status) | |
846 | { | |
847 | case HTTP_NOT_FOUND : | |
749b1e90 | 848 | _cupsSetError(IPP_NOT_FOUND, httpStatus(status), 0); |
355e94dc MS |
849 | break; |
850 | ||
851 | case HTTP_UNAUTHORIZED : | |
749b1e90 | 852 | _cupsSetError(IPP_NOT_AUTHORIZED, httpStatus(status), 0); |
355e94dc MS |
853 | break; |
854 | ||
855 | case HTTP_FORBIDDEN : | |
749b1e90 | 856 | _cupsSetError(IPP_FORBIDDEN, httpStatus(status), 0); |
355e94dc MS |
857 | break; |
858 | ||
859 | case HTTP_BAD_REQUEST : | |
749b1e90 | 860 | _cupsSetError(IPP_BAD_REQUEST, httpStatus(status), 0); |
355e94dc MS |
861 | break; |
862 | ||
863 | case HTTP_REQUEST_TOO_LARGE : | |
749b1e90 | 864 | _cupsSetError(IPP_REQUEST_VALUE, httpStatus(status), 0); |
355e94dc MS |
865 | break; |
866 | ||
867 | case HTTP_NOT_IMPLEMENTED : | |
749b1e90 | 868 | _cupsSetError(IPP_OPERATION_NOT_SUPPORTED, httpStatus(status), 0); |
355e94dc MS |
869 | break; |
870 | ||
871 | case HTTP_NOT_SUPPORTED : | |
749b1e90 | 872 | _cupsSetError(IPP_VERSION_NOT_SUPPORTED, httpStatus(status), 0); |
355e94dc MS |
873 | break; |
874 | ||
875 | default : | |
876 | DEBUG_printf(("HTTP error %d mapped to IPP_SERVICE_UNAVAILABLE!\n", | |
877 | status)); | |
749b1e90 | 878 | _cupsSetError(IPP_SERVICE_UNAVAILABLE, httpStatus(status), 0); |
355e94dc MS |
879 | break; |
880 | } | |
881 | } | |
882 | ||
883 | ||
884 | /* | |
b19ccc9e | 885 | * End of "$Id: request.c 7946 2008-09-16 23:27:54Z mike $". |
ecdc0628 | 886 | */ |