My Project
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
request_parser.c
Go to the documentation of this file.
1 /*
2 START OF LICENSE STUB
3  DeDOS: Declarative Dispersion-Oriented Software
4  Copyright (C) 2017 University of Pennsylvania, Georgetown University
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 END OF LICENSE STUB
19 */
21 #include "logging.h"
22 
23 #ifdef __GNUC__
24 #define UNUSED __attribute__ ((unused))
25 #else
26 #define UNUSED
27 #endif
28 
29 static int url_callback(http_parser *parser, const char *at, size_t length) {
30  struct parser_state *state = parser->data;
31  strncpy(&state->url[state->url_len], at, length);
32  state->url[state->url_len + length] = '\0';
33  state->url_len += length;
34  log(LOG_HTTP_PARSING, "Got URL: %s", state->url);
35  return 0;
36 }
37 
39  struct parser_state *state = parser->data;
40  state->headers_complete = 1;
41  log(LOG_HTTP_PARSING, "Got end of headers");
42  return 0;
43 }
44 
46  const char UNUSED *at,
47  size_t UNUSED length) {
48 #if LOG_HTTP_PARSING
49  char cpy[length+1];
50  strncpy(cpy, at, length);
51  cpy[length] = '\0'
52  log(LOG_HTTP_PARSING, "Got header field %s", cpy);
53 #endif
54  return 0;
55 }
56 
57 
59  memset(state, 0, sizeof(*state));
60  state->settings.on_url = url_callback;
65  state->parser.data = (void*)state;
66 }
67 
68 int parse_http(struct parser_state *state, char *buf, ssize_t bytes) {
69  if (state == NULL) {
70  log_error("Cannot handle connection with NULL state");
71  return WS_ERROR;
72  }
73 
74  if (state->headers_complete) {
75  log_warn("Parsing even though header is already complete");
76  }
77 
78  size_t nparsed = http_parser_execute(&state->parser, &state->settings,
79  buf, bytes);
80 
81  if (nparsed != bytes) {
82  buf[bytes] = '\0';
83  log_error("Error parsing HTTP request %s (rcvd: %d, parsed: %d)",
84  buf, (int)bytes, (int)nparsed);
85  return WS_ERROR;
86  }
87 
88  return state->headers_complete && state->url_len ? WS_COMPLETE : WS_INCOMPLETE_READ;
89 }
90 
http_data_cb on_header_field
Definition: http_parser.h:342
int parse_http(struct parser_state *state, char *buf, ssize_t bytes)
http_cb on_headers_complete
Definition: http_parser.h:344
static int header_field_or_value_callback(http_parser *parser, const char *at, size_t length)
size_t http_parser_execute(http_parser *parser, const http_parser_settings *settings, const char *data, size_t len)
Definition: http_parser.c:653
http_parser parser
#define WS_INCOMPLETE_READ
Definition: webserver.h:25
char url[256]
Logging of status messages to the terminal.
http_data_cb on_url
Definition: http_parser.h:340
#define UNUSED
static int headers_complete_callback(http_parser *parser)
#define log_error(fmt,...)
Definition: logging.h:101
http_parser_settings settings
void init_parser_state(struct parser_state *state)
void http_parser_init(http_parser *parser, enum http_parser_type t)
Definition: http_parser.c:2105
http_data_cb on_header_value
Definition: http_parser.h:343
void * data
PUBLIC.
Definition: http_parser.h:334
#define WS_COMPLETE
Definition: webserver.h:24
static int url_callback(http_parser *parser, const char *at, size_t length)
#define WS_ERROR
Definition: webserver.h:27
#define log(level, fmt,...)
Log at a custom level.
Definition: logging.h:147
#define log_warn(fmt,...)
Definition: logging.h:113
state
Definition: http_parser.c:298