]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/request.c
Merge changes from CUPS trunk, r6739.
[thirdparty/cups.git] / cups / request.c
CommitLineData
ecdc0628 1/*
355e94dc 2 * "$Id: request.c 6712 2007-07-24 00:13:05Z mike $"
ecdc0628 3 *
4 * IPP utilities for the Common UNIX Printing System (CUPS).
5 *
bc44d920 6 * Copyright 2007 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 *
19 * cupsDoFileRequest() - Do an IPP request with a file.
20 * cupsDoRequest() - Do an IPP request.
21 * _cupsSetError() - Set the last IPP status code and status-message.
355e94dc 22 * _cupsSetHTTPError() - Set the last error using the HTTP status.
ecdc0628 23 */
24
25/*
26 * Include necessary headers...
27 */
28
29#include "globals.h"
30#include "debug.h"
31#include <stdlib.h>
32#include <errno.h>
33#include <fcntl.h>
34#include <sys/stat.h>
35#if defined(WIN32) || defined(__EMX__)
36# include <io.h>
37#else
38# include <unistd.h>
39#endif /* WIN32 || __EMX__ */
b94498cf 40#ifndef O_BINARY
41# define O_BINARY 0
42#endif /* O_BINARY */
ecdc0628 43
44
45/*
46 * 'cupsDoFileRequest()' - Do an IPP request with a file.
47 *
48 * This function sends the IPP request to the specified server, retrying
49 * and authenticating as necessary. The request is freed with ippDelete()
50 * after receiving a valid IPP response.
51 */
52
53ipp_t * /* O - Response data */
54cupsDoFileRequest(http_t *http, /* I - HTTP connection to server */
55 ipp_t *request, /* I - IPP request */
56 const char *resource, /* I - HTTP resource for POST */
57 const char *filename) /* I - File to send or NULL for none */
b94498cf 58{
59 ipp_t *response; /* IPP response data */
60 int infile; /* Input file */
61
62
63 if (filename)
64 {
65 if ((infile = open(filename, O_RDONLY | O_BINARY)) < 0)
66 {
67 /*
68 * Can't get file information!
69 */
70
71 _cupsSetError(errno == ENOENT ? IPP_NOT_FOUND : IPP_NOT_AUTHORIZED,
72 strerror(errno));
73
74 ippDelete(request);
75
76 return (NULL);
77 }
78 }
79 else
80 infile = -1;
81
82 response = cupsDoIORequest(http, request, resource, infile, -1);
83
84 if (infile >= 0)
85 close(infile);
86
87 return (response);
88}
89
90
91/*
92 * 'cupsDoIORequest()' - Do an IPP request with file descriptors.
93 *
94 * This function sends the IPP request to the specified server, retrying
95 * and authenticating as necessary. The request is freed with ippDelete()
96 * after receiving a valid IPP response.
97 *
98 * If "infile" is a valid file descriptor, cupsDoIORequest() copies
99 * all of the data from the file after the IPP request message.
100 *
101 * If "outfile" is a valid file descriptor, cupsDoIORequest() copies
102 * all of the data after the IPP response message to the file.
103 *
104 * @since CUPS 1.3@
105 */
106
107ipp_t * /* O - Response data */
108cupsDoIORequest(http_t *http, /* I - HTTP connection to server */
109 ipp_t *request, /* I - IPP request */
110 const char *resource, /* I - HTTP resource for POST */
111 int infile, /* I - File to read from or -1 for none */
112 int outfile) /* I - File to write to or -1 for none */
ecdc0628 113{
114 ipp_t *response; /* IPP response data */
115 size_t length; /* Content-Length value */
116 http_status_t status; /* Status of HTTP request */
d6ae789d 117 int got_status; /* Did we get the status? */
b423cd4c 118 ipp_state_t state; /* State of IPP processing */
ecdc0628 119 struct stat fileinfo; /* File information */
120 int bytes; /* Number of bytes read/written */
a74454a7 121 char buffer[32768]; /* Output buffer */
f301802f 122 http_status_t expect; /* Expect: header to use */
ecdc0628 123
124
125 DEBUG_printf(("cupsDoFileRequest(%p, %p, \'%s\', \'%s\')\n",
126 http, request, resource ? resource : "(null)",
127 filename ? filename : "(null)"));
128
129 if (http == NULL || request == NULL || resource == NULL)
130 {
131 if (request != NULL)
132 ippDelete(request);
133
134 _cupsSetError(IPP_INTERNAL_ERROR, NULL);
135
136 return (NULL);
137 }
138
139 /*
140 * See if we have a file to send...
141 */
142
b94498cf 143 if (infile >= 0)
ecdc0628 144 {
b94498cf 145 if (fstat(infile, &fileinfo))
ecdc0628 146 {
147 /*
148 * Can't get file information!
149 */
150
151 _cupsSetError(errno == ENOENT ? IPP_NOT_FOUND : IPP_NOT_AUTHORIZED,
b94498cf 152 strerror(errno));
ecdc0628 153
154 ippDelete(request);
155
156 return (NULL);
157 }
158
159#ifdef WIN32
160 if (fileinfo.st_mode & _S_IFDIR)
161#else
162 if (S_ISDIR(fileinfo.st_mode))
163#endif /* WIN32 */
164 {
165 /*
166 * Can't send a directory...
167 */
168
169 ippDelete(request);
170
89d46774 171 _cupsSetError(IPP_NOT_POSSIBLE, strerror(EISDIR));
ecdc0628 172
173 return (NULL);
174 }
ecdc0628 175 }
ecdc0628 176
7594b224 177#ifdef HAVE_SSL
178 /*
179 * See if we have an auth-info attribute and are communicating over
180 * a non-local link. If so, encrypt the link so that we can pass
181 * the authentication information securely...
182 */
183
184 if (ippFindAttribute(request, "auth-info", IPP_TAG_TEXT) &&
185 !httpAddrLocalhost(http->hostaddr) && !http->tls &&
186 httpEncryption(http, HTTP_ENCRYPT_REQUIRED))
187 return (NULL);
188#endif /* HAVE_SSL */
189
ecdc0628 190 /*
191 * Loop until we can send the request without authorization problems.
192 */
193
194 response = NULL;
195 status = HTTP_ERROR;
f301802f 196 expect = HTTP_CONTINUE;
ecdc0628 197
198 while (response == NULL)
199 {
200 DEBUG_puts("cupsDoFileRequest: setup...");
201
202 /*
203 * Setup the HTTP variables needed...
204 */
205
206 length = ippLength(request);
b94498cf 207 if (infile >= 0)
208 {
209#ifndef WIN32
210 if (!S_ISREG(fileinfo.st_mode))
211 length = 0; /* Chunk when piping */
212 else
213#endif /* !WIN32 */
ecdc0628 214 length += fileinfo.st_size;
b94498cf 215 }
ecdc0628 216
217 httpClearFields(http);
218 httpSetLength(http, length);
219 httpSetField(http, HTTP_FIELD_CONTENT_TYPE, "application/ipp");
220 httpSetField(http, HTTP_FIELD_AUTHORIZATION, http->authstring);
f301802f 221 httpSetExpect(http, expect);
ecdc0628 222
223 DEBUG_printf(("cupsDoFileRequest: authstring=\"%s\"\n", http->authstring));
224
225 /*
226 * Try the request...
227 */
228
229 DEBUG_puts("cupsDoFileRequest: post...");
230
231 if (httpPost(http, resource))
232 {
233 if (httpReconnect(http))
234 {
235 status = HTTP_ERROR;
236 break;
237 }
238 else
239 continue;
240 }
241
242 /*
d6ae789d 243 * Send the IPP data...
ecdc0628 244 */
245
d6ae789d 246 DEBUG_puts("cupsDoFileRequest: ipp write...");
b423cd4c 247
d6ae789d 248 request->state = IPP_IDLE;
249 status = HTTP_CONTINUE;
250 got_status = 0;
251
252 while ((state = ippWrite(http, request)) != IPP_DATA)
253 if (state == IPP_ERROR)
254 break;
255 else if (httpCheck(http))
256 {
257 got_status = 1;
258
259 if ((status = httpUpdate(http)) != HTTP_CONTINUE)
260 break;
261 }
262
263 if (!got_status)
b423cd4c 264 {
265 /*
d6ae789d 266 * Wait up to 1 second to get the 100-continue response...
b423cd4c 267 */
ecdc0628 268
d6ae789d 269 if (httpWait(http, 1000))
270 status = httpUpdate(http);
271 }
272 else if (httpCheck(http))
273 status = httpUpdate(http);
ecdc0628 274
b94498cf 275 if (status == HTTP_CONTINUE && state == IPP_DATA && infile >= 0)
d6ae789d 276 {
277 DEBUG_puts("cupsDoFileRequest: file write...");
b423cd4c 278
d6ae789d 279 /*
280 * Send the file...
281 */
282
b94498cf 283#ifndef WIN32
284 if (S_ISREG(fileinfo.st_mode))
285#endif /* WIN32 */
286 lseek(infile, 0, SEEK_SET);
d6ae789d 287
b94498cf 288 while ((bytes = (int)read(infile, buffer, sizeof(buffer))) > 0)
d6ae789d 289 {
290 if (httpCheck(http))
b423cd4c 291 {
292 if ((status = httpUpdate(http)) != HTTP_CONTINUE)
293 break;
294 }
295
d6ae789d 296 if (httpWrite2(http, buffer, bytes) < bytes)
297 break;
ecdc0628 298 }
b423cd4c 299 }
ecdc0628 300
301 /*
302 * Get the server's return status...
303 */
304
305 DEBUG_puts("cupsDoFileRequest: update...");
306
307 while (status == HTTP_CONTINUE)
308 status = httpUpdate(http);
309
310 DEBUG_printf(("cupsDoFileRequest: status = %d\n", status));
311
312 if (status == HTTP_UNAUTHORIZED)
313 {
314 DEBUG_puts("cupsDoFileRequest: unauthorized...");
315
316 /*
317 * Flush any error message...
318 */
319
320 httpFlush(http);
321
322 /*
323 * See if we can do authentication...
324 */
325
326 if (cupsDoAuthentication(http, "POST", resource))
327 break;
328
329 if (httpReconnect(http))
330 {
331 status = HTTP_ERROR;
332 break;
333 }
334
335 continue;
336 }
337 else if (status == HTTP_ERROR)
338 {
89d46774 339 DEBUG_printf(("cupsDoFileRequest: http->error=%d (%s)\n", http->error,
340 strerror(http->error)));
341
ecdc0628 342#ifdef WIN32
89d46774 343 if (http->error != WSAENETDOWN && http->error != WSAENETUNREACH &&
b86bc4cf 344 http->error != WSAETIMEDOUT)
ecdc0628 345#else
89d46774 346 if (http->error != ENETDOWN && http->error != ENETUNREACH &&
347 http->error != ETIMEDOUT)
ecdc0628 348#endif /* WIN32 */
349 continue;
350 else
351 break;
352 }
353#ifdef HAVE_SSL
354 else if (status == HTTP_UPGRADE_REQUIRED)
355 {
356 /* Flush any error message... */
357 httpFlush(http);
358
359 /* Reconnect... */
360 if (httpReconnect(http))
361 {
362 status = HTTP_ERROR;
363 break;
364 }
365
366 /* Upgrade with encryption... */
367 httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
368
369 /* Try again, this time with encryption enabled... */
370 continue;
371 }
372#endif /* HAVE_SSL */
f301802f 373 else if (status == HTTP_EXPECTATION_FAILED)
374 {
375 /*
376 * Don't try using the Expect: header the next time around...
377 */
378
379 expect = (http_status_t)0;
380 }
ecdc0628 381 else if (status != HTTP_OK)
382 {
383 DEBUG_printf(("cupsDoFileRequest: error %d...\n", status));
384
385 /*
386 * Flush any error message...
387 */
388
389 httpFlush(http);
390 break;
391 }
392 else
393 {
394 /*
395 * Read the response...
396 */
397
398 DEBUG_puts("cupsDoFileRequest: response...");
399
400 response = ippNew();
401
89d46774 402 while ((state = ippRead(http, response)) != IPP_DATA)
403 if (state == IPP_ERROR)
c0e1af83 404 break;
405
406 if (state == IPP_ERROR)
407 {
408 /*
409 * Delete the response...
410 */
ecdc0628 411
c0e1af83 412 DEBUG_puts("IPP read error!");
413 ippDelete(response);
414 response = NULL;
ecdc0628 415
c0e1af83 416 _cupsSetError(IPP_SERVICE_UNAVAILABLE, strerror(errno));
ecdc0628 417
c0e1af83 418 break;
419 }
b94498cf 420 else if (outfile >= 0)
421 {
422 /*
423 * Write trailing data to file...
424 */
ecdc0628 425
b94498cf 426 while ((bytes = (int)httpRead2(http, buffer, sizeof(buffer))) > 0)
427 if (write(outfile, buffer, bytes) < bytes)
428 break;
429 }
430 else
431 {
432 /*
433 * Flush any remaining data...
434 */
ecdc0628 435
b94498cf 436 httpFlush(http);
437 }
438 }
439 }
ecdc0628 440
441 /*
442 * Delete the original request and return the response...
443 */
444
445 ippDelete(request);
446
447 if (response)
448 {
449 ipp_attribute_t *attr; /* status-message attribute */
450
451
452 attr = ippFindAttribute(response, "status-message", IPP_TAG_TEXT);
453
454 _cupsSetError(response->request.status.status_code,
455 attr ? attr->values[0].string.text :
456 ippErrorString(response->request.status.status_code));
457 }
458 else if (status != HTTP_OK)
355e94dc 459 _cupsSetHTTPError(status);
ecdc0628 460
461 return (response);
462}
463
464
465/*
466 * 'cupsDoRequest()' - Do an IPP request.
467 *
468 * This function sends the IPP request to the specified server, retrying
469 * and authenticating as necessary. The request is freed with ippDelete()
470 * after receiving a valid IPP response.
471 */
472
473ipp_t * /* O - Response data */
474cupsDoRequest(http_t *http, /* I - HTTP connection to server */
475 ipp_t *request, /* I - IPP request */
476 const char *resource) /* I - HTTP resource for POST */
477{
478 return (cupsDoFileRequest(http, request, resource, NULL));
479}
480
481
482/*
483 * '_cupsSetError()' - Set the last IPP status code and status-message.
484 */
485
486void
487_cupsSetError(ipp_status_t status, /* I - IPP status code */
488 const char *message) /* I - status-message value */
489{
490 _cups_globals_t *cg; /* Global data */
491
492
493 cg = _cupsGlobals();
494 cg->last_error = status;
495
496 if (cg->last_status_message)
497 {
498 free(cg->last_status_message);
499
500 cg->last_status_message = NULL;
501 }
502
503 if (message)
504 cg->last_status_message = strdup(message);
505}
506
507
508/*
355e94dc
MS
509 * '_cupsSetHTTPError()' - Set the last error using the HTTP status.
510 */
511
512void
513_cupsSetHTTPError(http_status_t status) /* I - HTTP status code */
514{
515 switch (status)
516 {
517 case HTTP_NOT_FOUND :
518 _cupsSetError(IPP_NOT_FOUND, httpStatus(status));
519 break;
520
521 case HTTP_UNAUTHORIZED :
522 _cupsSetError(IPP_NOT_AUTHORIZED, httpStatus(status));
523 break;
524
525 case HTTP_FORBIDDEN :
526 _cupsSetError(IPP_FORBIDDEN, httpStatus(status));
527 break;
528
529 case HTTP_BAD_REQUEST :
530 _cupsSetError(IPP_BAD_REQUEST, httpStatus(status));
531 break;
532
533 case HTTP_REQUEST_TOO_LARGE :
534 _cupsSetError(IPP_REQUEST_VALUE, httpStatus(status));
535 break;
536
537 case HTTP_NOT_IMPLEMENTED :
538 _cupsSetError(IPP_OPERATION_NOT_SUPPORTED, httpStatus(status));
539 break;
540
541 case HTTP_NOT_SUPPORTED :
542 _cupsSetError(IPP_VERSION_NOT_SUPPORTED, httpStatus(status));
543 break;
544
545 default :
546 DEBUG_printf(("HTTP error %d mapped to IPP_SERVICE_UNAVAILABLE!\n",
547 status));
548 _cupsSetError(IPP_SERVICE_UNAVAILABLE, httpStatus(status));
549 break;
550 }
551}
552
553
554/*
555 * End of "$Id: request.c 6712 2007-07-24 00:13:05Z mike $".
ecdc0628 556 */