]>
Commit | Line | Data |
---|---|---|
0e550316 | 1 | /* |
c9d3f842 | 2 | * "$Id$" |
0e550316 | 3 | * |
4 | * Get/put file functions for the Common UNIX Printing System (CUPS). | |
5 | * | |
f82d9dea | 6 | * Copyright 1997-2006 by Easy Software Products. |
0e550316 | 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 | |
c9d3f842 | 18 | * Hollywood, Maryland 20636 USA |
0e550316 | 19 | * |
c4dcf3cc | 20 | * Voice: (301) 373-9600 |
0e550316 | 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. | |
0341722a | 57 | * |
f82d9dea | 58 | * This function returns HTTP_OK when the file is successfully retrieved. |
59 | * | |
0341722a | 60 | * @since CUPS 1.1.20@ |
0e550316 | 61 | */ |
62 | ||
7fbf7fc4 | 63 | http_status_t /* O - HTTP status */ |
0e550316 | 64 | cupsGetFd(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 | ||
b310f962 | 77 | DEBUG_printf(("cupsGetFd(http=%p, resource=\"%s\", fd=%d)\n", http, |
78 | resource, fd)); | |
79 | ||
0e550316 | 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 | ||
0d8460f8 | 128 | if (httpReconnect(http)) |
129 | { | |
130 | status = HTTP_ERROR; | |
131 | break; | |
132 | } | |
0e550316 | 133 | |
134 | continue; | |
135 | } | |
c8f54662 | 136 | #ifdef HAVE_SSL |
0e550316 | 137 | else if (status == HTTP_UPGRADE_REQUIRED) |
138 | { | |
9b4bc2a5 | 139 | /* Flush any error message... */ |
0e550316 | 140 | httpFlush(http); |
141 | ||
9b4bc2a5 | 142 | /* Reconnect... */ |
0d8460f8 | 143 | if (httpReconnect(http)) |
144 | { | |
145 | status = HTTP_ERROR; | |
146 | break; | |
147 | } | |
0e550316 | 148 | |
9b4bc2a5 | 149 | /* Upgrade with encryption... */ |
0e550316 | 150 | httpEncryption(http, HTTP_ENCRYPT_REQUIRED); |
151 | ||
9b4bc2a5 | 152 | /* Try again, this time with encryption enabled... */ |
0e550316 | 153 | continue; |
154 | } | |
c8f54662 | 155 | #endif /* HAVE_SSL */ |
0e550316 | 156 | } |
4cdd993e | 157 | while (status == HTTP_UNAUTHORIZED || status == HTTP_UPGRADE_REQUIRED); |
0e550316 | 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 | ||
00a1fad8 | 169 | while ((bytes = httpRead2(http, buffer, sizeof(buffer))) > 0) |
0e550316 | 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. | |
0341722a | 185 | * |
f82d9dea | 186 | * This function returns HTTP_OK when the file is successfully retrieved. |
187 | * | |
0341722a | 188 | * @since CUPS 1.1.20@ |
0e550316 | 189 | */ |
190 | ||
7fbf7fc4 | 191 | http_status_t /* O - HTTP status */ |
0e550316 | 192 | cupsGetFile(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. | |
0341722a | 252 | * |
f82d9dea | 253 | * This function returns HTTP_CREATED when the file is stored successfully. |
254 | * | |
0341722a | 255 | * @since CUPS 1.1.20@ |
0e550316 | 256 | */ |
257 | ||
7fbf7fc4 | 258 | http_status_t /* O - HTTP status */ |
0e550316 | 259 | cupsPutFd(http_t *http, /* I - HTTP connection to server */ |
260 | const char *resource, /* I - Resource name */ | |
261 | int fd) /* I - File descriptor */ | |
262 | { | |
926aa697 | 263 | int bytes, /* Number of bytes read */ |
264 | retries; /* Number of retries */ | |
0e550316 | 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 | ||
b310f962 | 273 | DEBUG_printf(("cupsPutFd(http=%p, resource=\"%s\", fd=%d)\n", http, |
274 | resource, fd)); | |
275 | ||
0e550316 | 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 | ||
926aa697 | 288 | retries = 0; |
289 | ||
0e550316 | 290 | do |
291 | { | |
4cdd993e | 292 | DEBUG_printf(("cupsPutFd: starting attempt, authstring=\"%s\"...\n", |
293 | http->authstring)); | |
294 | ||
0e550316 | 295 | httpClearFields(http); |
296 | httpSetField(http, HTTP_FIELD_AUTHORIZATION, http->authstring); | |
297 | httpSetField(http, HTTP_FIELD_TRANSFER_ENCODING, "chunked"); | |
d834148f | 298 | httpSetExpect(http, HTTP_CONTINUE); |
0e550316 | 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 | /* | |
d834148f | 315 | * Wait up to 1 second for a 100-continue response... |
0e550316 | 316 | */ |
317 | ||
d834148f | 318 | if (httpWait(http, 1000)) |
319 | status = httpUpdate(http); | |
320 | else | |
321 | status = HTTP_CONTINUE; | |
0e550316 | 322 | |
d834148f | 323 | if (status == HTTP_CONTINUE) |
324 | { | |
325 | /* | |
326 | * Copy the file... | |
327 | */ | |
0e550316 | 328 | |
d834148f | 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 | } | |
0e550316 | 340 | |
341 | if (status == HTTP_CONTINUE) | |
342 | { | |
00a1fad8 | 343 | httpWrite2(http, buffer, 0); |
0e550316 | 344 | |
345 | while ((status = httpUpdate(http)) == HTTP_CONTINUE); | |
346 | } | |
347 | ||
926aa697 | 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 | ||
4cdd993e | 368 | DEBUG_printf(("cupsPutFd: status=%d\n", status)); |
369 | ||
0e550316 | 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 | ||
0d8460f8 | 385 | if (httpReconnect(http)) |
386 | { | |
387 | status = HTTP_ERROR; | |
388 | break; | |
389 | } | |
0e550316 | 390 | |
391 | continue; | |
392 | } | |
c8f54662 | 393 | #ifdef HAVE_SSL |
0e550316 | 394 | else if (status == HTTP_UPGRADE_REQUIRED) |
395 | { | |
396 | /* Flush any error message... */ | |
397 | httpFlush(http); | |
398 | ||
9b4bc2a5 | 399 | /* Reconnect... */ |
0d8460f8 | 400 | if (httpReconnect(http)) |
401 | { | |
402 | status = HTTP_ERROR; | |
403 | break; | |
404 | } | |
9b4bc2a5 | 405 | |
0e550316 | 406 | /* Upgrade with encryption... */ |
407 | httpEncryption(http, HTTP_ENCRYPT_REQUIRED); | |
408 | ||
409 | /* Try again, this time with encryption enabled... */ | |
410 | continue; | |
411 | } | |
c8f54662 | 412 | #endif /* HAVE_SSL */ |
0e550316 | 413 | } |
926aa697 | 414 | while (status == HTTP_UNAUTHORIZED || status == HTTP_UPGRADE_REQUIRED || |
415 | (status == HTTP_ERROR && retries < 2)); | |
0e550316 | 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. | |
0341722a | 430 | * |
f82d9dea | 431 | * This function returns HTTP_CREATED when the file is stored successfully. |
432 | * | |
0341722a | 433 | * @since CUPS 1.1.20@ |
0e550316 | 434 | */ |
435 | ||
7fbf7fc4 | 436 | http_status_t /* O - HTTP status */ |
0e550316 | 437 | cupsPutFile(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 | /* | |
c9d3f842 | 485 | * End of "$Id$". |
0e550316 | 486 | */ |