about summary refs log tree commit homepage
path: root/ext/unicorn_http/unicorn_http.rl
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2009-08-02 13:39:21 -0700
committerEric Wong <normalperson@yhbt.net>2009-08-09 01:24:30 -0700
commitca3fd8acb4bc3f5125a9f0f281951fb8f03778e5 (patch)
tree42752ea26cf989d9a8d02d750a87aff0d15dc1d8 /ext/unicorn_http/unicorn_http.rl
parente1d67bef994587ba77e1a3575773755b2b5e1558 (diff)
downloadunicorn-ca3fd8acb4bc3f5125a9f0f281951fb8f03778e5.tar.gz
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.
Diffstat (limited to 'ext/unicorn_http/unicorn_http.rl')
-rw-r--r--ext/unicorn_http/unicorn_http.rl328
1 files changed, 285 insertions, 43 deletions
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 <assert.h>
+#include <string.h>
 #include <sys/types.h>
-
-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);
+}