Bryan O'Sullivan avatar Bryan O'Sullivan committed b2c93d2

Slight performance and functionality improvements in the C code.

Comments (0)

Files changed (1)

examples/rfc2616.c

 #include <unistd.h>
 
 #include "http_parser.h"
-
+    
 struct http_string {
     size_t len;
     char value[0];
     struct http_string *uri;
     struct http_header *headers, *last;
 };
+
+struct data {
+    size_t count;
+    struct http_request req;
+};
   
 static void *xmalloc(size_t size)
 {
 
 static int begin(http_parser *p)
 {
-#ifdef LOOK_BUSY
-    struct http_request *req = xmalloc(sizeof(*req));
+    struct data *data = p->data;
 
-    req->method = NULL;
-    req->uri = NULL;
-    req->headers = NULL;
-    req->last = NULL;
-
-    p->data = req;
-#endif
+    data->count++;
 
     return 0;
 }
 static int url(http_parser *p, const char *at, size_t len)
 {
 #ifdef LOOK_BUSY
-    struct http_request *req = p->data;    
+    struct data *data = p->data;    
 
-    xstrcat(&req->uri, at, len);
+    xstrcat(&data->req.uri, at, len);
 #endif
 
     return 0;
 static int header_field(http_parser *p, const char *at, size_t len)
 {
 #ifdef LOOK_BUSY
-    struct http_request *req = p->data;
+    struct data *data = p->data;
 
-    if (req->last && req->last->value == NULL) {
-	xstrcat(&req->last->name, at, len);
+    if (data->req.last && data->req.last->value == NULL) {
+	xstrcat(&data->req.last->name, at, len);
     } else {
 	struct http_header *hdr = xmalloc(sizeof(*hdr));
 
 	hdr->value = NULL;
 	hdr->next = NULL;
     
-	if (req->last)
-	    req->last->next = hdr;
-	req->last = hdr;
-	if (req->headers == NULL)
-	    req->headers = hdr;
+	if (data->req.last != NULL)
+	    data->req.last->next = hdr;
+	data->req.last = hdr;
+	if (data->req.headers == NULL)
+	    data->req.headers = hdr;
     }
 #endif
 
 static int header_value(http_parser *p, const char *at, size_t len)
 {
 #ifdef LOOK_BUSY
-    struct http_request *req = p->data;
+    struct data *data = p->data;
 
-    xstrcat(&req->last->value, at, len);
+    xstrcat(&data->req.last->value, at, len);
 #endif
 
     return 0;
 static int complete(http_parser *p)
 {
 #ifdef LOOK_BUSY
-    struct http_request *req = p->data;
+    struct data *data = p->data;
     struct http_header *hdr, *next;
 
-    free(req->method);
-    free(req->uri);
+    free(data->req.method);
+    free(data->req.uri);
 	
-    for (hdr = req->headers; hdr != NULL; hdr = next) {
+    for (hdr = data->req.headers; hdr != NULL; hdr = next) {
 	next = hdr->next;
 	free(hdr->name);
 	free(hdr->value);
 	hdr = next;
     }
 
-    free(req);
+    data->req.method = NULL;
+    data->req.uri = NULL;
+    data->req.headers = NULL;
+    data->req.last = NULL;
 #endif
     
     /* Bludgeon http_parser into understanding that we really want to
 
 static void parse(const char *path, int fd)
 {
+    struct data data;
     http_parser p;
     ssize_t nread;
 
     p.on_header_field = header_field;
     p.on_header_value = header_value;
     p.on_message_complete = complete;
+    p.data = &data;
+    data.count = 0;
+    data.req.method = NULL;
+    data.req.uri = NULL;
+    data.req.headers = NULL;
+    data.req.last = NULL;
 
     do {
 	char buf[HTTP_MAX_HEADER_SIZE];
 	    break;
 	}
     } while (nread > 0);
+
+    printf("%ld\n", (unsigned long) data.count);
 }
 
 int main(int argc, char **argv)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.