]> 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/*
fa73b229 2 * "$Id: getputfile.c 4984 2006-01-25 21:55:36Z 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
63http_status_t /* O - Status */
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
169 while ((bytes = httpRead(http, buffer, sizeof(buffer))) > 0)
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
191http_status_t /* O - Status */
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
258http_status_t /* O - Status */
259cupsPutFd(http_t *http, /* I - HTTP connection to server */
260 const char *resource, /* I - Resource name */
261 int fd) /* I - File descriptor */
262{
263 int bytes; /* Number of bytes read */
264 char buffer[8192]; /* Buffer for file */
265 http_status_t status; /* HTTP status from server */
266
267
268 /*
269 * Range check input...
270 */
271
272 DEBUG_printf(("cupsPutFd(http=%p, resource=\"%s\", fd=%d)\n", http,
273 resource, fd));
274
275 if (!http || !resource || fd < 0)
276 {
277 if (http)
278 http->error = EINVAL;
279
280 return (HTTP_ERROR);
281 }
282
283 /*
284 * Then send PUT requests to the HTTP server...
285 */
286
287 do
288 {
289 DEBUG_printf(("cupsPutFd: starting attempt, authstring=\"%s\"...\n",
290 http->authstring));
291
292 httpClearFields(http);
293 httpSetField(http, HTTP_FIELD_AUTHORIZATION, http->authstring);
294 httpSetField(http, HTTP_FIELD_TRANSFER_ENCODING, "chunked");
295
296 if (httpPut(http, resource))
297 {
298 if (httpReconnect(http))
299 {
300 status = HTTP_ERROR;
301 break;
302 }
303 else
304 {
305 status = HTTP_UNAUTHORIZED;
306 continue;
307 }
308 }
309
310 /*
311 * Copy the file...
312 */
313
314 lseek(fd, 0, SEEK_SET);
315
316 status = HTTP_CONTINUE;
317
318 while ((bytes = read(fd, buffer, sizeof(buffer))) > 0)
319 if (httpCheck(http))
320 {
321 if ((status = httpUpdate(http)) != HTTP_CONTINUE)
322 break;
323 }
324 else
325 httpWrite(http, buffer, bytes);
326
327 if (status == HTTP_CONTINUE)
328 {
329 httpWrite(http, buffer, 0);
330
331 while ((status = httpUpdate(http)) == HTTP_CONTINUE);
332 }
333
334 DEBUG_printf(("cupsPutFd: status=%d\n", status));
335
336 if (status == HTTP_UNAUTHORIZED)
337 {
338 /*
339 * Flush any error message...
340 */
341
342 httpFlush(http);
343
344 /*
345 * See if we can do authentication...
346 */
347
348 if (cupsDoAuthentication(http, "PUT", resource))
349 break;
350
fa73b229 351 if (httpReconnect(http))
352 {
353 status = HTTP_ERROR;
354 break;
355 }
ef416fc2 356
357 continue;
358 }
359#ifdef HAVE_SSL
360 else if (status == HTTP_UPGRADE_REQUIRED)
361 {
362 /* Flush any error message... */
363 httpFlush(http);
364
365 /* Reconnect... */
fa73b229 366 if (httpReconnect(http))
367 {
368 status = HTTP_ERROR;
369 break;
370 }
ef416fc2 371
372 /* Upgrade with encryption... */
373 httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
374
375 /* Try again, this time with encryption enabled... */
376 continue;
377 }
378#endif /* HAVE_SSL */
379 }
380 while (status == HTTP_UNAUTHORIZED || status == HTTP_UPGRADE_REQUIRED);
381
382 /*
383 * See if we actually put the file or an error...
384 */
385
386 if (status != HTTP_CREATED)
387 httpFlush(http);
388
389 return (status);
390}
391
392
393/*
394 * 'cupsPutFile()' - Put a file on the server.
395 *
396 * This function returns HTTP_CREATED when the file is stored successfully.
397 *
398 * @since CUPS 1.1.20@
399 */
400
401http_status_t /* O - Status */
402cupsPutFile(http_t *http, /* I - HTTP connection to server */
403 const char *resource, /* I - Resource name */
404 const char *filename) /* I - Filename */
405{
406 int fd; /* File descriptor */
407 http_status_t status; /* Status */
408
409
410 /*
411 * Range check input...
412 */
413
414 if (!http || !resource || !filename)
415 {
416 if (http)
417 http->error = EINVAL;
418
419 return (HTTP_ERROR);
420 }
421
422 /*
423 * Open the local file...
424 */
425
426 if ((fd = open(filename, O_RDONLY)) < 0)
427 {
428 /*
429 * Couldn't open the file!
430 */
431
432 http->error = errno;
433
434 return (HTTP_ERROR);
435 }
436
437 /*
438 * Put the file...
439 */
440
441 status = cupsPutFd(http, resource, fd);
442
443 close(fd);
444
445 return (status);
446}
447
448
449/*
fa73b229 450 * End of "$Id: getputfile.c 4984 2006-01-25 21:55:36Z mike $".
ef416fc2 451 */