#include "http_parser.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <time.h>
static http_parser *parser;
int on_message_begin(http_parser* _) {
(void)_;
printf("\n***MESSAGE BEGIN***\n\n");
return 0;
}
int on_headers_complete(http_parser* _) {
(void)_;
printf("\n***HEADERS COMPLETE***\n\n");
return 0;
}
int on_message_complete(http_parser* _) {
(void)_;
printf("\n***MESSAGE COMPLETE***\n\n");
return 0;
}
int on_url(http_parser* _, const char* at, size_t length) {
(void)_;
printf("Url: %.*s\n", (int)length, at);
return 0;
}
int on_header_field(http_parser* _, const char* at, size_t length) {
(void)_;
printf("Header field: %.*s\n", (int)length, at);
return 0;
}
int on_header_value(http_parser* _, const char* at, size_t length) {
(void)_;
printf("Header value: %.*s\n", (int)length, at);
return 0;
}
int on_body(http_parser* _, const char* at, size_t length) {
(void)_;
printf("Body: %.*s\n", (int)length, at);
return 0;
}
http_parser_settings parser_set;
// http_parser的回调函数,需要获取HEADER后者BODY信息,可以在这里面处理。
parser_set.on_message_begin = on_message_begin;
parser_set.on_header_field = on_header_field;
parser_set.on_header_value = on_header_value;
parser_set.on_url = on_url;
parser_set.on_body = on_body;
parser_set.on_headers_complete = on_headers_complete;
parser_set.on_message_complete = on_message_complete;
char buf[1024]="GET /a/b/c/d HTTP/1.1";
size_t parsed;
parser = (http_parser*)malloc(sizeof(http_parser)); // 分配一个http_parser
for (int i = 0; i < 1; i++) {
http_parser_init(parser, HTTP_REQUEST); // 初始化parser为Request类型
parsed = http_parser_execute(parser, &parser_set, buf, strlen(buf)); // 执行解析过程
}
free(parser);
parser = NULL;