2 * Copyright (c) 2009 Eric Wong (all bugs are Eric's fault)
3 * Copyright (c) 2005 Zed A. Shaw
4 * You can redistribute it and/or modify it under the same terms as Ruby.
10 #include <sys/types.h>
11 #include "common_field_optimization.h"
12 #include "global_variables.h"
27 static void http_field(VALUE req, const char *field, size_t flen, VALUE val);
28 static void header_done(VALUE req, const char *at, size_t length);
30 #define LEN(AT, FPC) (FPC - buffer - hp->AT)
31 #define MARK(M,FPC) (hp->M = (FPC) - buffer)
32 #define PTR_TO(F) (buffer + hp->F)
33 #define STR_NEW(M,FPC) rb_str_new(PTR_TO(M), LEN(M, FPC))
40 action mark {MARK(mark, fpc); }
42 action start_field { MARK(start.field, fpc); }
43 action snake_upcase_field { snake_upcase_char((char *)fpc); }
44 action downcase_char { downcase_char((char *)fpc); }
45 action write_field { hp->field_len = LEN(start.field, fpc); }
46 action start_value { MARK(mark, fpc); }
48 VALIDATE_MAX_LENGTH(LEN(mark, fpc), FIELD_VALUE);
49 http_field(req, PTR_TO(start.field), hp->field_len, STR_NEW(mark, fpc));
51 action request_method {
52 rb_hash_aset(req, g_request_method, STR_NEW(mark, fpc));
55 rb_hash_aset(req, g_rack_url_scheme, STR_NEW(mark, fpc));
58 rb_hash_aset(req, g_http_host, STR_NEW(mark, fpc));
61 size_t len = LEN(mark, fpc);
62 VALIDATE_MAX_LENGTH(len, REQUEST_URI);
63 rb_hash_aset(req, g_request_uri, STR_NEW(mark, fpc));
65 * "OPTIONS * HTTP/1.1\r\n" is a valid request, but we can't have '*'
66 * in REQUEST_PATH or PATH_INFO or else Rack::Lint will complain
68 if (len == 1 && *PTR_TO(mark) == '*') {
69 VALUE val = rb_str_new(NULL, 0);
70 rb_hash_aset(req, g_request_path, val);
71 rb_hash_aset(req, g_path_info, val);
75 VALIDATE_MAX_LENGTH(LEN(mark, fpc), FRAGMENT);
76 rb_hash_aset(req, g_fragment, STR_NEW(mark, fpc));
78 action start_query {MARK(start.query, fpc); }
80 VALIDATE_MAX_LENGTH(LEN(start.query, fpc), QUERY_STRING);
81 rb_hash_aset(req, g_query_string, STR_NEW(start.query, fpc));
84 rb_hash_aset(req, g_http_version, STR_NEW(mark, fpc));
88 size_t len = LEN(mark, fpc);
90 VALIDATE_MAX_LENGTH(len, REQUEST_PATH);
91 val = STR_NEW(mark, fpc);
93 rb_hash_aset(req, g_request_path, val);
94 /* rack says PATH_INFO must start with "/" or be empty */
95 if (!(len == 1 && *PTR_TO(mark) == '*'))
96 rb_hash_aset(req, g_path_info, val);
99 hp->start.body = fpc - buffer + 1;
100 header_done(req, fpc + 1, pe - fpc - 1);
104 include unicorn_http_common "unicorn_http_common.rl";
110 static void http_parser_init(struct http_parser *hp)
113 memset(hp, 0, sizeof(struct http_parser));
119 static void http_parser_execute(struct http_parser *hp,
120 VALUE req, const char *buffer, size_t len)
124 size_t off = hp->start.offset;
126 assert(off <= len && "offset past end of buffer");
131 assert(pe - p == len - off && "pointers aren't same distance");
135 if (hp->cs != http_parser_error)
137 hp->start.offset = p - buffer;
139 assert(p <= pe && "buffer overflow after parsing execute");
140 assert(hp->start.offset <= len && "start.offset longer than length");
141 assert(hp->mark < len && "mark is after buffer end");
142 assert(hp->field_len <= len && "field has length longer than whole buffer");
145 static struct http_parser *data_get(VALUE self)
147 struct http_parser *hp;
149 Data_Get_Struct(self, struct http_parser, hp);
154 static void http_field(VALUE req, const char *field, size_t flen, VALUE val)
156 VALUE f = find_common_field(field, flen);
159 VALIDATE_MAX_LENGTH(flen, FIELD_NAME);
160 f = uncommon_field(field, flen);
161 } else if (f == g_http_host && rb_hash_aref(req, f) != Qnil) {
165 rb_hash_aset(req, f, val);
168 static int is_https(VALUE str)
170 return RSTRING_LEN(str) == 5 && !memcmp("https", RSTRING_PTR(str), 5);
173 static void set_server_params(VALUE req)
175 VALUE temp = rb_hash_aref(req, g_rack_url_scheme);
176 VALUE server_name = g_localhost;
177 VALUE server_port = g_port_80;
179 /* set rack.url_scheme to "https" or "http", no others are allowed by Rack */
181 temp = rb_hash_aref(req, g_http_x_forwarded_proto);
182 if (temp != Qnil && is_https(temp))
183 server_port = g_port_443;
186 rb_hash_aset(req, g_rack_url_scheme, temp);
187 } else if (is_https(temp)) {
188 server_port = g_port_443;
191 /* parse and set the SERVER_NAME and SERVER_PORT variables */
192 temp = rb_hash_aref(req, g_http_host);
194 char *colon = memchr(RSTRING_PTR(temp), ':', RSTRING_LEN(temp));
196 long port_start = colon - RSTRING_PTR(temp) + 1;
198 server_name = rb_str_substr(temp, 0, colon - RSTRING_PTR(temp));
199 if ((RSTRING_LEN(temp) - port_start) > 0)
200 server_port = rb_str_substr(temp, port_start, RSTRING_LEN(temp));
205 rb_hash_aset(req, g_server_name, server_name);
206 rb_hash_aset(req, g_server_port, server_port);
209 /** Finalizes the request header to have a bunch of stuff that's needed. */
210 static void header_done(VALUE req, const char *at, size_t length)
214 /* rack requires QUERY_STRING */
215 if (rb_hash_aref(req, g_query_string) == Qnil)
216 rb_hash_aset(req, g_query_string, rb_str_new(NULL, 0));
218 set_server_params(req);
219 rb_hash_aset(req, g_server_protocol, g_server_protocol_value);
221 /* grab the initial body and stuff it into the hash */
222 temp = rb_hash_aref(req, g_request_method);
224 long len = RSTRING_LEN(temp);
225 char *ptr = RSTRING_PTR(temp);
227 if (memcmp(ptr, "HEAD", len) && memcmp(ptr, "GET", len))
228 rb_hash_aset(req, sym_http_body, rb_str_new(at, length));
232 static VALUE HttpParser_alloc(VALUE klass)
234 struct http_parser *hp;
235 return Data_Make_Struct(klass, struct http_parser, NULL, NULL, hp);
241 * parser.new -> parser
243 * Creates a new parser.
245 static VALUE HttpParser_init(VALUE self)
247 http_parser_init(data_get(self));
255 * parser.reset -> nil
257 * Resets the parser to it's initial state so that you can reuse it
258 * rather than making new ones.
260 static VALUE HttpParser_reset(VALUE self)
262 http_parser_init(data_get(self));
270 * parser.execute(req, data) -> true/false
272 * Takes a Hash and a String of data, parses the String of data filling
273 * in the Hash returning a boolean to indicate whether or not parsing
276 * This function now throws an exception when there is a parsing error.
277 * This makes the logic for working with the parser much easier. You
278 * will need to wrap the parser with an exception handling block.
281 static VALUE HttpParser_execute(VALUE self, VALUE req, VALUE data)
283 struct http_parser *hp = data_get(self);
284 char *dptr = RSTRING_PTR(data);
285 long dlen = RSTRING_LEN(data);
287 if (hp->start.offset < dlen) {
288 http_parser_execute(hp, req, dptr, dlen);
290 VALIDATE_MAX_LENGTH(hp->start.offset, HEADER);
292 if (hp->cs != http_parser_error)
293 return hp->cs == http_parser_first_final ? Qtrue : Qfalse;
295 rb_raise(eHttpParserError, "Invalid HTTP format, parsing fails.");
297 rb_raise(eHttpParserError, "Requested start is after data buffer end.");
300 #define SET_GLOBAL(var,str) do { \
301 var = find_common_field(str, sizeof(str) - 1); \
302 assert(var != Qnil); \
305 void Init_unicorn_http(void)
308 rb_define_alloc_func(cHttpParser, HttpParser_alloc);
309 rb_define_method(cHttpParser, "initialize", HttpParser_init,0);
310 rb_define_method(cHttpParser, "reset", HttpParser_reset,0);
311 rb_define_method(cHttpParser, "execute", HttpParser_execute,2);
312 init_common_fields();
313 SET_GLOBAL(g_http_host, "HOST");