2 * "$Id: getputfile.c 4918 2006-01-12 05:14:40Z 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
))
133 else if (status
== HTTP_UPGRADE_REQUIRED
)
135 /* Flush any error message... */
141 /* Upgrade with encryption... */
142 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
144 /* Try again, this time with encryption enabled... */
147 #endif /* HAVE_SSL */
149 while (status
== HTTP_UNAUTHORIZED
|| status
== HTTP_UPGRADE_REQUIRED
);
152 * See if we actually got the file or an error...
155 if (status
== HTTP_OK
)
158 * Yes, copy the file...
161 while ((bytes
= httpRead(http
, buffer
, sizeof(buffer
))) > 0)
162 write(fd
, buffer
, bytes
);
168 * Return the request status...
176 * 'cupsGetFile()' - Get a file from the server.
178 * This function returns HTTP_OK when the file is successfully retrieved.
180 * @since CUPS 1.1.20@
183 http_status_t
/* O - Status */
184 cupsGetFile(http_t
*http
, /* I - HTTP connection to server */
185 const char *resource
, /* I - Resource name */
186 const char *filename
) /* I - Filename */
188 int fd
; /* File descriptor */
189 http_status_t status
; /* Status */
193 * Range check input...
196 if (!http
|| !resource
|| !filename
)
199 http
->error
= EINVAL
;
208 if ((fd
= open(filename
, O_WRONLY
| O_EXCL
| O_TRUNC
)) < 0)
211 * Couldn't open the file!
223 status
= cupsGetFd(http
, resource
, fd
);
226 * If the file couldn't be gotten, then remove the file...
231 if (status
!= HTTP_OK
)
235 * Return the HTTP status code...
243 * 'cupsPutFd()' - Put a file on the server.
245 * This function returns HTTP_CREATED when the file is stored successfully.
247 * @since CUPS 1.1.20@
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 DEBUG_printf(("cupsPutFd: starting attempt, authstring=\"%s\"...\n",
284 httpClearFields(http
);
285 httpSetField(http
, HTTP_FIELD_AUTHORIZATION
, http
->authstring
);
286 httpSetField(http
, HTTP_FIELD_TRANSFER_ENCODING
, "chunked");
288 if (httpPut(http
, resource
))
290 if (httpReconnect(http
))
297 status
= HTTP_UNAUTHORIZED
;
306 lseek(fd
, 0, SEEK_SET
);
308 status
= HTTP_CONTINUE
;
310 while ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
313 if ((status
= httpUpdate(http
)) != HTTP_CONTINUE
)
317 httpWrite(http
, buffer
, bytes
);
319 if (status
== HTTP_CONTINUE
)
321 httpWrite(http
, buffer
, 0);
323 while ((status
= httpUpdate(http
)) == HTTP_CONTINUE
);
326 DEBUG_printf(("cupsPutFd: status=%d\n", status
));
328 if (status
== HTTP_UNAUTHORIZED
)
331 * Flush any error message...
337 * See if we can do authentication...
340 if (cupsDoAuthentication(http
, "PUT", resource
))
348 else if (status
== HTTP_UPGRADE_REQUIRED
)
350 /* Flush any error message... */
356 /* Upgrade with encryption... */
357 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
359 /* Try again, this time with encryption enabled... */
362 #endif /* HAVE_SSL */
364 while (status
== HTTP_UNAUTHORIZED
|| status
== HTTP_UPGRADE_REQUIRED
);
367 * See if we actually put the file or an error...
370 if (status
!= HTTP_CREATED
)
378 * 'cupsPutFile()' - Put a file on the server.
380 * This function returns HTTP_CREATED when the file is stored successfully.
382 * @since CUPS 1.1.20@
385 http_status_t
/* O - Status */
386 cupsPutFile(http_t
*http
, /* I - HTTP connection to server */
387 const char *resource
, /* I - Resource name */
388 const char *filename
) /* I - Filename */
390 int fd
; /* File descriptor */
391 http_status_t status
; /* Status */
395 * Range check input...
398 if (!http
|| !resource
|| !filename
)
401 http
->error
= EINVAL
;
407 * Open the local file...
410 if ((fd
= open(filename
, O_RDONLY
)) < 0)
413 * Couldn't open the file!
425 status
= cupsPutFd(http
, resource
, fd
);
434 * End of "$Id: getputfile.c 4918 2006-01-12 05:14:40Z mike $".