]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * "$Id: request.c 7946 2008-09-16 23:27:54Z mike $" | |
3 | * | |
4 | * IPP utilities for the Common UNIX Printing System (CUPS). | |
5 | * | |
6 | * Copyright 2007-2008 by Apple Inc. | |
7 | * Copyright 1997-2007 by Easy Software Products. | |
8 | * | |
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/". | |
14 | * | |
15 | * This file is subject to the Apple OS-Developed Software exception. | |
16 | * | |
17 | * Contents: | |
18 | * | |
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. | |
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__ */ | |
45 | #ifndef O_BINARY | |
46 | # define O_BINARY 0 | |
47 | #endif /* O_BINARY */ | |
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 | |
54 | * and authenticating as necessary. The request is freed with @link ippDelete@ | |
55 | * after receiving a valid IPP response. | |
56 | */ | |
57 | ||
58 | ipp_t * /* O - Response data */ | |
59 | cupsDoFileRequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ | |
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 @code NULL@ for none */ | |
63 | { | |
64 | ipp_t *response; /* IPP response data */ | |
65 | int infile; /* Input file */ | |
66 | ||
67 | ||
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 | ||
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, | |
83 | NULL, 0); | |
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 | * | |
115 | * @since CUPS 1.3@ | |
116 | */ | |
117 | ||
118 | ipp_t * /* O - Response data */ | |
119 | cupsDoIORequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ | |
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 */ | |
124 | { | |
125 | ipp_t *response = NULL; /* IPP response data */ | |
126 | size_t length = 0; /* Content-Length value */ | |
127 | http_status_t status; /* Status of HTTP request */ | |
128 | struct stat fileinfo; /* File information */ | |
129 | int bytes; /* Number of bytes read/written */ | |
130 | char buffer[32768]; /* Output buffer */ | |
131 | ||
132 | ||
133 | DEBUG_printf(("cupsDoIORequest(http=%p, request=%p(%s), resource=\"%s\", " | |
134 | "infile=%d, outfile=%d)\n", http, request, | |
135 | request ? ippOpString(request->request.op.operation_id) : "?", | |
136 | resource ? resource : "(null)", infile, outfile)); | |
137 | ||
138 | /* | |
139 | * Range check input... | |
140 | */ | |
141 | ||
142 | if (!request || !resource) | |
143 | { | |
144 | ippDelete(request); | |
145 | ||
146 | _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); | |
147 | ||
148 | return (NULL); | |
149 | } | |
150 | ||
151 | /* | |
152 | * Get the default connection as needed... | |
153 | */ | |
154 | ||
155 | if (!http) | |
156 | if ((http = _cupsConnect()) == NULL) | |
157 | return (NULL); | |
158 | ||
159 | /* | |
160 | * See if we have a file to send... | |
161 | */ | |
162 | ||
163 | if (infile >= 0) | |
164 | { | |
165 | if (fstat(infile, &fileinfo)) | |
166 | { | |
167 | /* | |
168 | * Can't get file information! | |
169 | */ | |
170 | ||
171 | _cupsSetError(errno == EBADF ? IPP_NOT_FOUND : IPP_NOT_AUTHORIZED, | |
172 | NULL, 0); | |
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 | ||
191 | _cupsSetError(IPP_NOT_POSSIBLE, strerror(EISDIR), 0); | |
192 | ||
193 | return (NULL); | |
194 | } | |
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 | ||
206 | DEBUG_printf(("cupsDoIORequest: Request length=%ld, total length=%ld", | |
207 | (long)ippLength(request), (long)length)); | |
208 | ||
209 | /* | |
210 | * Loop until we can send the request without authorization problems. | |
211 | */ | |
212 | ||
213 | while (response == NULL) | |
214 | { | |
215 | DEBUG_puts("cupsDoIORequest: setup..."); | |
216 | ||
217 | /* | |
218 | * Send the request... | |
219 | */ | |
220 | ||
221 | status = cupsSendRequest(http, request, resource, length); | |
222 | ||
223 | DEBUG_printf(("cupsDoIORequest: status=%d\n", status)); | |
224 | ||
225 | if (status == HTTP_CONTINUE && request->state == IPP_DATA && infile >= 0) | |
226 | { | |
227 | DEBUG_puts("cupsDoIORequest: file write..."); | |
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 | ||
261 | if (status == HTTP_FORBIDDEN || status == HTTP_ERROR || | |
262 | status == HTTP_SERVICE_UNAVAILABLE) | |
263 | { | |
264 | _cupsSetHTTPError(status); | |
265 | break; | |
266 | } | |
267 | ||
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 */ | |
310 | cupsDoRequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ | |
311 | ipp_t *request, /* I - IPP request */ | |
312 | const char *resource) /* I - HTTP resource for POST */ | |
313 | { | |
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 | ||
319 | return (cupsDoIORequest(http, request, resource, -1, -1)); | |
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 | ||
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@ */ | |
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 | ||
342 | DEBUG_printf(("cupsGetReponse(http=%p, resource=\"%s\")\n", http, | |
343 | resource ? resource : "(null)")); | |
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 | ||
375 | DEBUG_printf(("cupsGetResponse: Update loop, http->status=%d...\n", | |
376 | http->status)); | |
377 | ||
378 | status = http->status; | |
379 | while (status == HTTP_CONTINUE) | |
380 | status = httpUpdate(http); | |
381 | ||
382 | DEBUG_printf(("cupsGetResponse: status=%d\n", status)); | |
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 | ||
402 | DEBUG_puts("cupsGetResponse: IPP read error!"); | |
403 | ||
404 | ippDelete(response); | |
405 | response = NULL; | |
406 | ||
407 | _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0); | |
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 | ||
428 | int auth_result; | |
429 | ||
430 | DEBUG_puts("cupsGetResponse: Need authorization..."); | |
431 | ||
432 | if ((auth_result = cupsDoAuthentication(http, "POST", resource)) == 0) | |
433 | httpReconnect(http); | |
434 | else if (auth_result < 0) | |
435 | http->status = status = HTTP_FORBIDDEN; | |
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 | ||
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 | ||
464 | _cupsSetError(response->request.status.status_code, | |
465 | attr ? attr->values[0].string.text : | |
466 | ippErrorString(response->request.status.status_code), 0); | |
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( | |
486 | http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ | |
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 | ||
494 | DEBUG_printf(("cupsReadResponseData(http=%p, buffer=%p, " | |
495 | "length=" CUPS_LLFMT ")\n", http, buffer, CUPS_LLCAST length)); | |
496 | ||
497 | if (!http) | |
498 | { | |
499 | _cups_globals_t *cg = _cupsGlobals(); | |
500 | /* Pointer to library globals */ | |
501 | ||
502 | if ((http = cg->http) == NULL) | |
503 | { | |
504 | _cupsSetError(IPP_INTERNAL_ERROR, _("No active connection"), 1); | |
505 | return (-1); | |
506 | } | |
507 | } | |
508 | ||
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 */ | |
532 | cupsSendRequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ | |
533 | ipp_t *request, /* I - IPP request */ | |
534 | const char *resource, /* I - Resource path */ | |
535 | size_t length) /* I - Length of data to follow or @code CUPS_LENGTH_VARIABLE@ */ | |
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 | ||
543 | DEBUG_printf(("cupsSendRequest(http=%p, request=%p(%s), resource=\"%s\", " | |
544 | "length=" CUPS_LLFMT ")\n", http, request, | |
545 | request ? ippOpString(request->request.op.operation_id) : "?", | |
546 | resource ? resource : "(null)", CUPS_LLCAST length)); | |
547 | ||
548 | /* | |
549 | * Range check input... | |
550 | */ | |
551 | ||
552 | if (!request || !resource) | |
553 | { | |
554 | _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); | |
555 | ||
556 | return (HTTP_ERROR); | |
557 | } | |
558 | ||
559 | /* | |
560 | * Get the default connection as needed... | |
561 | */ | |
562 | ||
563 | if (!http) | |
564 | if ((http = _cupsConnect()) == NULL) | |
565 | return (HTTP_SERVICE_UNAVAILABLE); | |
566 | ||
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)) | |
577 | { | |
578 | _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0); | |
579 | return (HTTP_SERVICE_UNAVAILABLE); | |
580 | } | |
581 | #endif /* HAVE_SSL */ | |
582 | ||
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)) | |
589 | { | |
590 | _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0); | |
591 | return (HTTP_SERVICE_UNAVAILABLE); | |
592 | } | |
593 | ||
594 | /* | |
595 | * Loop until we can send the request without authorization problems. | |
596 | */ | |
597 | ||
598 | expect = HTTP_CONTINUE; | |
599 | ||
600 | for (;;) | |
601 | { | |
602 | DEBUG_puts("cupsSendRequest: Setup..."); | |
603 | ||
604 | /* | |
605 | * Setup the HTTP variables needed... | |
606 | */ | |
607 | ||
608 | httpClearFields(http); | |
609 | httpSetLength(http, length); | |
610 | httpSetField(http, HTTP_FIELD_CONTENT_TYPE, "application/ipp"); | |
611 | httpSetField(http, HTTP_FIELD_AUTHORIZATION, http->authstring); | |
612 | httpSetExpect(http, expect); | |
613 | ||
614 | DEBUG_printf(("cupsSendRequest: authstring=\"%s\"\n", http->authstring)); | |
615 | ||
616 | /* | |
617 | * Try the request... | |
618 | */ | |
619 | ||
620 | DEBUG_puts("cupsSendRequest: Sending HTTP POST..."); | |
621 | ||
622 | if (httpPost(http, resource)) | |
623 | { | |
624 | if (httpReconnect(http)) | |
625 | { | |
626 | _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0); | |
627 | return (HTTP_SERVICE_UNAVAILABLE); | |
628 | } | |
629 | else | |
630 | continue; | |
631 | } | |
632 | ||
633 | /* | |
634 | * Send the IPP data... | |
635 | */ | |
636 | ||
637 | DEBUG_puts("cupsSendRequest: Writing IPP request..."); | |
638 | ||
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 | ||
654 | /* | |
655 | * Wait up to 1 second to get the 100-continue response as needed... | |
656 | */ | |
657 | ||
658 | if (!got_status && expect == HTTP_CONTINUE) | |
659 | { | |
660 | DEBUG_puts("cupsSendRequest: Waiting for 100-continue..."); | |
661 | ||
662 | if (httpWait(http, 1000)) | |
663 | status = httpUpdate(http); | |
664 | } | |
665 | else if (httpCheck(http)) | |
666 | status = httpUpdate(http); | |
667 | ||
668 | DEBUG_printf(("cupsSendRequest: status=%d\n", status)); | |
669 | ||
670 | /* | |
671 | * Process the current HTTP status... | |
672 | */ | |
673 | ||
674 | switch (status) | |
675 | { | |
676 | case HTTP_ERROR : | |
677 | case HTTP_CONTINUE : | |
678 | case HTTP_OK : | |
679 | return (status); | |
680 | ||
681 | case HTTP_UNAUTHORIZED : | |
682 | if (!cupsDoAuthentication(http, "POST", resource)) | |
683 | if (httpReconnect(http)) | |
684 | { | |
685 | _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0); | |
686 | return (HTTP_SERVICE_UNAVAILABLE); | |
687 | } | |
688 | ||
689 | return (status); | |
690 | ||
691 | #ifdef HAVE_SSL | |
692 | case HTTP_UPGRADE_REQUIRED : | |
693 | /* | |
694 | * Flush any error message, reconnect, and then upgrade with | |
695 | * encryption... | |
696 | */ | |
697 | ||
698 | if (httpReconnect(http)) | |
699 | { | |
700 | _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0); | |
701 | return (HTTP_SERVICE_UNAVAILABLE); | |
702 | } | |
703 | ||
704 | httpEncryption(http, HTTP_ENCRYPT_REQUIRED); | |
705 | ||
706 | return (status); | |
707 | #endif /* HAVE_SSL */ | |
708 | ||
709 | case HTTP_EXPECTATION_FAILED : | |
710 | /* | |
711 | * Don't try using the Expect: header the next time around... | |
712 | */ | |
713 | ||
714 | expect = (http_status_t)0; | |
715 | ||
716 | if (httpReconnect(http)) | |
717 | { | |
718 | _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0); | |
719 | return (HTTP_SERVICE_UNAVAILABLE); | |
720 | } | |
721 | break; | |
722 | ||
723 | default : | |
724 | /* | |
725 | * Some other error... | |
726 | */ | |
727 | ||
728 | return (status); | |
729 | } | |
730 | } | |
731 | } | |
732 | ||
733 | ||
734 | /* | |
735 | * 'cupsWriteRequestData()' - Write additional data after an IPP request. | |
736 | * | |
737 | * This function is used after @link cupsSendRequest@ to provide a PPD and | |
738 | * after @link cupsStartDocument@ to provide a document file. | |
739 | * | |
740 | * @since CUPS 1.4@ | |
741 | */ | |
742 | ||
743 | http_status_t /* O - @code HTTP_CONTINUE@ if OK or HTTP status on error */ | |
744 | cupsWriteRequestData( | |
745 | http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ | |
746 | const char *buffer, /* I - Bytes to write */ | |
747 | size_t length) /* I - Number of bytes to write */ | |
748 | { | |
749 | /* | |
750 | * Get the default connection as needed... | |
751 | */ | |
752 | ||
753 | DEBUG_printf(("cupsWriteRequestData(http=%p, buffer=%p, " | |
754 | "length=" CUPS_LLFMT ")\n", http, buffer, CUPS_LLCAST length)); | |
755 | ||
756 | if (!http) | |
757 | { | |
758 | _cups_globals_t *cg = _cupsGlobals(); | |
759 | /* Pointer to library globals */ | |
760 | ||
761 | if ((http = cg->http) == NULL) | |
762 | { | |
763 | _cupsSetError(IPP_INTERNAL_ERROR, _("No active connection"), 1); | |
764 | return (HTTP_ERROR); | |
765 | } | |
766 | } | |
767 | ||
768 | /* | |
769 | * Then write to the HTTP connection... | |
770 | */ | |
771 | ||
772 | if (httpWrite2(http, buffer, length) < 0) | |
773 | return (HTTP_ERROR); | |
774 | ||
775 | /* | |
776 | * Finally, check if we have any pending data from the server... | |
777 | */ | |
778 | ||
779 | if (httpCheck(http)) | |
780 | return (httpUpdate(http)); | |
781 | else | |
782 | return (HTTP_CONTINUE); | |
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 */ | |
792 | const char *message, /* I - status-message value */ | |
793 | int localize) /* I - Localize the message? */ | |
794 | { | |
795 | _cups_globals_t *cg; /* Global data */ | |
796 | ||
797 | ||
798 | if (!message && errno) | |
799 | { | |
800 | message = strerror(errno); | |
801 | localize = 0; | |
802 | } | |
803 | ||
804 | cg = _cupsGlobals(); | |
805 | cg->last_error = status; | |
806 | ||
807 | if (cg->last_status_message) | |
808 | { | |
809 | _cupsStrFree(cg->last_status_message); | |
810 | ||
811 | cg->last_status_message = NULL; | |
812 | } | |
813 | ||
814 | if (message) | |
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 | } | |
831 | ||
832 | DEBUG_printf(("_cupsSetError: last_error=%s, last_status_message=\"%s\"\n", | |
833 | ippErrorString(cg->last_error), | |
834 | cg->last_status_message ? cg->last_status_message : "")); | |
835 | } | |
836 | ||
837 | ||
838 | /* | |
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 : | |
848 | _cupsSetError(IPP_NOT_FOUND, httpStatus(status), 0); | |
849 | break; | |
850 | ||
851 | case HTTP_UNAUTHORIZED : | |
852 | _cupsSetError(IPP_NOT_AUTHORIZED, httpStatus(status), 0); | |
853 | break; | |
854 | ||
855 | case HTTP_FORBIDDEN : | |
856 | _cupsSetError(IPP_FORBIDDEN, httpStatus(status), 0); | |
857 | break; | |
858 | ||
859 | case HTTP_BAD_REQUEST : | |
860 | _cupsSetError(IPP_BAD_REQUEST, httpStatus(status), 0); | |
861 | break; | |
862 | ||
863 | case HTTP_REQUEST_TOO_LARGE : | |
864 | _cupsSetError(IPP_REQUEST_VALUE, httpStatus(status), 0); | |
865 | break; | |
866 | ||
867 | case HTTP_NOT_IMPLEMENTED : | |
868 | _cupsSetError(IPP_OPERATION_NOT_SUPPORTED, httpStatus(status), 0); | |
869 | break; | |
870 | ||
871 | case HTTP_NOT_SUPPORTED : | |
872 | _cupsSetError(IPP_VERSION_NOT_SUPPORTED, httpStatus(status), 0); | |
873 | break; | |
874 | ||
875 | default : | |
876 | DEBUG_printf(("HTTP error %d mapped to IPP_SERVICE_UNAVAILABLE!\n", | |
877 | status)); | |
878 | _cupsSetError(IPP_SERVICE_UNAVAILABLE, httpStatus(status), 0); | |
879 | break; | |
880 | } | |
881 | } | |
882 | ||
883 | ||
884 | /* | |
885 | * End of "$Id: request.c 7946 2008-09-16 23:27:54Z mike $". | |
886 | */ |