]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/getputfile.c
Import CUPS 1.4svn-r7356.
[thirdparty/cups.git] / cups / getputfile.c
1 /*
2 * "$Id: getputfile.c 6720 2007-07-25 00:40:03Z mike $"
3 *
4 * Get/put file functions for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
8 *
9 * These coded instructions, statements, and computer programs are the
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/".
14 *
15 * This file is subject to the Apple OS-Developed Software exception.
16 *
17 * Contents:
18 *
19 * cupsGetFd() - Get a file from the server.
20 * cupsGetFile() - Get a file from the server.
21 * cupsPutFd() - Put a file on the server.
22 * cupsPutFile() - Put a file on the server.
23 */
24
25 /*
26 * Include necessary headers...
27 */
28
29 #include "globals.h"
30 #include "cups.h"
31 #include "language.h"
32 #include "debug.h"
33 #include <stdlib.h>
34 #include <ctype.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <sys/stat.h>
38 #if defined(WIN32) || defined(__EMX__)
39 # include <io.h>
40 #else
41 # include <unistd.h>
42 #endif /* WIN32 || __EMX__ */
43
44
45 /*
46 * 'cupsGetFd()' - Get a file from the server.
47 *
48 * This function returns @code HTTP_OK@ when the file is successfully retrieved.
49 *
50 * @since CUPS 1.1.20@
51 */
52
53 http_status_t /* O - HTTP status */
54 cupsGetFd(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
55 const char *resource, /* I - Resource name */
56 int fd) /* I - File descriptor */
57 {
58 int bytes; /* Number of bytes read */
59 char buffer[8192]; /* Buffer for file */
60 http_status_t status; /* HTTP status from server */
61 char if_modified_since[HTTP_MAX_VALUE];
62 /* If-Modified-Since header */
63
64
65 /*
66 * Range check input...
67 */
68
69 DEBUG_printf(("cupsGetFd(http=%p, resource=\"%s\", fd=%d)\n", http,
70 resource, fd));
71
72 if (!resource || fd < 0)
73 {
74 if (http)
75 http->error = EINVAL;
76
77 return (HTTP_ERROR);
78 }
79
80 if (!http)
81 http = _cupsConnect();
82
83 /*
84 * Then send GET requests to the HTTP server...
85 */
86
87 strlcpy(if_modified_since, httpGetField(http, HTTP_FIELD_IF_MODIFIED_SINCE),
88 sizeof(if_modified_since));
89
90 do
91 {
92 httpClearFields(http);
93 httpSetField(http, HTTP_FIELD_AUTHORIZATION, http->authstring);
94 httpSetField(http, HTTP_FIELD_IF_MODIFIED_SINCE, if_modified_since);
95
96 if (httpGet(http, resource))
97 {
98 if (httpReconnect(http))
99 {
100 status = HTTP_ERROR;
101 break;
102 }
103 else
104 {
105 status = HTTP_UNAUTHORIZED;
106 continue;
107 }
108 }
109
110 while ((status = httpUpdate(http)) == HTTP_CONTINUE);
111
112 if (status == HTTP_UNAUTHORIZED)
113 {
114 /*
115 * Flush any error message...
116 */
117
118 httpFlush(http);
119
120 /*
121 * See if we can do authentication...
122 */
123
124 if (cupsDoAuthentication(http, "GET", resource))
125 break;
126
127 if (httpReconnect(http))
128 {
129 status = HTTP_ERROR;
130 break;
131 }
132
133 continue;
134 }
135 #ifdef HAVE_SSL
136 else if (status == HTTP_UPGRADE_REQUIRED)
137 {
138 /* Flush any error message... */
139 httpFlush(http);
140
141 /* Reconnect... */
142 if (httpReconnect(http))
143 {
144 status = HTTP_ERROR;
145 break;
146 }
147
148 /* Upgrade with encryption... */
149 httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
150
151 /* Try again, this time with encryption enabled... */
152 continue;
153 }
154 #endif /* HAVE_SSL */
155 }
156 while (status == HTTP_UNAUTHORIZED || status == HTTP_UPGRADE_REQUIRED);
157
158 /*
159 * See if we actually got the file or an error...
160 */
161
162 if (status == HTTP_OK)
163 {
164 /*
165 * Yes, copy the file...
166 */
167
168 while ((bytes = httpRead2(http, buffer, sizeof(buffer))) > 0)
169 write(fd, buffer, bytes);
170 }
171 else
172 {
173 _cupsSetHTTPError(status);
174 httpFlush(http);
175 }
176
177 /*
178 * Return the request status...
179 */
180
181 return (status);
182 }
183
184
185 /*
186 * 'cupsGetFile()' - Get a file from the server.
187 *
188 * This function returns @code HTTP_OK@ when the file is successfully retrieved.
189 *
190 * @since CUPS 1.1.20@
191 */
192
193 http_status_t /* O - HTTP status */
194 cupsGetFile(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
195 const char *resource, /* I - Resource name */
196 const char *filename) /* I - Filename */
197 {
198 int fd; /* File descriptor */
199 http_status_t status; /* Status */
200
201
202 /*
203 * Range check input...
204 */
205
206 if (!http || !resource || !filename)
207 {
208 if (http)
209 http->error = EINVAL;
210
211 return (HTTP_ERROR);
212 }
213
214 /*
215 * Create the file...
216 */
217
218 if ((fd = open(filename, O_WRONLY | O_EXCL | O_TRUNC)) < 0)
219 {
220 /*
221 * Couldn't open the file!
222 */
223
224 http->error = errno;
225
226 return (HTTP_ERROR);
227 }
228
229 /*
230 * Get the file...
231 */
232
233 status = cupsGetFd(http, resource, fd);
234
235 /*
236 * If the file couldn't be gotten, then remove the file...
237 */
238
239 close(fd);
240
241 if (status != HTTP_OK)
242 unlink(filename);
243
244 /*
245 * Return the HTTP status code...
246 */
247
248 return (status);
249 }
250
251
252 /*
253 * 'cupsPutFd()' - Put a file on the server.
254 *
255 * This function returns @code HTTP_CREATED@ when the file is stored
256 * successfully.
257 *
258 * @since CUPS 1.1.20@
259 */
260
261 http_status_t /* O - HTTP status */
262 cupsPutFd(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
263 const char *resource, /* I - Resource name */
264 int fd) /* I - File descriptor */
265 {
266 int bytes, /* Number of bytes read */
267 retries; /* Number of retries */
268 char buffer[8192]; /* Buffer for file */
269 http_status_t status; /* HTTP status from server */
270
271
272 /*
273 * Range check input...
274 */
275
276 DEBUG_printf(("cupsPutFd(http=%p, resource=\"%s\", fd=%d)\n", http,
277 resource, fd));
278
279 if (!resource || fd < 0)
280 {
281 if (http)
282 http->error = EINVAL;
283
284 return (HTTP_ERROR);
285 }
286
287 if (!http)
288 http = _cupsConnect();
289
290 /*
291 * Then send PUT requests to the HTTP server...
292 */
293
294 retries = 0;
295
296 do
297 {
298 DEBUG_printf(("cupsPutFd: starting attempt, authstring=\"%s\"...\n",
299 http->authstring));
300
301 httpClearFields(http);
302 httpSetField(http, HTTP_FIELD_AUTHORIZATION, http->authstring);
303 httpSetField(http, HTTP_FIELD_TRANSFER_ENCODING, "chunked");
304 httpSetExpect(http, HTTP_CONTINUE);
305
306 if (httpPut(http, resource))
307 {
308 if (httpReconnect(http))
309 {
310 status = HTTP_ERROR;
311 break;
312 }
313 else
314 {
315 status = HTTP_UNAUTHORIZED;
316 continue;
317 }
318 }
319
320 /*
321 * Wait up to 1 second for a 100-continue response...
322 */
323
324 if (httpWait(http, 1000))
325 status = httpUpdate(http);
326 else
327 status = HTTP_CONTINUE;
328
329 if (status == HTTP_CONTINUE)
330 {
331 /*
332 * Copy the file...
333 */
334
335 lseek(fd, 0, SEEK_SET);
336
337 while ((bytes = read(fd, buffer, sizeof(buffer))) > 0)
338 if (httpCheck(http))
339 {
340 if ((status = httpUpdate(http)) != HTTP_CONTINUE)
341 break;
342 }
343 else
344 httpWrite2(http, buffer, bytes);
345 }
346
347 if (status == HTTP_CONTINUE)
348 {
349 httpWrite2(http, buffer, 0);
350
351 while ((status = httpUpdate(http)) == HTTP_CONTINUE);
352 }
353
354 if (status == HTTP_ERROR && !retries)
355 {
356 DEBUG_printf(("cupsPutFd: retry on status %d\n", status));
357
358 retries ++;
359
360 /* Flush any error message... */
361 httpFlush(http);
362
363 /* Reconnect... */
364 if (httpReconnect(http))
365 {
366 status = HTTP_ERROR;
367 break;
368 }
369
370 /* Try again... */
371 continue;
372 }
373
374 DEBUG_printf(("cupsPutFd: status=%d\n", status));
375
376 if (status == HTTP_UNAUTHORIZED)
377 {
378 /*
379 * Flush any error message...
380 */
381
382 httpFlush(http);
383
384 /*
385 * See if we can do authentication...
386 */
387
388 if (cupsDoAuthentication(http, "PUT", resource))
389 break;
390
391 if (httpReconnect(http))
392 {
393 status = HTTP_ERROR;
394 break;
395 }
396
397 continue;
398 }
399 #ifdef HAVE_SSL
400 else if (status == HTTP_UPGRADE_REQUIRED)
401 {
402 /* Flush any error message... */
403 httpFlush(http);
404
405 /* Reconnect... */
406 if (httpReconnect(http))
407 {
408 status = HTTP_ERROR;
409 break;
410 }
411
412 /* Upgrade with encryption... */
413 httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
414
415 /* Try again, this time with encryption enabled... */
416 continue;
417 }
418 #endif /* HAVE_SSL */
419 }
420 while (status == HTTP_UNAUTHORIZED || status == HTTP_UPGRADE_REQUIRED ||
421 (status == HTTP_ERROR && retries < 2));
422
423 /*
424 * See if we actually put the file or an error...
425 */
426
427 if (status != HTTP_CREATED)
428 {
429 _cupsSetHTTPError(status);
430 httpFlush(http);
431 }
432
433 return (status);
434 }
435
436
437 /*
438 * 'cupsPutFile()' - Put a file on the server.
439 *
440 * This function returns @code HTTP_CREATED@ when the file is stored
441 * successfully.
442 *
443 * @since CUPS 1.1.20@
444 */
445
446 http_status_t /* O - HTTP status */
447 cupsPutFile(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
448 const char *resource, /* I - Resource name */
449 const char *filename) /* I - Filename */
450 {
451 int fd; /* File descriptor */
452 http_status_t status; /* Status */
453
454
455 /*
456 * Range check input...
457 */
458
459 if (!http || !resource || !filename)
460 {
461 if (http)
462 http->error = EINVAL;
463
464 return (HTTP_ERROR);
465 }
466
467 /*
468 * Open the local file...
469 */
470
471 if ((fd = open(filename, O_RDONLY)) < 0)
472 {
473 /*
474 * Couldn't open the file!
475 */
476
477 http->error = errno;
478
479 return (HTTP_ERROR);
480 }
481
482 /*
483 * Put the file...
484 */
485
486 status = cupsPutFd(http, resource, fd);
487
488 close(fd);
489
490 return (status);
491 }
492
493
494 /*
495 * End of "$Id: getputfile.c 6720 2007-07-25 00:40:03Z mike $".
496 */