Created
December 14, 2014 08:28
-
-
Save josephg/997f0f66b72c3b546a6c to your computer and use it in GitHub Desktop.
AFL harness for joyent/http-parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include "http_parser.h" | |
int on_message_begin(http_parser *parser) { | |
printf("on_message_begin\n"); | |
return 0; | |
} | |
int W(const char *name, const char *at, size_t length) { | |
printf("%s: ", name); | |
fwrite(at, 1, length, stdout); | |
printf("\n"); | |
return 0; | |
} | |
int on_url(http_parser *parser, const char *at, size_t length) { return W("on_url", at, length); } | |
int on_status(http_parser *parser, const char *at, size_t length) { return W("on_status", at, length); } | |
int on_header_field(http_parser *parser, const char *at, size_t length) { return W("on_header_field", at, length); } | |
int on_header_value(http_parser *parser, const char *at, size_t length) { return W("on_header_value", at, length); } | |
int on_headers_complete(http_parser *parser) { | |
printf("on_headers_complete\n"); | |
return 0; | |
} | |
int on_body(http_parser *parser, const char *at, size_t length) { return W("on_body", at, length); } | |
int on_message_complete(http_parser *parser) { | |
printf("on_message_complete\n"); | |
return 0; | |
} | |
void parse(http_parser *parser, http_parser_settings *settings, char *buf, size_t num_read) { | |
ssize_t nparsed = http_parser_execute(parser, settings, buf, num_read); | |
if (parser->upgrade) { | |
printf("handled upgrade. exiting\n"); | |
exit(0); | |
} | |
if (nparsed != num_read) { | |
fprintf(stderr, "Parse error. Exiting\n"); | |
exit(1); | |
} | |
} | |
int main() { | |
http_parser_settings settings = { | |
.on_message_begin = on_message_begin, | |
.on_url = on_url, | |
.on_status = on_status, | |
.on_header_field = on_header_field, | |
.on_header_value = on_header_value, | |
.on_headers_complete = on_headers_complete, | |
.on_body = on_body, | |
.on_message_complete = on_message_complete | |
}; | |
http_parser parser; | |
http_parser_init(&parser, HTTP_REQUEST); | |
char buf[1024]; | |
do { | |
size_t num_read = fread(buf, 1, 1024, stdin); | |
printf("read %ld bytes\n", num_read); | |
parse(&parser, &settings, buf, num_read); | |
} while (!feof(stdin)); | |
parse(&parser, &settings, NULL, 0); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment