]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
355e94dc | 2 | * "$Id: getputfile.c 6720 2007-07-25 00:40:03Z mike $" |
ef416fc2 | 3 | * |
4 | * Get/put file functions for the Common UNIX Printing System (CUPS). | |
5 | * | |
5a738aea | 6 | * Copyright 2007-2008 by Apple Inc. |
ef416fc2 | 7 | * Copyright 1997-2006 by Easy Software Products. |
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/". | |
ef416fc2 | 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 | ||
355e94dc | 29 | #include "globals.h" |
ef416fc2 | 30 | #include "cups.h" |
ef416fc2 | 31 | #include "language.h" |
ef416fc2 | 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 | * | |
5a738aea | 48 | * This function returns @code HTTP_OK@ when the file is successfully retrieved. |
ef416fc2 | 49 | * |
50 | * @since CUPS 1.1.20@ | |
51 | */ | |
52 | ||
ecdc0628 | 53 | http_status_t /* O - HTTP status */ |
5a738aea | 54 | cupsGetFd(http_t *http, /* I - HTTP connection to server or @code CUPS_HTTP_DEFAULT@ */ |
ef416fc2 | 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 */ | |
757d2cad | 61 | char if_modified_since[HTTP_MAX_VALUE]; |
62 | /* If-Modified-Since header */ | |
ef416fc2 | 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 | ||
5a738aea | 72 | if (!resource || fd < 0) |
ef416fc2 | 73 | { |
74 | if (http) | |
75 | http->error = EINVAL; | |
76 | ||
77 | return (HTTP_ERROR); | |
78 | } | |
79 | ||
5a738aea MS |
80 | if (!http) |
81 | http = _cupsConnect(); | |
82 | ||
ef416fc2 | 83 | /* |
84 | * Then send GET requests to the HTTP server... | |
85 | */ | |
86 | ||
757d2cad | 87 | strlcpy(if_modified_since, httpGetField(http, HTTP_FIELD_IF_MODIFIED_SINCE), |
88 | sizeof(if_modified_since)); | |
89 | ||
ef416fc2 | 90 | do |
91 | { | |
92 | httpClearFields(http); | |
93 | httpSetField(http, HTTP_FIELD_AUTHORIZATION, http->authstring); | |
757d2cad | 94 | httpSetField(http, HTTP_FIELD_IF_MODIFIED_SINCE, if_modified_since); |
ef416fc2 | 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 | ||
fa73b229 | 127 | if (httpReconnect(http)) |
128 | { | |
129 | status = HTTP_ERROR; | |
130 | break; | |
131 | } | |
ef416fc2 | 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... */ | |
fa73b229 | 142 | if (httpReconnect(http)) |
143 | { | |
144 | status = HTTP_ERROR; | |
145 | break; | |
146 | } | |
ef416fc2 | 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 | ||
a4d04587 | 168 | while ((bytes = httpRead2(http, buffer, sizeof(buffer))) > 0) |
ef416fc2 | 169 | write(fd, buffer, bytes); |
170 | } | |
171 | else | |
355e94dc MS |
172 | { |
173 | _cupsSetHTTPError(status); | |
ef416fc2 | 174 | httpFlush(http); |
355e94dc | 175 | } |
ef416fc2 | 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 | * | |
5a738aea | 188 | * This function returns @code HTTP_OK@ when the file is successfully retrieved. |
ef416fc2 | 189 | * |
190 | * @since CUPS 1.1.20@ | |
191 | */ | |
192 | ||
ecdc0628 | 193 | http_status_t /* O - HTTP status */ |
5a738aea | 194 | cupsGetFile(http_t *http, /* I - HTTP connection to server or @code CUPS_HTTP_DEFAULT@ */ |
ef416fc2 | 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 | * | |
5a738aea MS |
255 | * This function returns @code HTTP_CREATED@ when the file is stored |
256 | * successfully. | |
ef416fc2 | 257 | * |
258 | * @since CUPS 1.1.20@ | |
259 | */ | |
260 | ||
ecdc0628 | 261 | http_status_t /* O - HTTP status */ |
5a738aea | 262 | cupsPutFd(http_t *http, /* I - HTTP connection to server or @code CUPS_HTTP_DEFAULT@ */ |
ef416fc2 | 263 | const char *resource, /* I - Resource name */ |
264 | int fd) /* I - File descriptor */ | |
265 | { | |
bd7854cb | 266 | int bytes, /* Number of bytes read */ |
267 | retries; /* Number of retries */ | |
ef416fc2 | 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 | ||
5a738aea | 279 | if (!resource || fd < 0) |
ef416fc2 | 280 | { |
281 | if (http) | |
282 | http->error = EINVAL; | |
283 | ||
284 | return (HTTP_ERROR); | |
285 | } | |
286 | ||
5a738aea MS |
287 | if (!http) |
288 | http = _cupsConnect(); | |
289 | ||
ef416fc2 | 290 | /* |
291 | * Then send PUT requests to the HTTP server... | |
292 | */ | |
293 | ||
bd7854cb | 294 | retries = 0; |
295 | ||
ef416fc2 | 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"); | |
b423cd4c | 304 | httpSetExpect(http, HTTP_CONTINUE); |
ef416fc2 | 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 | /* | |
b423cd4c | 321 | * Wait up to 1 second for a 100-continue response... |
ef416fc2 | 322 | */ |
323 | ||
b423cd4c | 324 | if (httpWait(http, 1000)) |
325 | status = httpUpdate(http); | |
326 | else | |
327 | status = HTTP_CONTINUE; | |
ef416fc2 | 328 | |
b423cd4c | 329 | if (status == HTTP_CONTINUE) |
330 | { | |
331 | /* | |
332 | * Copy the file... | |
333 | */ | |
ef416fc2 | 334 | |
b423cd4c | 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 | } | |
ef416fc2 | 346 | |
347 | if (status == HTTP_CONTINUE) | |
348 | { | |
a4d04587 | 349 | httpWrite2(http, buffer, 0); |
ef416fc2 | 350 | |
351 | while ((status = httpUpdate(http)) == HTTP_CONTINUE); | |
352 | } | |
353 | ||
bd7854cb | 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 | ||
ef416fc2 | 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 | ||
fa73b229 | 391 | if (httpReconnect(http)) |
392 | { | |
393 | status = HTTP_ERROR; | |
394 | break; | |
395 | } | |
ef416fc2 | 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... */ | |
fa73b229 | 406 | if (httpReconnect(http)) |
407 | { | |
408 | status = HTTP_ERROR; | |
409 | break; | |
410 | } | |
ef416fc2 | 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 | } | |
bd7854cb | 420 | while (status == HTTP_UNAUTHORIZED || status == HTTP_UPGRADE_REQUIRED || |
421 | (status == HTTP_ERROR && retries < 2)); | |
ef416fc2 | 422 | |
423 | /* | |
424 | * See if we actually put the file or an error... | |
425 | */ | |
426 | ||
427 | if (status != HTTP_CREATED) | |
355e94dc MS |
428 | { |
429 | _cupsSetHTTPError(status); | |
ef416fc2 | 430 | httpFlush(http); |
355e94dc | 431 | } |
ef416fc2 | 432 | |
433 | return (status); | |
434 | } | |
435 | ||
436 | ||
437 | /* | |
438 | * 'cupsPutFile()' - Put a file on the server. | |
439 | * | |
5a738aea MS |
440 | * This function returns @code HTTP_CREATED@ when the file is stored |
441 | * successfully. | |
ef416fc2 | 442 | * |
443 | * @since CUPS 1.1.20@ | |
444 | */ | |
445 | ||
ecdc0628 | 446 | http_status_t /* O - HTTP status */ |
5a738aea | 447 | cupsPutFile(http_t *http, /* I - HTTP connection to server or @code CUPS_HTTP_DEFAULT@ */ |
ef416fc2 | 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 | /* | |
355e94dc | 495 | * End of "$Id: getputfile.c 6720 2007-07-25 00:40:03Z mike $". |
ef416fc2 | 496 | */ |