Add a simple debug/example utility #143

Closed
wants to merge 8 commits into
from

Projects

None yet

2 participants

@cmr

No description provided.

@bnoordhuis bnoordhuis and 1 other commented on an outdated diff Dec 13, 2012
utils/parsertrace.c
@@ -0,0 +1,79 @@
+/* Dump what the parser finds to stdout as it happen */
+
+#include <stdio.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include "../http_parser.h"
+
+int on_message_begin(http_parser *_ __attribute__((unused))) {
@bnoordhuis
bnoordhuis Dec 13, 2012

Preferably use (void) arg; if you want to squelch unused argument warnings. __attribute__((unused)) is a gcc extension.

@cmr
cmr Dec 13, 2012

I had tried (void*) arg but that didn't shut gcc up.

@bnoordhuis
bnoordhuis Dec 13, 2012

Not (void*), just (void). :-)

@cmr
cmr Dec 13, 2012

Not sure what was going on there.. works fine here on my laptop now. It was late, what can I say 😕

@bnoordhuis bnoordhuis commented on an outdated diff Dec 13, 2012
utils/parsertrace.c
+ if (argc != 2) {
+ fprintf(stderr, "Usage: ./parsertrace $filename\n");
+ return EXIT_FAILURE;
+ }
+ char *filename = argv[1];
+ int fd = open(filename, O_RDONLY);
+
+ http_parser_settings settings;
+ settings.on_message_begin = on_message_begin;
+ settings.on_url = on_url;
+ settings.on_header_field = on_header_field;
+ settings.on_header_value = on_header_value;
+ settings.on_headers_complete = on_headers_complete;
+ settings.on_body = on_body;
+ settings.on_message_complete = on_message_complete;
+ http_parser *parser = malloc(sizeof(http_parser));
@bnoordhuis
bnoordhuis Dec 13, 2012

Doesn't have to be malloc'd.

@bnoordhuis bnoordhuis commented on an outdated diff Dec 13, 2012
utils/parsertrace.c
+ settings.on_url = on_url;
+ settings.on_header_field = on_header_field;
+ settings.on_header_value = on_header_value;
+ settings.on_headers_complete = on_headers_complete;
+ settings.on_body = on_body;
+ settings.on_message_complete = on_message_complete;
+ http_parser *parser = malloc(sizeof(http_parser));
+ http_parser_init(parser, HTTP_RESPONSE);
+
+ struct stat statinfo;
+ if(stat(argv[1], &statinfo)) {
+ fprintf(stderr, "can't stat file\n");
+ return EXIT_FAILURE;
+ }
+
+ char *data = malloc(statinfo.st_size);
@bnoordhuis
bnoordhuis Dec 13, 2012

Free this at the end.

@bnoordhuis bnoordhuis commented on an outdated diff Dec 13, 2012
utils/parsertrace.c
+ printf("Header value: %.*s\n", (int)length, at);
+ return 0;
+}
+
+int on_body(http_parser *_ __attribute__((unused)), const char *at, size_t length) {
+ printf("Body: %.*s\n", (int)length, at);
+ return 0;
+}
+
+int main(int argc, char *argv[]) {
+ if (argc != 2) {
+ fprintf(stderr, "Usage: ./parsertrace $filename\n");
+ return EXIT_FAILURE;
+ }
+ char *filename = argv[1];
+ int fd = open(filename, O_RDONLY);
@bnoordhuis
bnoordhuis Dec 13, 2012

Use fopen/fread/fclose instead? open/read/close are only available on POSIX platforms.

@cmr

Thanks for the feedback. Anything else?

@cmr

Hm, I'm going to add a switch for response/request/both, don't merge it in yet please.

@cmr

I think it's fairly complete now.

@bnoordhuis bnoordhuis commented on an outdated diff Dec 16, 2012
utils/parsertrace.c
+ return 0;
+}
+
+int on_body(http_parser *_ , const char *at, size_t length) {
+ (void)_;
+ printf("Body: %.*s\n", (int)length, at);
+ return 0;
+}
+
+int main(int argc, char *argv[]) {
+ enum http_parser_type file_type;
+ if (argc != 3) {
+err: fprintf(stderr, "Usage: %s $type $filename\n"
+ "\ttype: -x, where x is one of {r,b,q}\n"
+ "\tparses file a Response, reQuest, or Both\n"
+ , argv[0]);
@bnoordhuis
bnoordhuis Dec 16, 2012

Put this in a function void usage(void). Backward gotos are a minor evil.

@bnoordhuis
bnoordhuis Dec 16, 2012

And a style thing: arguments need to line up.

@bnoordhuis bnoordhuis commented on an outdated diff Dec 16, 2012
utils/parsertrace.c
+ break;
+ default:
+ goto err;
+ break;
+ }
+ }
+ char *filename = argv[2];
+ FILE *file = fopen(filename, "r");
+ if (file == NULL) {
+ fprintf(stderr, "Error: couldn't open %s\n", filename);
+ return EXIT_FAILURE;
+ }
+
+ fseek(file, 0, SEEK_END);
+ unsigned long file_length = (unsigned long)ftell(file);
+ fseek(file, 0, SEEK_SET);
@bnoordhuis bnoordhuis commented on an outdated diff Dec 16, 2012
utils/parsertrace.c
+ file_type = HTTP_BOTH;
+ break;
+ default:
+ goto err;
+ break;
+ }
+ }
+ char *filename = argv[2];
+ FILE *file = fopen(filename, "r");
+ if (file == NULL) {
+ fprintf(stderr, "Error: couldn't open %s\n", filename);
+ return EXIT_FAILURE;
+ }
+
+ fseek(file, 0, SEEK_END);
+ unsigned long file_length = (unsigned long)ftell(file);
@bnoordhuis
bnoordhuis Dec 16, 2012

Why the cast to unsigned? It does the wrong thing when ftell() returns -1.

@bnoordhuis bnoordhuis commented on an outdated diff Dec 16, 2012
utils/parsertrace.c
+ break;
+ }
+ }
+ char *filename = argv[2];
+ FILE *file = fopen(filename, "r");
+ if (file == NULL) {
+ fprintf(stderr, "Error: couldn't open %s\n", filename);
+ return EXIT_FAILURE;
+ }
+
+ fseek(file, 0, SEEK_END);
+ unsigned long file_length = (unsigned long)ftell(file);
+ fseek(file, 0, SEEK_SET);
+
+ char *data = malloc(file_length);
+ if(fread(data, sizeof(char), file_length, file) != file_length) {
@bnoordhuis
bnoordhuis Dec 16, 2012

Style: space between if and paren.

@bnoordhuis bnoordhuis and 1 other commented on an outdated diff Dec 16, 2012
utils/parsertrace.c
@@ -0,0 +1,114 @@
+/* Dump what the parser finds to stdout as it happen */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "../http_parser.h"
@bnoordhuis
bnoordhuis Dec 16, 2012

Can't this be #include "http_parser.h"?

@cmr
cmr Dec 16, 2012

Sure can, after a tiny fix to the makefile.

@bnoordhuis bnoordhuis commented on an outdated diff Dec 16, 2012
utils/parsertrace.c
@@ -0,0 +1,114 @@
+/* Dump what the parser finds to stdout as it happen */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "../http_parser.h"
+
+int on_message_begin(http_parser *_ ) {
@bnoordhuis
bnoordhuis Dec 16, 2012

Last style issue: http_parser uses type*, not type * (i.e. no space).

@cmr cmr Style fixes
Thanks @bnoordhuis for the patience, you're awesome :)
4501d9d
@bnoordhuis
Node.js Foundation member

Thanks Corey, landed with some touch-ups in 798eb90.

@bnoordhuis bnoordhuis closed this Dec 16, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment