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