141 } |
141 } |
142 |
142 |
143 void* basic_run_session(void *data) { |
143 void* basic_run_session(void *data) { |
144 Connection *conn = (Connection*)data; |
144 Connection *conn = (Connection*)data; |
145 |
145 |
146 HTTPRequest request; |
146 HTTPRequest *request = malloc(sizeof(HTTPRequest)); |
147 http_request_init(&request); |
147 http_request_init(request); |
148 request.connection = conn; |
148 request->connection = conn; |
149 |
149 |
150 // read request |
150 // read request |
151 netbuf *buf = malloc(sizeof(netbuf)); |
151 netbuf *buf = malloc(sizeof(netbuf)); |
152 buf->rdtimeout = 120; |
152 buf->rdtimeout = 120; |
153 buf->pos = 0; |
153 buf->pos = 0; |
155 buf->maxsize = 2048; |
155 buf->maxsize = 2048; |
156 buf->sd = &conn->fd; |
156 buf->sd = &conn->fd; |
157 buf->inbuf = malloc(2048); |
157 buf->inbuf = malloc(2048); |
158 buf->errmsg = NULL; |
158 buf->errmsg = NULL; |
159 |
159 |
160 request.netbuf = buf; |
160 request->netbuf = buf; |
161 |
161 |
162 HttpParser *parser = http_parser_new(&request); |
162 HttpParser *parser = http_parser_new(request); |
163 int state; |
163 int state; |
164 int r; |
164 int r; |
165 r = conn->read(conn, buf->inbuf + buf->pos, buf->maxsize - buf->pos); |
165 r = conn->read(conn, buf->inbuf + buf->pos, buf->maxsize - buf->pos); |
166 if(r <= 0) { |
166 |
167 // TODO: error handling |
167 if(r > 0) { |
168 fprintf(stderr, "%s\n", "Error: Cannot read from socket"); |
168 int err = 0; |
169 return NULL; |
|
170 } |
|
171 buf->cursize += r; |
|
172 while((state = http_parser_process(parser)) != 0) { |
|
173 if(state == 2) { |
|
174 // TODO: error handling |
|
175 fprintf(stderr, "%s\n", "Error: Cannot parse http request"); |
|
176 return NULL; |
|
177 } |
|
178 r = conn->read(conn, buf->inbuf + buf->pos, buf->maxsize - buf->pos); |
|
179 if(r == -1) { |
|
180 // TODO: error handling |
|
181 fprintf(stderr, "%s\n", "Error: Cannot read from socket"); |
|
182 return NULL; |
|
183 } |
|
184 buf->cursize += r; |
169 buf->cursize += r; |
185 } |
170 while((state = http_parser_process(parser)) != 0) { |
186 if(!http_parser_validate(parser)) { |
171 if(state == 2) { |
187 log_ereport(LOG_FAILURE, "http_parser_validate failed"); |
172 log_ereport(LOG_FAILURE, "basic_run_session: invalid http request"); |
188 // TODO: send error 400 bad request |
173 err = 1; |
189 return NULL; |
174 break; |
190 } |
175 } |
191 |
176 r = conn->read(conn, buf->inbuf + buf->pos, buf->maxsize - buf->pos); |
192 // process request |
177 if(r == -1) { |
193 r = handle_request(&request, NULL, NULL); // TODO: use correct thread pool |
178 log_ereport(LOG_FAILURE, "basic_run_session: IO error: %s", strerror(errno)); |
194 |
179 err = 1; |
195 // TODO: free, see evt_request_finish |
180 break; |
196 |
181 } |
|
182 buf->cursize += r; |
|
183 } |
|
184 |
|
185 if(!err) { |
|
186 if(http_parser_validate(parser)) { |
|
187 // process request |
|
188 r = handle_request(request, NULL, NULL); // TODO: use correct thread pool |
|
189 } else { |
|
190 log_ereport(LOG_FAILURE, "basic_run_session: http parser validation failed"); |
|
191 fatal_error(request, 400); |
|
192 } |
|
193 } |
|
194 } else { |
|
195 log_ereport(LOG_FAILURE, "basic_run_session: IO error: %s", strerror(errno)); |
|
196 } |
|
197 |
|
198 free(buf->inbuf); |
|
199 free(buf); |
|
200 connection_destroy(conn); |
|
201 http_parser_free(parser); |
|
202 http_request_cleanup(request); |
|
203 |
197 return NULL; |
204 return NULL; |
198 } |
205 } |
199 |
206 |
200 void basic_keep_alive(SessionHandler *handler, Connection *conn) { |
207 void basic_keep_alive(SessionHandler *handler, Connection *conn) { |
201 connection_destroy(conn); |
208 connection_destroy(conn); |