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