2 * "$Id: getputfile.c 6720 2007-07-25 00:40:03Z mike $"
4 * Get/put file functions for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
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/".
15 * This file is subject to the Apple OS-Developed Software exception.
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.
26 * Include necessary headers...
38 #if defined(WIN32) || defined(__EMX__)
42 #endif /* WIN32 || __EMX__ */
46 * 'cupsGetFd()' - Get a file from the server.
48 * This function returns @code HTTP_OK@ when the file is successfully retrieved.
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 */
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 */
66 * Range check input...
69 DEBUG_printf(("cupsGetFd(http=%p, resource=\"%s\", fd=%d)\n", http
,
72 if (!resource
|| fd
< 0)
81 http
= _cupsConnect();
84 * Then send GET requests to the HTTP server...
87 strlcpy(if_modified_since
, httpGetField(http
, HTTP_FIELD_IF_MODIFIED_SINCE
),
88 sizeof(if_modified_since
));
92 httpClearFields(http
);
93 httpSetField(http
, HTTP_FIELD_AUTHORIZATION
, http
->authstring
);
94 httpSetField(http
, HTTP_FIELD_IF_MODIFIED_SINCE
, if_modified_since
);
96 if (httpGet(http
, resource
))
98 if (httpReconnect(http
))
105 status
= HTTP_UNAUTHORIZED
;
110 while ((status
= httpUpdate(http
)) == HTTP_CONTINUE
);
112 if (status
== HTTP_UNAUTHORIZED
)
115 * Flush any error message...
121 * See if we can do authentication...
124 if (cupsDoAuthentication(http
, "GET", resource
))
127 if (httpReconnect(http
))
136 else if (status
== HTTP_UPGRADE_REQUIRED
)
138 /* Flush any error message... */
142 if (httpReconnect(http
))
148 /* Upgrade with encryption... */
149 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
151 /* Try again, this time with encryption enabled... */
154 #endif /* HAVE_SSL */
156 while (status
== HTTP_UNAUTHORIZED
|| status
== HTTP_UPGRADE_REQUIRED
);
159 * See if we actually got the file or an error...
162 if (status
== HTTP_OK
)
165 * Yes, copy the file...
168 while ((bytes
= httpRead2(http
, buffer
, sizeof(buffer
))) > 0)
169 write(fd
, buffer
, bytes
);
173 _cupsSetHTTPError(status
);
178 * Return the request status...
186 * 'cupsGetFile()' - Get a file from the server.
188 * This function returns @code HTTP_OK@ when the file is successfully retrieved.
190 * @since CUPS 1.1.20@
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 */
198 int fd
; /* File descriptor */
199 http_status_t status
; /* Status */
203 * Range check input...
206 if (!http
|| !resource
|| !filename
)
209 http
->error
= EINVAL
;
218 if ((fd
= open(filename
, O_WRONLY
| O_EXCL
| O_TRUNC
)) < 0)
221 * Couldn't open the file!
233 status
= cupsGetFd(http
, resource
, fd
);
236 * If the file couldn't be gotten, then remove the file...
241 if (status
!= HTTP_OK
)
245 * Return the HTTP status code...
253 * 'cupsPutFd()' - Put a file on the server.
255 * This function returns @code HTTP_CREATED@ when the file is stored
258 * @since CUPS 1.1.20@
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 */
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 */
273 * Range check input...
276 DEBUG_printf(("cupsPutFd(http=%p, resource=\"%s\", fd=%d)\n", http
,
279 if (!resource
|| fd
< 0)
282 http
->error
= EINVAL
;
288 http
= _cupsConnect();
291 * Then send PUT requests to the HTTP server...
298 DEBUG_printf(("cupsPutFd: starting attempt, authstring=\"%s\"...\n",
301 httpClearFields(http
);
302 httpSetField(http
, HTTP_FIELD_AUTHORIZATION
, http
->authstring
);
303 httpSetField(http
, HTTP_FIELD_TRANSFER_ENCODING
, "chunked");
304 httpSetExpect(http
, HTTP_CONTINUE
);
306 if (httpPut(http
, resource
))
308 if (httpReconnect(http
))
315 status
= HTTP_UNAUTHORIZED
;
321 * Wait up to 1 second for a 100-continue response...
324 if (httpWait(http
, 1000))
325 status
= httpUpdate(http
);
327 status
= HTTP_CONTINUE
;
329 if (status
== HTTP_CONTINUE
)
335 lseek(fd
, 0, SEEK_SET
);
337 while ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
340 if ((status
= httpUpdate(http
)) != HTTP_CONTINUE
)
344 httpWrite2(http
, buffer
, bytes
);
347 if (status
== HTTP_CONTINUE
)
349 httpWrite2(http
, buffer
, 0);
351 while ((status
= httpUpdate(http
)) == HTTP_CONTINUE
);
354 if (status
== HTTP_ERROR
&& !retries
)
356 DEBUG_printf(("cupsPutFd: retry on status %d\n", status
));
360 /* Flush any error message... */
364 if (httpReconnect(http
))
374 DEBUG_printf(("cupsPutFd: status=%d\n", status
));
376 if (status
== HTTP_UNAUTHORIZED
)
379 * Flush any error message...
385 * See if we can do authentication...
388 if (cupsDoAuthentication(http
, "PUT", resource
))
391 if (httpReconnect(http
))
400 else if (status
== HTTP_UPGRADE_REQUIRED
)
402 /* Flush any error message... */
406 if (httpReconnect(http
))
412 /* Upgrade with encryption... */
413 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
415 /* Try again, this time with encryption enabled... */
418 #endif /* HAVE_SSL */
420 while (status
== HTTP_UNAUTHORIZED
|| status
== HTTP_UPGRADE_REQUIRED
||
421 (status
== HTTP_ERROR
&& retries
< 2));
424 * See if we actually put the file or an error...
427 if (status
!= HTTP_CREATED
)
429 _cupsSetHTTPError(status
);
438 * 'cupsPutFile()' - Put a file on the server.
440 * This function returns @code HTTP_CREATED@ when the file is stored
443 * @since CUPS 1.1.20@
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 */
451 int fd
; /* File descriptor */
452 http_status_t status
; /* Status */
456 * Range check input...
459 if (!http
|| !resource
|| !filename
)
462 http
->error
= EINVAL
;
468 * Open the local file...
471 if ((fd
= open(filename
, O_RDONLY
)) < 0)
474 * Couldn't open the file!
486 status
= cupsPutFd(http
, resource
, fd
);
495 * End of "$Id: getputfile.c 6720 2007-07-25 00:40:03Z mike $".