1 /* Based on src/http/ngx_http_parse.c from NGINX copyright Igor Sysoev
3 * Additional changes are licensed under the same terms as NGINX and
4 * copyright Joyent, Inc. and other Node contributors. All rights reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 /* Dump what the parser finds to stdout as it happen */
27 #include "http_parser.h"
32 int on_message_begin(http_parser* _) {
34 printf("\n***MESSAGE BEGIN***\n\n");
38 int on_headers_complete(http_parser* _) {
40 printf("\n***HEADERS COMPLETE***\n\n");
44 int on_message_complete(http_parser* _) {
46 printf("\n***MESSAGE COMPLETE***\n\n");
50 int on_url(http_parser* _, const char* at, size_t length) {
52 printf("Url: %.*s\n", (int)length, at);
56 int on_header_field(http_parser* _, const char* at, size_t length) {
58 printf("Header field: %.*s\n", (int)length, at);
62 int on_header_value(http_parser* _, const char* at, size_t length) {
64 printf("Header value: %.*s\n", (int)length, at);
68 int on_body(http_parser* _, const char* at, size_t length) {
70 printf("Body: %.*s\n", (int)length, at);
74 void usage(const char* name) {
76 "Usage: %s $type $filename\n"
77 " type: -x, where x is one of {r,b,q}\n"
78 " parses file as a Response, reQuest, or Both\n",
83 int main(int argc, char* argv[]) {
84 enum http_parser_type file_type;
96 /* in the case of "-", type[1] will be NUL */
98 file_type = HTTP_RESPONSE;
101 file_type = HTTP_REQUEST;
104 file_type = HTTP_BOTH;
110 char* filename = argv[2];
111 FILE* file = fopen(filename, "r");
117 fseek(file, 0, SEEK_END);
118 long file_length = ftell(file);
119 if (file_length == -1) {
123 fseek(file, 0, SEEK_SET);
125 char* data = malloc(file_length);
126 if (fread(data, 1, file_length, file) != (size_t)file_length) {
127 fprintf(stderr, "couldn't read entire file\n");
132 http_parser_settings settings;
133 memset(&settings, 0, sizeof(settings));
134 settings.on_message_begin = on_message_begin;
135 settings.on_url = on_url;
136 settings.on_header_field = on_header_field;
137 settings.on_header_value = on_header_value;
138 settings.on_headers_complete = on_headers_complete;
139 settings.on_body = on_body;
140 settings.on_message_complete = on_message_complete;
143 http_parser_init(&parser, file_type);
144 size_t nparsed = http_parser_execute(&parser, &settings, data, file_length);
147 if (nparsed != (size_t)file_length) {
150 http_errno_description(HTTP_PARSER_ERRNO(&parser)),
151 http_errno_name(HTTP_PARSER_ERRNO(&parser)));