h3 http parser

Category: c library
Tags: C library

Fast HTTP header parser library

History

Unkown

FAQ

What is it?

Http request parser

How cool is it?

Rather.

Competitors?

http_parser from joyent

When to use it?

When you want to parse http requests resolving (duh)

Is it dead?

Probably yes.

Where is it?

https://github.com/c9s/h3

Notes

It is extremely difficult to get a http_parser right.

In the nidium project the homebrew http parser was ditched in favor for joyents'.

How to use it

The documentation and the examples are rather good.

Download

file: 1_download.sh

#!/bin/bash

mkdir ./tmp
cd ./tmp
wget -q https://github.com/c9s/h3/archive/master.zip -O h3.zip
unzip -qq h3.zip
cd h3-master
make -f Makefile.dist libh3.a libh3.so

Init

file: main.c

#include <stdio.h>
#include <string.h>

#include <h3.h>

int main() {
    const char *headerBody = "Cache-Control: no-cache"
"\r\n" "Content-Encoding: gzip"
"\r\n" "Content-Type: text/html; charset=utf-8"
"\r\n" "Date: Thu, 27 Apr 2017 14:53:06 GMT"
"\r\n" "Server: GitHub.com"
"\r\n" "Strict-Transport-Security: max-age=31536000; includeSubdomains; preload"
"\r\n" "Transfer-Encoding: chunked"
"\r\n" "Vary: X-PJAX, Accept-Encoding"
"\r\n" "X-Frame-Options: deny"
"\r\n" "status: 200 OK"
"\r\n" "x-content-type-options: nosniff"
"\r\n" "x-ua-compatible: IE=Edge,chrome=1";
    int len = strlen(headerBody);

    RequestHeader *header;
    header = h3_request_header_new();
    h3_request_header_parse(header, headerBody, len);

    printf("HEADER\n");
    printf("===========================\n");
    printf("%s", headerBody);
    printf("\n---------------------------\n");
    printf("Method: %.*s\n", header->RequestMethodLen, header->RequestMethod);
    printf("Request-URI: %.*s\n", header->RequestURILen, header->RequestURI);
    printf("HTTP-Version: %.*s\n", header->HTTPVersionLen, header->HTTPVersion);
    printf("===========================\n");

    h3_request_header_free(header);

    return 0;
}

file: 2_compile_and_run.sh

#!/bin/bash

gcc -std=c99 -o ./tmp/main main.c -I./tmp/h3-master/include ./tmp/h3-master/libh3.a
./tmp/main

Output

HEADER
===========================
Cache-Control: no-cache
Content-Encoding: gzip
Content-Type: text/html; charset=utf-8
Date: Thu, 27 Apr 2017 14:53:06 GMT
Server: GitHub.com
Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
Transfer-Encoding: chunked
Vary: X-PJAX, Accept-Encoding
X-Frame-Options: deny
status: 200 OK
x-content-type-options: nosniff
x-ua-compatible: IE=Edge,chrome=1
---------------------------
Method: Cache-Control:
Request-URI: no-cache
HTTP-Version: 
===========================

Further reading:

We only tipped the top of the iceberg here. There is much more:

https://github.com/c9s/h3/blob/master/README.md#high-level-api