Skip to content

xishell/lwhttp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

lwhttp

A lightweight HTTP/1.1 library in C, built from the wire format up using only the C standard library and POSIX.

Why

Built to understand HTTP/1.1 end-to-end without leaning on libcurl, nginx, or a framework as a black box. The goal is a small, readable codebase that maps cleanly onto the relevant sections of RFC 9110 and RFC 9112.

What works today

  • Open-addressing hashmap with case-insensitive keys (for headers)
  • Method parsing: GET, HEAD, POST, PUT, DELETE, OPTIONS, PATCH
  • Request struct: method, URI, version, headers, body
  • Response struct: status, reason, version, headers, body
  • Request and response serialization to wire format
  • Request parsing from a byte buffer (Content-Length bodies)
  • Socket server loop
  • Transfer-Encoding: chunked
  • Persistent connections (Connection: keep-alive)
  • Client-side request sending

Non-goals

  • TLS (delegate to a reverse proxy)
  • HTTP/2 or HTTP/3
  • Async I/O — blocking sockets are sufficient for the learning scope

Layout

include/hashmap.h, src/hashmap.c   open-addressing map, case-insensitive keys
include/http.h,    src/http.c      methods, request/response types, parse, serialize
src/main.c                         test driver exercising the library

Building

Requires Meson and a C2x-capable compiler.

meson setup build
meson compile -C build
./build/lwhttp-test

Design notes

  • Hashmap for headers. Header lookup is case-insensitive per RFC 9110 §5.1, and a request can carry dozens of headers. A small open-addressing map keeps lookup O(1) average and avoids per-lookup strcasecmp scans.
  • Incremental parser API. lw_request_parse returns 1 when the buffer is incomplete so a caller can read more from a socket and retry, rather than forcing the whole request to be buffered before parsing begins.
  • C2x + warning level 3. Lets the compiler catch as much as possible at build time given the manual memory management.

About

Lightweight HTTP/1.1 protocol in C

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors