@@ -0,0 +1,93 @@
1+/*
2+ * This is a simple driver for Ryan Dahl's hand-bummed C http-parser
3+ * package. It is intended to read one HTTP request after another
4+ * from a file, nothing more.
5+ *
6+ * For the http-parser source, see http://github.com/ry/http-parser/
7+ */
8+#include <fcntl.h>
9+#include <stdio.h>
10+#include <sys/stat.h>
11+#include <sys/types.h>
12+#include <unistd.h>
13+
14+#include "http_parser.h"
15+
16+int begin(http_parser *p)
17+{
18+ return 0;
19+}
20+
21+int url(http_parser *p, const char *at, size_t len)
22+{
23+ return 0;
24+}
25+
26+int header_field(http_parser *p, const char *at, size_t len)
27+{
28+ return 0;
29+}
30+
31+int header_value(http_parser *p, const char *at, size_t len)
32+{
33+ return 0;
34+}
35+
36+int complete(http_parser *p)
37+{
38+ /* Bludgeon http_parser into understanding that we really want to
39+ * keep parsing after a request that in principle ought to close
40+ * the "connection". */
41+ if (!http_should_keep_alive(p)) {
42+ p->http_major = 1;
43+ p->http_minor = 1;
44+ p->flags &= ~6;
45+ }
46+ return 0;
47+}
48+
49+void parse(const char *path, int fd)
50+{
51+ http_parser p;
52+ ssize_t nread;
53+
54+ http_parser_init(&p, HTTP_REQUEST);
55+ p.on_message_begin = begin;
56+ p.on_url = url;
57+ p.on_header_field = header_field;
58+ p.on_header_value = header_value;
59+ p.on_message_complete = complete;
60+
61+ do {
62+ char buf[HTTP_MAX_HEADER_SIZE];
63+ size_t np;
64+
65+ nread = read(fd, buf, sizeof(buf));
66+
67+ np = http_parser_execute(&p, buf, nread);
68+ if (np != nread) {
69+ fprintf(stderr, "%s: parse failed\n", path);
70+ break;
71+ }
72+ } while (nread > 0);
73+}
74+
75+
76+int main(int argc, char **argv)
77+{
78+ int i;
79+
80+ for (i = 1; i < argc; i++) {
81+ int fd;
82+
83+ fd = open(argv[i], O_RDONLY);
84+ if (fd == -1) {
85+ perror(argv[i]);
86+ continue;
87+ }
88+ parse(argv[i], fd);
89+ close(fd);
90+ }
91+
92+ return 0;
93+}