2006-06-30 16:42:12 -04:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2005 Zed A. Shaw
|
|
|
|
* You can redistribute it and/or modify it under the same terms as Ruby.
|
|
|
|
*/
|
2012-06-13 03:27:09 -04:00
|
|
|
|
|
|
|
#define RSTRING_NOT_MODIFIED 1
|
|
|
|
|
2006-01-28 14:03:53 -05:00
|
|
|
#include "ruby.h"
|
|
|
|
#include "ext_help.h"
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "http11_parser.h"
|
2008-03-23 23:48:10 -04:00
|
|
|
|
2011-09-15 01:07:27 -04:00
|
|
|
#ifndef MANAGED_STRINGS
|
|
|
|
|
2008-03-23 23:48:10 -04:00
|
|
|
#ifndef RSTRING_PTR
|
|
|
|
#define RSTRING_PTR(s) (RSTRING(s)->ptr)
|
|
|
|
#endif
|
|
|
|
#ifndef RSTRING_LEN
|
|
|
|
#define RSTRING_LEN(s) (RSTRING(s)->len)
|
|
|
|
#endif
|
2006-01-28 14:03:53 -05:00
|
|
|
|
2011-09-15 01:07:27 -04:00
|
|
|
#define rb_extract_chars(e, sz) (*sz = RSTRING_LEN(e), RSTRING_PTR(e))
|
|
|
|
#define rb_free_chars(e) /* nothing */
|
|
|
|
|
2007-12-11 18:14:17 -05:00
|
|
|
#endif
|
|
|
|
|
2006-04-02 22:27:59 -04:00
|
|
|
static VALUE eHttpParserError;
|
2006-07-13 18:34:59 -04:00
|
|
|
|
2008-03-23 23:48:10 -04:00
|
|
|
#define HTTP_PREFIX "HTTP_"
|
|
|
|
#define HTTP_PREFIX_LEN (sizeof(HTTP_PREFIX) - 1)
|
2006-01-28 14:03:53 -05:00
|
|
|
|
2006-02-02 01:53:32 -05:00
|
|
|
static VALUE global_request_method;
|
2006-02-03 00:42:08 -05:00
|
|
|
static VALUE global_request_uri;
|
2007-10-15 14:00:40 -04:00
|
|
|
static VALUE global_fragment;
|
2006-02-02 01:53:32 -05:00
|
|
|
static VALUE global_query_string;
|
|
|
|
static VALUE global_http_version;
|
2006-08-08 05:26:23 -04:00
|
|
|
static VALUE global_request_path;
|
2006-01-28 14:03:53 -05:00
|
|
|
|
2006-04-02 22:27:59 -04:00
|
|
|
/** Defines common length and error messages for input length validation. */
|
2013-07-18 14:22:18 -04:00
|
|
|
#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 (was %d)"
|
2006-04-02 22:27:59 -04:00
|
|
|
|
|
|
|
/** Validates the max length of given input and throws an HttpParserError exception if over. */
|
2013-07-18 14:22:18 -04:00
|
|
|
#define VALIDATE_MAX_LENGTH(len, N) if(len > MAX_##N##_LENGTH) { rb_raise(eHttpParserError, MAX_##N##_LENGTH_ERR, len); }
|
2006-04-02 22:27:59 -04:00
|
|
|
|
|
|
|
/** Defines global strings in the init method. */
|
2011-09-15 01:07:27 -04:00
|
|
|
#define DEF_GLOBAL(N, val) global_##N = rb_str_new2(val); rb_global_variable(&global_##N)
|
2006-04-02 22:27:59 -04:00
|
|
|
|
|
|
|
|
|
|
|
/* Defines the maximum allowed lengths for various input elements.*/
|
|
|
|
DEF_MAX_LENGTH(FIELD_NAME, 256);
|
|
|
|
DEF_MAX_LENGTH(FIELD_VALUE, 80 * 1024);
|
2006-10-03 04:18:26 -04:00
|
|
|
DEF_MAX_LENGTH(REQUEST_URI, 1024 * 12);
|
2007-10-15 14:00:40 -04:00
|
|
|
DEF_MAX_LENGTH(FRAGMENT, 1024); /* Don't know if this length is specified somewhere or not */
|
2013-11-10 02:48:24 -05:00
|
|
|
DEF_MAX_LENGTH(REQUEST_PATH, 2048);
|
2006-04-02 22:27:59 -04:00
|
|
|
DEF_MAX_LENGTH(QUERY_STRING, (1024 * 10));
|
|
|
|
DEF_MAX_LENGTH(HEADER, (1024 * (80 + 32)));
|
|
|
|
|
2008-03-23 23:48:10 -04:00
|
|
|
struct common_field {
|
2012-08-10 13:12:33 -04:00
|
|
|
const size_t len;
|
2008-03-23 23:48:10 -04:00
|
|
|
const char *name;
|
2011-09-18 16:02:34 -04:00
|
|
|
int raw;
|
2008-03-23 23:48:10 -04:00
|
|
|
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[] = {
|
2011-09-18 16:02:34 -04:00
|
|
|
# define f(N) { (sizeof(N) - 1), N, 0, Qnil }
|
|
|
|
# define fr(N) { (sizeof(N) - 1), N, 1, Qnil }
|
2008-03-23 23:48:10 -04:00
|
|
|
f("ACCEPT"),
|
|
|
|
f("ACCEPT_CHARSET"),
|
|
|
|
f("ACCEPT_ENCODING"),
|
|
|
|
f("ACCEPT_LANGUAGE"),
|
|
|
|
f("ALLOW"),
|
|
|
|
f("AUTHORIZATION"),
|
|
|
|
f("CACHE_CONTROL"),
|
|
|
|
f("CONNECTION"),
|
|
|
|
f("CONTENT_ENCODING"),
|
2011-09-18 16:02:34 -04:00
|
|
|
fr("CONTENT_LENGTH"),
|
|
|
|
fr("CONTENT_TYPE"),
|
2008-03-23 23:48:10 -04:00
|
|
|
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_REAL_IP"), /* common for proxies */
|
|
|
|
f("WARNING")
|
|
|
|
# undef f
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* qsort(3) and bsearch(3) improve average performance slightly, but may
|
|
|
|
* not be worth it for lack of portability to certain platforms...
|
|
|
|
*/
|
|
|
|
#if defined(HAVE_QSORT_BSEARCH)
|
|
|
|
/* sort by length, then by name if there's a tie */
|
|
|
|
static int common_field_cmp(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
struct common_field *cfa = (struct common_field *)a;
|
|
|
|
struct common_field *cfb = (struct common_field *)b;
|
|
|
|
signed long diff = cfa->len - cfb->len;
|
|
|
|
return diff ? diff : memcmp(cfa->name, cfb->name, cfa->len);
|
|
|
|
}
|
|
|
|
#endif /* HAVE_QSORT_BSEARCH */
|
|
|
|
|
|
|
|
static void init_common_fields(void)
|
|
|
|
{
|
2012-08-10 13:12:33 -04:00
|
|
|
unsigned i;
|
2008-03-23 23:48:10 -04:00
|
|
|
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++) {
|
2011-09-18 16:02:34 -04:00
|
|
|
if(cf->raw) {
|
|
|
|
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);
|
|
|
|
}
|
2008-03-23 23:48:10 -04:00
|
|
|
rb_global_variable(&cf->value);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(HAVE_QSORT_BSEARCH)
|
|
|
|
qsort(common_http_fields,
|
|
|
|
ARRAY_SIZE(common_http_fields),
|
|
|
|
sizeof(struct common_field),
|
|
|
|
common_field_cmp);
|
|
|
|
#endif /* HAVE_QSORT_BSEARCH */
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE find_common_field_value(const char *field, size_t flen)
|
|
|
|
{
|
|
|
|
#if defined(HAVE_QSORT_BSEARCH)
|
|
|
|
struct common_field key;
|
|
|
|
struct common_field *found;
|
|
|
|
key.name = field;
|
|
|
|
key.len = (signed long)flen;
|
|
|
|
found = (struct common_field *)bsearch(&key, common_http_fields,
|
|
|
|
ARRAY_SIZE(common_http_fields),
|
|
|
|
sizeof(struct common_field),
|
|
|
|
common_field_cmp);
|
|
|
|
return found ? found->value : Qnil;
|
|
|
|
#else /* !HAVE_QSORT_BSEARCH */
|
2012-08-10 13:12:33 -04:00
|
|
|
unsigned i;
|
2008-03-23 23:48:10 -04:00
|
|
|
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 /* !HAVE_QSORT_BSEARCH */
|
|
|
|
}
|
2006-03-30 04:31:14 -05:00
|
|
|
|
2013-07-18 14:22:18 -04:00
|
|
|
void http_field(puma_parser* hp, const char *field, size_t flen,
|
2011-09-15 01:07:27 -04:00
|
|
|
const char *value, size_t vlen)
|
2006-01-28 14:03:53 -05:00
|
|
|
{
|
2006-04-02 22:27:59 -04:00
|
|
|
VALUE v = Qnil;
|
|
|
|
VALUE f = Qnil;
|
|
|
|
|
|
|
|
VALIDATE_MAX_LENGTH(flen, FIELD_NAME);
|
|
|
|
VALIDATE_MAX_LENGTH(vlen, FIELD_VALUE);
|
2006-02-02 01:53:32 -05:00
|
|
|
|
2006-04-02 22:27:59 -04:00
|
|
|
v = rb_str_new(value, vlen);
|
2006-05-21 06:25:19 -04:00
|
|
|
|
2008-03-23 23:48:10 -04:00
|
|
|
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.
|
|
|
|
*/
|
2011-09-15 01:07:27 -04:00
|
|
|
|
|
|
|
size_t new_size = HTTP_PREFIX_LEN + flen;
|
|
|
|
assert(new_size < BUFFER_LEN);
|
|
|
|
|
|
|
|
memcpy(hp->buf, HTTP_PREFIX, HTTP_PREFIX_LEN);
|
|
|
|
memcpy(hp->buf + HTTP_PREFIX_LEN, field, flen);
|
|
|
|
|
|
|
|
f = rb_str_new(hp->buf, new_size);
|
2006-01-28 14:03:53 -05:00
|
|
|
}
|
|
|
|
|
2011-09-15 01:07:27 -04:00
|
|
|
rb_hash_aset(hp->request, f, v);
|
2006-01-28 14:03:53 -05:00
|
|
|
}
|
|
|
|
|
2013-07-18 14:22:18 -04:00
|
|
|
void request_method(puma_parser* hp, const char *at, size_t length)
|
2006-01-28 14:03:53 -05:00
|
|
|
{
|
2006-04-02 22:27:59 -04:00
|
|
|
VALUE val = Qnil;
|
|
|
|
|
|
|
|
val = rb_str_new(at, length);
|
2011-09-15 01:07:27 -04:00
|
|
|
rb_hash_aset(hp->request, global_request_method, val);
|
2006-01-28 14:03:53 -05:00
|
|
|
}
|
|
|
|
|
2013-07-18 14:22:18 -04:00
|
|
|
void request_uri(puma_parser* hp, const char *at, size_t length)
|
2006-01-28 14:03:53 -05:00
|
|
|
{
|
2006-04-02 22:27:59 -04:00
|
|
|
VALUE val = Qnil;
|
|
|
|
|
|
|
|
VALIDATE_MAX_LENGTH(length, REQUEST_URI);
|
|
|
|
|
|
|
|
val = rb_str_new(at, length);
|
2011-09-15 01:07:27 -04:00
|
|
|
rb_hash_aset(hp->request, global_request_uri, val);
|
2006-01-28 14:03:53 -05:00
|
|
|
}
|
|
|
|
|
2013-07-18 14:22:18 -04:00
|
|
|
void fragment(puma_parser* hp, const char *at, size_t length)
|
2007-10-15 14:00:40 -04:00
|
|
|
{
|
|
|
|
VALUE val = Qnil;
|
|
|
|
|
|
|
|
VALIDATE_MAX_LENGTH(length, FRAGMENT);
|
|
|
|
|
|
|
|
val = rb_str_new(at, length);
|
2011-09-15 01:07:27 -04:00
|
|
|
rb_hash_aset(hp->request, global_fragment, val);
|
2007-10-15 14:00:40 -04:00
|
|
|
}
|
|
|
|
|
2013-07-18 14:22:18 -04:00
|
|
|
void request_path(puma_parser* hp, const char *at, size_t length)
|
2006-08-08 05:26:23 -04:00
|
|
|
{
|
|
|
|
VALUE val = Qnil;
|
|
|
|
|
|
|
|
VALIDATE_MAX_LENGTH(length, REQUEST_PATH);
|
|
|
|
|
|
|
|
val = rb_str_new(at, length);
|
2011-09-15 01:07:27 -04:00
|
|
|
rb_hash_aset(hp->request, global_request_path, val);
|
2006-08-08 05:26:23 -04:00
|
|
|
}
|
2006-01-28 14:03:53 -05:00
|
|
|
|
2013-07-18 14:22:18 -04:00
|
|
|
void query_string(puma_parser* hp, const char *at, size_t length)
|
2006-01-28 14:03:53 -05:00
|
|
|
{
|
2006-04-02 22:27:59 -04:00
|
|
|
VALUE val = Qnil;
|
2006-05-21 06:25:19 -04:00
|
|
|
|
2006-04-02 22:27:59 -04:00
|
|
|
VALIDATE_MAX_LENGTH(length, QUERY_STRING);
|
|
|
|
|
|
|
|
val = rb_str_new(at, length);
|
2011-09-15 01:07:27 -04:00
|
|
|
rb_hash_aset(hp->request, global_query_string, val);
|
2006-01-28 14:03:53 -05:00
|
|
|
}
|
|
|
|
|
2013-07-18 14:22:18 -04:00
|
|
|
void http_version(puma_parser* hp, const char *at, size_t length)
|
2006-01-28 14:03:53 -05:00
|
|
|
{
|
|
|
|
VALUE val = rb_str_new(at, length);
|
2011-09-15 01:07:27 -04:00
|
|
|
rb_hash_aset(hp->request, global_http_version, val);
|
2006-01-28 14:03:53 -05:00
|
|
|
}
|
|
|
|
|
2006-03-19 00:18:11 -05:00
|
|
|
/** Finalizes the request header to have a bunch of stuff that's
|
2006-07-13 18:34:59 -04:00
|
|
|
needed. */
|
2006-01-28 14:03:53 -05:00
|
|
|
|
2013-07-18 14:22:18 -04:00
|
|
|
void header_done(puma_parser* hp, const char *at, size_t length)
|
2006-03-19 00:18:11 -05:00
|
|
|
{
|
2011-09-18 16:02:34 -04:00
|
|
|
hp->body = rb_str_new(at, length);
|
2006-03-19 00:18:11 -05:00
|
|
|
}
|
2006-01-28 14:03:53 -05:00
|
|
|
|
|
|
|
|
|
|
|
void HttpParser_free(void *data) {
|
2006-07-13 18:34:59 -04:00
|
|
|
TRACE();
|
|
|
|
|
|
|
|
if(data) {
|
2012-01-06 05:47:10 -05:00
|
|
|
xfree(data);
|
2006-07-13 18:34:59 -04:00
|
|
|
}
|
2006-01-28 14:03:53 -05:00
|
|
|
}
|
|
|
|
|
2013-07-18 14:22:18 -04:00
|
|
|
void HttpParser_mark(puma_parser* hp) {
|
2011-09-15 01:07:27 -04:00
|
|
|
if(hp->request) rb_gc_mark(hp->request);
|
2011-09-18 16:02:34 -04:00
|
|
|
if(hp->body) rb_gc_mark(hp->body);
|
2011-09-15 01:07:27 -04:00
|
|
|
}
|
2006-01-28 14:03:53 -05:00
|
|
|
|
|
|
|
VALUE HttpParser_alloc(VALUE klass)
|
|
|
|
{
|
2013-07-18 14:22:18 -04:00
|
|
|
puma_parser *hp = ALLOC_N(puma_parser, 1);
|
2006-07-13 18:34:59 -04:00
|
|
|
TRACE();
|
|
|
|
hp->http_field = http_field;
|
|
|
|
hp->request_method = request_method;
|
|
|
|
hp->request_uri = request_uri;
|
2007-10-15 14:00:40 -04:00
|
|
|
hp->fragment = fragment;
|
2006-08-08 05:26:23 -04:00
|
|
|
hp->request_path = request_path;
|
2006-07-13 18:34:59 -04:00
|
|
|
hp->query_string = query_string;
|
|
|
|
hp->http_version = http_version;
|
|
|
|
hp->header_done = header_done;
|
2011-09-15 01:07:27 -04:00
|
|
|
hp->request = Qnil;
|
2006-07-13 18:34:59 -04:00
|
|
|
|
2013-07-18 14:22:18 -04:00
|
|
|
puma_parser_init(hp);
|
2006-07-13 18:34:59 -04:00
|
|
|
|
2011-09-15 01:07:27 -04:00
|
|
|
return Data_Wrap_Struct(klass, HttpParser_mark, HttpParser_free, hp);
|
2006-01-28 14:03:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* call-seq:
|
|
|
|
* parser.new -> parser
|
|
|
|
*
|
|
|
|
* Creates a new parser.
|
|
|
|
*/
|
|
|
|
VALUE HttpParser_init(VALUE self)
|
|
|
|
{
|
2013-07-18 14:22:18 -04:00
|
|
|
puma_parser *http = NULL;
|
|
|
|
DATA_GET(self, puma_parser, http);
|
|
|
|
puma_parser_init(http);
|
2006-07-13 18:34:59 -04:00
|
|
|
|
2006-01-28 14:03:53 -05:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
VALUE HttpParser_reset(VALUE self)
|
|
|
|
{
|
2013-07-18 14:22:18 -04:00
|
|
|
puma_parser *http = NULL;
|
|
|
|
DATA_GET(self, puma_parser, http);
|
|
|
|
puma_parser_init(http);
|
2006-07-13 18:34:59 -04:00
|
|
|
|
2006-01-28 14:03:53 -05:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* call-seq:
|
|
|
|
* parser.finish -> true/false
|
|
|
|
*
|
|
|
|
* Finishes a parser early which could put in a "good" or bad state.
|
|
|
|
* You should call reset after finish it or bad things will happen.
|
|
|
|
*/
|
|
|
|
VALUE HttpParser_finish(VALUE self)
|
|
|
|
{
|
2013-07-18 14:22:18 -04:00
|
|
|
puma_parser *http = NULL;
|
|
|
|
DATA_GET(self, puma_parser, http);
|
|
|
|
puma_parser_finish(http);
|
2006-07-13 18:34:59 -04:00
|
|
|
|
2013-07-18 14:22:18 -04:00
|
|
|
return puma_parser_is_finished(http) ? Qtrue : Qfalse;
|
2006-01-28 14:03:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* call-seq:
|
2006-05-14 19:41:22 -04:00
|
|
|
* parser.execute(req_hash, data, start) -> Integer
|
2006-01-28 14:03:53 -05:00
|
|
|
*
|
|
|
|
* 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.
|
2010-07-24 13:04:37 -04:00
|
|
|
*
|
|
|
|
* 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
|
2006-01-30 01:25:20 -05:00
|
|
|
* error, but now you need to wrap the parser with an exception handling block.
|
2006-05-14 19:41:22 -04:00
|
|
|
*
|
|
|
|
* The third argument allows for parsing a partial request and then continuing
|
2010-07-24 13:04:37 -04:00
|
|
|
* the parsing from that position. It needs all of the original data as well
|
2006-05-14 19:41:22 -04:00
|
|
|
* so you have to append to the data buffer as you read.
|
2006-01-28 14:03:53 -05:00
|
|
|
*/
|
2006-05-14 19:41:22 -04:00
|
|
|
VALUE HttpParser_execute(VALUE self, VALUE req_hash, VALUE data, VALUE start)
|
2006-01-28 14:03:53 -05:00
|
|
|
{
|
2013-07-18 14:22:18 -04:00
|
|
|
puma_parser *http = NULL;
|
2006-05-14 19:41:22 -04:00
|
|
|
int from = 0;
|
|
|
|
char *dptr = NULL;
|
|
|
|
long dlen = 0;
|
2006-01-30 01:25:20 -05:00
|
|
|
|
2013-07-18 14:22:18 -04:00
|
|
|
DATA_GET(self, puma_parser, http);
|
2006-04-02 22:27:59 -04:00
|
|
|
|
2006-05-14 19:41:22 -04:00
|
|
|
from = FIX2INT(start);
|
2011-09-15 01:07:27 -04:00
|
|
|
dptr = rb_extract_chars(data, &dlen);
|
2006-07-13 18:34:59 -04:00
|
|
|
|
2006-05-14 19:41:22 -04:00
|
|
|
if(from >= dlen) {
|
2011-09-15 01:07:27 -04:00
|
|
|
rb_free_chars(dptr);
|
2012-07-10 18:47:49 -04:00
|
|
|
rb_raise(eHttpParserError, "%s", "Requested start is after data buffer end.");
|
2006-01-30 01:25:20 -05:00
|
|
|
} else {
|
2011-09-15 01:07:27 -04:00
|
|
|
http->request = req_hash;
|
2013-07-18 14:22:18 -04:00
|
|
|
puma_parser_execute(http, dptr, dlen, from);
|
2006-07-13 18:34:59 -04:00
|
|
|
|
2011-09-15 01:07:27 -04:00
|
|
|
rb_free_chars(dptr);
|
2013-07-18 14:22:18 -04:00
|
|
|
VALIDATE_MAX_LENGTH(puma_parser_nread(http), HEADER);
|
2006-07-13 18:34:59 -04:00
|
|
|
|
2013-07-18 14:22:18 -04:00
|
|
|
if(puma_parser_has_error(http)) {
|
2012-07-10 18:47:49 -04:00
|
|
|
rb_raise(eHttpParserError, "%s", "Invalid HTTP format, parsing fails.");
|
2006-05-14 19:41:22 -04:00
|
|
|
} else {
|
2013-07-18 14:22:18 -04:00
|
|
|
return INT2FIX(puma_parser_nread(http));
|
2006-05-14 19:41:22 -04:00
|
|
|
}
|
2006-01-30 01:25:20 -05:00
|
|
|
}
|
2006-01-28 14:03:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-14 19:41:22 -04:00
|
|
|
|
2006-01-28 14:03:53 -05:00
|
|
|
/**
|
|
|
|
* call-seq:
|
|
|
|
* parser.error? -> true/false
|
|
|
|
*
|
|
|
|
* Tells you whether the parser is in an error state.
|
|
|
|
*/
|
|
|
|
VALUE HttpParser_has_error(VALUE self)
|
|
|
|
{
|
2013-07-18 14:22:18 -04:00
|
|
|
puma_parser *http = NULL;
|
|
|
|
DATA_GET(self, puma_parser, http);
|
2006-07-13 18:34:59 -04:00
|
|
|
|
2013-07-18 14:22:18 -04:00
|
|
|
return puma_parser_has_error(http) ? Qtrue : Qfalse;
|
2006-01-28 14:03:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* call-seq:
|
|
|
|
* parser.finished? -> true/false
|
|
|
|
*
|
|
|
|
* Tells you whether the parser is finished or not and in a good state.
|
|
|
|
*/
|
|
|
|
VALUE HttpParser_is_finished(VALUE self)
|
|
|
|
{
|
2013-07-18 14:22:18 -04:00
|
|
|
puma_parser *http = NULL;
|
|
|
|
DATA_GET(self, puma_parser, http);
|
2006-07-13 18:34:59 -04:00
|
|
|
|
2013-07-18 14:22:18 -04:00
|
|
|
return puma_parser_is_finished(http) ? Qtrue : Qfalse;
|
2006-01-28 14:03:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* call-seq:
|
|
|
|
* parser.nread -> Integer
|
|
|
|
*
|
|
|
|
* Returns the amount of data processed so far during this processing cycle. It is
|
|
|
|
* set to 0 on initialize or reset calls and is incremented each time execute is called.
|
|
|
|
*/
|
|
|
|
VALUE HttpParser_nread(VALUE self)
|
|
|
|
{
|
2013-07-18 14:22:18 -04:00
|
|
|
puma_parser *http = NULL;
|
|
|
|
DATA_GET(self, puma_parser, http);
|
2006-07-13 18:34:59 -04:00
|
|
|
|
2006-01-28 14:03:53 -05:00
|
|
|
return INT2FIX(http->nread);
|
|
|
|
}
|
|
|
|
|
2011-09-18 16:02:34 -04:00
|
|
|
/**
|
|
|
|
* call-seq:
|
|
|
|
* parser.body -> nil or String
|
|
|
|
*
|
|
|
|
* If the request included a body, returns it.
|
|
|
|
*/
|
|
|
|
VALUE HttpParser_body(VALUE self) {
|
2013-07-18 14:22:18 -04:00
|
|
|
puma_parser *http = NULL;
|
|
|
|
DATA_GET(self, puma_parser, http);
|
2011-09-18 16:02:34 -04:00
|
|
|
|
|
|
|
return http->body;
|
|
|
|
}
|
|
|
|
|
2012-08-11 18:09:09 -04:00
|
|
|
void Init_io_buffer(VALUE puma);
|
2012-08-23 01:12:12 -04:00
|
|
|
void Init_mini_ssl(VALUE mod);
|
2012-08-11 18:09:09 -04:00
|
|
|
|
2011-10-02 12:33:48 -04:00
|
|
|
void Init_puma_http11()
|
2006-01-28 14:03:53 -05:00
|
|
|
{
|
2006-01-30 23:45:26 -05:00
|
|
|
|
2011-09-22 22:24:43 -04:00
|
|
|
VALUE mPuma = rb_define_module("Puma");
|
2012-08-10 13:12:33 -04:00
|
|
|
VALUE cHttpParser = rb_define_class_under(mPuma, "HttpParser", rb_cObject);
|
2006-02-02 01:53:32 -05:00
|
|
|
|
2006-03-19 00:18:11 -05:00
|
|
|
DEF_GLOBAL(request_method, "REQUEST_METHOD");
|
|
|
|
DEF_GLOBAL(request_uri, "REQUEST_URI");
|
2007-10-15 14:00:40 -04:00
|
|
|
DEF_GLOBAL(fragment, "FRAGMENT");
|
2006-03-19 00:18:11 -05:00
|
|
|
DEF_GLOBAL(query_string, "QUERY_STRING");
|
|
|
|
DEF_GLOBAL(http_version, "HTTP_VERSION");
|
2006-08-08 05:26:23 -04:00
|
|
|
DEF_GLOBAL(request_path, "REQUEST_PATH");
|
2006-03-19 00:18:11 -05:00
|
|
|
|
2011-09-22 22:24:43 -04:00
|
|
|
eHttpParserError = rb_define_class_under(mPuma, "HttpParserError", rb_eIOError);
|
2011-09-15 01:07:27 -04:00
|
|
|
rb_global_variable(&eHttpParserError);
|
2006-04-02 22:27:59 -04:00
|
|
|
|
2006-01-28 14:03:53 -05:00
|
|
|
rb_define_alloc_func(cHttpParser, HttpParser_alloc);
|
2011-09-18 16:02:34 -04:00
|
|
|
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, 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);
|
|
|
|
rb_define_method(cHttpParser, "body", HttpParser_body, 0);
|
2008-03-23 23:48:10 -04:00
|
|
|
init_common_fields();
|
2012-08-11 18:09:09 -04:00
|
|
|
|
|
|
|
Init_io_buffer(mPuma);
|
2012-08-23 01:12:12 -04:00
|
|
|
Init_mini_ssl(mPuma);
|
2006-01-28 14:03:53 -05:00
|
|
|
}
|