diff --git a/README.md b/README.md index aca5503..7b1400c 100644 --- a/README.md +++ b/README.md @@ -1 +1,71 @@ -Addon Libraries to C Programming Language \ No newline at end of file +# pico-http + +A minimal HTTP server in C, with the hashtable and linked-list libraries it is built on. + +## Layout + +``` +http/ server and routing framework +hashtable/ route table +llist/ bucket chaining for the hashtable +``` + +## Build + +Needs `gcc` and `make`. + +```sh +cd http && make +``` + +## Run + +```sh +./pico-http +``` + +Listens on port 3000, set by `PORT` in `http/http.h`. + +```sh +$ curl localhost:3000/login +Login works! + +$ curl localhost:3000/signup +Sign Up works! +``` + +Unregistered paths return `400 Bad request`. + +## Adding a route + +A handler fills in `Res`: + +```c +void hello(Req* req, Res* res) +{ + char* content = "Hello\n"; + + res->status = 200; + res->content_type = "text/html"; + res->content_length = strlen(content); + res->body = content; +} +``` + +Register it before starting the server, in `main.c`: + +```c +use("/hello", &hello); +http_start(PORT); +``` + +Paths match exactly — `/hello` will not match `/hello/`. + +## Library tests + +```sh +cd llist && make && ./test +cd hashtable && make && ./test +``` + +`make clean` in any of the three directories removes its objects and binary. diff --git a/hashtable/Makefile b/hashtable/Makefile index c4d0f72..fe800e4 100644 --- a/hashtable/Makefile +++ b/hashtable/Makefile @@ -1,21 +1,25 @@ CC=gcc -CFLAGS=-Wall -Wextra +CFLAGS=-Wall -Wextra -I. -I../llist OBJS=test.o hashtable.o llist.o all: test test: $(OBJS) - gcc -o $@ $^ + $(CC) -o $@ $^ -test.o: test.c http.h +# Explicit recipes rather than VPATH: VPATH also searches for *targets*, so a +# stale ../llist/test.o would get linked in place of this directory's own. +test.o: test.c hashtable.h + $(CC) $(CFLAGS) -c -o $@ $< -hashtable.o: hashtable.c hashtable.h +hashtable.o: hashtable.c hashtable.h ../llist/llist.h + $(CC) $(CFLAGS) -c -o $@ $< -llist.o: llist.c llist.h +llist.o: ../llist/llist.c ../llist/llist.h + $(CC) $(CFLAGS) -c -o $@ $< clean: - rm -f $(OBJS) - rm -f test + rm -f $(OBJS) test -.PHONY: all, clean +.PHONY: all clean diff --git a/http/Makefile b/http/Makefile index a8303f5..2447f0b 100644 --- a/http/Makefile +++ b/http/Makefile @@ -1,25 +1,33 @@ CC=gcc -CFLAGS=-Wall -Wextra +CFLAGS=-Wall -Wextra -I. -I../hashtable -I../llist -OBJS=test.o http.o net.o hashtable.o llist.o +BIN=pico-http +OBJS=main.o http.o listen.o hashtable.o llist.o -all: test +all: $(BIN) -test: $(OBJS) - gcc -o $@ $^ +$(BIN): $(OBJS) + $(CC) -o $@ $^ -test.o: test.c http.h +# hashtable.c and llist.c live in sibling directories and are compiled into +# this one. Explicit recipes rather than VPATH: VPATH also searches for +# *targets*, so a stale sibling .o would get linked in place of a fresh build. +main.o: main.c http.h + $(CC) $(CFLAGS) -c -o $@ $< -http.o: http.c http.h +http.o: http.c http.h listen.h ../hashtable/hashtable.h + $(CC) $(CFLAGS) -c -o $@ $< -net.o: net.c net.h +listen.o: listen.c listen.h + $(CC) $(CFLAGS) -c -o $@ $< -hashtable.o: hashtable.c hashtable.h +hashtable.o: ../hashtable/hashtable.c ../hashtable/hashtable.h ../llist/llist.h + $(CC) $(CFLAGS) -c -o $@ $< -llist.o: llist.c llist.h +llist.o: ../llist/llist.c ../llist/llist.h + $(CC) $(CFLAGS) -c -o $@ $< clean: - rm -f $(OBJS) - rm -f test + rm -f $(OBJS) $(BIN) -.PHONY: all, clean +.PHONY: all clean diff --git a/llist/Makefile b/llist/Makefile index 1a1b704..0cf0e94 100644 --- a/llist/Makefile +++ b/llist/Makefile @@ -6,14 +6,12 @@ OBJS=test.o llist.o all: test test: $(OBJS) - gcc -o $@ $^ - -test.o: test.c + $(CC) -o $@ $^ +test.o: test.c llist.h llist.o: llist.c llist.h clean: - rm -f $(OBJS) - rm -f test + rm -f $(OBJS) test -.PHONY: all, clean +.PHONY: all clean