]>
Commit | Line | Data |
---|---|---|
ecdc0628 | 1 | /* |
2e4ff8af | 2 | * "$Id: request.c 6879 2007-08-29 20:26:50Z 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, | |
83 | strerror(errno)); | |
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 */ | |
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 | |
3d052e43 | 146 | _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL)); |
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, |
b94498cf | 172 | strerror(errno)); |
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 | ||
89d46774 | 191 | _cupsSetError(IPP_NOT_POSSIBLE, strerror(EISDIR)); |
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 | ||
206 | /* | |
207 | * Loop until we can send the request without authorization problems. | |
208 | */ | |
209 | ||
210 | while (response == NULL) | |
211 | { | |
212 | DEBUG_puts("cupsDoFileRequest: setup..."); | |
213 | ||
214 | /* | |
215 | * Send the request... | |
216 | */ | |
217 | ||
218 | status = cupsSendRequest(http, request, resource, length); | |
219 | ||
220 | DEBUG_printf(("cupsDoFileRequest: status=%d\n", status)); | |
221 | ||
222 | if (status == HTTP_CONTINUE && request->state == IPP_DATA && infile >= 0) | |
223 | { | |
224 | DEBUG_puts("cupsDoFileRequest: file write..."); | |
225 | ||
226 | /* | |
227 | * Send the file with the request... | |
228 | */ | |
229 | ||
230 | #ifndef WIN32 | |
231 | if (S_ISREG(fileinfo.st_mode)) | |
232 | #endif /* WIN32 */ | |
233 | lseek(infile, 0, SEEK_SET); | |
234 | ||
235 | while ((bytes = (int)read(infile, buffer, sizeof(buffer))) > 0) | |
236 | { | |
237 | if (httpCheck(http)) | |
238 | { | |
239 | if ((status = httpUpdate(http)) != HTTP_CONTINUE) | |
240 | break; | |
241 | } | |
242 | ||
243 | if (httpWrite2(http, buffer, bytes) < bytes) | |
244 | break; | |
245 | } | |
246 | } | |
247 | ||
248 | /* | |
249 | * Get the server's response... | |
250 | */ | |
251 | ||
252 | if (status == HTTP_CONTINUE || status == HTTP_OK) | |
253 | { | |
254 | response = cupsGetResponse(http, resource); | |
255 | status = http->status; | |
256 | } | |
257 | ||
568fa3fa MS |
258 | if (status == HTTP_FORBIDDEN) |
259 | break; | |
260 | ||
3d052e43 MS |
261 | if (response) |
262 | { | |
263 | if (outfile >= 0) | |
264 | { | |
265 | /* | |
266 | * Write trailing data to file... | |
267 | */ | |
268 | ||
269 | while ((bytes = (int)httpRead2(http, buffer, sizeof(buffer))) > 0) | |
270 | if (write(outfile, buffer, bytes) < bytes) | |
271 | break; | |
272 | } | |
273 | else | |
274 | { | |
275 | /* | |
276 | * Flush any remaining data... | |
277 | */ | |
278 | ||
279 | httpFlush(http); | |
280 | } | |
281 | } | |
282 | } | |
283 | ||
284 | /* | |
285 | * Delete the original request and return the response... | |
286 | */ | |
287 | ||
288 | ippDelete(request); | |
289 | ||
290 | return (response); | |
291 | } | |
292 | ||
293 | ||
294 | /* | |
295 | * 'cupsDoRequest()' - Do an IPP request. | |
296 | * | |
297 | * This function sends the IPP request to the specified server, retrying | |
298 | * and authenticating as necessary. The request is freed with ippDelete() | |
299 | * after receiving a valid IPP response. | |
300 | */ | |
301 | ||
302 | ipp_t * /* O - Response data */ | |
568fa3fa | 303 | cupsDoRequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
3d052e43 MS |
304 | ipp_t *request, /* I - IPP request */ |
305 | const char *resource) /* I - HTTP resource for POST */ | |
306 | { | |
a603edef MS |
307 | DEBUG_printf(("cupsDoRequest(http=%p, request=%p(%s), resource=\"%s\")\n", |
308 | http, request, | |
309 | request ? ippOpString(request->request.op.operation_id) : "?", | |
310 | resource ? resource : "(null)")); | |
311 | ||
3d052e43 MS |
312 | return (cupsDoFileRequest(http, request, resource, NULL)); |
313 | } | |
314 | ||
315 | ||
316 | /* | |
317 | * 'cupsGetResponse()' - Get a response to an IPP request. | |
318 | * | |
319 | * Use this function to get the response for an IPP request sent using | |
320 | * cupsSendDocument() or cupsSendRequest(). For requests that return | |
321 | * additional data, use httpRead() after getting a successful response. | |
322 | * | |
323 | * @since CUPS 1.4@ | |
324 | */ | |
325 | ||
568fa3fa MS |
326 | ipp_t * /* O - Response or @code NULL@ on HTTP error */ |
327 | cupsGetResponse(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ | |
3d052e43 MS |
328 | const char *resource) /* I - HTTP resource for POST */ |
329 | { | |
330 | http_status_t status; /* HTTP status */ | |
331 | ipp_state_t state; /* IPP read state */ | |
332 | ipp_t *response = NULL; /* IPP response */ | |
333 | ||
334 | ||
a603edef MS |
335 | DEBUG_printf(("cupsGetReponse(http=%p, resource=\"%s\")\n", http, |
336 | resource ? resource : "(null)")); | |
3d052e43 MS |
337 | |
338 | /* | |
339 | * Connect to the default server as needed... | |
340 | */ | |
341 | ||
342 | if (!http) | |
343 | http = _cupsConnect(); | |
344 | ||
345 | if (!http || (http->state != HTTP_POST_RECV && http->state != HTTP_POST_SEND)) | |
346 | return (NULL); | |
347 | ||
348 | /* | |
349 | * Check for an unfinished chunked request... | |
350 | */ | |
351 | ||
352 | if (http->data_encoding == HTTP_ENCODE_CHUNKED) | |
353 | { | |
354 | /* | |
355 | * Send a 0-length chunk to finish off the request... | |
356 | */ | |
357 | ||
358 | DEBUG_puts("cupsGetResponse: Finishing chunked POST..."); | |
359 | ||
360 | if (httpWrite2(http, "", 0) < 0) | |
361 | return (NULL); | |
362 | } | |
363 | ||
364 | /* | |
365 | * Wait for a response from the server... | |
366 | */ | |
367 | ||
a603edef | 368 | DEBUG_puts("cupsGetResponse: Update loop..."); |
3d052e43 MS |
369 | |
370 | while ((status = httpUpdate(http)) == HTTP_CONTINUE) | |
371 | /* Do nothing but update */; | |
372 | ||
a603edef | 373 | DEBUG_printf(("cupsGetResponse: status=%d\n", status)); |
3d052e43 MS |
374 | |
375 | if (status == HTTP_OK) | |
376 | { | |
377 | /* | |
378 | * Get the IPP response... | |
379 | */ | |
380 | ||
381 | response = ippNew(); | |
382 | ||
383 | while ((state = ippRead(http, response)) != IPP_DATA) | |
384 | if (state == IPP_ERROR) | |
385 | break; | |
386 | ||
387 | if (state == IPP_ERROR) | |
388 | { | |
389 | /* | |
390 | * Delete the response... | |
391 | */ | |
392 | ||
a603edef | 393 | DEBUG_puts("cupsGetResponse: IPP read error!"); |
3d052e43 MS |
394 | |
395 | ippDelete(response); | |
396 | response = NULL; | |
397 | ||
398 | _cupsSetError(IPP_SERVICE_UNAVAILABLE, strerror(errno)); | |
399 | } | |
400 | } | |
401 | else if (status != HTTP_ERROR) | |
402 | { | |
403 | /* | |
404 | * Flush any error message... | |
405 | */ | |
406 | ||
407 | httpFlush(http); | |
408 | ||
409 | /* | |
410 | * Then handle encryption and authentication... | |
411 | */ | |
412 | ||
413 | if (status == HTTP_UNAUTHORIZED) | |
414 | { | |
415 | /* | |
416 | * See if we can do authentication... | |
417 | */ | |
418 | ||
568fa3fa MS |
419 | int auth_result; |
420 | ||
3d052e43 MS |
421 | DEBUG_puts("cupsGetResponse: Need authorization..."); |
422 | ||
ae71f5de | 423 | if ((auth_result = cupsDoAuthentication(http, "POST", resource)) == 0) |
3d052e43 | 424 | httpReconnect(http); |
568fa3fa MS |
425 | else if (auth_result < 0) |
426 | http->status = status = HTTP_FORBIDDEN; | |
3d052e43 MS |
427 | } |
428 | ||
429 | #ifdef HAVE_SSL | |
430 | else if (status == HTTP_UPGRADE_REQUIRED) | |
431 | { | |
432 | /* | |
433 | * Force a reconnect with encryption... | |
434 | */ | |
435 | ||
436 | DEBUG_puts("cupsGetResponse: Need encryption..."); | |
437 | ||
438 | if (!httpReconnect(http)) | |
439 | httpEncryption(http, HTTP_ENCRYPT_REQUIRED); | |
440 | } | |
441 | #endif /* HAVE_SSL */ | |
442 | } | |
443 | ||
444 | if (response) | |
445 | { | |
446 | ipp_attribute_t *attr; /* status-message attribute */ | |
447 | ||
448 | ||
449 | attr = ippFindAttribute(response, "status-message", IPP_TAG_TEXT); | |
450 | ||
a603edef MS |
451 | DEBUG_printf(("cupsGetResponse: status-code=%s, status-message=\"%s\"\n", |
452 | ippErrorString(response->request.status.status_code), | |
453 | attr ? attr->values[0].string.text : "")); | |
454 | ||
3d052e43 MS |
455 | _cupsSetError(response->request.status.status_code, |
456 | attr ? attr->values[0].string.text : | |
457 | ippErrorString(response->request.status.status_code)); | |
458 | } | |
459 | else if (status != HTTP_OK) | |
460 | _cupsSetHTTPError(status); | |
461 | ||
462 | return (response); | |
463 | } | |
464 | ||
465 | ||
466 | /* | |
467 | * 'cupsReadResponseData()' - Read additional data after the IPP response. | |
468 | * | |
469 | * This function is used after cupsGetResponse() to read the PPD or document | |
470 | * files for CUPS_GET_PPD and CUPS_GET_DOCUMENT requests, respectively. | |
471 | * | |
472 | * @since CUPS 1.4@ | |
473 | */ | |
474 | ||
475 | ssize_t /* O - Bytes read, 0 on EOF, -1 on error */ | |
476 | cupsReadResponseData( | |
568fa3fa | 477 | http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
3d052e43 MS |
478 | char *buffer, /* I - Buffer to use */ |
479 | size_t length) /* I - Number of bytes to read */ | |
480 | { | |
481 | /* | |
482 | * Get the default connection as needed... | |
483 | */ | |
484 | ||
a603edef MS |
485 | DEBUG_printf(("cupsReadResponseData(http=%p, buffer=%p, " |
486 | "length=" CUPS_LLFMT ")\n", http, buffer, CUPS_LLCAST length)); | |
487 | ||
3d052e43 MS |
488 | if (!http) |
489 | { | |
490 | _cups_globals_t *cg = _cupsGlobals(); | |
491 | /* Pointer to library globals */ | |
492 | ||
493 | if ((http = cg->http) == NULL) | |
494 | { | |
495 | _cupsSetError(IPP_INTERNAL_ERROR, "No active connection"); | |
496 | return (-1); | |
497 | } | |
ecdc0628 | 498 | } |
ecdc0628 | 499 | |
3d052e43 MS |
500 | /* |
501 | * Then read from the HTTP connection... | |
502 | */ | |
503 | ||
504 | return (httpRead2(http, buffer, length)); | |
505 | } | |
506 | ||
507 | ||
508 | /* | |
509 | * 'cupsSendRequest()' - Send an IPP request. | |
510 | * | |
511 | * Use httpWrite() to write any additional data (document, PPD file, etc.) | |
512 | * for the request, cupsGetResponse() to get the IPP response, and httpRead() | |
513 | * to read any additional data following the response. Only one request can be | |
514 | * sent/queued at a time. | |
515 | * | |
516 | * Unlike cupsDoFileRequest(), cupsDoIORequest(), and cupsDoRequest(), the | |
517 | * request is not freed. | |
518 | * | |
519 | * @since CUPS 1.4@ | |
520 | */ | |
521 | ||
522 | http_status_t /* O - Initial HTTP status */ | |
568fa3fa | 523 | cupsSendRequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
3d052e43 MS |
524 | ipp_t *request, /* I - IPP request */ |
525 | const char *resource, /* I - Resource path */ | |
ae71f5de | 526 | size_t length) /* I - Length of data to follow or @code CUPS_LENGTH_VARIABLE@ */ |
3d052e43 MS |
527 | { |
528 | http_status_t status; /* Status of HTTP request */ | |
529 | int got_status; /* Did we get the status? */ | |
530 | ipp_state_t state; /* State of IPP processing */ | |
531 | http_status_t expect; /* Expect: header to use */ | |
532 | ||
533 | ||
a603edef | 534 | DEBUG_printf(("cupsSendRequest(http=%p, request=%p(%s), resource=\"%s\", " |
3d052e43 | 535 | "length=" CUPS_LLFMT ")\n", http, request, |
a603edef | 536 | request ? ippOpString(request->request.op.operation_id) : "?", |
3d052e43 MS |
537 | resource ? resource : "(null)", CUPS_LLCAST length)); |
538 | ||
539 | /* | |
540 | * Range check input... | |
541 | */ | |
542 | ||
543 | if (!request || !resource) | |
544 | { | |
545 | _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL)); | |
546 | ||
547 | return (HTTP_ERROR); | |
548 | } | |
549 | ||
550 | /* | |
551 | * Get the default connection as needed... | |
552 | */ | |
553 | ||
554 | if (!http) | |
a603edef MS |
555 | if ((http = _cupsConnect()) == NULL) |
556 | return (HTTP_SERVICE_UNAVAILABLE); | |
3d052e43 | 557 | |
7594b224 | 558 | #ifdef HAVE_SSL |
559 | /* | |
560 | * See if we have an auth-info attribute and are communicating over | |
561 | * a non-local link. If so, encrypt the link so that we can pass | |
562 | * the authentication information securely... | |
563 | */ | |
564 | ||
565 | if (ippFindAttribute(request, "auth-info", IPP_TAG_TEXT) && | |
566 | !httpAddrLocalhost(http->hostaddr) && !http->tls && | |
567 | httpEncryption(http, HTTP_ENCRYPT_REQUIRED)) | |
3d052e43 | 568 | return (HTTP_ERROR); |
7594b224 | 569 | #endif /* HAVE_SSL */ |
570 | ||
ecdc0628 | 571 | /* |
572 | * Loop until we can send the request without authorization problems. | |
573 | */ | |
574 | ||
3d052e43 MS |
575 | status = HTTP_ERROR; |
576 | expect = HTTP_CONTINUE; | |
ecdc0628 | 577 | |
3d052e43 | 578 | for (;;) |
ecdc0628 | 579 | { |
a603edef | 580 | DEBUG_puts("cupsSendRequest: Setup..."); |
ecdc0628 | 581 | |
582 | /* | |
583 | * Setup the HTTP variables needed... | |
584 | */ | |
585 | ||
ecdc0628 | 586 | httpClearFields(http); |
587 | httpSetLength(http, length); | |
588 | httpSetField(http, HTTP_FIELD_CONTENT_TYPE, "application/ipp"); | |
589 | httpSetField(http, HTTP_FIELD_AUTHORIZATION, http->authstring); | |
f301802f | 590 | httpSetExpect(http, expect); |
ecdc0628 | 591 | |
3d052e43 | 592 | DEBUG_printf(("cupsSendRequest: authstring=\"%s\"\n", http->authstring)); |
ecdc0628 | 593 | |
594 | /* | |
595 | * Try the request... | |
596 | */ | |
597 | ||
a603edef | 598 | DEBUG_puts("cupsSendRequest: Sending HTTP POST..."); |
ecdc0628 | 599 | |
600 | if (httpPost(http, resource)) | |
601 | { | |
602 | if (httpReconnect(http)) | |
3d052e43 | 603 | return (HTTP_ERROR); |
ecdc0628 | 604 | else |
605 | continue; | |
606 | } | |
607 | ||
608 | /* | |
d6ae789d | 609 | * Send the IPP data... |
ecdc0628 | 610 | */ |
611 | ||
a603edef | 612 | DEBUG_puts("cupsSendRequest: Writing IPP request..."); |
b423cd4c | 613 | |
d6ae789d | 614 | request->state = IPP_IDLE; |
615 | status = HTTP_CONTINUE; | |
616 | got_status = 0; | |
617 | ||
618 | while ((state = ippWrite(http, request)) != IPP_DATA) | |
619 | if (state == IPP_ERROR) | |
620 | break; | |
621 | else if (httpCheck(http)) | |
622 | { | |
623 | got_status = 1; | |
624 | ||
625 | if ((status = httpUpdate(http)) != HTTP_CONTINUE) | |
626 | break; | |
627 | } | |
628 | ||
3d052e43 MS |
629 | /* |
630 | * Wait up to 1 second to get the 100-continue response as needed... | |
631 | */ | |
ecdc0628 | 632 | |
3d052e43 MS |
633 | if (!got_status && expect == HTTP_CONTINUE) |
634 | { | |
a603edef MS |
635 | DEBUG_puts("cupsSendRequest: Waiting for 100-continue..."); |
636 | ||
d6ae789d | 637 | if (httpWait(http, 1000)) |
638 | status = httpUpdate(http); | |
a603edef MS |
639 | else |
640 | status = HTTP_EXPECTATION_FAILED; | |
d6ae789d | 641 | } |
642 | else if (httpCheck(http)) | |
643 | status = httpUpdate(http); | |
ecdc0628 | 644 | |
a603edef MS |
645 | DEBUG_printf(("cupsSendRequest: status=%d\n", status)); |
646 | ||
ecdc0628 | 647 | /* |
3d052e43 | 648 | * Process the current HTTP status... |
ecdc0628 | 649 | */ |
650 | ||
3d052e43 | 651 | switch (status) |
ecdc0628 | 652 | { |
3d052e43 MS |
653 | case HTTP_ERROR : |
654 | case HTTP_CONTINUE : | |
655 | case HTTP_OK : | |
656 | return (status); | |
ecdc0628 | 657 | |
3d052e43 MS |
658 | case HTTP_UNAUTHORIZED : |
659 | if (!cupsDoAuthentication(http, "POST", resource)) | |
660 | if (httpReconnect(http)) | |
661 | return (HTTP_ERROR); | |
ecdc0628 | 662 | |
3d052e43 | 663 | return (status); |
89d46774 | 664 | |
ecdc0628 | 665 | #ifdef HAVE_SSL |
3d052e43 MS |
666 | case HTTP_UPGRADE_REQUIRED : |
667 | /* | |
668 | * Flush any error message, reconnect, and then upgrade with | |
669 | * encryption... | |
670 | */ | |
ecdc0628 | 671 | |
3d052e43 MS |
672 | if (httpReconnect(http)) |
673 | return (HTTP_ERROR); | |
ecdc0628 | 674 | |
3d052e43 | 675 | httpEncryption(http, HTTP_ENCRYPT_REQUIRED); |
ecdc0628 | 676 | |
3d052e43 | 677 | return (status); |
ecdc0628 | 678 | #endif /* HAVE_SSL */ |
f301802f | 679 | |
3d052e43 MS |
680 | case HTTP_EXPECTATION_FAILED : |
681 | /* | |
682 | * Don't try using the Expect: header the next time around... | |
683 | */ | |
ecdc0628 | 684 | |
3d052e43 | 685 | expect = (http_status_t)0; |
a603edef | 686 | break; |
ecdc0628 | 687 | |
3d052e43 MS |
688 | default : |
689 | /* | |
690 | * Some other error... | |
691 | */ | |
ecdc0628 | 692 | |
3d052e43 | 693 | return (status); |
b94498cf | 694 | } |
695 | } | |
3d052e43 MS |
696 | } |
697 | ||
698 | ||
699 | /* | |
700 | * 'cupsWriteRequestData()' - Write additional data after an IPP request. | |
701 | * | |
568fa3fa MS |
702 | * This function is used after @link cupsSendRequest@ to provide a PPD and |
703 | * after @link cupsStartDocument@ to provide a document file. | |
3d052e43 MS |
704 | * |
705 | * @since CUPS 1.4@ | |
706 | */ | |
ecdc0628 | 707 | |
568fa3fa | 708 | http_status_t /* O - @code HTTP_CONTINUE@ if OK or HTTP status on error */ |
3d052e43 | 709 | cupsWriteRequestData( |
568fa3fa | 710 | http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
3d052e43 MS |
711 | const char *buffer, /* I - Bytes to write */ |
712 | size_t length) /* I - Number of bytes to write */ | |
713 | { | |
ecdc0628 | 714 | /* |
3d052e43 | 715 | * Get the default connection as needed... |
ecdc0628 | 716 | */ |
ecdc0628 | 717 | |
a603edef MS |
718 | DEBUG_printf(("cupsWriteRequestData(http=%p, buffer=%p, " |
719 | "length=" CUPS_LLFMT ")\n", http, buffer, CUPS_LLCAST length)); | |
720 | ||
3d052e43 | 721 | if (!http) |
ecdc0628 | 722 | { |
3d052e43 MS |
723 | _cups_globals_t *cg = _cupsGlobals(); |
724 | /* Pointer to library globals */ | |
ecdc0628 | 725 | |
3d052e43 MS |
726 | if ((http = cg->http) == NULL) |
727 | { | |
728 | _cupsSetError(IPP_INTERNAL_ERROR, "No active connection"); | |
729 | return (HTTP_ERROR); | |
730 | } | |
ecdc0628 | 731 | } |
ecdc0628 | 732 | |
3d052e43 MS |
733 | /* |
734 | * Then write to the HTTP connection... | |
735 | */ | |
ecdc0628 | 736 | |
3d052e43 MS |
737 | if (httpWrite2(http, buffer, length) < 0) |
738 | return (HTTP_ERROR); | |
ecdc0628 | 739 | |
3d052e43 MS |
740 | /* |
741 | * Finally, check if we have any pending data from the server... | |
742 | */ | |
ecdc0628 | 743 | |
3d052e43 MS |
744 | if (httpCheck(http)) |
745 | return (httpUpdate(http)); | |
746 | else | |
747 | return (HTTP_CONTINUE); | |
ecdc0628 | 748 | } |
749 | ||
750 | ||
751 | /* | |
752 | * '_cupsSetError()' - Set the last IPP status code and status-message. | |
753 | */ | |
754 | ||
755 | void | |
756 | _cupsSetError(ipp_status_t status, /* I - IPP status code */ | |
757 | const char *message) /* I - status-message value */ | |
758 | { | |
759 | _cups_globals_t *cg; /* Global data */ | |
760 | ||
761 | ||
762 | cg = _cupsGlobals(); | |
763 | cg->last_error = status; | |
764 | ||
765 | if (cg->last_status_message) | |
766 | { | |
767 | free(cg->last_status_message); | |
768 | ||
769 | cg->last_status_message = NULL; | |
770 | } | |
771 | ||
772 | if (message) | |
773 | cg->last_status_message = strdup(message); | |
a603edef MS |
774 | |
775 | DEBUG_printf(("_cupsSetError: last_error=%s, last_status_message=\"%s\"\n", | |
776 | ippErrorString(cg->last_error), message ? message : "")); | |
ecdc0628 | 777 | } |
778 | ||
779 | ||
780 | /* | |
355e94dc MS |
781 | * '_cupsSetHTTPError()' - Set the last error using the HTTP status. |
782 | */ | |
783 | ||
784 | void | |
785 | _cupsSetHTTPError(http_status_t status) /* I - HTTP status code */ | |
786 | { | |
787 | switch (status) | |
788 | { | |
789 | case HTTP_NOT_FOUND : | |
790 | _cupsSetError(IPP_NOT_FOUND, httpStatus(status)); | |
791 | break; | |
792 | ||
793 | case HTTP_UNAUTHORIZED : | |
794 | _cupsSetError(IPP_NOT_AUTHORIZED, httpStatus(status)); | |
795 | break; | |
796 | ||
797 | case HTTP_FORBIDDEN : | |
798 | _cupsSetError(IPP_FORBIDDEN, httpStatus(status)); | |
799 | break; | |
800 | ||
801 | case HTTP_BAD_REQUEST : | |
802 | _cupsSetError(IPP_BAD_REQUEST, httpStatus(status)); | |
803 | break; | |
804 | ||
805 | case HTTP_REQUEST_TOO_LARGE : | |
806 | _cupsSetError(IPP_REQUEST_VALUE, httpStatus(status)); | |
807 | break; | |
808 | ||
809 | case HTTP_NOT_IMPLEMENTED : | |
810 | _cupsSetError(IPP_OPERATION_NOT_SUPPORTED, httpStatus(status)); | |
811 | break; | |
812 | ||
813 | case HTTP_NOT_SUPPORTED : | |
814 | _cupsSetError(IPP_VERSION_NOT_SUPPORTED, httpStatus(status)); | |
815 | break; | |
816 | ||
817 | default : | |
818 | DEBUG_printf(("HTTP error %d mapped to IPP_SERVICE_UNAVAILABLE!\n", | |
819 | status)); | |
820 | _cupsSetError(IPP_SERVICE_UNAVAILABLE, httpStatus(status)); | |
821 | break; | |
822 | } | |
823 | } | |
824 | ||
825 | ||
826 | /* | |
2e4ff8af | 827 | * End of "$Id: request.c 6879 2007-08-29 20:26:50Z mike $". |
ecdc0628 | 828 | */ |