http11: minor cleanups in return types
[unicorn.git] / ext / unicorn / http11 / http11_parser.rl
blob991ba58d4d438bd8d5703b183a5b0f92321424a8
1 /**
2  * Copyright (c) 2005 Zed A. Shaw
3  * You can redistribute it and/or modify it under the same terms as Ruby.
4  */
5 #ifndef http11_parser_h
6 #define http11_parser_h
8 #include <sys/types.h>
10 static void http_field(void *data, const char *field,
11                        size_t flen, const char *value, size_t vlen);
12 static void request_method(void *data, const char *at, size_t length);
13 static void request_uri(void *data, const char *at, size_t length);
14 static void fragment(void *data, const char *at, size_t length);
15 static void request_path(void *data, const char *at, size_t length);
16 static void query_string(void *data, const char *at, size_t length);
17 static void http_version(void *data, const char *at, size_t length);
18 static void header_done(void *data, const char *at, size_t length);
20 typedef struct http_parser {
21   int cs;
22   size_t body_start;
23   size_t nread;
24   size_t mark;
25   size_t field_start;
26   size_t field_len;
27   size_t query_start;
29   void *data;
30 } http_parser;
32 static int http_parser_has_error(http_parser *parser);
33 static int http_parser_is_finished(http_parser *parser);
36  * capitalizes all lower-case ASCII characters,
37  * converts dashes to underscores.
38  */
39 static void snake_upcase_char(char *c)
41   if (*c >= 'a' && *c <= 'z')
42     *c &= ~0x20;
43   else if (*c == '-')
44     *c = '_';
47 static void downcase_char(char *c)
49   if (*c >= 'A' && *c <= 'Z')
50     *c |= 0x20;
53 #define LEN(AT, FPC) (FPC - buffer - parser->AT)
54 #define MARK(M,FPC) (parser->M = (FPC) - buffer)
55 #define PTR_TO(F) (buffer + parser->F)
57 /** Machine **/
59 %%{
60   machine http_parser;
62   action mark {MARK(mark, fpc); }
64   action start_field { MARK(field_start, fpc); }
65   action snake_upcase_field { snake_upcase_char((char *)fpc); }
66   action downcase_char { downcase_char((char *)fpc); }
67   action write_field {
68     parser->field_len = LEN(field_start, fpc);
69   }
71   action start_value { MARK(mark, fpc); }
72   action write_value {
73     http_field(parser->data, PTR_TO(field_start), parser->field_len, PTR_TO(mark), LEN(mark, fpc));
74   }
75   action request_method {
76     request_method(parser->data, PTR_TO(mark), LEN(mark, fpc));
77   }
78   action request_uri {
79     request_uri(parser->data, PTR_TO(mark), LEN(mark, fpc));
80   }
81   action fragment {
82     fragment(parser->data, PTR_TO(mark), LEN(mark, fpc));
83   }
85   action start_query {MARK(query_start, fpc); }
86   action query_string {
87     query_string(parser->data, PTR_TO(query_start), LEN(query_start, fpc));
88   }
90   action http_version {
91     http_version(parser->data, PTR_TO(mark), LEN(mark, fpc));
92   }
94   action request_path {
95     request_path(parser->data, PTR_TO(mark), LEN(mark,fpc));
96   }
98   action done {
99     parser->body_start = fpc - buffer + 1;
100     header_done(parser->data, fpc + 1, pe - fpc - 1);
101     fbreak;
102   }
104   include http_parser_common "http11_parser_common.rl";
107 /** Data **/
108 %% write data;
110 static void http_parser_init(http_parser *parser) {
111   int cs = 0;
112   memset(parser, 0, sizeof(*parser));
113   %% write init;
114   parser->cs = cs;
117 /** exec **/
118 static void http_parser_execute(
119   http_parser *parser, const char *buffer, size_t len)
121   const char *p, *pe;
122   int cs = parser->cs;
123   size_t off = parser->nread;
125   assert(off <= len && "offset past end of buffer");
127   p = buffer+off;
128   pe = buffer+len;
130   assert(*pe == '\0' && "pointer does not end on NUL");
131   assert(pe - p == len - off && "pointers aren't same distance");
133   %% write exec;
135   if (!http_parser_has_error(parser))
136     parser->cs = cs;
137   parser->nread += p - (buffer + off);
139   assert(p <= pe && "buffer overflow after parsing execute");
140   assert(parser->nread <= len && "nread longer than length");
141   assert(parser->body_start <= len && "body starts after buffer end");
142   assert(parser->mark < len && "mark is after buffer end");
143   assert(parser->field_len <= len && "field has length longer than whole buffer");
144   assert(parser->field_start < len && "field starts after buffer end");
147 static int http_parser_has_error(http_parser *parser) {
148   return parser->cs == http_parser_error;
151 static int http_parser_is_finished(http_parser *parser) {
152   return parser->cs == http_parser_first_final;
154 #endif /* http11_parser_h */