2 * "$Id: http.c,v 1.57 1999/12/21 02:26:45 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 * httpCheck() - Check to see if there is a pending response from
30 * httpClose() - Close an HTTP connection...
31 * httpConnect() - Connect to a HTTP server.
32 * httpReconnect() - Reconnect to a HTTP server...
33 * httpSeparate() - Separate a Universal Resource Identifier into its
35 * httpSetField() - Set the value of an HTTP header.
36 * httpDelete() - Send a DELETE request to the server.
37 * httpGet() - Send a GET request to the server.
38 * httpHead() - Send a HEAD request to the server.
39 * httpOptions() - Send an OPTIONS request to the server.
40 * httpPost() - Send a POST request to the server.
41 * httpPut() - Send a PUT request to the server.
42 * httpTrace() - Send an TRACE request to the server.
43 * httpFlush() - Flush data from a HTTP connection.
44 * httpRead() - Read data from a HTTP connection.
45 * httpWrite() - Write data to a HTTP connection.
46 * httpGets() - Get a line of text from a HTTP connection.
47 * httpPrintf() - Print a formatted string to a HTTP connection.
48 * httpStatus() - Return a short string describing a HTTP status code.
49 * httpGetDateString() - Get a formatted date/time string from a time value.
50 * httpGetDateTime() - Get a time value from a formatted date/time string.
51 * httpUpdate() - Update the current HTTP state for incoming data.
52 * httpDecode64() - Base64-decode a string.
53 * httpEncode64() - Base64-encode a string.
54 * httpGetLength() - Get the amount of data remaining from the
55 * content-length or transfer-encoding fields.
56 * http_field() - Return the field index for a field name.
57 * http_send() - Send a request with all fields and the trailing
62 * Include necessary headers...
77 #if !defined(WIN32) && !defined(__EMX__)
79 #endif /* !WIN32 && !__EMX__ */
82 * Some operating systems have done away with the Fxxxx constants for
83 * the fcntl() call; this works around that "feature"...
87 # define FNONBLK O_NONBLOCK
95 static http_field_t
http_field(const char *name
);
96 static int http_send(http_t
*http
, http_state_t request
,
104 static const char *http_fields
[] =
121 "If-Unmodified-since",
134 static const char *days
[7] =
144 static const char *months
[12] =
162 * 'httpInitialize()' - Initialize the HTTP interface library and set the
163 * default HTTP proxy (if any).
169 #if defined(WIN32) || defined(__EMX__)
170 WSADATA winsockdata
; /* WinSock data */
171 static int initialized
= 0;/* Has WinSock been initialized? */
175 WSAStartup(MAKEWORD(1,1), &winsockdata
);
176 #elif defined(HAVE_SIGSET)
177 sigset(SIGPIPE
, SIG_IGN
);
178 #elif defined(HAVE_SIGACTION)
179 struct sigaction action
; /* POSIX sigaction data */
183 * Ignore SIGPIPE signals...
186 memset(&action
, 0, sizeof(action
));
187 action
.sa_handler
= SIG_IGN
;
188 sigaction(SIGPIPE
, &action
, NULL
);
190 signal(SIGPIPE
, SIG_IGN
);
191 #endif /* WIN32 || __EMX__ */
196 * 'httpCheck()' - Check to see if there is a pending response from the server.
199 int /* O - 0 = no data, 1 = data available */
200 httpCheck(http_t
*http
) /* I - HTTP connection */
202 fd_set input
; /* Input set for select() */
203 struct timeval timeout
; /* Timeout */
207 * First see if there is data in the buffer...
217 * Then try doing a select() to poll the socket...
221 FD_SET(http
->fd
, &input
);
226 return (select(http
->fd
+ 1, &input
, NULL
, NULL
, &timeout
) > 0);
231 * 'httpClose()' - Close an HTTP connection...
235 httpClose(http_t
*http
) /* I - Connection to close */
241 closesocket(http
->fd
);
251 * 'httpConnect()' - Connect to a HTTP server.
254 http_t
* /* O - New HTTP connection */
255 httpConnect(const char *host
, /* I - Host to connect to */
256 int port
) /* I - Port number */
258 http_t
*http
; /* New HTTP connection */
259 struct hostent
*hostaddr
; /* Host address data */
268 if ((hostaddr
= gethostbyname(host
)) == NULL
)
272 * Allocate memory for the structure...
275 http
= calloc(sizeof(http_t
), 1);
279 http
->version
= HTTP_1_1
;
281 http
->activity
= time(NULL
);
284 * Copy the hostname and port and then "reconnect"...
287 strncpy(http
->hostname
, host
, sizeof(http
->hostname
) - 1);
288 memcpy((char *)&(http
->hostaddr
.sin_addr
), hostaddr
->h_addr
, hostaddr
->h_length
);
289 http
->hostaddr
.sin_family
= hostaddr
->h_addrtype
;
291 http
->hostaddr
.sin_port
= htons((u_short
)port
);
293 http
->hostaddr
.sin_port
= htons(port
);
295 if (httpReconnect(http
))
306 * 'httpReconnect()' - Reconnect to a HTTP server...
309 int /* O - 0 on success, non-zero on failure */
310 httpReconnect(http_t
*http
) /* I - HTTP data */
312 int val
; /* Socket option value */
316 * Close any previously open socket...
321 closesocket(http
->fd
);
327 * Create the socket and set options to allow reuse.
330 if ((http
->fd
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0)
332 #if defined(WIN32) || defined(__EMX__)
333 http
->error
= WSAGetLastError();
336 #endif /* WIN32 || __EMX__ */
337 http
->status
= HTTP_ERROR
;
342 fcntl(http
->fd
, F_SETFD
, FD_CLOEXEC
); /* Close this socket when starting *
343 * other processes... */
344 #endif /* FD_CLOEXEC */
347 setsockopt(http
->fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
351 setsockopt(http
->fd
, SOL_SOCKET
, SO_REUSEPORT
, &val
, sizeof(val
));
352 #endif /* SO_REUSEPORT */
355 * Connect to the server...
358 if (connect(http
->fd
, (struct sockaddr
*)&(http
->hostaddr
),
359 sizeof(http
->hostaddr
)) < 0)
361 #if defined(WIN32) || defined(__EMX__)
362 http
->error
= WSAGetLastError();
365 #endif /* WIN32 || __EMX__ */
366 http
->status
= HTTP_ERROR
;
369 closesocket(http
->fd
);
378 http
->status
= HTTP_CONTINUE
;
385 * 'httpSeparate()' - Separate a Universal Resource Identifier into its
390 httpSeparate(const char *uri
, /* I - Universal Resource Identifier */
391 char *method
, /* O - Method [32] (http, https, etc.) */
392 char *username
, /* O - Username [32] */
393 char *host
, /* O - Hostname [32] */
394 int *port
, /* O - Port number to use */
395 char *resource
) /* O - Resource/filename [1024] */
397 char *ptr
; /* Pointer into string... */
400 if (uri
== NULL
|| method
== NULL
|| username
== NULL
|| host
== NULL
||
401 port
== NULL
|| resource
== NULL
)
405 * Grab the method portion of the URI...
409 while (*uri
!= ':' && *uri
!= '\0')
417 * If the method contains a period or slash, then it's probably
418 * hostname/filename...
421 if (strchr(host
, '.') != NULL
|| strchr(host
, '/') != NULL
|| *uri
== '\0')
423 if ((ptr
= strchr(host
, '/')) != NULL
)
425 strncpy(resource
, ptr
, HTTP_MAX_URI
- 1);
426 resource
[HTTP_MAX_URI
- 1] = '\0';
435 * OK, we have "hostname:port[/resource]"...
438 *port
= strtol(uri
, (char **)&uri
, 10);
442 strncpy(resource
, uri
, HTTP_MAX_URI
- 1);
443 resource
[HTTP_MAX_URI
- 1] = '\0';
449 strcpy(method
, "http");
455 strncpy(method
, host
, 31);
460 * If the method starts with less than 2 slashes then it is a local resource...
463 if (strncmp(uri
, "//", 2) != 0)
465 strncpy(resource
, uri
, 1023);
466 resource
[1023] = '\0';
475 * Grab the hostname...
482 while (!(*uri
== ':' && isdigit(uri
[1])) && *uri
!= '@' && *uri
!= '/' && *uri
!= '\0')
493 strncpy(username
, host
, 31);
498 while (*uri
!= ':' && *uri
!= '/' && *uri
!= '\0')
509 * Hostname but no port or path...
517 else if (*uri
== ':')
520 * Parse port number...
525 while (isdigit(*uri
))
527 *port
= (*port
* 10) + *uri
- '0';
534 * Figure out the default port number based on the method...
537 if (strcasecmp(method
, "http") == 0)
539 else if (strcasecmp(method
, "https") == 0)
541 else if (strcasecmp(method
, "ipp") == 0) /* Not registered yet... */
543 else if (strcasecmp(method
, "socket") == 0) /* Not registered yet... */
550 * The remaining portion is the resource string...
553 strncpy(resource
, uri
, HTTP_MAX_URI
- 1);
554 resource
[HTTP_MAX_URI
- 1] = '\0';
559 * 'httpSetField()' - Set the value of an HTTP header.
563 httpSetField(http_t
*http
, /* I - HTTP data */
564 http_field_t field
, /* I - Field index */
565 const char *value
) /* I - Value */
567 strncpy(http
->fields
[field
], value
, HTTP_MAX_VALUE
- 1);
568 http
->fields
[field
][HTTP_MAX_VALUE
- 1] = '\0';
573 * 'httpDelete()' - Send a DELETE request to the server.
576 int /* O - Status of call (0 = success) */
577 httpDelete(http_t
*http
, /* I - HTTP data */
578 const char *uri
) /* I - URI to delete */
580 return (http_send(http
, HTTP_DELETE
, uri
));
585 * 'httpGet()' - Send a GET request to the server.
588 int /* O - Status of call (0 = success) */
589 httpGet(http_t
*http
, /* I - HTTP data */
590 const char *uri
) /* I - URI to get */
592 return (http_send(http
, HTTP_GET
, uri
));
597 * 'httpHead()' - Send a HEAD request to the server.
600 int /* O - Status of call (0 = success) */
601 httpHead(http_t
*http
, /* I - HTTP data */
602 const char *uri
) /* I - URI for head */
604 return (http_send(http
, HTTP_HEAD
, uri
));
609 * 'httpOptions()' - Send an OPTIONS request to the server.
612 int /* O - Status of call (0 = success) */
613 httpOptions(http_t
*http
, /* I - HTTP data */
614 const char *uri
) /* I - URI for options */
616 return (http_send(http
, HTTP_OPTIONS
, uri
));
621 * 'httpPost()' - Send a POST request to the server.
624 int /* O - Status of call (0 = success) */
625 httpPost(http_t
*http
, /* I - HTTP data */
626 const char *uri
) /* I - URI for post */
630 return (http_send(http
, HTTP_POST
, uri
));
635 * 'httpPut()' - Send a PUT request to the server.
638 int /* O - Status of call (0 = success) */
639 httpPut(http_t
*http
, /* I - HTTP data */
640 const char *uri
) /* I - URI to put */
644 return (http_send(http
, HTTP_PUT
, uri
));
649 * 'httpTrace()' - Send an TRACE request to the server.
652 int /* O - Status of call (0 = success) */
653 httpTrace(http_t
*http
, /* I - HTTP data */
654 const char *uri
) /* I - URI for trace */
656 return (http_send(http
, HTTP_TRACE
, uri
));
661 * 'httpFlush()' - Flush data from a HTTP connection.
665 httpFlush(http_t
*http
) /* I - HTTP data */
667 char buffer
[8192]; /* Junk buffer */
670 while (httpRead(http
, buffer
, sizeof(buffer
)) > 0);
675 * 'httpRead()' - Read data from a HTTP connection.
678 int /* O - Number of bytes read */
679 httpRead(http_t
*http
, /* I - HTTP data */
680 char *buffer
, /* I - Buffer for data */
681 int length
) /* I - Maximum number of bytes */
683 int bytes
; /* Bytes read */
684 char len
[32]; /* Length string */
687 DEBUG_printf(("httpRead(%08x, %08x, %d)\n", http
, buffer
, length
));
689 if (http
== NULL
|| buffer
== NULL
)
692 http
->activity
= time(NULL
);
697 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
&&
698 http
->data_remaining
<= 0)
700 DEBUG_puts("httpRead: Getting chunk length...");
702 if (httpGets(len
, sizeof(len
), http
) == NULL
)
704 DEBUG_puts("httpRead: Could not get length!");
708 http
->data_remaining
= strtol(len
, NULL
, 16);
711 DEBUG_printf(("httpRead: data_remaining = %d\n", http
->data_remaining
));
713 if (http
->data_remaining
== 0)
716 * A zero-length chunk ends a transfer; unless we are reading POST
720 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
721 httpGets(len
, sizeof(len
), http
);
723 if (http
->state
== HTTP_POST_RECV
)
726 http
->state
= HTTP_WAITING
;
730 else if (length
> http
->data_remaining
)
731 length
= http
->data_remaining
;
735 if (length
> http
->used
)
740 DEBUG_printf(("httpRead: grabbing %d bytes from input buffer...\n", bytes
));
742 memcpy(buffer
, http
->buffer
, length
);
743 http
->used
-= length
;
746 memcpy(http
->buffer
, http
->buffer
+ length
, http
->used
);
750 DEBUG_printf(("httpRead: reading %d bytes from socket...\n", length
));
751 bytes
= recv(http
->fd
, buffer
, length
, 0);
752 DEBUG_printf(("httpRead: read %d bytes from socket...\n", bytes
));
756 http
->data_remaining
-= bytes
;
758 #if defined(WIN32) || defined(__EMX__)
759 http
->error
= WSAGetLastError();
762 #endif /* WIN32 || __EMX__ */
764 if (http
->data_remaining
== 0)
766 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
767 httpGets(len
, sizeof(len
), http
);
769 if (http
->data_encoding
!= HTTP_ENCODE_CHUNKED
)
771 if (http
->state
== HTTP_POST_RECV
)
774 http
->state
= HTTP_WAITING
;
783 * 'httpWrite()' - Write data to a HTTP connection.
786 int /* O - Number of bytes written */
787 httpWrite(http_t
*http
, /* I - HTTP data */
788 const char *buffer
, /* I - Buffer for data */
789 int length
) /* I - Number of bytes to write */
791 int tbytes
, /* Total bytes sent */
792 bytes
; /* Bytes sent */
795 if (http
== NULL
|| buffer
== NULL
)
798 http
->activity
= time(NULL
);
800 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
802 if (httpPrintf(http
, "%x\r\n", length
) < 0)
808 * A zero-length chunk ends a transfer; unless we are sending POST
812 DEBUG_puts("httpWrite: changing states...");
814 if (http
->state
== HTTP_POST_RECV
)
817 http
->state
= HTTP_WAITING
;
819 if (httpPrintf(http
, "\r\n") < 0)
830 bytes
= send(http
->fd
, buffer
, length
, 0);
833 DEBUG_puts("httpWrite: error writing data...\n");
841 if (http
->data_encoding
== HTTP_ENCODE_LENGTH
)
842 http
->data_remaining
-= bytes
;
845 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
846 if (httpPrintf(http
, "\r\n") < 0)
849 if (http
->data_remaining
== 0 && http
->data_encoding
== HTTP_ENCODE_LENGTH
)
852 * Finished with the transfer; unless we are sending POST data, go idle...
855 DEBUG_puts("httpWrite: changing states...");
857 if (http
->state
== HTTP_POST_RECV
)
860 http
->state
= HTTP_WAITING
;
863 DEBUG_printf(("httpWrite: wrote %d bytes...\n", tbytes
));
870 * 'httpGets()' - Get a line of text from a HTTP connection.
873 char * /* O - Line or NULL */
874 httpGets(char *line
, /* I - Line to read into */
875 int length
, /* I - Max length of buffer */
876 http_t
*http
) /* I - HTTP data */
878 char *lineptr
, /* Pointer into line */
879 *bufptr
, /* Pointer into input buffer */
880 *bufend
; /* Pointer to end of buffer */
881 int bytes
; /* Number of bytes read */
884 DEBUG_printf(("httpGets(%08x, %d, %08x)\n", line
, length
, http
));
886 if (http
== NULL
|| line
== NULL
)
890 * Pre-scan the buffer and see if there is a newline in there...
893 #if defined(WIN32) || defined(__EMX__)
897 #endif /* WIN32 || __EMX__ */
901 bufptr
= http
->buffer
;
902 bufend
= http
->buffer
+ http
->used
;
904 while (bufptr
< bufend
)
910 if (bufptr
>= bufend
)
913 * No newline; see if there is more data to be read...
916 if ((bytes
= recv(http
->fd
, bufend
, HTTP_MAX_BUFFER
- http
->used
, 0)) < 0)
919 * Nope, can't get a line this time...
922 #if defined(WIN32) || defined(__EMX__)
923 if (WSAGetLastError() != http
->error
)
925 http
->error
= WSAGetLastError();
929 DEBUG_printf(("httpGets(): recv() error %d!\n", WSAGetLastError()));
931 if (errno
!= http
->error
)
937 DEBUG_printf(("httpGets(): recv() error %d!\n", errno
));
938 #endif /* WIN32 || __EMX__ */
951 * Yup, update the amount used and the end pointer...
958 while (bufptr
>= bufend
);
960 http
->activity
= time(NULL
);
963 * Read a line from the buffer...
967 bufptr
= http
->buffer
;
970 while (bufptr
< bufend
&& bytes
< length
)
981 memcpy(http
->buffer
, bufptr
, http
->used
);
983 DEBUG_printf(("httpGets(): Returning \"%s\"\n", line
));
986 else if (*bufptr
== 0x0d)
989 *lineptr
++ = *bufptr
++;
992 DEBUG_puts("httpGets(): No new line available!");
999 * 'httpPrintf()' - Print a formatted string to a HTTP connection.
1002 int /* O - Number of bytes written */
1003 httpPrintf(http_t
*http
, /* I - HTTP data */
1004 const char *format
, /* I - printf-style format string */
1005 ...) /* I - Additional args as needed */
1007 int bytes
, /* Number of bytes to write */
1008 nbytes
, /* Number of bytes written */
1009 tbytes
; /* Number of bytes all together */
1010 char buf
[HTTP_MAX_BUFFER
], /* Buffer for formatted string */
1011 *bufptr
; /* Pointer into buffer */
1012 va_list ap
; /* Variable argument pointer */
1015 va_start(ap
, format
);
1016 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
1019 DEBUG_printf(("httpPrintf: %s", buf
));
1021 for (tbytes
= 0, bufptr
= buf
; tbytes
< bytes
; tbytes
+= nbytes
, bufptr
+= nbytes
)
1022 if ((nbytes
= send(http
->fd
, bufptr
, bytes
- tbytes
, 0)) < 0)
1030 * 'httpStatus()' - Return a short string describing a HTTP status code.
1033 const char * /* O - String or NULL */
1034 httpStatus(http_status_t status
) /* I - HTTP status code */
1042 case HTTP_ACCEPTED
:
1043 return ("Accepted");
1044 case HTTP_NO_CONTENT
:
1045 return ("No Content");
1046 case HTTP_NOT_MODIFIED
:
1047 return ("Not Modified");
1048 case HTTP_BAD_REQUEST
:
1049 return ("Bad Request");
1050 case HTTP_UNAUTHORIZED
:
1051 return ("Unauthorized");
1052 case HTTP_FORBIDDEN
:
1053 return ("Forbidden");
1054 case HTTP_NOT_FOUND
:
1055 return ("Not Found");
1056 case HTTP_REQUEST_TOO_LARGE
:
1057 return ("Request Entity Too Large");
1058 case HTTP_URI_TOO_LONG
:
1059 return ("URI Too Long");
1060 case HTTP_NOT_IMPLEMENTED
:
1061 return ("Not Implemented");
1062 case HTTP_NOT_SUPPORTED
:
1063 return ("Not Supported");
1071 * 'httpGetDateString()' - Get a formatted date/time string from a time value.
1074 const char * /* O - Date/time string */
1075 httpGetDateString(time_t t
) /* I - UNIX time */
1078 static char datetime
[256];
1082 sprintf(datetime
, "%s, %02d %s %d %02d:%02d:%02d GMT",
1083 days
[tdate
->tm_wday
], tdate
->tm_mday
, months
[tdate
->tm_mon
],
1084 tdate
->tm_year
+ 1900, tdate
->tm_hour
, tdate
->tm_min
, tdate
->tm_sec
);
1091 * 'httpGetDateTime()' - Get a time value from a formatted date/time string.
1094 time_t /* O - UNIX time */
1095 httpGetDateTime(const char *s
) /* I - Date/time string */
1097 int i
; /* Looping var */
1098 struct tm tdate
; /* Time/date structure */
1099 char mon
[16]; /* Abbreviated month name */
1100 int day
, year
; /* Day of month and year */
1101 int hour
, min
, sec
; /* Time */
1104 if (sscanf(s
, "%*s%d%15s%d%d:%d:%d", &day
, mon
, &year
, &hour
, &min
, &sec
) < 6)
1107 for (i
= 0; i
< 12; i
++)
1108 if (strcasecmp(mon
, months
[i
]) == 0)
1115 tdate
.tm_mday
= day
;
1116 tdate
.tm_year
= year
- 1900;
1117 tdate
.tm_hour
= hour
;
1122 return (mktime(&tdate
));
1127 * 'httpUpdate()' - Update the current HTTP state for incoming data.
1130 http_status_t
/* O - HTTP status */
1131 httpUpdate(http_t
*http
) /* I - HTTP data */
1133 char line
[1024], /* Line from connection... */
1134 *value
; /* Pointer to value on line */
1135 http_field_t field
; /* Field index */
1136 int major
, minor
; /* HTTP version numbers */
1137 http_status_t status
; /* Authorization status */
1140 DEBUG_printf(("httpUpdate(%08x)\n", http
));
1143 * If we haven't issued any commands, then there is nothing to "update"...
1146 if (http
->state
== HTTP_WAITING
)
1147 return (HTTP_CONTINUE
);
1150 * Grab all of the lines we can from the connection...
1153 while (httpGets(line
, sizeof(line
), http
) != NULL
)
1157 if (line
[0] == '\0')
1160 * Blank line means the start of the data section (if any). Return
1161 * the result code, too...
1163 * If we get status 100 (HTTP_CONTINUE), then we *don't* change states.
1164 * Instead, we just return HTTP_CONTINUE to the caller and keep on
1168 if (http
->status
== HTTP_CONTINUE
)
1169 return (http
->status
);
1171 httpGetLength(http
);
1173 switch (http
->state
)
1177 case HTTP_POST_RECV
:
1183 http
->state
= HTTP_WAITING
;
1187 return (http
->status
);
1189 else if (strncmp(line
, "HTTP/", 5) == 0)
1192 * Got the beginning of a response...
1195 if (sscanf(line
, "HTTP/%d.%d%d", &major
, &minor
, &status
) != 3)
1196 return (HTTP_ERROR
);
1198 http
->version
= (http_version_t
)(major
* 100 + minor
);
1199 http
->status
= status
;
1201 else if ((value
= strchr(line
, ':')) != NULL
)
1208 while (isspace(*value
))
1212 * Be tolerants of servers that send unknown attribute fields...
1215 if ((field
= http_field(line
)) == HTTP_FIELD_UNKNOWN
)
1217 DEBUG_printf(("httpUpdate: unknown field %s seen!\n", line
));
1221 httpSetField(http
, field
, value
);
1225 http
->status
= HTTP_ERROR
;
1226 return (HTTP_ERROR
);
1231 * See if there was an error...
1236 http
->status
= HTTP_ERROR
;
1237 return (HTTP_ERROR
);
1241 * If we haven't already returned, then there is nothing new...
1244 return (HTTP_CONTINUE
);
1249 * 'httpDecode64()' - Base64-decode a string.
1252 char * /* O - Decoded string */
1253 httpDecode64(char *out
, /* I - String to write to */
1254 const char *in
) /* I - String to read from */
1256 int pos
, /* Bit position */
1257 base64
; /* Value of this character */
1258 char *outptr
; /* Output pointer */
1261 for (outptr
= out
, pos
= 0; *in
!= '\0'; in
++)
1264 * Decode this character into a number from 0 to 63...
1267 if (*in
>= 'A' && *in
<= 'Z')
1269 else if (*in
>= 'a' && *in
<= 'z')
1270 base64
= *in
- 'a' + 26;
1271 else if (*in
>= '0' && *in
<= '9')
1272 base64
= *in
- '0' + 52;
1273 else if (*in
== '+')
1275 else if (*in
== '/')
1277 else if (*in
== '=')
1283 * Store the result in the appropriate chars...
1289 *outptr
= base64
<< 2;
1293 *outptr
++ |= (base64
>> 4) & 3;
1294 *outptr
= (base64
<< 4) & 255;
1298 *outptr
++ |= (base64
>> 2) & 15;
1299 *outptr
= (base64
<< 6) & 255;
1303 *outptr
++ |= base64
;
1312 * Return the decoded string...
1320 * 'httpEncode64()' - Base64-encode a string.
1323 char * /* O - Encoded string */
1324 httpEncode64(char *out
, /* I - String to write to */
1325 const char *in
) /* I - String to read from */
1327 char *outptr
; /* Output pointer */
1328 static char base64
[] = /* Base64 characters... */
1330 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1331 "abcdefghijklmnopqrstuvwxyz"
1337 for (outptr
= out
; *in
!= '\0'; in
++)
1340 * Encode the up to 3 characters as 4 Base64 numbers...
1343 *outptr
++ = base64
[in
[0] >> 2];
1344 *outptr
++ = base64
[((in
[0] << 4) | (in
[1] >> 4)) & 63];
1353 *outptr
++ = base64
[((in
[0] << 2) | (in
[1] >> 6)) & 63];
1359 *outptr
++ = base64
[in
[0] & 63];
1366 * Return the encoded string...
1374 * 'httpGetLength()' - Get the amount of data remaining from the
1375 * content-length or transfer-encoding fields.
1378 int /* O - Content length */
1379 httpGetLength(http_t
*http
) /* I - HTTP data */
1381 DEBUG_printf(("httpGetLength(%08x)\n", http
));
1383 if (strcasecmp(http
->fields
[HTTP_FIELD_TRANSFER_ENCODING
], "chunked") == 0)
1385 DEBUG_puts("httpGetLength: chunked request!");
1387 http
->data_encoding
= HTTP_ENCODE_CHUNKED
;
1388 http
->data_remaining
= 0;
1392 http
->data_encoding
= HTTP_ENCODE_LENGTH
;
1395 * The following is a hack for HTTP servers that don't send a
1396 * content-length or transfer-encoding field...
1398 * If there is no content-length then the connection must close
1399 * after the transfer is complete...
1402 if (http
->fields
[HTTP_FIELD_CONTENT_LENGTH
][0] == '\0')
1403 http
->data_remaining
= 2147483647;
1405 http
->data_remaining
= atoi(http
->fields
[HTTP_FIELD_CONTENT_LENGTH
]);
1407 DEBUG_printf(("httpGetLength: content_length = %d\n", http
->data_remaining
));
1410 return (http
->data_remaining
);
1415 * 'http_field()' - Return the field index for a field name.
1418 static http_field_t
/* O - Field index */
1419 http_field(const char *name
) /* I - String name */
1421 int i
; /* Looping var */
1424 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
1425 if (strcasecmp(name
, http_fields
[i
]) == 0)
1426 return ((http_field_t
)i
);
1428 return (HTTP_FIELD_UNKNOWN
);
1433 * 'http_send()' - Send a request with all fields and the trailing blank line.
1436 static int /* O - 0 on success, non-zero on error */
1437 http_send(http_t
*http
, /* I - HTTP data */
1438 http_state_t request
, /* I - Request code */
1439 const char *uri
) /* I - URI */
1441 int i
; /* Looping var */
1442 char *ptr
, /* Pointer in buffer */
1443 buf
[1024]; /* Encoded URI buffer */
1444 static const char *codes
[] = /* Request code strings */
1460 static const char *hex
= "0123456789ABCDEF";
1464 if (http
== NULL
|| uri
== NULL
)
1468 * Encode the URI as needed...
1471 for (ptr
= buf
; *uri
!= '\0'; uri
++)
1472 if (*uri
<= ' ' || *uri
>= 127)
1475 *ptr
++ = hex
[(*uri
>> 4) & 15];
1476 *ptr
++ = hex
[*uri
& 15];
1484 * See if we had an error the last time around; if so, reconnect...
1487 if (http
->status
== HTTP_ERROR
|| http
->status
>= HTTP_BAD_REQUEST
)
1488 httpReconnect(http
);
1491 * Send the request header...
1494 http
->state
= request
;
1495 if (request
== HTTP_POST
|| request
== HTTP_PUT
)
1498 http
->status
= HTTP_CONTINUE
;
1500 if (httpPrintf(http
, "%s %s HTTP/1.1\r\n", codes
[request
], buf
) < 1)
1502 http
->status
= HTTP_ERROR
;
1506 for (i
= 0; i
< HTTP_FIELD_MAX
; i
++)
1507 if (http
->fields
[i
][0] != '\0')
1509 DEBUG_printf(("%s: %s\n", http_fields
[i
], http
->fields
[i
]));
1511 if (httpPrintf(http
, "%s: %s\r\n", http_fields
[i
], http
->fields
[i
]) < 1)
1513 http
->status
= HTTP_ERROR
;
1518 if (httpPrintf(http
, "\r\n") < 1)
1520 http
->status
= HTTP_ERROR
;
1524 httpClearFields(http
);
1531 * End of "$Id: http.c,v 1.57 1999/12/21 02:26:45 mike Exp $".