2 * "$Id: http.c,v 1.37 1999/06/18 18:36:08 mike Exp $"
4 * HTTP routines for the Common UNIX Printing System (CUPS) scheduler.
6 * Copyright 1997-1999 by Easy Software Products, all rights reserved.
8 * These statusd 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
26 * httpInitialize() - Initialize the HTTP interface library and set the
27 * default HTTP proxy (if any).
28 * httpClose() - Close an HTTP connection...
29 * httpConnect() - Connect to a HTTP server.
30 * httpReconnect() - Reconnect to a HTTP server...
31 * httpSeparate() - Separate a Universal Resource Identifier into its
33 * httpSetField() - Set the value of an HTTP header.
34 * httpDelete() - Send a DELETE request to the server.
35 * httpGet() - Send a GET request to the server.
36 * httpHead() - Send a HEAD request to the server.
37 * httpOptions() - Send an OPTIONS request to the server.
38 * httpPost() - Send a POST request to the server.
39 * httpPut() - Send a PUT request to the server.
40 * httpTrace() - Send an TRACE request to the server.
41 * httpFlush() - Flush data from a HTTP connection.
42 * httpRead() - Read data from a HTTP connection.
43 * httpWrite() - Write data to a HTTP connection.
44 * httpGets() - Get a line of text from a HTTP connection.
45 * httpPrintf() - Print a formatted string to a HTTP connection.
46 * httpStatus() - Return a short string describing a HTTP status code.
47 * httpGetDateString() - Get a formatted date/time string from a time value.
48 * httpGetDateTime() - Get a time value from a formatted date/time string.
49 * httpUpdate() - Update the current HTTP state for incoming data.
50 * httpDecode64() - Base64-decode a string.
51 * httpEncode64() - Base64-encode a string.
52 * httpGetLength() - Get the amount of data remaining from the
53 * content-length or transfer-encoding fields.
54 * http_field() - Return the field index for a field name.
55 * http_send() - Send a request with all fields and the trailing
60 * Include necessary headers...
74 #if !defined(WIN32) && !defined(__EMX__)
76 #endif /* !WIN32 && !__EMX__ */
79 * Some operating systems have done away with the Fxxxx constants for
80 * the fcntl() call; this works around that "feature"...
84 # define FNONBLK O_NONBLOCK
92 static http_field_t
http_field(char *name
);
93 static int http_send(http_t
*http
, http_state_t request
, char *uri
);
100 static char *http_fields
[] =
132 "If-Unmodified-since",
141 "Proxy-Authenticate",
142 "Proxy-Authorization",
157 static char *days
[7] =
167 static char *months
[12] =
185 * 'httpInitialize()' - Initialize the HTTP interface library and set the
186 * default HTTP proxy (if any).
192 #if defined(WIN32) || defined(__EMX__)
193 WSADATA winsockdata
; /* WinSock data */
194 static int initialized
= 0;/* Has WinSock been initialized? */
198 WSAStartup(MAKEWORD(1,1), &winsockdata
);
199 #elif defined(HAVE_SIGSET)
200 sigset(SIGPIPE
, SIG_IGN
);
201 #elif defined(HAVE_SIGACTION)
202 struct sigaction action
; /* POSIX sigaction data */
206 * Ignore SIGPIPE signals...
209 memset(&action
, 0, sizeof(action
));
210 action
.sa_handler
= SIG_IGN
;
211 sigaction(SIGPIPE
, &action
, NULL
);
213 signal(SIGPIPE
, SIG_IGN
);
214 #endif /* WIN32 || __EMX__ */
219 * 'httpClose()' - Close an HTTP connection...
223 httpClose(http_t
*http
) /* I - Connection to close */
229 closesocket(http
->fd
);
239 * 'httpConnect()' - Connect to a HTTP server.
242 http_t
* /* O - New HTTP connection */
243 httpConnect(char *host
, /* I - Host to connect to */
244 int port
) /* I - Port number */
246 http_t
*http
; /* New HTTP connection */
247 struct hostent
*hostaddr
; /* Host address data */
256 if ((hostaddr
= gethostbyname(host
)) == NULL
)
260 * Allocate memory for the structure...
263 http
= calloc(sizeof(http_t
), 1);
267 http
->version
= HTTP_1_1
;
269 http
->activity
= time(NULL
);
272 * Copy the hostname and port and then "reconnect"...
275 strcpy(http
->hostname
, host
);
276 memset((char *)&(http
->hostaddr
), 0, sizeof(http
->hostaddr
));
277 memcpy((char *)&(http
->hostaddr
.sin_addr
), hostaddr
->h_addr
, hostaddr
->h_length
);
278 http
->hostaddr
.sin_family
= hostaddr
->h_addrtype
;
280 http
->hostaddr
.sin_port
= htons((u_short
)port
);
282 http
->hostaddr
.sin_port
= htons(port
);
284 if (httpReconnect(http
))
295 * 'httpReconnect()' - Reconnect to a HTTP server...
298 int /* O - 0 on success, non-zero on failure */
299 httpReconnect(http_t
*http
) /* I - HTTP data */
301 int val
; /* Socket option value */
305 * Close any previously open socket...
310 closesocket(http
->fd
);
316 * Create the socket and set options to allow reuse.
319 if ((http
->fd
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0)
323 fcntl(http
->fd
, F_SETFD
, FD_CLOEXEC
); /* Close this socket when starting *
324 * other processes... */
325 #endif /* FD_CLOEXEC */
328 setsockopt(http
->fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
332 setsockopt(http
->fd
, SOL_SOCKET
, SO_REUSEPORT
, &val
, sizeof(val
));
333 #endif /* SO_REUSEPORT */
336 * Connect to the server...
339 if (connect(http
->fd
, (struct sockaddr
*)&(http
->hostaddr
),
340 sizeof(http
->hostaddr
)) < 0)
343 closesocket(http
->fd
);
356 * 'httpSeparate()' - Separate a Universal Resource Identifier into its
361 httpSeparate(char *uri
, /* I - Universal Resource Identifier */
362 char *method
, /* O - Method (http, https, etc.) */
363 char *username
, /* O - Username */
364 char *host
, /* O - Hostname */
365 int *port
, /* O - Port number to use */
366 char *resource
) /* O - Resource/filename */
368 char *ptr
; /* Pointer into string... */
371 if (uri
== NULL
|| method
== NULL
|| username
== NULL
|| host
== NULL
||
372 port
== NULL
|| resource
== NULL
)
376 * Grab the method portion of the URI...
380 while (*uri
!= ':' && *uri
!= '\0')
388 * If the method contains a period or slash, then it's probably
389 * hostname/filename...
392 if (strchr(host
, '.') != NULL
|| strchr(host
, '/') != NULL
|| *uri
== '\0')
394 if ((ptr
= strchr(host
, '/')) != NULL
)
396 strcpy(resource
, ptr
);
405 * OK, we have "hostname:port[/resource]"...
408 *port
= strtol(uri
, &uri
, 10);
411 strcpy(resource
, uri
);
416 strcpy(method
, "http");
421 strcpy(method
, host
);
424 * If the method starts with less than 2 slashes then it is a local resource...
427 if (strncmp(uri
, "//", 2) != 0)
429 strcpy(resource
, uri
);
437 * Grab the hostname...
444 while (*uri
!= ':' && *uri
!= '@' && *uri
!= '/' && *uri
!= '\0')
455 strcpy(username
, host
);
458 while (*uri
!= ':' && *uri
!= '/' && *uri
!= '\0')
469 * Hostname but no port or path...
477 else if (*uri
== ':')
480 * Parse port number...
485 while (isdigit(*uri
))
487 *port
= (*port
* 10) + *uri
- '0';
494 * Figure out the default port number based on the method...
497 if (strcasecmp(method
, "http") == 0)
499 else if (strcasecmp(method
, "https") == 0)
501 else if (strcasecmp(method
, "ipp") == 0) /* Not registered yet... */
508 * The remaining portion is the resource string...
511 strcpy(resource
, uri
);
516 * 'httpSetField()' - Set the value of an HTTP header.
520 httpSetField(http_t
*http
, /* I - HTTP data */
521 http_field_t field
, /* I - Field index */
522 char *value
) /* I - Value */
524 strncpy(http
->fields
[field
], value
, HTTP_MAX_VALUE
- 1);
525 http
->fields
[field
][HTTP_MAX_VALUE
- 1] = '\0';
530 * 'httpDelete()' - Send a DELETE request to the server.
533 int /* O - Status of call (0 = success) */
534 httpDelete(http_t
*http
, /* I - HTTP data */
535 char *uri
) /* I - URI to delete */
537 return (http_send(http
, HTTP_DELETE
, uri
));
542 * 'httpGet()' - Send a GET request to the server.
545 int /* O - Status of call (0 = success) */
546 httpGet(http_t
*http
, /* I - HTTP data */
547 char *uri
) /* I - URI to get */
549 return (http_send(http
, HTTP_GET
, uri
));
554 * 'httpHead()' - Send a HEAD request to the server.
557 int /* O - Status of call (0 = success) */
558 httpHead(http_t
*http
, /* I - HTTP data */
559 char *uri
) /* I - URI for head */
561 return (http_send(http
, HTTP_HEAD
, uri
));
566 * 'httpOptions()' - Send an OPTIONS request to the server.
569 int /* O - Status of call (0 = success) */
570 httpOptions(http_t
*http
, /* I - HTTP data */
571 char *uri
) /* I - URI for options */
573 return (http_send(http
, HTTP_OPTIONS
, uri
));
578 * 'httpPost()' - Send a POST request to the server.
581 int /* O - Status of call (0 = success) */
582 httpPost(http_t
*http
, /* I - HTTP data */
583 char *uri
) /* I - URI for post */
587 return (http_send(http
, HTTP_POST
, uri
));
592 * 'httpPut()' - Send a PUT request to the server.
595 int /* O - Status of call (0 = success) */
596 httpPut(http_t
*http
, /* I - HTTP data */
597 char *uri
) /* I - URI to put */
601 return (http_send(http
, HTTP_PUT
, uri
));
606 * 'httpTrace()' - Send an TRACE request to the server.
609 int /* O - Status of call (0 = success) */
610 httpTrace(http_t
*http
, /* I - HTTP data */
611 char *uri
) /* I - URI for trace */
613 return (http_send(http
, HTTP_TRACE
, uri
));
618 * 'httpFlush()' - Flush data from a HTTP connection.
622 httpFlush(http_t
*http
) /* I - HTTP data */
624 char buffer
[8192]; /* Junk buffer */
627 while (httpRead(http
, buffer
, sizeof(buffer
)) > 0);
632 * 'httpRead()' - Read data from a HTTP connection.
635 int /* O - Number of bytes read */
636 httpRead(http_t
*http
, /* I - HTTP data */
637 char *buffer
, /* I - Buffer for data */
638 int length
) /* I - Maximum number of bytes */
640 int bytes
; /* Bytes read */
641 char len
[32]; /* Length string */
644 DEBUG_printf(("httpRead(%08x, %08x, %d)\n", http
, buffer
, length
));
646 if (http
== NULL
|| buffer
== NULL
)
649 http
->activity
= time(NULL
);
654 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
&&
655 http
->data_remaining
<= 0 &&
656 (http
->state
== HTTP_GET_SEND
|| http
->state
== HTTP_POST_RECV
||
657 http
->state
== HTTP_POST_SEND
|| http
->state
== HTTP_PUT_RECV
))
659 if (httpGets(len
, sizeof(len
), http
) == NULL
)
662 http
->data_remaining
= strtol(len
, NULL
, 16);
665 DEBUG_printf(("httpRead: data_remaining = %d\n", http
->data_remaining
));
667 if (http
->data_remaining
== 0)
670 * A zero-length chunk ends a transfer; unless we are reading POST
674 if (http
->state
== HTTP_POST_RECV
)
677 http
->state
= HTTP_WAITING
;
681 else if (length
> http
->data_remaining
)
682 length
= http
->data_remaining
;
686 if (length
> http
->used
)
691 DEBUG_printf(("httpRead: grabbing %d bytes from input buffer...\n", bytes
));
693 memcpy(buffer
, http
->buffer
, length
);
694 http
->used
-= length
;
697 memcpy(http
->buffer
, http
->buffer
+ length
, http
->used
);
701 DEBUG_printf(("httpRead: reading %d bytes from socket...\n", length
));
702 bytes
= recv(http
->fd
, buffer
, length
, 0);
703 DEBUG_printf(("httpRead: read %d bytes from socket...\n", bytes
));
707 http
->data_remaining
-= bytes
;
709 if (http
->data_remaining
== 0 && http
->data_encoding
!= HTTP_ENCODE_CHUNKED
)
711 if (http
->state
== HTTP_POST_RECV
)
714 http
->state
= HTTP_WAITING
;
722 * 'httpWrite()' - Write data to a HTTP connection.
725 int /* O - Number of bytes written */
726 httpWrite(http_t
*http
, /* I - HTTP data */
727 char *buffer
, /* I - Buffer for data */
728 int length
) /* I - Number of bytes to write */
730 int tbytes
, /* Total bytes sent */
731 bytes
; /* Bytes sent */
732 char len
[32]; /* Length string */
735 if (http
== NULL
|| buffer
== NULL
)
738 http
->activity
= time(NULL
);
740 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
&&
741 (http
->state
== HTTP_GET_SEND
|| http
->state
== HTTP_POST_RECV
||
742 http
->state
== HTTP_POST_SEND
|| http
->state
== HTTP_PUT_RECV
))
744 sprintf(len
, "%x\r\n", length
);
745 if (send(http
->fd
, len
, strlen(len
), 0) < 3)
752 * A zero-length chunk ends a transfer; unless we are sending POST
756 if (http
->state
== HTTP_POST_RECV
)
759 http
->state
= HTTP_WAITING
;
768 bytes
= send(http
->fd
, buffer
, length
, 0);
771 DEBUG_puts("httpWrite: error writing data...\n");
778 if (http
->data_encoding
== HTTP_ENCODE_LENGTH
)
779 http
->data_remaining
-= bytes
;
782 if (http
->data_remaining
== 0 && http
->data_encoding
== HTTP_ENCODE_LENGTH
)
785 * Finished with the transfer; unless we are sending POST data, go idle...
788 if (http
->state
== HTTP_POST_RECV
)
791 http
->state
= HTTP_WAITING
;
794 DEBUG_printf(("httpWrite: wrote %d bytes...\n", tbytes
));
801 * 'httpGets()' - Get a line of text from a HTTP connection.
804 char * /* O - Line or NULL */
805 httpGets(char *line
, /* I - Line to read into */
806 int length
, /* I - Max length of buffer */
807 http_t
*http
) /* I - HTTP data */
809 char *lineptr
, /* Pointer into line */
810 *bufptr
, /* Pointer into input buffer */
811 *bufend
; /* Pointer to end of buffer */
812 int bytes
; /* Number of bytes read */
815 DEBUG_printf(("httpGets(%08x, %d, %08x)\n", line
, length
, http
));
817 if (http
== NULL
|| line
== NULL
)
821 * Pre-scan the buffer and see if there is a newline in there...
826 bufptr
= http
->buffer
;
827 bufend
= http
->buffer
+ http
->used
;
829 while (bufptr
< bufend
)
835 if (bufptr
>= bufend
)
838 * No newline; see if there is more data to be read...
841 if ((bytes
= recv(http
->fd
, bufend
, HTTP_MAX_BUFFER
- http
->used
, 0)) < 1)
844 * Nope, can't get a line this time...
852 * Yup, update the amount used and the end pointer...
860 while (bufptr
>= bufend
);
862 http
->activity
= time(NULL
);
865 * Read a line from the buffer...
869 bufptr
= http
->buffer
;
872 while (bufptr
< bufend
&& bytes
< length
)
883 memcpy(http
->buffer
, bufptr
, http
->used
);
887 else if (*bufptr
== 0x0d)
890 *lineptr
++ = *bufptr
++;
898 * 'httpPrintf()' - Print a formatted string to a HTTP connection.
901 int /* O - Number of bytes written */
902 httpPrintf(http_t
*http
, /* I - HTTP data */
903 const char *format
, /* I - printf-style format string */
904 ...) /* I - Additional args as needed */
906 int bytes
; /* Number of bytes to write */
907 char buf
[HTTP_MAX_BUFFER
]; /* Buffer for formatted string */
908 va_list ap
; /* Variable argument pointer */
911 va_start(ap
, format
);
912 bytes
= vsprintf(buf
, format
, ap
);
915 DEBUG_printf(("httpPrintf: %s", buf
));
917 return (send(http
->fd
, buf
, bytes
, 0));
922 * 'httpStatus()' - Return a short string describing a HTTP status code.
925 char * /* O - String or NULL */
926 httpStatus(http_status_t status
) /* I - HTTP status code */
936 case HTTP_NO_CONTENT
:
937 return ("No Content");
938 case HTTP_NOT_MODIFIED
:
939 return ("Not Modified");
940 case HTTP_BAD_REQUEST
:
941 return ("Bad Request");
942 case HTTP_UNAUTHORIZED
:
943 return ("Unauthorized");
944 case HTTP_FORBIDDEN
:
945 return ("Forbidden");
946 case HTTP_NOT_FOUND
:
947 return ("Not Found");
948 case HTTP_REQUEST_TOO_LARGE
:
949 return ("Request Entity Too Large");
950 case HTTP_URI_TOO_LONG
:
951 return ("URI Too Long");
952 case HTTP_NOT_IMPLEMENTED
:
953 return ("Not Implemented");
954 case HTTP_NOT_SUPPORTED
:
955 return ("Not Supported");
963 * 'httpGetDateString()' - Get a formatted date/time string from a time value.
966 char * /* O - Date/time string */
967 httpGetDateString(time_t t
) /* I - UNIX time */
970 static char datetime
[256];
974 sprintf(datetime
, "%s, %02d %s %d %02d:%02d:%02d GMT",
975 days
[tdate
->tm_wday
], tdate
->tm_mday
, months
[tdate
->tm_mon
],
976 tdate
->tm_year
+ 1900, tdate
->tm_hour
, tdate
->tm_min
, tdate
->tm_sec
);
983 * 'httpGetDateTime()' - Get a time value from a formatted date/time string.
986 time_t /* O - UNIX time */
987 httpGetDateTime(char *s
) /* I - Date/time string */
989 int i
; /* Looping var */
990 struct tm tdate
; /* Time/date structure */
991 char mon
[16]; /* Abbreviated month name */
992 int day
, year
; /* Day of month and year */
993 int hour
, min
, sec
; /* Time */
996 if (sscanf(s
, "%*s%d%s%d%d:%d:%d", &day
, mon
, &year
, &hour
, &min
, &sec
) < 6)
999 for (i
= 0; i
< 12; i
++)
1000 if (strcasecmp(mon
, months
[i
]) == 0)
1007 tdate
.tm_mday
= day
;
1008 tdate
.tm_year
= year
- 1900;
1009 tdate
.tm_hour
= hour
;
1014 return (mktime(&tdate
));
1019 * 'httpUpdate()' - Update the current HTTP state for incoming data.
1022 http_status_t
/* O - HTTP status */
1023 httpUpdate(http_t
*http
) /* I - HTTP data */
1025 char line
[1024], /* Line from connection... */
1026 *value
; /* Pointer to value on line */
1027 http_field_t field
; /* Field index */
1028 int major
, minor
; /* HTTP version numbers */
1029 http_status_t status
; /* Authorization status */
1032 DEBUG_printf(("httpUpdate(%08x)\n", http
));
1035 * If we haven't issued any commands, then there is nothing to "update"...
1038 if (http
->state
== HTTP_WAITING
)
1039 return (HTTP_CONTINUE
);
1042 * Grab all of the lines we can from the connection...
1045 while (httpGets(line
, sizeof(line
), http
) != NULL
)
1049 if (line
[0] == '\0')
1052 * Blank line means the start of the data section (if any). Return
1053 * the result code, too...
1055 * If we get status 100 (HTTP_CONTINUE), then we *don't* change states.
1056 * Instead, we just return HTTP_CONTINUE to the caller and keep on
1060 if (http
->status
== HTTP_CONTINUE
)
1061 return (http
->status
);
1063 httpGetLength(http
);
1065 switch (http
->state
)
1069 case HTTP_POST_RECV
:
1075 http
->state
= HTTP_WAITING
;
1079 return (http
->status
);
1081 else if (strncmp(line
, "HTTP/", 5) == 0)
1084 * Got the beginning of a response...
1087 if (sscanf(line
, "HTTP/%d.%d%d", &major
, &minor
, &status
) != 3)
1088 return (HTTP_ERROR
);
1090 http
->version
= (http_version_t
)(major
* 100 + minor
);
1091 http
->status
= status
;
1093 else if ((value
= strchr(line
, ':')) != NULL
)
1100 while (isspace(*value
))
1104 * Be tolerants of servers that send unknown attribute fields...
1107 if ((field
= http_field(line
)) == HTTP_FIELD_UNKNOWN
)
1109 DEBUG_printf(("httpUpdate: unknown field %s seen!\n", line
));
1113 httpSetField(http
, field
, value
);
1116 return (HTTP_ERROR
);
1120 * If we haven't already returned, then there is nothing new...
1123 return (HTTP_CONTINUE
);
1128 * 'httpDecode64()' - Base64-decode a string.
1131 char * /* O - Decoded string */
1132 httpDecode64(char *out
, /* I - String to write to */
1133 char *in
) /* I - String to read from */
1135 int pos
, /* Bit position */
1136 base64
; /* Value of this character */
1137 char *outptr
; /* Output pointer */
1140 for (outptr
= out
, pos
= 0; *in
!= '\0'; in
++)
1143 * Decode this character into a number from 0 to 63...
1146 if (*in
>= 'A' && *in
<= 'Z')
1148 else if (*in
>= 'a' && *in
<= 'z')
1149 base64
= *in
- 'a' + 26;
1150 else if (*in
>= '0' && *in
<= '9')
1151 base64
= *in
- '0' + 52;
1152 else if (*in
== '+')
1154 else if (*in
== '/')
1156 else if (*in
== '=')
1162 * Store the result in the appropriate chars...
1168 *outptr
= base64
<< 2;
1172 *outptr
++ |= (base64
>> 4) & 3;
1173 *outptr
= (base64
<< 4) & 255;
1177 *outptr
++ |= (base64
>> 2) & 15;
1178 *outptr
= (base64
<< 6) & 255;
1182 *outptr
++ |= base64
;
1191 * Return the decoded string...
1199 * 'httpEncode64()' - Base64-encode a string.
1202 char * /* O - Encoded string */
1203 httpEncode64(char *out
, /* I - String to write to */
1204 char *in
) /* I - String to read from */
1206 char *outptr
; /* Output pointer */
1207 static char base64
[] = /* Base64 characters... */
1209 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1210 "abcdefghijklmnopqrstuvwxyz"
1216 for (outptr
= out
; *in
!= '\0'; in
++)
1219 * Encode the up to 3 characters as 4 Base64 numbers...
1222 *outptr
++ = base64
[in
[0] >> 2];
1223 *outptr
++ = base64
[((in
[0] << 4) | (in
[1] >> 4)) & 63];
1229 *outptr
++ = base64
[((in
[0] << 2) | (in
[1] >> 6)) & 63];
1235 *outptr
++ = base64
[in
[0] & 63];
1242 * Return the encoded string...
1250 * 'httpGetLength()' - Get the amount of data remaining from the
1251 * content-length or transfer-encoding fields.
1255 httpGetLength(http_t
*http
) /* I - HTTP data */
1257 if (strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked") == 0)
1259 http
->data_encoding
= HTTP_ENCODE_CHUNKED
;
1260 http
->data_remaining
= 0;
1264 http
->data_encoding
= HTTP_ENCODE_LENGTH
;
1267 * The following is a hack for HTTP servers that don't send a
1268 * content-length or transfer-encoding field...
1270 * If there is no content-length then the connection must close
1271 * after the transfer is complete...
1274 if (http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0] == '\0')
1275 http
->data_remaining
= 2147483647;
1277 http
->data_remaining
= atoi(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
]);
1280 return (http
->data_remaining
);
1285 * 'http_field()' - Return the field index for a field name.
1288 static http_field_t
/* O - Field index */
1289 http_field(char *name
) /* I - String name */
1291 int i
; /* Looping var */
1294 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
1295 if (strcasecmp(name
, http_fields
[i
]) == 0)
1296 return ((http_field_t
)i
);
1298 return (HTTP_FIELD_UNKNOWN
);
1303 * 'http_send()' - Send a request with all fields and the trailing blank line.
1306 static int /* O - 0 on success, non-zero on error */
1307 http_send(http_t
*http
, /* I - HTTP data */
1308 http_state_t request
, /* I - Request code */
1309 char *uri
) /* I - URI */
1311 int i
; /* Looping var */
1312 char *ptr
, /* Pointer in buffer */
1313 buf
[1024]; /* Encoded URI buffer */
1314 static char *codes
[] = /* Request code strings */
1330 static char *hex
= "0123456789ABCDEF";
1334 if (http
== NULL
|| uri
== NULL
)
1338 * Encode the URI as needed...
1341 for (ptr
= buf
; *uri
!= '\0'; uri
++)
1342 if (*uri
<= ' ' || *uri
>= 127)
1345 *ptr
++ = hex
[(*uri
>> 4) & 15];
1346 *ptr
++ = hex
[*uri
& 15];
1354 * Send the request header...
1357 http
->state
= request
;
1358 if (request
== HTTP_POST
|| request
== HTTP_PUT
)
1361 if (httpPrintf(http
, "%s %s HTTP/1.1\n", codes
[request
], buf
) < 1)
1364 * Might have lost connection; try to reconnect...
1367 if (httpReconnect(http
))
1371 * OK, we've reconnected, send the request again...
1374 if (httpPrintf(http
, "%s %s HTTP/%d.%d\n", codes
[request
], buf
,
1375 http
->version
/ 100, http
->version
% 100) < 1)
1379 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
1380 if (http
->fields
[i
][0] != '\0')
1382 DEBUG_printf(("%s: %s\n", http_fields
[i
], http
->fields
[i
]));
1384 if (httpPrintf(http
, "%s: %s\n", http_fields
[i
], http
->fields
[i
]) < 1)
1388 if (httpPrintf(http
, "\n") < 1)
1391 httpClearFields(http
);
1398 * End of "$Id: http.c,v 1.37 1999/06/18 18:36:08 mike Exp $".