1 /* Based on src/http/ngx_http_parse.c from NGINX copyright Igor Sysoev
2 *
3 * Additional changes are licensed under the same terms as NGINX and
4 * copyright Joyent, Inc. and other Node contributors. All rights reserved.
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * IN THE SOFTWARE.
23 */
24
25 /* Dump what the parser finds to stdout as it happen */
26
27 #include "http_parser.h"
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
on_message_begin(http_parser * _)32 int on_message_begin(http_parser* _) {
33 (void)_;
34 printf("\n***MESSAGE BEGIN***\n\n");
35 return 0;
36 }
37
on_headers_complete(http_parser * _)38 int on_headers_complete(http_parser* _) {
39 (void)_;
40 printf("\n***HEADERS COMPLETE***\n\n");
41 return 0;
42 }
43
on_message_complete(http_parser * _)44 int on_message_complete(http_parser* _) {
45 (void)_;
46 printf("\n***MESSAGE COMPLETE***\n\n");
47 return 0;
48 }
49
on_url(http_parser * _,const char * at,size_t length)50 int on_url(http_parser* _, const char* at, size_t length) {
51 (void)_;
52 printf("Url: %.*s\n", (int)length, at);
53 return 0;
54 }
55
on_header_field(http_parser * _,const char * at,size_t length)56 int on_header_field(http_parser* _, const char* at, size_t length) {
57 (void)_;
58 printf("Header field: %.*s\n", (int)length, at);
59 return 0;
60 }
61
on_header_value(http_parser * _,const char * at,size_t length)62 int on_header_value(http_parser* _, const char* at, size_t length) {
63 (void)_;
64 printf("Header value: %.*s\n", (int)length, at);
65 return 0;
66 }
67
on_body(http_parser * _,const char * at,size_t length)68 int on_body(http_parser* _, const char* at, size_t length) {
69 (void)_;
70 printf("Body: %.*s\n", (int)length, at);
71 return 0;
72 }
73
usage(const char * name)74 void usage(const char* name) {
75 fprintf(stderr,
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",
79 name);
80 exit(EXIT_FAILURE);
81 }
82
main(int argc,char * argv[])83 int main(int argc, char* argv[]) {
84 enum http_parser_type file_type;
85
86 if (argc != 3) {
87 usage(argv[0]);
88 }
89
90 char* type = argv[1];
91 if (type[0] != '-') {
92 usage(argv[0]);
93 }
94
95 switch (type[1]) {
96 /* in the case of "-", type[1] will be NUL */
97 case 'r':
98 file_type = HTTP_RESPONSE;
99 break;
100 case 'q':
101 file_type = HTTP_REQUEST;
102 break;
103 case 'b':
104 file_type = HTTP_BOTH;
105 break;
106 default:
107 usage(argv[0]);
108 }
109
110 char* filename = argv[2];
111 FILE* file = fopen(filename, "r");
112 if (file == NULL) {
113 perror("fopen");
114 goto fail;
115 }
116
117 fseek(file, 0, SEEK_END);
118 long file_length = ftell(file);
119 if (file_length == -1) {
120 perror("ftell");
121 goto fail;
122 }
123 fseek(file, 0, SEEK_SET);
124
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");
128 free(data);
129 goto fail;
130 }
131
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;
141
142 http_parser parser;
143 http_parser_init(&parser, file_type);
144 size_t nparsed = http_parser_execute(&parser, &settings, data, file_length);
145 free(data);
146
147 if (nparsed != (size_t)file_length) {
148 fprintf(stderr,
149 "Error: %s (%s)\n",
150 http_errno_description(HTTP_PARSER_ERRNO(&parser)),
151 http_errno_name(HTTP_PARSER_ERRNO(&parser)));
152 goto fail;
153 }
154
155 return EXIT_SUCCESS;
156
157 fail:
158 fclose(file);
159 return EXIT_FAILURE;
160 }
161