1 #ifndef HTTP_HEADER_H
  2 #define HTTP_HEADER_H
  3 
  4 #include "http.h"
  5 #include "uri.h"
  6 #include "queue.h"
  7 #include "rh_chunk.h"
  8 #include "rh_gmtime.h"
  9 
 10 typedef struct http_header              http_header_t;
 11 typedef struct http_header_key          http_header_key_t;
 12 typedef struct http_header_entry        http_header_entry_t;
 13 
 14 /*
 15  *
 16  * DONT change the numbers for this enums unless you change the
 17  * positon in http_header.c - http_header_keys[HTTP_HEADER_KEY_MAX]
 18  * 
 19  *
 20  */
 21 enum http_header_key_type {
 22         HTTP_HEADER_KEY_RESPONSE        = 0,
 23         HTTP_HEADER_KEY_REQUEST         = 0,
 24 
 25         HTTP_HEADER_KEY_HOST,
 26         HTTP_HEADER_KEY_ETAG,
 27         HTTP_HEADER_KEY_DATE,
 28         HTTP_HEADER_KEY_FROM,
 29         HTTP_HEADER_KEY_VARY,
 30         
 31         HTTP_HEADER_KEY_TE,
 32         HTTP_HEADER_KEY_VIA,
 33         HTTP_HEADER_KEY_AGE,
 34         
 35         HTTP_HEADER_KEY_TITLE,
 36         HTTP_HEADER_KEY_RANGE,
 37         HTTP_HEADER_KEY_X_PAD,
 38         HTTP_HEADER_KEY_ALLOW,
 39         HTTP_HEADER_KEY_UA_OS,
 40         
 41         HTTP_HEADER_KEY_SET_COOKIE,
 42         HTTP_HEADER_KEY_CONNECTION,
 43         HTTP_HEADER_KEY_USER_AGENT,
 44         HTTP_HEADER_KEY_KEEP_ALIVE,
 45         
 46         HTTP_HEADER_KEY_ACCEPT,
 47         HTTP_HEADER_KEY_COOKIE,
 48         HTTP_HEADER_KEY_UA_CPU,
 49         HTTP_HEADER_KEY_EXPECT,
 50         HTTP_HEADER_KEY_PRAGMA,
 51         HTTP_HEADER_KEY_PUBLIC,
 52         HTTP_HEADER_KEY_SERVER,
 53 
 54         HTTP_HEADER_KEY_UA_DISP,
 55         HTTP_HEADER_KEY_X_CACHE,
 56         HTTP_HEADER_KEY_TRAILER,
 57         HTTP_HEADER_KEY_UPGRADE,
 58         HTTP_HEADER_KEY_COOKIE2,
 59         HTTP_HEADER_KEY_EXPIRES,
 60         HTTP_HEADER_KEY_REFERER,
 61         HTTP_HEADER_KEY_WARNING,
 62 
 63         HTTP_HEADER_KEY_X_FORWARDED_FOR,
 64         HTTP_HEADER_KEY_X_SERIAL_NUMBER,
 65         HTTP_HEADER_KEY_ACCEPT_ENCODING,
 66         HTTP_HEADER_KEY_ACCEPT_LANGUAGE,
 67 
 68         HTTP_HEADER_KEY_UA_COLOR,
 69         HTTP_HEADER_KEY_IF_MATCH,
 70         HTTP_HEADER_KEY_IF_RANGE,
 71         HTTP_HEADER_KEY_LOCATION,
 72 
 73         HTTP_HEADER_KEY_UA_PIXELS,
 74         HTTP_HEADER_KEY_CLIENT_IP,
 75 
 76         HTTP_HEADER_KEY_CONTENT_TYPE,
 77         HTTP_HEADER_KEY_CONTENT_BASE,
 78         HTTP_HEADER_KEY_MAX_FORWARDS,
 79         HTTP_HEADER_KEY_MIME_VERSION,
 80 
 81         HTTP_HEADER_KEY_CONTENT_LENGTH,
 82         HTTP_HEADER_KEY_ACCEPT_CHARSET,
 83 
 84         HTTP_HEADER_KEY_LAST_MODIFIED,
 85         HTTP_HEADER_KEY_CONTENT_RANGE,
 86         HTTP_HEADER_KEY_AUTHORIZATION,
 87         HTTP_HEADER_KEY_ACCEPT_RANGES,
 88         HTTP_HEADER_KEY_CACHE_CONTROL,
 89         HTTP_HEADER_KEY_IF_NONE_MATCH,
 90 
 91         HTTP_HEADER_KEY_TRANSFER_ENCODING,
 92         HTTP_HEADER_KEY_IF_MODIFIED_SINCE,
 93 
 94         HTTP_HEADER_KEY_RETRY_AFTER,
 95         HTTP_HEADER_KEY_SET_COOKIE2,
 96         HTTP_HEADER_KEY_CONTENT_MD5,
 97 
 98         HTTP_HEADER_KEY_WWW_AUTHENTICATE,
 99         HTTP_HEADER_KEY_PROXY_CONNECTION,
100         HTTP_HEADER_KEY_CONTENT_ENCODING,
101         HTTP_HEADER_KEY_CONTENT_LANGUAGE,
102         HTTP_HEADER_KEY_CONTENT_LOCATION,
103 
104         HTTP_HEADER_KEY_PROXY_AUTHENTICATE,
105 
106         HTTP_HEADER_KEY_IF_UNMODIFIED_SINCE,
107         HTTP_HEADER_KEY_PROXY_AUTHORIZATION,
108         
109         HTTP_HEADER_KEY_STATUS,
110 
111 
112         HTTP_HEADER_KEY_MAX
113 };
114 
115 enum {
116         HTTP_HEADER_KEY_FLAG_PARSED     = 1,
117         HTTP_HEADER_KEY_FLAG_UNKNOWN    = 2
118 };
119 
120 struct http_header_key {
121         char            *key;
122         size_t          nkey;
123 
124         unsigned char   merge_char;
125 
126         unsigned short  type;
127 
128         http_status_t   (*parse_first)  (http_header_entry_t *);
129         http_status_t   (*parse_full )  (http_header_entry_t *);
130         
131         http_status_t   (*destroy)      (http_header_entry_t *);
132 };
133 
134 TAILQ_HEAD(http_header_entry_list, http_header_entry);
135         
136 struct http_header_entry {
137         http_header_t                   *header;
138         
139         /* the key, eg: "Connection" */
140         const struct http_header_key    *key;
141         
142         /* the value, eg: "Keep-Alive" */
143         rh_buffer_t                     value;
144         
145         size_t                          found;
146         
147         unsigned int                    flags;
148         
149         void                            *data;
150         
151         TAILQ_ENTRY(http_header_entry)  entries;
152         
153         TAILQ_ENTRY(http_header_entry)  unknown;
154 };
155 
156 struct http_header {
157         int                             type;
158 
159         http_method_t                   method;
160         http_version_t                  version;
161         http_status_t                   status;
162         
163         unsigned int                    flags;
164         int                             keepalive;
165         /* true if "Connection: Keep-Alive" was found */
166         int                             keepalive_header;
167 
168         uri_t                           uri;
169 
170         const char                      *buffer;
171         size_t                          nbuffer;
172 
173         FIXED_BUFFER(,32)               content_length;
174         FIXED_BUFFER(,128)              etag;
175 
176         struct http_header_entry_list   entries_raw;
177         struct http_header_entry_list   entries_parsed;
178 
179         struct http_header_entry        entries[HTTP_HEADER_KEY_MAX];
180         struct http_header_entry_list   entries_unknown;
181 };
182 
183 
184 /* GET / .. */
185 #define HTTP_HEADER_SERVER      1
186 
187 /* HTTP/1.1 200 OK .. */
188 #define HTTP_HEADER_CLIENT      2
189 
190 /* Status: ... */
191 #define HTTP_HEADER_CGI         3
192 
193 
194 void http_header_init (http_header_t *header, int fullclear);
195 void http_header_destroy (http_header_t *);
196 void http_header_reset   (http_header_t *);
197 
198 void http_header_dump (http_header_t *header);
199 
200 http_status_t http_header_set_errno_status (http_header_t *header, int error);
201 
202 int http_header_parse (http_header_t *hdr, int type,
203                 const char *buffer, size_t nbuffer);
204 
205 const rh_buffer_t * http_header_get_response (http_header_t *header);
206 
207 int http_header_into_chunk (http_header_t *header, rh_chunk_t *chunk, int term);
208 
209 /*
210  *
211  * Key: interface
212  *
213  */
214 
215 int http_header_if_match (http_header_t *header_in, http_header_t *header_out);
216 int http_header_if_none_match (http_header_t *header_in, http_header_t *header_out);
217 int http_header_have_range (http_header_t *header);
218 
219 int http_header_is_modified (http_header_t *header, rh_gmtime_t *gmtime);
220 
221 /* remove all keys */
222 int http_header_clear (http_header_t *header);
223 
224 int http_header_key_default (http_header_t *header,
225                 enum http_header_key_type key);
226 
227 #define HTTP_HEADER_KEY_DEFAULT(_header,_key)   \
228         http_header_key_default((_header), HTTP_HEADER_KEY_ ## _key)
229 
230 int http_header_key_clear (http_header_t *header,
231                 enum http_header_key_type key);
232 
233 #define HTTP_HEADER_KEY_CLEAR(_header,_key)     \
234         http_header_key_clear((_header), HTTP_HEADER_KEY_ ## _key)
235 
236 int http_header_key_append (http_header_t *hdr,
237                 enum http_header_key_type key,
238                 const char *src, size_t nsrc);
239 
240 #define HTTP_HEADER_KEY_APPEND(_header,_key,_src,_nsrc) \
241         http_header_key_append((_header),HTTP_HEADER_KEY_ ## _key,(_src),(_nsrc))
242 
243 #define HTTP_HEADER_KEY_APPEND_CONST(_header,_key,_src) \
244         http_header_key_append((_header),HTTP_HEADER_KEY_ ## _key,CONST_STR_LEN(_src))
245 
246 int http_header_key_set_const (http_header_t *header,
247                 enum http_header_key_type key,
248                 const char *src, size_t nsrc);
249 
250 #define HTTP_HEADER_KEY_SET_CONST(_header,_key,_src,_nsrc)      \
251         http_header_key_set_const((_header),HTTP_HEADER_KEY_ ## _key,(_src),(_nsrc))
252 
253 #define HTTP_HEADER_KEY_SET_CONST_STR(_header,_key,_str)                    \
254         http_header_key_set_const(      (_header),HTTP_HEADER_KEY_ ## _key, \
255                                         (_str),CONST_LEN(_str) )
256 
257 #define HTTP_HEADER_KEY_SET_CONST_BUFFER(_header,_key,_buffer)  \
258         HTTP_HEADER_KEY_SET_CONST(_header,_key,(_buffer)->data,(_buffer)->used)
259                 
260 int http_header_key_set_content_length (http_header_t *header, size_t length);
261 int http_header_key_set_keepalive (http_header_t *header, int keepalive);
262 
263 http_header_entry_t * http_header_key_get (http_header_t *header,
264                 enum http_header_key_type key );
265 
266 #define HTTP_HEADER_KEY_GET(_header,_key)                       \
267         http_header_key_get((_header), HTTP_HEADER_KEY_ ## _key)
268 
269 http_header_entry_t * http_header_key_use (http_header_t *header,
270                 enum http_header_key_type key);
271 
272 #define HTTP_HEADER_KEY_USE(_header,_key)                       \
273         http_header_key_use((_header), HTTP_HEADER_KEY_ ## _key)
274 
275 #define HTTP_HEADER_KEY_GET_DATA(_header,_key)                  \
276         http_header_key_get((_header), HTTP_HEADER_KEY_ ## _key)->data
277 
278 #define HTTP_HEADER_KEY_ISSET(_header,_key)                     \
279         ( 0 != (_header)->entries[HTTP_HEADER_KEY_ ## _key].found )
280 
281 #endif /* HTTP_HEADER_H */


syntax highlighted by Code2HTML, v. 0.9.1