ext/http11/http11.c in mongrel-0.3.12.4 vs ext/http11/http11.c in mongrel-0.3.13
- old
+ new
@@ -1,5 +1,24 @@
+/* Mongrel Web Server - A Mostly Ruby Webserver and Library
+ *
+ * Copyright (C) 2005 Zed A. Shaw zedshaw AT zedshaw dot com
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
#include "ruby.h"
#include "ext_help.h"
#include <assert.h>
#include <string.h>
#include "http11_parser.h"
@@ -21,20 +40,21 @@
static VALUE global_http_content_length;
static VALUE global_content_type;
static VALUE global_http_content_type;
static VALUE global_gateway_interface;
static VALUE global_gateway_interface_value;
-static VALUE global_interface_value;
static VALUE global_server_name;
static VALUE global_server_port;
static VALUE global_server_protocol;
static VALUE global_server_protocol_value;
static VALUE global_http_host;
static VALUE global_mongrel_version;
static VALUE global_server_software;
static VALUE global_port_80;
+#define TRIE_INCREASE 30
+
/** Defines common length and error messages for input length validation. */
#define DEF_MAX_LENGTH(N,length) const size_t MAX_##N##_LENGTH = length; const char *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) if(len > MAX_##N##_LENGTH) { rb_raise(eHttpParserError, MAX_##N##_LENGTH_ERR); }
@@ -62,11 +82,11 @@
VALIDATE_MAX_LENGTH(vlen, FIELD_VALUE);
v = rb_str_new(value, vlen);
f = rb_str_dup(global_http_prefix);
f = rb_str_buf_cat(f, field, flen);
-
+
for(ch = RSTRING(f)->ptr, end = ch + RSTRING(f)->len; ch < end; ch++) {
if(*ch == '-') {
*ch = '_';
} else {
*ch = toupper(*ch);
@@ -99,11 +119,11 @@
void query_string(void *data, const char *at, size_t length)
{
VALUE req = (VALUE)data;
VALUE val = Qnil;
-
+
VALIDATE_MAX_LENGTH(length, QUERY_STRING);
val = rb_str_new(at, length);
rb_hash_aset(req, global_query_string, val);
}
@@ -120,12 +140,10 @@
void header_done(void *data, const char *at, size_t length)
{
VALUE req = (VALUE)data;
VALUE temp = Qnil;
- VALUE host = Qnil;
- VALUE port = Qnil;
VALUE ctype = Qnil;
VALUE clen = Qnil;
char *colon = NULL;
clen = rb_hash_aref(req, global_http_content_length);
@@ -236,39 +254,56 @@
}
/**
* call-seq:
- * parser.execute(req_hash, data) -> Integer
+ * parser.execute(req_hash, data, start) -> Integer
*
* Takes a Hash and a String of data, parses the String of data filling in the Hash
* returning an Integer to indicate how much of the data has been read. No matter
* what the return value, you should call HttpParser#finished? and HttpParser#error?
* to figure out if it's done parsing or there was an error.
*
* This function now throws an exception when there is a parsing error. This makes
* the logic for working with the parser much easier. You can still test for an
* error, but now you need to wrap the parser with an exception handling block.
+ *
+ * The third argument allows for parsing a partial request and then continuing
+ * the parsing from that position. It needs all of the original data as well
+ * so you have to append to the data buffer as you read.
*/
-VALUE HttpParser_execute(VALUE self, VALUE req_hash, VALUE data)
+VALUE HttpParser_execute(VALUE self, VALUE req_hash, VALUE data, VALUE start)
{
http_parser *http = NULL;
+ int from = 0;
+ char *dptr = NULL;
+ long dlen = 0;
+
DATA_GET(self, http_parser, http);
- http->data = (void *)req_hash;
- http_parser_execute(http, RSTRING(data)->ptr, RSTRING(data)->len);
-
- VALIDATE_MAX_LENGTH(http_parser_nread(http), HEADER);
-
- if(http_parser_has_error(http)) {
- rb_raise(eHttpParserError, "Invalid HTTP format, parsing fails.");
+ from = FIX2INT(start);
+ dptr = RSTRING(data)->ptr;
+ dlen = RSTRING(data)->len;
+
+ if(from >= dlen) {
+ rb_raise(eHttpParserError, "Requested start is after data buffer end.");
} else {
- return INT2FIX(http_parser_nread(http));
+ http->data = (void *)req_hash;
+ http_parser_execute(http, dptr, dlen, from);
+
+ VALIDATE_MAX_LENGTH(http_parser_nread(http), HEADER);
+
+ if(http_parser_has_error(http)) {
+ rb_raise(eHttpParserError, "Invalid HTTP format, parsing fails.");
+ } else {
+ return INT2FIX(http_parser_nread(http));
+ }
}
}
+
/**
* call-seq:
* parser.error? -> true/false
*
* Tells you whether the parser is in an error state.
@@ -321,11 +356,10 @@
tst_cleanup((struct tst *)data);
}
}
-#define TRIE_INCREASE 30
VALUE URIClassifier_alloc(VALUE klass)
{
VALUE obj;
struct tst *tst = tst_init(TRIE_INCREASE);
@@ -518,21 +552,21 @@
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_host, "HTTP_HOST");
- DEF_GLOBAL(mongrel_version, "Mongrel 0.3.12.4");
+ DEF_GLOBAL(mongrel_version, "Mongrel 0.3.13");
DEF_GLOBAL(server_software, "SERVER_SOFTWARE");
DEF_GLOBAL(port_80, "80");
eHttpParserError = rb_define_class_under(mMongrel, "HttpParserError", rb_eIOError);
cHttpParser = rb_define_class_under(mMongrel, "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, "finish", HttpParser_finish,0);
- rb_define_method(cHttpParser, "execute", HttpParser_execute,2);
+ rb_define_method(cHttpParser, "execute", HttpParser_execute,3);
rb_define_method(cHttpParser, "error?", HttpParser_has_error,0);
rb_define_method(cHttpParser, "finished?", HttpParser_is_finished,0);
rb_define_method(cHttpParser, "nread", HttpParser_nread,0);
cURIClassifier = rb_define_class_under(mMongrel, "URIClassifier", rb_cObject);