Permalink
Browse files

Remove "ebb_message" object from libebb's parser.

And much more:
 * Begin the framework for parsing HTTP responses
 * Modify the test code with the new layout
 * Remove EBB_ prefix and use just HTTP_
 * Start simple new Makefile

Currently the test passes but it is only checking HTTP requests. I get the
feeling that it is much slower than before, although I have no solid
evidence for this.
  • Loading branch information...
0 parents commit 97d4572c6828a8d34259cae6e67e490b3877088c @ry ry committed Apr 25, 2009
Showing with 1,290 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +14 −0 Makefile
  3. +128 −0 http_parser.h
  4. +381 −0 http_parser.rl
  5. +763 −0 test.c
@@ -0,0 +1,4 @@
+tags
+*.o
+test
+http_parser.c
@@ -0,0 +1,14 @@
+test: http_parser.o test.c
+ gcc -g -O2 $^ -o $@
+
+http_parser.o: http_parser.c http_parser.h Makefile
+ gcc -g -c -O2 $<
+
+http_parser.c: http_parser.rl Makefile
+ ragel -s -G2 $< -o $@
+
+clean:
+ rm -f *.o http_parser.c test
+
+.PHONY: clean
+
@@ -0,0 +1,128 @@
+/* Copyright (c) 2008 Ryan Dahl (ry@tinyclouds.org)
+ * All rights reserved.
+ *
+ * This parser is based on code from Zed Shaw's Mongrel.
+ * Copyright (c) 2005 Zed A. Shaw
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#ifndef http_parser_h
+#define http_parser_h
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <sys/types.h>
+
+typedef struct http_parser http_parser;
+
+/* Callbacks should return non-zero to indicate an error. The parse will
+ * then halt execution.
+ *
+ * http_data_cb does not return data chunks. It will be call arbitrarally
+ * many times for each string. E.G. you might get 10 callbacks for "on_path"
+ * each providing just a few characters more data.
+ */
+typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);
+typedef int (*http_cb) (http_parser*);
+
+/* Request Methods */
+#define HTTP_COPY 0x0001
+#define HTTP_DELETE 0x0002
+#define HTTP_GET 0x0004
+#define HTTP_HEAD 0x0008
+#define HTTP_LOCK 0x0010
+#define HTTP_MKCOL 0x0020
+#define HTTP_MOVE 0x0040
+#define HTTP_OPTIONS 0x0080
+#define HTTP_POST 0x0100
+#define HTTP_PROPFIND 0x0200
+#define HTTP_PROPPATCH 0x0400
+#define HTTP_PUT 0x0800
+#define HTTP_TRACE 0x1000
+#define HTTP_UNLOCK 0x2000
+/* Transfer Encodings */
+#define HTTP_IDENTITY 0x01
+#define HTTP_CHUNKED 0x02
+
+struct http_parser {
+ /** PRIVATE **/
+ int cs;
+ int is_request_stream;
+
+ size_t chunk_size;
+ unsigned eating:1;
+ size_t body_read;
+
+ const char *header_field_mark;
+ const char *header_value_mark;
+ const char *query_string_mark;
+ const char *path_mark;
+ const char *uri_mark;
+ const char *fragment_mark;
+
+ /** READ-ONLY **/
+ unsigned int status_code; /* responses only */
+ unsigned int method; /* requests only */
+ int transfer_encoding;
+ unsigned int version_major;
+ unsigned int version_minor;
+ unsigned int number_of_headers;
+ int keep_alive;
+ size_t content_length;
+
+ /** PUBLIC **/
+ void *data; /* A pointer to get hook to the "connection" or "socket" object */
+
+ /* an ordered list of callbacks */
+
+ http_cb on_message_begin;
+
+ /* requests only */
+ http_data_cb on_path;
+ http_data_cb on_query_string;
+ http_data_cb on_uri;
+ http_data_cb on_fragment;
+
+ http_data_cb on_header_field;
+ http_data_cb on_header_value;
+ http_cb on_headers_complete;
+ http_data_cb on_body;
+ http_cb on_message_complete;
+};
+
+/* Initializes an http_parser structure. The second argument specifies if
+ * it will be parsing requests or responses. Set the second argument to 1
+ * for requests; 0 for responses.
+ */
+void http_parser_init (http_parser *parser, int is_request_stream);
+
+size_t http_parser_execute (http_parser *parser, const char *data, size_t len);
+
+int http_parser_has_error (http_parser *parser);
+
+int http_message_should_keep_alive (http_parser *message);
+#define http_message_has_body(parser) \
+ (parser->transfer_encoding == HTTP_CHUNKED || parser->content_length > 0 )
+
+#ifdef __cplusplus
+}
+#endif
+#endif
Oops, something went wrong. Retry.

0 comments on commit 97d4572

Please sign in to comment.