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