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.
|
|
|
|
*/
|
2006-01-28 14:03:53 -05:00
|
|
|
#include "http11_parser.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2008-03-23 23:48:10 -04:00
|
|
|
/*
|
|
|
|
* 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 = '_';
|
|
|
|
}
|
|
|
|
|
2006-05-14 19:41:22 -04:00
|
|
|
#define LEN(AT, FPC) (FPC - buffer - parser->AT)
|
|
|
|
#define MARK(M,FPC) (parser->M = (FPC) - buffer)
|
|
|
|
#define PTR_TO(F) (buffer + parser->F)
|
2006-01-28 14:03:53 -05:00
|
|
|
|
2007-10-17 03:03:35 -04:00
|
|
|
/** Machine **/
|
2006-01-28 14:03:53 -05:00
|
|
|
|
2007-10-17 03:05:32 -04:00
|
|
|
%%{
|
|
|
|
|
2013-07-18 14:22:18 -04:00
|
|
|
machine puma_parser;
|
2007-10-17 03:05:32 -04:00
|
|
|
|
2011-09-15 01:07:27 -04:00
|
|
|
action mark { MARK(mark, fpc); }
|
2007-10-17 03:05:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
action start_field { MARK(field_start, fpc); }
|
2009-07-07 00:25:24 -04:00
|
|
|
action snake_upcase_field { snake_upcase_char((char *)fpc); }
|
2007-10-17 03:05:32 -04:00
|
|
|
action write_field {
|
|
|
|
parser->field_len = LEN(field_start, fpc);
|
|
|
|
}
|
|
|
|
|
|
|
|
action start_value { MARK(mark, fpc); }
|
2009-07-07 00:25:24 -04:00
|
|
|
action write_value {
|
2011-09-15 01:07:27 -04:00
|
|
|
parser->http_field(parser, PTR_TO(field_start), parser->field_len, PTR_TO(mark), LEN(mark, fpc));
|
2007-10-17 03:05:32 -04:00
|
|
|
}
|
|
|
|
action request_method {
|
2011-09-15 01:07:27 -04:00
|
|
|
parser->request_method(parser, PTR_TO(mark), LEN(mark, fpc));
|
2007-10-17 03:05:32 -04:00
|
|
|
}
|
|
|
|
action request_uri {
|
2011-09-15 01:07:27 -04:00
|
|
|
parser->request_uri(parser, PTR_TO(mark), LEN(mark, fpc));
|
2007-10-17 03:05:32 -04:00
|
|
|
}
|
2009-07-07 00:25:24 -04:00
|
|
|
action fragment {
|
2011-09-15 01:07:27 -04:00
|
|
|
parser->fragment(parser, PTR_TO(mark), LEN(mark, fpc));
|
2007-10-17 03:05:32 -04:00
|
|
|
}
|
|
|
|
|
2011-09-15 01:07:27 -04:00
|
|
|
action start_query { MARK(query_start, fpc); }
|
2007-10-17 03:05:32 -04:00
|
|
|
action query_string {
|
2011-09-15 01:07:27 -04:00
|
|
|
parser->query_string(parser, PTR_TO(query_start), LEN(query_start, fpc));
|
2007-10-17 03:05:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
action http_version {
|
2011-09-15 01:07:27 -04:00
|
|
|
parser->http_version(parser, PTR_TO(mark), LEN(mark, fpc));
|
2007-10-17 03:05:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
action request_path {
|
2011-09-15 01:07:27 -04:00
|
|
|
parser->request_path(parser, PTR_TO(mark), LEN(mark,fpc));
|
2007-10-17 03:05:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
action done {
|
|
|
|
parser->body_start = fpc - buffer + 1;
|
2011-09-15 01:07:27 -04:00
|
|
|
parser->header_done(parser, fpc + 1, pe - fpc - 1);
|
2007-10-17 03:05:32 -04:00
|
|
|
fbreak;
|
|
|
|
}
|
|
|
|
|
2013-07-18 14:22:18 -04:00
|
|
|
include puma_parser_common "http11_parser_common.rl";
|
2007-10-17 03:10:43 -04:00
|
|
|
|
2007-10-17 03:05:32 -04:00
|
|
|
}%%
|
|
|
|
|
2006-01-28 14:03:53 -05:00
|
|
|
/** Data **/
|
|
|
|
%% write data;
|
|
|
|
|
2013-07-18 14:22:18 -04:00
|
|
|
int puma_parser_init(puma_parser *parser) {
|
2006-07-14 12:13:51 -04:00
|
|
|
int cs = 0;
|
|
|
|
%% write init;
|
|
|
|
parser->cs = cs;
|
|
|
|
parser->body_start = 0;
|
|
|
|
parser->content_len = 0;
|
|
|
|
parser->mark = 0;
|
|
|
|
parser->nread = 0;
|
|
|
|
parser->field_len = 0;
|
2011-09-15 01:07:27 -04:00
|
|
|
parser->field_start = 0;
|
|
|
|
parser->request = Qnil;
|
2011-09-18 16:02:34 -04:00
|
|
|
parser->body = Qnil;
|
2006-07-14 12:13:51 -04:00
|
|
|
|
2011-09-15 01:07:27 -04:00
|
|
|
return 1;
|
2006-01-28 14:03:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** exec **/
|
2013-07-18 14:22:18 -04:00
|
|
|
size_t puma_parser_execute(puma_parser *parser, const char *buffer, size_t len, size_t off) {
|
2006-07-14 12:13:51 -04:00
|
|
|
const char *p, *pe;
|
|
|
|
int cs = parser->cs;
|
2006-01-28 14:03:53 -05:00
|
|
|
|
2006-07-14 12:13:51 -04:00
|
|
|
assert(off <= len && "offset past end of buffer");
|
2006-05-14 19:41:22 -04:00
|
|
|
|
2006-07-14 12:13:51 -04:00
|
|
|
p = buffer+off;
|
|
|
|
pe = buffer+len;
|
2006-01-28 14:03:53 -05:00
|
|
|
|
2008-03-23 23:48:10 -04:00
|
|
|
/* assert(*pe == '\0' && "pointer does not end on NUL"); */
|
2006-07-14 12:13:51 -04:00
|
|
|
assert(pe - p == len - off && "pointers aren't same distance");
|
2006-05-14 19:41:22 -04:00
|
|
|
|
2006-07-14 12:13:51 -04:00
|
|
|
%% write exec;
|
2006-01-28 14:03:53 -05:00
|
|
|
|
2013-07-18 14:22:18 -04:00
|
|
|
if (!puma_parser_has_error(parser))
|
2009-07-07 00:25:24 -04:00
|
|
|
parser->cs = cs;
|
2006-07-14 12:13:51 -04:00
|
|
|
parser->nread += p - (buffer + off);
|
2006-05-14 19:41:22 -04:00
|
|
|
|
2006-07-14 12:13:51 -04:00
|
|
|
assert(p <= pe && "buffer overflow after parsing execute");
|
|
|
|
assert(parser->nread <= len && "nread longer than length");
|
|
|
|
assert(parser->body_start <= len && "body starts after buffer end");
|
|
|
|
assert(parser->mark < len && "mark is after buffer end");
|
|
|
|
assert(parser->field_len <= len && "field has length longer than whole buffer");
|
|
|
|
assert(parser->field_start < len && "field starts after buffer end");
|
2006-05-14 19:41:22 -04:00
|
|
|
|
2006-07-14 12:13:51 -04:00
|
|
|
return(parser->nread);
|
2006-01-28 14:03:53 -05:00
|
|
|
}
|
|
|
|
|
2013-07-18 14:22:18 -04:00
|
|
|
int puma_parser_finish(puma_parser *parser)
|
2006-01-28 14:03:53 -05:00
|
|
|
{
|
2013-07-18 14:22:18 -04:00
|
|
|
if (puma_parser_has_error(parser) ) {
|
2006-07-14 12:13:51 -04:00
|
|
|
return -1;
|
2013-07-18 14:22:18 -04:00
|
|
|
} else if (puma_parser_is_finished(parser) ) {
|
2006-07-14 12:13:51 -04:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2006-01-28 14:03:53 -05:00
|
|
|
}
|
|
|
|
|
2013-07-18 14:22:18 -04:00
|
|
|
int puma_parser_has_error(puma_parser *parser) {
|
|
|
|
return parser->cs == puma_parser_error;
|
2006-01-28 14:03:53 -05:00
|
|
|
}
|
|
|
|
|
2013-07-18 14:22:18 -04:00
|
|
|
int puma_parser_is_finished(puma_parser *parser) {
|
|
|
|
return parser->cs >= puma_parser_first_final;
|
2006-01-28 14:03:53 -05:00
|
|
|
}
|