/*
- * "$Id: request.c 5555 2006-05-20 13:35:58Z mike $"
+ * "$Id: request.c 6506 2007-05-03 18:12:35Z mike $"
*
* IPP utilities for the Common UNIX Printing System (CUPS).
*
- * Copyright 1997-2006 by Easy Software Products.
+ * Copyright 1997-2007 by Easy Software Products.
*
* These coded instructions, statements, and computer programs are the
* property of Easy Software Products and are protected by Federal
#else
# include <unistd.h>
#endif /* WIN32 || __EMX__ */
+#ifndef O_BINARY
+# define O_BINARY 0
+#endif /* O_BINARY */
/*
ipp_t *request, /* I - IPP request */
const char *resource, /* I - HTTP resource for POST */
const char *filename) /* I - File to send or NULL for none */
+{
+ ipp_t *response; /* IPP response data */
+ int infile; /* Input file */
+
+
+ if (filename)
+ {
+ if ((infile = open(filename, O_RDONLY | O_BINARY)) < 0)
+ {
+ /*
+ * Can't get file information!
+ */
+
+ _cupsSetError(errno == ENOENT ? IPP_NOT_FOUND : IPP_NOT_AUTHORIZED,
+ strerror(errno));
+
+ ippDelete(request);
+
+ return (NULL);
+ }
+ }
+ else
+ infile = -1;
+
+ response = cupsDoIORequest(http, request, resource, infile, -1);
+
+ if (infile >= 0)
+ close(infile);
+
+ return (response);
+}
+
+
+/*
+ * 'cupsDoIORequest()' - Do an IPP request with file descriptors.
+ *
+ * This function sends the IPP request to the specified server, retrying
+ * and authenticating as necessary. The request is freed with ippDelete()
+ * after receiving a valid IPP response.
+ *
+ * If "infile" is a valid file descriptor, cupsDoIORequest() copies
+ * all of the data from the file after the IPP request message.
+ *
+ * If "outfile" is a valid file descriptor, cupsDoIORequest() copies
+ * all of the data after the IPP response message to the file.
+ *
+ * @since CUPS 1.3@
+ */
+
+ipp_t * /* O - Response data */
+cupsDoIORequest(http_t *http, /* I - HTTP connection to server */
+ ipp_t *request, /* I - IPP request */
+ const char *resource, /* I - HTTP resource for POST */
+ int infile, /* I - File to read from or -1 for none */
+ int outfile) /* I - File to write to or -1 for none */
{
ipp_t *response; /* IPP response data */
size_t length; /* Content-Length value */
http_status_t status; /* Status of HTTP request */
int got_status; /* Did we get the status? */
ipp_state_t state; /* State of IPP processing */
- FILE *file; /* File to send */
struct stat fileinfo; /* File information */
int bytes; /* Number of bytes read/written */
char buffer[32768]; /* Output buffer */
* See if we have a file to send...
*/
- if (filename != NULL)
+ if (infile >= 0)
{
- if (stat(filename, &fileinfo))
+ if (fstat(infile, &fileinfo))
{
/*
* Can't get file information!
*/
_cupsSetError(errno == ENOENT ? IPP_NOT_FOUND : IPP_NOT_AUTHORIZED,
- strerror(errno));
+ strerror(errno));
ippDelete(request);
return (NULL);
}
+ }
- if ((file = fopen(filename, "rb")) == NULL)
- {
- /*
- * Can't open file!
- */
-
- _cupsSetError(errno == ENOENT ? IPP_NOT_FOUND : IPP_NOT_AUTHORIZED,
- strerror(errno));
-
- ippDelete(request);
+#ifdef HAVE_SSL
+ /*
+ * See if we have an auth-info attribute and are communicating over
+ * a non-local link. If so, encrypt the link so that we can pass
+ * the authentication information securely...
+ */
- return (NULL);
- }
- }
- else
- file = NULL;
+ if (ippFindAttribute(request, "auth-info", IPP_TAG_TEXT) &&
+ !httpAddrLocalhost(http->hostaddr) && !http->tls &&
+ httpEncryption(http, HTTP_ENCRYPT_REQUIRED))
+ return (NULL);
+#endif /* HAVE_SSL */
/*
* Loop until we can send the request without authorization problems.
*/
length = ippLength(request);
- if (filename)
+ if (infile >= 0)
+ {
+#ifndef WIN32
+ if (!S_ISREG(fileinfo.st_mode))
+ length = 0; /* Chunk when piping */
+ else
+#endif /* !WIN32 */
length += fileinfo.st_size;
+ }
httpClearFields(http);
httpSetLength(http, length);
else if (httpCheck(http))
status = httpUpdate(http);
- if (status == HTTP_CONTINUE && state == IPP_DATA && filename)
+ if (status == HTTP_CONTINUE && state == IPP_DATA && infile >= 0)
{
DEBUG_puts("cupsDoFileRequest: file write...");
* Send the file...
*/
- rewind(file);
+#ifndef WIN32
+ if (S_ISREG(fileinfo.st_mode))
+#endif /* WIN32 */
+ lseek(infile, 0, SEEK_SET);
- while ((bytes = (int)fread(buffer, 1, sizeof(buffer), file)) > 0)
+ while ((bytes = (int)read(infile, buffer, sizeof(buffer))) > 0)
{
if (httpCheck(http))
{
#ifdef WIN32
if (http->error != WSAENETDOWN && http->error != WSAENETUNREACH &&
- http->error != ETIMEDOUT)
+ http->error != WSAETIMEDOUT)
#else
if (http->error != ENETDOWN && http->error != ENETUNREACH &&
http->error != ETIMEDOUT)
while ((state = ippRead(http, response)) != IPP_DATA)
if (state == IPP_ERROR)
- {
- /*
- * Delete the response...
- */
-
- DEBUG_puts("IPP read error!");
- ippDelete(response);
- response = NULL;
+ break;
- _cupsSetError(IPP_SERVICE_UNAVAILABLE, strerror(errno));
+ if (state == IPP_ERROR)
+ {
+ /*
+ * Delete the response...
+ */
- break;
- }
- }
- }
+ DEBUG_puts("IPP read error!");
+ ippDelete(response);
+ response = NULL;
- /*
- * Close the file if needed...
- */
+ _cupsSetError(IPP_SERVICE_UNAVAILABLE, strerror(errno));
- if (filename != NULL)
- fclose(file);
+ break;
+ }
+ else if (outfile >= 0)
+ {
+ /*
+ * Write trailing data to file...
+ */
- /*
- * Flush any remaining data...
- */
+ while ((bytes = (int)httpRead2(http, buffer, sizeof(buffer))) > 0)
+ if (write(outfile, buffer, bytes) < bytes)
+ break;
+ }
+ else
+ {
+ /*
+ * Flush any remaining data...
+ */
- httpFlush(http);
+ httpFlush(http);
+ }
+ }
+ }
/*
* Delete the original request and return the response...
/*
- * End of "$Id: request.c 5555 2006-05-20 13:35:58Z mike $".
+ * End of "$Id: request.c 6506 2007-05-03 18:12:35Z mike $".
*/