From ca3fd8acb4bc3f5125a9f0f281951fb8f03778e5 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 2 Aug 2009 13:39:21 -0700 Subject: Refactoring unicorn_http C/Ragel code More tightly integrate the C/Ruby portions with C/Ragel to avoid the confusing the flow. Split out some files into hopefully logical areas so it's easier to focus on more interesting/volatile code. --- ext/unicorn_http/c_util.h | 28 ++ ext/unicorn_http/common_field_optimization.h | 96 +++++++ ext/unicorn_http/global_variables.h | 57 ++++ ext/unicorn_http/unicorn_http.c | 410 --------------------------- ext/unicorn_http/unicorn_http.rl | 328 ++++++++++++++++++--- 5 files changed, 466 insertions(+), 453 deletions(-) create mode 100644 ext/unicorn_http/c_util.h create mode 100644 ext/unicorn_http/common_field_optimization.h create mode 100644 ext/unicorn_http/global_variables.h delete mode 100644 ext/unicorn_http/unicorn_http.c (limited to 'ext') diff --git a/ext/unicorn_http/c_util.h b/ext/unicorn_http/c_util.h new file mode 100644 index 0000000..30e7ae2 --- /dev/null +++ b/ext/unicorn_http/c_util.h @@ -0,0 +1,28 @@ +/* + * Generic C functions and macros go here, there are no dependencies + * on Unicorn internal structures or the Ruby C API in here. + */ + +#ifndef UH_util_h +#define UH_util_h + +/* + * capitalizes all lower-case ASCII characters and converts dashes + * to underscores for HTTP headers. Locale-agnostic. + */ +static void snake_upcase_char(char *c) +{ + if (*c >= 'a' && *c <= 'z') + *c &= ~0x20; + else if (*c == '-') + *c = '_'; +} + +/* Downcases a single ASCII character. Locale-agnostic. */ +static void downcase_char(char *c) +{ + if (*c >= 'A' && *c <= 'Z') + *c |= 0x20; +} + +#endif /* UH_util_h */ diff --git a/ext/unicorn_http/common_field_optimization.h b/ext/unicorn_http/common_field_optimization.h new file mode 100644 index 0000000..978f0d7 --- /dev/null +++ b/ext/unicorn_http/common_field_optimization.h @@ -0,0 +1,96 @@ +#ifndef common_field_optimization +#define common_field_optimization +#include "ruby.h" + +struct common_field { + const signed long len; + const char *name; + VALUE value; +}; + +/* + * A list of common HTTP headers we expect to receive. + * This allows us to avoid repeatedly creating identical string + * objects to be used with rb_hash_aset(). + */ +static struct common_field common_http_fields[] = { +# define f(N) { (sizeof(N) - 1), N, Qnil } + f("ACCEPT"), + f("ACCEPT_CHARSET"), + f("ACCEPT_ENCODING"), + f("ACCEPT_LANGUAGE"), + f("ALLOW"), + f("AUTHORIZATION"), + f("CACHE_CONTROL"), + f("CONNECTION"), + f("CONTENT_ENCODING"), + f("CONTENT_LENGTH"), + f("CONTENT_TYPE"), + f("COOKIE"), + f("DATE"), + f("EXPECT"), + f("FROM"), + f("HOST"), + f("IF_MATCH"), + f("IF_MODIFIED_SINCE"), + f("IF_NONE_MATCH"), + f("IF_RANGE"), + f("IF_UNMODIFIED_SINCE"), + f("KEEP_ALIVE"), /* Firefox sends this */ + f("MAX_FORWARDS"), + f("PRAGMA"), + f("PROXY_AUTHORIZATION"), + f("RANGE"), + f("REFERER"), + f("TE"), + f("TRAILER"), + f("TRANSFER_ENCODING"), + f("UPGRADE"), + f("USER_AGENT"), + f("VIA"), + f("X_FORWARDED_FOR"), /* common for proxies */ + f("X_FORWARDED_PROTO"), /* common for proxies */ + f("X_REAL_IP"), /* common for proxies */ + f("WARNING") +# undef f +}; + +#define HTTP_PREFIX "HTTP_" +#define HTTP_PREFIX_LEN (sizeof(HTTP_PREFIX) - 1) + +/* this function is not performance-critical, called only at load time */ +static void init_common_fields(void) +{ + int i; + struct common_field *cf = common_http_fields; + char tmp[64]; + memcpy(tmp, HTTP_PREFIX, HTTP_PREFIX_LEN); + + for(i = 0; i < ARRAY_SIZE(common_http_fields); cf++, i++) { + /* Rack doesn't like certain headers prefixed with "HTTP_" */ + if (!strcmp("CONTENT_LENGTH", cf->name) || + !strcmp("CONTENT_TYPE", cf->name)) { + cf->value = rb_str_new(cf->name, cf->len); + } else { + memcpy(tmp + HTTP_PREFIX_LEN, cf->name, cf->len + 1); + cf->value = rb_str_new(tmp, HTTP_PREFIX_LEN + cf->len); + } + cf->value = rb_obj_freeze(cf->value); + rb_global_variable(&cf->value); + } +} + +/* this function is called for every header set */ +static VALUE find_common_field_value(const char *field, size_t flen) +{ + int i; + struct common_field *cf = common_http_fields; + + for(i = 0; i < ARRAY_SIZE(common_http_fields); i++, cf++) { + if (cf->len == flen && !memcmp(cf->name, field, flen)) + return cf->value; + } + return Qnil; +} + +#endif /* common_field_optimization_h */ diff --git a/ext/unicorn_http/global_variables.h b/ext/unicorn_http/global_variables.h new file mode 100644 index 0000000..1091909 --- /dev/null +++ b/ext/unicorn_http/global_variables.h @@ -0,0 +1,57 @@ +#ifndef global_variables_h +#define global_variables_h +static VALUE mUnicorn; +static VALUE cHttpParser; +static VALUE eHttpParserError; +static VALUE sym_http_body; + +static VALUE g_rack_url_scheme; +static VALUE g_request_method; +static VALUE g_request_uri; +static VALUE g_fragment; +static VALUE g_query_string; +static VALUE g_http_version; +static VALUE g_request_path; +static VALUE g_path_info; +static VALUE g_server_name; +static VALUE g_server_port; +static VALUE g_server_protocol; +static VALUE g_server_protocol_value; +static VALUE g_http_host; +static VALUE g_http_x_forwarded_proto; +static VALUE g_port_80; +static VALUE g_port_443; +static VALUE g_localhost; +static VALUE g_http; + +/** Defines common length and error messages for input length validation. */ +#define DEF_MAX_LENGTH(N, length) \ + static const size_t MAX_##N##_LENGTH = length; \ + static const char * const MAX_##N##_LENGTH_ERR = \ + "HTTP element " # N " is longer than the " # length " allowed length." + +/** + * Validates the max length of given input and throws an HttpParserError + * exception if over. + */ +#define VALIDATE_MAX_LENGTH(len, N) do { \ + if (len > MAX_##N##_LENGTH) \ + rb_raise(eHttpParserError, MAX_##N##_LENGTH_ERR); \ +} while (0) + +/** Defines global strings in the init method. */ +#define DEF_GLOBAL(N, val) do { \ + g_##N = rb_obj_freeze(rb_str_new(val, sizeof(val) - 1)); \ + rb_global_variable(&g_##N); \ +} while (0) + +/* Defines the maximum allowed lengths for various input elements.*/ +DEF_MAX_LENGTH(FIELD_NAME, 256); +DEF_MAX_LENGTH(FIELD_VALUE, 80 * 1024); +DEF_MAX_LENGTH(REQUEST_URI, 1024 * 12); +DEF_MAX_LENGTH(FRAGMENT, 1024); /* Don't know if this length is specified somewhere or not */ +DEF_MAX_LENGTH(REQUEST_PATH, 1024); +DEF_MAX_LENGTH(QUERY_STRING, (1024 * 10)); +DEF_MAX_LENGTH(HEADER, (1024 * (80 + 32))); + +#endif /* global_variables_h */ diff --git a/ext/unicorn_http/unicorn_http.c b/ext/unicorn_http/unicorn_http.c deleted file mode 100644 index 3348d73..0000000 --- a/ext/unicorn_http/unicorn_http.c +++ /dev/null @@ -1,410 +0,0 @@ -/** - * Copyright (c) 2009 Eric Wong (all bugs are Eric's fault) - * Copyright (c) 2005 Zed A. Shaw - * You can redistribute it and/or modify it under the same terms as Ruby. - */ -#include -#include -#include "unicorn_http.h" - -static struct http_parser *data_get(VALUE self) -{ - struct http_parser *http; - - Data_Get_Struct(self, struct http_parser, http); - assert(http); - return http; -} - -static VALUE mUnicorn; -static VALUE cHttpParser; -static VALUE eHttpParserError; -static VALUE sym_http_body; - -#define HTTP_PREFIX "HTTP_" -#define HTTP_PREFIX_LEN (sizeof(HTTP_PREFIX) - 1) - -static VALUE g_rack_url_scheme; -static VALUE g_request_method; -static VALUE g_request_uri; -static VALUE g_fragment; -static VALUE g_query_string; -static VALUE g_http_version; -static VALUE g_request_path; -static VALUE g_path_info; -static VALUE g_server_name; -static VALUE g_server_port; -static VALUE g_server_protocol; -static VALUE g_server_protocol_value; -static VALUE g_http_host; -static VALUE g_http_x_forwarded_proto; -static VALUE g_port_80; -static VALUE g_port_443; -static VALUE g_localhost; -static VALUE g_http; - -/** Defines common length and error messages for input length validation. */ -#define DEF_MAX_LENGTH(N, length) \ - static const size_t MAX_##N##_LENGTH = length; \ - static const char * const MAX_##N##_LENGTH_ERR = \ - "HTTP element " # N " is longer than the " # length " allowed length." - -/** - * Validates the max length of given input and throws an HttpParserError - * exception if over. - */ -#define VALIDATE_MAX_LENGTH(len, N) do { \ - if (len > MAX_##N##_LENGTH) \ - rb_raise(eHttpParserError, MAX_##N##_LENGTH_ERR); \ -} while (0) - -/** Defines global strings in the init method. */ -#define DEF_GLOBAL(N, val) do { \ - g_##N = rb_obj_freeze(rb_str_new(val, sizeof(val) - 1)); \ - rb_global_variable(&g_##N); \ -} while (0) - -/* Defines the maximum allowed lengths for various input elements.*/ -DEF_MAX_LENGTH(FIELD_NAME, 256); -DEF_MAX_LENGTH(FIELD_VALUE, 80 * 1024); -DEF_MAX_LENGTH(REQUEST_URI, 1024 * 12); -DEF_MAX_LENGTH(FRAGMENT, 1024); /* Don't know if this length is specified somewhere or not */ -DEF_MAX_LENGTH(REQUEST_PATH, 1024); -DEF_MAX_LENGTH(QUERY_STRING, (1024 * 10)); -DEF_MAX_LENGTH(HEADER, (1024 * (80 + 32))); - -struct common_field { - const signed long len; - const char *name; - VALUE value; -}; - -/* - * A list of common HTTP headers we expect to receive. - * This allows us to avoid repeatedly creating identical string - * objects to be used with rb_hash_aset(). - */ -static struct common_field common_http_fields[] = { -# define f(N) { (sizeof(N) - 1), N, Qnil } - f("ACCEPT"), - f("ACCEPT_CHARSET"), - f("ACCEPT_ENCODING"), - f("ACCEPT_LANGUAGE"), - f("ALLOW"), - f("AUTHORIZATION"), - f("CACHE_CONTROL"), - f("CONNECTION"), - f("CONTENT_ENCODING"), - f("CONTENT_LENGTH"), - f("CONTENT_TYPE"), - f("COOKIE"), - f("DATE"), - f("EXPECT"), - f("FROM"), - f("HOST"), - f("IF_MATCH"), - f("IF_MODIFIED_SINCE"), - f("IF_NONE_MATCH"), - f("IF_RANGE"), - f("IF_UNMODIFIED_SINCE"), - f("KEEP_ALIVE"), /* Firefox sends this */ - f("MAX_FORWARDS"), - f("PRAGMA"), - f("PROXY_AUTHORIZATION"), - f("RANGE"), - f("REFERER"), - f("TE"), - f("TRAILER"), - f("TRANSFER_ENCODING"), - f("UPGRADE"), - f("USER_AGENT"), - f("VIA"), - f("X_FORWARDED_FOR"), /* common for proxies */ - f("X_FORWARDED_PROTO"), /* common for proxies */ - f("X_REAL_IP"), /* common for proxies */ - f("WARNING") -# undef f -}; - -/* this function is not performance-critical */ -static void init_common_fields(void) -{ - int i; - struct common_field *cf = common_http_fields; - char tmp[256]; /* MAX_FIELD_NAME_LENGTH */ - memcpy(tmp, HTTP_PREFIX, HTTP_PREFIX_LEN); - - for(i = 0; i < ARRAY_SIZE(common_http_fields); cf++, i++) { - /* Rack doesn't like certain headers prefixed with "HTTP_" */ - if (!strcmp("CONTENT_LENGTH", cf->name) || - !strcmp("CONTENT_TYPE", cf->name)) { - cf->value = rb_str_new(cf->name, cf->len); - } else { - memcpy(tmp + HTTP_PREFIX_LEN, cf->name, cf->len + 1); - cf->value = rb_str_new(tmp, HTTP_PREFIX_LEN + cf->len); - } - cf->value = rb_obj_freeze(cf->value); - rb_global_variable(&cf->value); - } -} - -static VALUE find_common_field_value(const char *field, size_t flen) -{ - int i; - struct common_field *cf = common_http_fields; - for(i = 0; i < ARRAY_SIZE(common_http_fields); i++, cf++) { - if (cf->len == flen && !memcmp(cf->name, field, flen)) - return cf->value; - } - return Qnil; -} - -static void http_field(VALUE req, const char *field, - size_t flen, const char *value, size_t vlen) -{ - VALUE f = Qnil; - - VALIDATE_MAX_LENGTH(flen, FIELD_NAME); - VALIDATE_MAX_LENGTH(vlen, FIELD_VALUE); - - f = find_common_field_value(field, flen); - - if (f == Qnil) { - /* - * We got a strange header that we don't have a memoized value for. - * Fallback to creating a new string to use as a hash key. - * - * using rb_str_new(NULL, len) here is faster than rb_str_buf_new(len) - * in my testing, because: there's no minimum allocation length (and - * no check for it, either), RSTRING_LEN(f) does not need to be - * written twice, and and RSTRING_PTR(f) will already be - * null-terminated for us. - */ - f = rb_str_new(NULL, HTTP_PREFIX_LEN + flen); - memcpy(RSTRING_PTR(f), HTTP_PREFIX, HTTP_PREFIX_LEN); - memcpy(RSTRING_PTR(f) + HTTP_PREFIX_LEN, field, flen); - assert(*(RSTRING_PTR(f) + RSTRING_LEN(f)) == '\0'); /* paranoia */ - /* fprintf(stderr, "UNKNOWN HEADER <%s>\n", RSTRING_PTR(f)); */ - } else if (f == g_http_host && rb_hash_aref(req, f) != Qnil) { - return; - } - - rb_hash_aset(req, f, rb_str_new(value, vlen)); -} - -static void request_method(VALUE req, const char *at, size_t length) -{ - rb_hash_aset(req, g_request_method, rb_str_new(at, length)); -} - -static void scheme(VALUE req, const char *at, size_t length) -{ - rb_hash_aset(req, g_rack_url_scheme, rb_str_new(at, length)); -} - -static void host(VALUE req, const char *at, size_t length) -{ - rb_hash_aset(req, g_http_host, rb_str_new(at, length)); -} - -static void request_uri(VALUE req, const char *at, size_t length) -{ - VALIDATE_MAX_LENGTH(length, REQUEST_URI); - - rb_hash_aset(req, g_request_uri, rb_str_new(at, length)); - - /* "OPTIONS * HTTP/1.1\r\n" is a valid request */ - if (length == 1 && *at == '*') { - VALUE val = rb_str_new(NULL, 0); - rb_hash_aset(req, g_request_path, val); - rb_hash_aset(req, g_path_info, val); - } -} - -static void fragment(VALUE req, const char *at, size_t length) -{ - VALIDATE_MAX_LENGTH(length, FRAGMENT); - - rb_hash_aset(req, g_fragment, rb_str_new(at, length)); -} - -static void request_path(VALUE req, const char *at, size_t length) -{ - VALUE val = Qnil; - - VALIDATE_MAX_LENGTH(length, REQUEST_PATH); - - val = rb_str_new(at, length); - rb_hash_aset(req, g_request_path, val); - - /* rack says PATH_INFO must start with "/" or be empty */ - if (!(length == 1 && *at == '*')) - rb_hash_aset(req, g_path_info, val); -} - -static void query_string(VALUE req, const char *at, size_t length) -{ - VALIDATE_MAX_LENGTH(length, QUERY_STRING); - - rb_hash_aset(req, g_query_string, rb_str_new(at, length)); -} - -static void http_version(VALUE req, const char *at, size_t length) -{ - rb_hash_aset(req, g_http_version, rb_str_new(at, length)); -} - -/** Finalizes the request header to have a bunch of stuff that's needed. */ -static void header_done(VALUE req, const char *at, size_t length) -{ - VALUE server_name = g_localhost; - VALUE server_port = g_port_80; - VALUE temp; - - /* rack requires QUERY_STRING */ - if (rb_hash_aref(req, g_query_string) == Qnil) - rb_hash_aset(req, g_query_string, rb_str_new(NULL, 0)); - - /* set rack.url_scheme to "https" or "http", no others are allowed by Rack */ - if ((temp = rb_hash_aref(req, g_rack_url_scheme)) == Qnil) { - if ((temp = rb_hash_aref(req, g_http_x_forwarded_proto)) != Qnil && - RSTRING_LEN(temp) == 5 && - !memcmp("https", RSTRING_PTR(temp), 5)) - server_port = g_port_443; - else - temp = g_http; - rb_hash_aset(req, g_rack_url_scheme, temp); - } else if (RSTRING_LEN(temp) == 5 && !memcmp("https", RSTRING_PTR(temp), 5)) { - server_port = g_port_443; - } - - /* parse and set the SERVER_NAME and SERVER_PORT variables */ - if ((temp = rb_hash_aref(req, g_http_host)) != Qnil) { - char *colon = memchr(RSTRING_PTR(temp), ':', RSTRING_LEN(temp)); - if (colon) { - long port_start = colon - RSTRING_PTR(temp) + 1; - - server_name = rb_str_substr(temp, 0, colon - RSTRING_PTR(temp)); - if ((RSTRING_LEN(temp) - port_start) > 0) - server_port = rb_str_substr(temp, port_start, RSTRING_LEN(temp)); - } else { - server_name = temp; - } - } - rb_hash_aset(req, g_server_name, server_name); - rb_hash_aset(req, g_server_port, server_port); - rb_hash_aset(req, g_server_protocol, g_server_protocol_value); - - /* grab the initial body and stuff it into the hash */ - temp = rb_hash_aref(req, g_request_method); - if (temp != Qnil) { - long len = RSTRING_LEN(temp); - char *ptr = RSTRING_PTR(temp); - - if (memcmp(ptr, "HEAD", len) && memcmp(ptr, "GET", len)) - rb_hash_aset(req, sym_http_body, rb_str_new(at, length)); - } -} - -static VALUE HttpParser_alloc(VALUE klass) -{ - struct http_parser *http; - return Data_Make_Struct(klass, struct http_parser, NULL, NULL, http); -} - - -/** - * call-seq: - * parser.new -> parser - * - * Creates a new parser. - */ -static VALUE HttpParser_init(VALUE self) -{ - http_parser_init(data_get(self)); - - return self; -} - - -/** - * call-seq: - * parser.reset -> nil - * - * Resets the parser to it's initial state so that you can reuse it - * rather than making new ones. - */ -static VALUE HttpParser_reset(VALUE self) -{ - http_parser_init(data_get(self)); - - return Qnil; -} - - -/** - * call-seq: - * parser.execute(req, data) -> true/false - * - * Takes a Hash and a String of data, parses the String of data filling - * in the Hash returning a boolean to indicate whether or not parsing - * is finished. - * - * This function now throws an exception when there is a parsing error. - * This makes the logic for working with the parser much easier. You - * will need to wrap the parser with an exception handling block. - */ - -static VALUE HttpParser_execute(VALUE self, VALUE req, VALUE data) -{ - struct http_parser *http = data_get(self); - char *dptr = RSTRING_PTR(data); - long dlen = RSTRING_LEN(data); - - if (http->start.offset < dlen) { - http_parser_execute(http, req, dptr, dlen); - - VALIDATE_MAX_LENGTH(http->start.offset, HEADER); - - if (!http_parser_has_error(http)) - return http_parser_is_finished(http) ? Qtrue : Qfalse; - - rb_raise(eHttpParserError, "Invalid HTTP format, parsing fails."); - } - rb_raise(eHttpParserError, "Requested start is after data buffer end."); -} - -void Init_unicorn_http(void) -{ - mUnicorn = rb_define_module("Unicorn"); - - DEF_GLOBAL(rack_url_scheme, "rack.url_scheme"); - DEF_GLOBAL(request_method, "REQUEST_METHOD"); - DEF_GLOBAL(request_uri, "REQUEST_URI"); - DEF_GLOBAL(fragment, "FRAGMENT"); - DEF_GLOBAL(query_string, "QUERY_STRING"); - DEF_GLOBAL(http_version, "HTTP_VERSION"); - DEF_GLOBAL(request_path, "REQUEST_PATH"); - DEF_GLOBAL(path_info, "PATH_INFO"); - DEF_GLOBAL(server_name, "SERVER_NAME"); - DEF_GLOBAL(server_port, "SERVER_PORT"); - DEF_GLOBAL(server_protocol, "SERVER_PROTOCOL"); - DEF_GLOBAL(server_protocol_value, "HTTP/1.1"); - DEF_GLOBAL(http_x_forwarded_proto, "HTTP_X_FORWARDED_PROTO"); - DEF_GLOBAL(port_80, "80"); - DEF_GLOBAL(port_443, "443"); - DEF_GLOBAL(localhost, "localhost"); - DEF_GLOBAL(http, "http"); - - eHttpParserError = rb_define_class_under(mUnicorn, "HttpParserError", rb_eIOError); - - cHttpParser = rb_define_class_under(mUnicorn, "HttpParser", rb_cObject); - rb_define_alloc_func(cHttpParser, HttpParser_alloc); - rb_define_method(cHttpParser, "initialize", HttpParser_init,0); - rb_define_method(cHttpParser, "reset", HttpParser_reset,0); - rb_define_method(cHttpParser, "execute", HttpParser_execute,2); - sym_http_body = ID2SYM(rb_intern("http_body")); - init_common_fields(); - g_http_host = find_common_field_value("HOST", 4); - assert(g_http_host != Qnil); -} diff --git a/ext/unicorn_http/unicorn_http.rl b/ext/unicorn_http/unicorn_http.rl index 2a56334..86ef46e 100644 --- a/ext/unicorn_http/unicorn_http.rl +++ b/ext/unicorn_http/unicorn_http.rl @@ -3,24 +3,14 @@ * Copyright (c) 2005 Zed A. Shaw * You can redistribute it and/or modify it under the same terms as Ruby. */ -#ifndef unicorn_http_h -#define unicorn_http_h - #include "ruby.h" #include "ext_help.h" +#include +#include #include - -static void http_field(VALUE req, const char *field, - size_t flen, const char *value, size_t vlen); -static void request_method(VALUE req, const char *at, size_t length); -static void scheme(VALUE req, const char *at, size_t length); -static void host(VALUE req, const char *at, size_t length); -static void request_uri(VALUE req, const char *at, size_t length); -static void fragment(VALUE req, const char *at, size_t length); -static void request_path(VALUE req, const char *at, size_t length); -static void query_string(VALUE req, const char *at, size_t length); -static void http_version(VALUE req, const char *at, size_t length); -static void header_done(VALUE req, const char *at, size_t length); +#include "common_field_optimization.h" +#include "global_variables.h" +#include "c_util.h" struct http_parser { int cs; @@ -34,26 +24,21 @@ struct http_parser { size_t field_len; }; +static void http_field(VALUE req, const char *field, + size_t flen, const char *value, size_t vlen); +static void request_method(VALUE req, const char *at, size_t length); +static void scheme(VALUE req, const char *at, size_t length); +static void host(VALUE req, const char *at, size_t length); +static void request_uri(VALUE req, const char *at, size_t length); +static void fragment(VALUE req, const char *at, size_t length); +static void request_path(VALUE req, const char *at, size_t length); +static void query_string(VALUE req, const char *at, size_t length); +static void http_version(VALUE req, const char *at, size_t length); +static void header_done(VALUE req, const char *at, size_t length); + static int http_parser_has_error(struct http_parser *parser); static int http_parser_is_finished(struct http_parser *parser); -/* - * capitalizes all lower-case ASCII characters, - * converts dashes to underscores. - */ -static void snake_upcase_char(char *c) -{ - if (*c >= 'a' && *c <= 'z') - *c &= ~0x20; - else if (*c == '-') - *c = '_'; -} - -static void downcase_char(char *c) -{ - if (*c >= 'A' && *c <= 'Z') - *c |= 0x20; -} #define LEN(AT, FPC) (FPC - buffer - parser->AT) #define MARK(M,FPC) (parser->M = (FPC) - buffer) @@ -69,10 +54,7 @@ static void downcase_char(char *c) action start_field { MARK(start.field, fpc); } action snake_upcase_field { snake_upcase_char((char *)fpc); } action downcase_char { downcase_char((char *)fpc); } - action write_field { - parser->field_len = LEN(start.field, fpc); - } - + action write_field { parser->field_len = LEN(start.field, fpc); } action start_value { MARK(mark, fpc); } action write_value { http_field(req, PTR_TO(start.field), parser->field_len, @@ -104,7 +86,8 @@ static void downcase_char(char *c) /** Data **/ %% write data; -static void http_parser_init(struct http_parser *parser) { +static void http_parser_init(struct http_parser *parser) +{ int cs = 0; memset(parser, 0, sizeof(*parser)); %% write init; @@ -112,8 +95,8 @@ static void http_parser_init(struct http_parser *parser) { } /** exec **/ -static void http_parser_execute( - struct http_parser *parser, VALUE req, const char *buffer, size_t len) +static void http_parser_execute(struct http_parser *parser, + VALUE req, const char *buffer, size_t len) { const char *p, *pe; int cs = parser->cs; @@ -124,7 +107,6 @@ static void http_parser_execute( p = buffer+off; pe = buffer+len; - assert(*pe == '\0' && "pointer does not end on NUL"); assert(pe - p == len - off && "pointers aren't same distance"); %% write exec; @@ -139,11 +121,271 @@ static void http_parser_execute( assert(parser->field_len <= len && "field has length longer than whole buffer"); } -static int http_parser_has_error(struct http_parser *parser) { +static int http_parser_has_error(struct http_parser *parser) +{ return parser->cs == http_parser_error; } -static int http_parser_is_finished(struct http_parser *parser) { +static int http_parser_is_finished(struct http_parser *parser) +{ return parser->cs == http_parser_first_final; } -#endif /* unicorn_http_h */ + +static struct http_parser *data_get(VALUE self) +{ + struct http_parser *http; + + Data_Get_Struct(self, struct http_parser, http); + assert(http); + return http; +} + +static void http_field(VALUE req, const char *field, + size_t flen, const char *value, size_t vlen) +{ + VALUE f = Qnil; + + VALIDATE_MAX_LENGTH(flen, FIELD_NAME); + VALIDATE_MAX_LENGTH(vlen, FIELD_VALUE); + + f = find_common_field_value(field, flen); + + if (f == Qnil) { + /* + * We got a strange header that we don't have a memoized value for. + * Fallback to creating a new string to use as a hash key. + * + * using rb_str_new(NULL, len) here is faster than rb_str_buf_new(len) + * in my testing, because: there's no minimum allocation length (and + * no check for it, either), RSTRING_LEN(f) does not need to be + * written twice, and and RSTRING_PTR(f) will already be + * null-terminated for us. + */ + f = rb_str_new(NULL, HTTP_PREFIX_LEN + flen); + memcpy(RSTRING_PTR(f), HTTP_PREFIX, HTTP_PREFIX_LEN); + memcpy(RSTRING_PTR(f) + HTTP_PREFIX_LEN, field, flen); + assert(*(RSTRING_PTR(f) + RSTRING_LEN(f)) == '\0'); /* paranoia */ + /* fprintf(stderr, "UNKNOWN HEADER <%s>\n", RSTRING_PTR(f)); */ + } else if (f == g_http_host && rb_hash_aref(req, f) != Qnil) { + return; + } + + rb_hash_aset(req, f, rb_str_new(value, vlen)); +} + +static void request_method(VALUE req, const char *at, size_t length) +{ + rb_hash_aset(req, g_request_method, rb_str_new(at, length)); +} + +static void scheme(VALUE req, const char *at, size_t length) +{ + rb_hash_aset(req, g_rack_url_scheme, rb_str_new(at, length)); +} + +static void host(VALUE req, const char *at, size_t length) +{ + rb_hash_aset(req, g_http_host, rb_str_new(at, length)); +} + +static void request_uri(VALUE req, const char *at, size_t length) +{ + VALIDATE_MAX_LENGTH(length, REQUEST_URI); + + rb_hash_aset(req, g_request_uri, rb_str_new(at, length)); + + /* "OPTIONS * HTTP/1.1\r\n" is a valid request */ + if (length == 1 && *at == '*') { + VALUE val = rb_str_new(NULL, 0); + rb_hash_aset(req, g_request_path, val); + rb_hash_aset(req, g_path_info, val); + } +} + +static void fragment(VALUE req, const char *at, size_t length) +{ + VALIDATE_MAX_LENGTH(length, FRAGMENT); + + rb_hash_aset(req, g_fragment, rb_str_new(at, length)); +} + +static void request_path(VALUE req, const char *at, size_t length) +{ + VALUE val = Qnil; + + VALIDATE_MAX_LENGTH(length, REQUEST_PATH); + + val = rb_str_new(at, length); + rb_hash_aset(req, g_request_path, val); + + /* rack says PATH_INFO must start with "/" or be empty */ + if (!(length == 1 && *at == '*')) + rb_hash_aset(req, g_path_info, val); +} + +static void query_string(VALUE req, const char *at, size_t length) +{ + VALIDATE_MAX_LENGTH(length, QUERY_STRING); + + rb_hash_aset(req, g_query_string, rb_str_new(at, length)); +} + +static void http_version(VALUE req, const char *at, size_t length) +{ + rb_hash_aset(req, g_http_version, rb_str_new(at, length)); +} + +/** Finalizes the request header to have a bunch of stuff that's needed. */ +static void header_done(VALUE req, const char *at, size_t length) +{ + VALUE server_name = g_localhost; + VALUE server_port = g_port_80; + VALUE temp; + + /* rack requires QUERY_STRING */ + if (rb_hash_aref(req, g_query_string) == Qnil) + rb_hash_aset(req, g_query_string, rb_str_new(NULL, 0)); + + /* set rack.url_scheme to "https" or "http", no others are allowed by Rack */ + if ((temp = rb_hash_aref(req, g_rack_url_scheme)) == Qnil) { + if ((temp = rb_hash_aref(req, g_http_x_forwarded_proto)) != Qnil && + RSTRING_LEN(temp) == 5 && + !memcmp("https", RSTRING_PTR(temp), 5)) + server_port = g_port_443; + else + temp = g_http; + rb_hash_aset(req, g_rack_url_scheme, temp); + } else if (RSTRING_LEN(temp) == 5 && !memcmp("https", RSTRING_PTR(temp), 5)) { + server_port = g_port_443; + } + + /* parse and set the SERVER_NAME and SERVER_PORT variables */ + if ((temp = rb_hash_aref(req, g_http_host)) != Qnil) { + char *colon = memchr(RSTRING_PTR(temp), ':', RSTRING_LEN(temp)); + if (colon) { + long port_start = colon - RSTRING_PTR(temp) + 1; + + server_name = rb_str_substr(temp, 0, colon - RSTRING_PTR(temp)); + if ((RSTRING_LEN(temp) - port_start) > 0) + server_port = rb_str_substr(temp, port_start, RSTRING_LEN(temp)); + } else { + server_name = temp; + } + } + rb_hash_aset(req, g_server_name, server_name); + rb_hash_aset(req, g_server_port, server_port); + rb_hash_aset(req, g_server_protocol, g_server_protocol_value); + + /* grab the initial body and stuff it into the hash */ + temp = rb_hash_aref(req, g_request_method); + if (temp != Qnil) { + long len = RSTRING_LEN(temp); + char *ptr = RSTRING_PTR(temp); + + if (memcmp(ptr, "HEAD", len) && memcmp(ptr, "GET", len)) + rb_hash_aset(req, sym_http_body, rb_str_new(at, length)); + } +} + +static VALUE HttpParser_alloc(VALUE klass) +{ + struct http_parser *http; + return Data_Make_Struct(klass, struct http_parser, NULL, NULL, http); +} + + +/** + * call-seq: + * parser.new -> parser + * + * Creates a new parser. + */ +static VALUE HttpParser_init(VALUE self) +{ + http_parser_init(data_get(self)); + + return self; +} + + +/** + * call-seq: + * parser.reset -> nil + * + * Resets the parser to it's initial state so that you can reuse it + * rather than making new ones. + */ +static VALUE HttpParser_reset(VALUE self) +{ + http_parser_init(data_get(self)); + + return Qnil; +} + + +/** + * call-seq: + * parser.execute(req, data) -> true/false + * + * Takes a Hash and a String of data, parses the String of data filling + * in the Hash returning a boolean to indicate whether or not parsing + * is finished. + * + * This function now throws an exception when there is a parsing error. + * This makes the logic for working with the parser much easier. You + * will need to wrap the parser with an exception handling block. + */ + +static VALUE HttpParser_execute(VALUE self, VALUE req, VALUE data) +{ + struct http_parser *http = data_get(self); + char *dptr = RSTRING_PTR(data); + long dlen = RSTRING_LEN(data); + + if (http->start.offset < dlen) { + http_parser_execute(http, req, dptr, dlen); + + VALIDATE_MAX_LENGTH(http->start.offset, HEADER); + + if (!http_parser_has_error(http)) + return http_parser_is_finished(http) ? Qtrue : Qfalse; + + rb_raise(eHttpParserError, "Invalid HTTP format, parsing fails."); + } + rb_raise(eHttpParserError, "Requested start is after data buffer end."); +} + +void Init_unicorn_http(void) +{ + mUnicorn = rb_define_module("Unicorn"); + + DEF_GLOBAL(rack_url_scheme, "rack.url_scheme"); + DEF_GLOBAL(request_method, "REQUEST_METHOD"); + DEF_GLOBAL(request_uri, "REQUEST_URI"); + DEF_GLOBAL(fragment, "FRAGMENT"); + DEF_GLOBAL(query_string, "QUERY_STRING"); + DEF_GLOBAL(http_version, "HTTP_VERSION"); + DEF_GLOBAL(request_path, "REQUEST_PATH"); + DEF_GLOBAL(path_info, "PATH_INFO"); + DEF_GLOBAL(server_name, "SERVER_NAME"); + DEF_GLOBAL(server_port, "SERVER_PORT"); + DEF_GLOBAL(server_protocol, "SERVER_PROTOCOL"); + DEF_GLOBAL(server_protocol_value, "HTTP/1.1"); + DEF_GLOBAL(http_x_forwarded_proto, "HTTP_X_FORWARDED_PROTO"); + DEF_GLOBAL(port_80, "80"); + DEF_GLOBAL(port_443, "443"); + DEF_GLOBAL(localhost, "localhost"); + DEF_GLOBAL(http, "http"); + + eHttpParserError = rb_define_class_under(mUnicorn, "HttpParserError", rb_eIOError); + + cHttpParser = rb_define_class_under(mUnicorn, "HttpParser", rb_cObject); + rb_define_alloc_func(cHttpParser, HttpParser_alloc); + rb_define_method(cHttpParser, "initialize", HttpParser_init,0); + rb_define_method(cHttpParser, "reset", HttpParser_reset,0); + rb_define_method(cHttpParser, "execute", HttpParser_execute,2); + sym_http_body = ID2SYM(rb_intern("http_body")); + init_common_fields(); + g_http_host = find_common_field_value("HOST", 4); + assert(g_http_host != Qnil); +} -- cgit v1.2.3-24-ge0c7