1 /*
2  * http-core.h - http core definitions
3  *
4  * Copyright (C) 2011-2013 Thien-Thi Nguyen
5  * Copyright (C) 2000, 2003 Stefan Jahn <stefan@lkcc.org>
6  *
7  * This is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this package.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef __HTTP_CORE_H__
22 #define __HTTP_CORE_H__
23 
24 /* Some definitions.  */
25 #define HTTP_MAJOR_VERSION  1          /* accepted MajorVersion */
26 #define MAJOR_VERSION       0          /* MajorVersion index */
27 #define MINOR_VERSION       1          /* MinorVersion index */
28 #define MAX_HTTP_PROPERTIES 32         /* all http properties */
29 #define HTTP_REQUESTS       8          /* number of known request types */
30 #define HTTP_TIMEOUT        15         /* default timeout value */
31 #define HTTP_MAXKEEPALIVE   10         /* number of requests per connection */
32 #define HTTP_HEADER_SIZE    (1 * 1024) /* maximum header size */
33 
34 #define STANDARD_EOL  "\r\n\r\n"
35 #define EOL1_P(p)     (!memcmp (STANDARD_EOL, (p), 2))
36 #define EOL2_P(p)     (!memcmp (STANDARD_EOL, (p), 4))
37 
38 /* This is like ‘PACKAGE_STRING’, except delim is slash, not space,
39    and the initial "GNU " is omitted, so that the result has the form
40    TOKEN "/" TOKEN, per HTTP 1.0 (RFC 1945) and CGI 1.1 (RFC 3875).
41    We could have used ‘PACKAGE_TARNAME’, but that's not as pretty.  */
42 #define SERVER_STRING   (4 + (PACKAGE_NAME "/" PACKAGE_VERSION))
43 
44 /*
45  * The following structure is meant to hold a http response headers
46  * data.
47  */
48 typedef struct
49 {
50   char *response;               /* text representation of response */
51   int code;                     /* response code */
52   char field[HTTP_HEADER_SIZE]; /* holds header fields */
53 }
54 http_header_t;
55 
56 /*
57  * The content range structure defines a http content range for partial
58  * entity content.
59  */
60 typedef struct
61 {
62   off_t first;  /* first byte in range */
63   off_t last;   /* last byte (inclusive) */
64   off_t length; /* total length of entity (can be zero "*") */
65 }
66 http_range_t;
67 
68 /*
69  * This structure is used to process a http connection.  It will be stored
70  * within the original socket structure (sock->data).
71  */
72 typedef struct http_socket http_socket_t;
73 
74 struct http_socket
75 {
76   http_cache_t *cache;   /* a http file cache structure */
77   char **property;       /* property list of a http request */
78   int contentlength;     /* the content length for the cgi pipe */
79   int filelength;        /* content length for the http file */
80   int keepalive;         /* how many requests left for a connection */
81   off_t fileoffset;      /* file offset used by sendfile */
82   svz_t_handle pid;      /* the pid of the cgi (process handle) */
83   time_t timestamp;      /* connection access time */
84   char *request;         /* the original request */
85   char *host;            /* resolved host name of client */
86   int response;          /* the server's response code */
87   int length;            /* content length sent so far */
88   char *ident;           /* identity information */
89   char *auth;            /* user authentication */
90   http_range_t range;    /* partial content range */
91 };
92 
93 /* the current HTTP protocol version */
94 #define HTTP_VERSION "HTTP/1.0"
95 
96 /* Common Log Format string */
97 #define HTTP_CLF "%h %i %u [%t] \"%R\" %c %l"
98 
99 /* HTTP resonse header definitions */
100 #define HTTP_OK              HTTP_VERSION " 200 OK\r\n"
101 #define HTTP_ACCEPTED        HTTP_VERSION " 202 Accepted\r\n"
102 #define HTTP_PARTIAL         HTTP_VERSION " 206 Partial Content\r\n"
103 #define HTTP_RELOCATE        HTTP_VERSION " 302 Temporary Relocation\r\n"
104 #define HTTP_NOT_MODIFIED    HTTP_VERSION " 304 Not Modified\r\n"
105 #define HTTP_BAD_REQUEST     HTTP_VERSION " 400 Bad Request\r\n"
106 #define HTTP_ACCESS_DENIED   HTTP_VERSION " 403 Forbidden\r\n"
107 #define HTTP_FILE_NOT_FOUND  HTTP_VERSION " 404 Not Found\r\n"
108 #define HTTP_INVALID_RANGE   HTTP_VERSION " 416 Requested Range Not Satisfiable\r\n"
109 #define HTTP_INTERNAL_ERROR  HTTP_VERSION " 500 Internal Server Error\r\n"
110 #define HTTP_NOT_IMPLEMENTED HTTP_VERSION " 501 Not Implemented\r\n"
111 
112 #define HTTP_FLAG_CACHE    0x0001 /* use cache if possible */
113 #define HTTP_FLAG_NOFILE   0x0002 /* do not send content, but header */
114 #define HTTP_FLAG_SIMPLE   0x0004 /* HTTP/0.9 simple GET */
115 #define HTTP_FLAG_DONE     0x0008 /* http request done */
116 #define HTTP_FLAG_POST     0x0010 /* http cgi pipe posting data */
117 #define HTTP_FLAG_CGI      0x0020 /* http cgi pipe getting data */
118 #define HTTP_FLAG_KEEP     0x0040 /* keep alive connection */
119 #define HTTP_FLAG_SENDFILE 0x0080 /* use sendfile for HTTP requests */
120 #define HTTP_FLAG_PARTIAL  0x0100 /* partial content requested */
121 
122 /* all of the additional http flags */
123 #define HTTP_FLAG (HTTP_FLAG_DONE      | \
124                    HTTP_FLAG_POST      | \
125                    HTTP_FLAG_CGI       | \
126                    HTTP_FLAG_CACHE     | \
127                    HTTP_FLAG_KEEP      | \
128                    HTTP_FLAG_SENDFILE  | \
129                    HTTP_FLAG_PARTIAL)
130 
131 /* exported http core functions */
132 int http_keep_alive (svz_socket_t *sock);
133 void http_check_keepalive (svz_socket_t *sock);
134 
135 int http_read_types (http_config_t *cfg);
136 char *http_find_content_type (svz_socket_t *sock, char *file);
137 
138 int http_parse_property (svz_socket_t *sock, char *request, char *end);
139 char *http_find_property (http_socket_t *sock, char *key);
140 
141 int http_check_range (http_range_t *range, off_t filesize);
142 int http_get_range (char *line, http_range_t *range);
143 char *http_userdir (svz_socket_t *sock, char *uri);
144 int http_remotehost (char *host, void *closure);
145 int http_localhost (char *host, void *closure);
146 int http_identification (char *ident, void *closure);
147 void http_process_uri (char *uri);
148 int http_error_response (svz_socket_t *sock, int response);
149 void http_log (svz_socket_t *sock);
150 time_t http_parse_date (char *date);
151 char *http_asc_date (time_t t);
152 char *http_clf_date (time_t t);
153 
154 void http_set_header (char *response);
155 int http_send_header (svz_socket_t *sock);
156 void http_reset_header (void);
157 void http_add_header (const char *fmt, ...);
158 
159 #ifdef __MINGW32__
160 void http_start_netapi (void);
161 void http_stop_netapi (void);
162 #endif
163 
164 #endif /* __HTTP_CORE_H__ */
165