2 * "$Id: getputfile.c 5103 2006-02-14 19:27:42Z mike $"
4 * Get/put file functions for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products.
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
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
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.
35 * Include necessary headers...
48 #if defined(WIN32) || defined(__EMX__)
52 #endif /* WIN32 || __EMX__ */
56 * 'cupsGetFd()' - Get a file from the server.
58 * This function returns HTTP_OK when the file is successfully retrieved.
63 http_status_t
/* O - Status */
64 cupsGetFd(http_t
*http
, /* I - HTTP connection to server */
65 const char *resource
, /* I - Resource name */
66 int fd
) /* I - File descriptor */
68 int bytes
; /* Number of bytes read */
69 char buffer
[8192]; /* Buffer for file */
70 http_status_t status
; /* HTTP status from server */
74 * Range check input...
77 DEBUG_printf(("cupsGetFd(http=%p, resource=\"%s\", fd=%d)\n", http
,
80 if (!http
|| !resource
|| fd
< 0)
89 * Then send GET requests to the HTTP server...
94 httpClearFields(http
);
95 httpSetField(http
, HTTP_FIELD_AUTHORIZATION
, http
->authstring
);
97 if (httpGet(http
, resource
))
99 if (httpReconnect(http
))
106 status
= HTTP_UNAUTHORIZED
;
111 while ((status
= httpUpdate(http
)) == HTTP_CONTINUE
);
113 if (status
== HTTP_UNAUTHORIZED
)
116 * Flush any error message...
122 * See if we can do authentication...
125 if (cupsDoAuthentication(http
, "GET", resource
))
128 if (httpReconnect(http
))
137 else if (status
== HTTP_UPGRADE_REQUIRED
)
139 /* Flush any error message... */
143 if (httpReconnect(http
))
149 /* Upgrade with encryption... */
150 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
152 /* Try again, this time with encryption enabled... */
155 #endif /* HAVE_SSL */
157 while (status
== HTTP_UNAUTHORIZED
|| status
== HTTP_UPGRADE_REQUIRED
);
160 * See if we actually got the file or an error...
163 if (status
== HTTP_OK
)
166 * Yes, copy the file...
169 while ((bytes
= httpRead2(http
, buffer
, sizeof(buffer
))) > 0)
170 write(fd
, buffer
, bytes
);
176 * Return the request status...
184 * 'cupsGetFile()' - Get a file from the server.
186 * This function returns HTTP_OK when the file is successfully retrieved.
188 * @since CUPS 1.1.20@
191 http_status_t
/* O - Status */
192 cupsGetFile(http_t
*http
, /* I - HTTP connection to server */
193 const char *resource
, /* I - Resource name */
194 const char *filename
) /* I - Filename */
196 int fd
; /* File descriptor */
197 http_status_t status
; /* Status */
201 * Range check input...
204 if (!http
|| !resource
|| !filename
)
207 http
->error
= EINVAL
;
216 if ((fd
= open(filename
, O_WRONLY
| O_EXCL
| O_TRUNC
)) < 0)
219 * Couldn't open the file!
231 status
= cupsGetFd(http
, resource
, fd
);
234 * If the file couldn't be gotten, then remove the file...
239 if (status
!= HTTP_OK
)
243 * Return the HTTP status code...
251 * 'cupsPutFd()' - Put a file on the server.
253 * This function returns HTTP_CREATED when the file is stored successfully.
255 * @since CUPS 1.1.20@
258 http_status_t
/* O - Status */
259 cupsPutFd(http_t
*http
, /* I - HTTP connection to server */
260 const char *resource
, /* I - Resource name */
261 int fd
) /* I - File descriptor */
263 int bytes
, /* Number of bytes read */
264 retries
; /* Number of retries */
265 char buffer
[8192]; /* Buffer for file */
266 http_status_t status
; /* HTTP status from server */
270 * Range check input...
273 DEBUG_printf(("cupsPutFd(http=%p, resource=\"%s\", fd=%d)\n", http
,
276 if (!http
|| !resource
|| fd
< 0)
279 http
->error
= EINVAL
;
285 * Then send PUT requests to the HTTP server...
292 DEBUG_printf(("cupsPutFd: starting attempt, authstring=\"%s\"...\n",
295 httpClearFields(http
);
296 httpSetField(http
, HTTP_FIELD_AUTHORIZATION
, http
->authstring
);
297 httpSetField(http
, HTTP_FIELD_TRANSFER_ENCODING
, "chunked");
299 if (httpPut(http
, resource
))
301 if (httpReconnect(http
))
308 status
= HTTP_UNAUTHORIZED
;
317 lseek(fd
, 0, SEEK_SET
);
319 status
= HTTP_CONTINUE
;
321 while ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
324 if ((status
= httpUpdate(http
)) != HTTP_CONTINUE
)
328 httpWrite2(http
, buffer
, bytes
);
330 if (status
== HTTP_CONTINUE
)
332 httpWrite2(http
, buffer
, 0);
334 while ((status
= httpUpdate(http
)) == HTTP_CONTINUE
);
337 if (status
== HTTP_ERROR
&& !retries
)
339 DEBUG_printf(("cupsPutFd: retry on status %d\n", status
));
343 /* Flush any error message... */
347 if (httpReconnect(http
))
357 DEBUG_printf(("cupsPutFd: status=%d\n", status
));
359 if (status
== HTTP_UNAUTHORIZED
)
362 * Flush any error message...
368 * See if we can do authentication...
371 if (cupsDoAuthentication(http
, "PUT", resource
))
374 if (httpReconnect(http
))
383 else if (status
== HTTP_UPGRADE_REQUIRED
)
385 /* Flush any error message... */
389 if (httpReconnect(http
))
395 /* Upgrade with encryption... */
396 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
398 /* Try again, this time with encryption enabled... */
401 #endif /* HAVE_SSL */
403 while (status
== HTTP_UNAUTHORIZED
|| status
== HTTP_UPGRADE_REQUIRED
||
404 (status
== HTTP_ERROR
&& retries
< 2));
407 * See if we actually put the file or an error...
410 if (status
!= HTTP_CREATED
)
418 * 'cupsPutFile()' - Put a file on the server.
420 * This function returns HTTP_CREATED when the file is stored successfully.
422 * @since CUPS 1.1.20@
425 http_status_t
/* O - Status */
426 cupsPutFile(http_t
*http
, /* I - HTTP connection to server */
427 const char *resource
, /* I - Resource name */
428 const char *filename
) /* I - Filename */
430 int fd
; /* File descriptor */
431 http_status_t status
; /* Status */
435 * Range check input...
438 if (!http
|| !resource
|| !filename
)
441 http
->error
= EINVAL
;
447 * Open the local file...
450 if ((fd
= open(filename
, O_RDONLY
)) < 0)
453 * Couldn't open the file!
465 status
= cupsPutFd(http
, resource
, fd
);
474 * End of "$Id: getputfile.c 5103 2006-02-14 19:27:42Z mike $".