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