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