src/server/httpparser.c

changeset 1
3c066d52342d
equal deleted inserted replaced
0:4c89c7683fb6 1:3c066d52342d
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2011 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 #include "httpparser.h"
33 #include "nsapi.h"
34 //include "request.h"
35
36
37 HttpParser* http_parser_new(HTTPRequest *request) {
38 HttpParser *parser = malloc(sizeof(HttpParser));
39 parser->request = request;
40
41 parser->state = 0;
42 parser->start_line.ptr = (char*)request->netbuf->inbuf;
43 parser->start_line.length = 0;
44
45 return parser;
46 }
47
48 void http_parser_free(HttpParser *parser) {
49 free(parser);
50 }
51
52 int http_parser_process(HttpParser *parser) {
53 switch(parser->state) {
54 case 0: {
55 int r = get_start_line(parser);
56 switch(r) {
57 case 0: break;
58 default: return r;
59 }
60 parse_request_line(parser);
61 parser->state++;
62 }
63 case 1: {
64 return http_parser_parse_header(parser);
65 }
66 case 2: {
67 return 0;
68 }
69 }
70 }
71
72 int get_start_line(HttpParser *parser) {
73 netbuf *buf = parser->request->netbuf;
74 while(buf->pos < buf->cursize) {
75 unsigned char c = buf->inbuf[buf->pos];
76 if(c == '\n') {
77 if(buf->pos <= 1) {
78 // insufficient chars for request, return error
79 return 2;
80 }
81 if(buf->inbuf[buf->pos - 1] == '\r') {
82 parser->start_line.length = buf->pos;
83 } else {
84 parser->start_line.length = buf->pos + 1;
85 }
86 parser->start_line.ptr = (char*)buf->inbuf;
87 buf->pos++;
88 return 0;
89 }
90 buf->pos++;
91 }
92 return 1;
93 }
94
95 int http_parser_parse_header(HttpParser *parser) {
96 netbuf *buf = parser->request->netbuf;
97
98 parser->offset = buf->pos; // line offset
99 parser->name.ptr = NULL;
100 parser->value.ptr = NULL;
101 while(1) {
102 if(buf->pos >= buf->cursize) {
103 return 1;
104 }
105 char c = (char)buf->inbuf[buf->pos++];
106
107 if(c > 32) {
108 parser->wl = 0;
109 if(c == ':' && parser->value.ptr == NULL) {
110 parser->name.ptr = (char*)buf->inbuf + parser->offset;
111 buf->inbuf[buf->pos-1] = 0;
112 } else if(parser->name.ptr != NULL && parser->value.ptr == NULL) {
113 parser->value.ptr = (char*)buf->inbuf + buf->pos - 1;
114 }
115 } else if(c == '\n') {
116 if(parser->wl) {
117 // line contains only white space -> end of request
118 parser->state++;
119 return 0;
120 } else {
121 parser->offset = buf->pos;
122 if(parser->value.ptr != NULL) {
123 buf->inbuf[buf->pos-1] = 0;
124 // add header
125 header_add(
126 parser->request->headers,
127 parser->name.ptr,
128 parser->value.ptr);
129 } else {
130 // error: no value
131 return 2;
132 }
133 parser->name.ptr = NULL;
134 parser->value.ptr = NULL;
135 parser->wl = 1;
136 }
137 }
138 }
139 }
140
141 int parse_request_line(HttpParser *parser) {
142 sstr_t line = parser->start_line;
143 parser->request->request_line = line;
144
145 /*
146 * parse method, url and http version
147 */
148
149 int i = 0;
150 int ns = 0;
151
152 parser->request->method.ptr = line.ptr;
153 for(;i<line.length;i++) {
154 if(!ns && line.ptr[i] == ' ') {
155 ns = 1;
156 //line.ptr[i] = 0; // TODO: remove
157 parser->request->method.length = i;
158 } else if(ns) {
159 if(line.ptr[i] != ' ') {
160 break;
161 }
162 }
163 }
164
165 parser->request->uri.ptr = line.ptr + i;
166 ns = 0;
167 int s = i;
168 for(;i<line.length;i++) {
169 if(!ns && line.ptr[i] < 33) {
170 ns = 1;
171 //line.ptr[i] = 0; // TODO: remove
172 parser->request->uri.length = i - s;
173 } else if(ns) {
174 if(line.ptr[i] > 32) {
175 break;
176 }
177 }
178 }
179
180 parser->request->httpv.ptr = line.ptr + i;
181 ns = 0;
182 s = i;
183 for(;i<line.length;i++) {
184 if(!ns && line.ptr[i] < 33) {
185 ns = 1;
186 //line.ptr[i] = 0; // TODO: remove
187 parser->request->httpv.length = i - s;
188 } else if(ns) {
189 if(line.ptr[i] > 32) {
190 break;
191 }
192 }
193 }
194
195 return 0;
196 }

mercurial