2 * "$Id: getputfile.c,v 1.3 2004/02/04 19:06:49 mike Exp $"
4 * Get/put file functions for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2003 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-3111 USA
20 * Voice: (301) 373-9603
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.
59 http_status_t
/* O - Status */
60 cupsGetFd(http_t
*http
, /* I - HTTP connection to server */
61 const char *resource
, /* I - Resource name */
62 int fd
) /* I - File descriptor */
64 int bytes
; /* Number of bytes read */
65 char buffer
[8192]; /* Buffer for file */
66 http_status_t status
; /* HTTP status from server */
70 * Range check input...
73 DEBUG_printf(("cupsGetFd(http=%p, resource=\"%s\", fd=%d)\n", http
,
76 if (!http
|| !resource
|| fd
< 0)
85 * Then send GET requests to the HTTP server...
90 httpClearFields(http
);
91 httpSetField(http
, HTTP_FIELD_AUTHORIZATION
, http
->authstring
);
93 if (httpGet(http
, resource
))
95 if (httpReconnect(http
))
102 status
= HTTP_UNAUTHORIZED
;
107 while ((status
= httpUpdate(http
)) == HTTP_CONTINUE
);
109 if (status
== HTTP_UNAUTHORIZED
)
112 * Flush any error message...
118 * See if we can do authentication...
121 if (cupsDoAuthentication(http
, "GET", resource
))
128 else if (status
== HTTP_ERROR
)
131 if (http
->error
!= WSAENETDOWN
&& http
->error
!= WSAENETUNREACH
)
133 if (http
->error
!= ENETDOWN
&& http
->error
!= ENETUNREACH
)
140 else if (status
== HTTP_UPGRADE_REQUIRED
)
142 /* Flush any error message... */
148 /* Upgrade with encryption... */
149 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
151 /* Try again, this time with encryption enabled... */
154 #endif /* HAVE_LIBSSL */
156 while (status
== HTTP_UNAUTHORIZED
|| status
== HTTP_UPGRADE_REQUIRED
||
157 status
== HTTP_ERROR
);
160 * See if we actually got the file or an error...
163 if (status
== HTTP_OK
)
166 * Yes, copy the file...
169 while ((bytes
= httpRead(http
, buffer
, sizeof(buffer
))) > 0)
170 write(fd
, buffer
, bytes
);
176 * Return the request status...
184 * 'cupsGetFile()' - Get a file from the server.
187 http_status_t
/* O - Status */
188 cupsGetFile(http_t
*http
, /* I - HTTP connection to server */
189 const char *resource
, /* I - Resource name */
190 const char *filename
) /* I - Filename */
192 int fd
; /* File descriptor */
193 http_status_t status
; /* Status */
197 * Range check input...
200 if (!http
|| !resource
|| !filename
)
203 http
->error
= EINVAL
;
212 if ((fd
= open(filename
, O_WRONLY
| O_EXCL
| O_TRUNC
)) < 0)
215 * Couldn't open the file!
227 status
= cupsGetFd(http
, resource
, fd
);
230 * If the file couldn't be gotten, then remove the file...
235 if (status
!= HTTP_OK
)
239 * Return the HTTP status code...
247 * 'cupsPutFd()' - Put a file on the server.
250 http_status_t
/* O - Status */
251 cupsPutFd(http_t
*http
, /* I - HTTP connection to server */
252 const char *resource
, /* I - Resource name */
253 int fd
) /* I - File descriptor */
255 int bytes
; /* Number of bytes read */
256 char buffer
[8192]; /* Buffer for file */
257 http_status_t status
; /* HTTP status from server */
261 * Range check input...
264 DEBUG_printf(("cupsPutFd(http=%p, resource=\"%s\", fd=%d)\n", http
,
267 if (!http
|| !resource
|| fd
< 0)
270 http
->error
= EINVAL
;
276 * Then send PUT requests to the HTTP server...
281 httpClearFields(http
);
282 httpSetField(http
, HTTP_FIELD_AUTHORIZATION
, http
->authstring
);
283 httpSetField(http
, HTTP_FIELD_TRANSFER_ENCODING
, "chunked");
285 if (httpPut(http
, resource
))
287 if (httpReconnect(http
))
294 status
= HTTP_UNAUTHORIZED
;
303 lseek(fd
, 0, SEEK_SET
);
305 status
= HTTP_CONTINUE
;
307 while ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
310 if ((status
= httpUpdate(http
)) != HTTP_CONTINUE
)
314 httpWrite(http
, buffer
, bytes
);
316 if (status
== HTTP_CONTINUE
)
318 httpWrite(http
, buffer
, 0);
320 while ((status
= httpUpdate(http
)) == HTTP_CONTINUE
);
323 if (status
== HTTP_UNAUTHORIZED
)
326 * Flush any error message...
332 * See if we can do authentication...
335 if (cupsDoAuthentication(http
, "PUT", resource
))
342 else if (status
== HTTP_ERROR
)
345 if (http
->error
!= WSAENETDOWN
&& http
->error
!= WSAENETUNREACH
)
347 if (http
->error
!= ENETDOWN
&& http
->error
!= ENETUNREACH
)
354 else if (status
== HTTP_UPGRADE_REQUIRED
)
356 /* Flush any error message... */
362 /* Upgrade with encryption... */
363 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
365 /* Try again, this time with encryption enabled... */
368 #endif /* HAVE_LIBSSL */
370 while (status
== HTTP_UNAUTHORIZED
|| status
== HTTP_UPGRADE_REQUIRED
||
371 status
== HTTP_ERROR
);
374 * See if we actually put the file or an error...
377 if (status
!= HTTP_CREATED
)
385 * 'cupsPutFile()' - Put a file on the server.
388 http_status_t
/* O - Status */
389 cupsPutFile(http_t
*http
, /* I - HTTP connection to server */
390 const char *resource
, /* I - Resource name */
391 const char *filename
) /* I - Filename */
393 int fd
; /* File descriptor */
394 http_status_t status
; /* Status */
398 * Range check input...
401 if (!http
|| !resource
|| !filename
)
404 http
->error
= EINVAL
;
410 * Open the local file...
413 if ((fd
= open(filename
, O_RDONLY
)) < 0)
416 * Couldn't open the file!
428 status
= cupsPutFd(http
, resource
, fd
);
437 * End of "$Id: getputfile.c,v 1.3 2004/02/04 19:06:49 mike Exp $".