2 * Copyright (c) 2005 Zed A. Shaw
3 * You can redistribute it and/or modify it under the same terms as Ruby.
5 #include "http11_parser.h"
11 * capitalizes all lower-case ASCII characters,
12 * converts dashes to underscores.
14 static void snake_upcase_char(char *c)
16 if (*c >= 'a' && *c <= 'z')
22 static void downcase_char(char *c)
24 if (*c >= 'A' && *c <= 'Z')
28 #define LEN(AT, FPC) (FPC - buffer - parser->AT)
29 #define MARK(M,FPC) (parser->M = (FPC) - buffer)
30 #define PTR_TO(F) (buffer + parser->F)
37 action mark {MARK(mark, fpc); }
39 action start_field { MARK(field_start, fpc); }
40 action snake_upcase_field { snake_upcase_char((char *)fpc); }
41 action downcase_char { downcase_char((char *)fpc); }
43 parser->field_len = LEN(field_start, fpc);
46 action start_value { MARK(mark, fpc); }
48 if(parser->http_field != NULL) {
49 parser->http_field(parser->data, PTR_TO(field_start), parser->field_len, PTR_TO(mark), LEN(mark, fpc));
52 action request_method {
53 if(parser->request_method != NULL)
54 parser->request_method(parser->data, PTR_TO(mark), LEN(mark, fpc));
57 if(parser->request_uri != NULL)
58 parser->request_uri(parser->data, PTR_TO(mark), LEN(mark, fpc));
61 if(parser->fragment != NULL)
62 parser->fragment(parser->data, PTR_TO(mark), LEN(mark, fpc));
65 action start_query {MARK(query_start, fpc); }
67 if(parser->query_string != NULL)
68 parser->query_string(parser->data, PTR_TO(query_start), LEN(query_start, fpc));
72 if(parser->http_version != NULL)
73 parser->http_version(parser->data, PTR_TO(mark), LEN(mark, fpc));
77 if(parser->request_path != NULL)
78 parser->request_path(parser->data, PTR_TO(mark), LEN(mark,fpc));
82 parser->body_start = fpc - buffer + 1;
83 if(parser->header_done != NULL)
84 parser->header_done(parser->data, fpc + 1, pe - fpc - 1);
88 include http_parser_common "http11_parser_common.rl";
94 int http_parser_init(http_parser *parser) {
98 parser->body_start = 0;
101 parser->field_len = 0;
102 parser->field_start = 0;
108 size_t http_parser_execute(http_parser *parser, const char *buffer, size_t len) {
111 size_t off = parser->nread;
113 assert(off <= len && "offset past end of buffer");
118 assert(*pe == '\0' && "pointer does not end on NUL");
119 assert(pe - p == len - off && "pointers aren't same distance");
123 if (!http_parser_has_error(parser))
125 parser->nread += p - (buffer + off);
127 assert(p <= pe && "buffer overflow after parsing execute");
128 assert(parser->nread <= len && "nread longer than length");
129 assert(parser->body_start <= len && "body starts after buffer end");
130 assert(parser->mark < len && "mark is after buffer end");
131 assert(parser->field_len <= len && "field has length longer than whole buffer");
132 assert(parser->field_start < len && "field starts after buffer end");
134 return(parser->nread);
137 int http_parser_finish(http_parser *parser)
139 if (http_parser_has_error(parser) ) {
141 } else if (http_parser_is_finished(parser) ) {
148 int http_parser_has_error(http_parser *parser) {
149 return parser->cs == http_parser_error;
152 int http_parser_is_finished(http_parser *parser) {
153 return parser->cs == http_parser_first_final;