Commit 7b3e4eb6 authored by Josh Kunz's avatar Josh Kunz

Makes HTTP compile to a library.

parent 19534368
OCAMLC = ocamlc
OCAMLOPT = ocamlopt
OCAMLDEP = ocamldep
OCAMLLEX = ocamllex
OCAMLYACC = ocamlyacc
OCAMLLEX_POST = Lex
OCAMLYACC_POST = Yacc
%.cmo: %.ml
ocamlc $(OCAMLFLAGS) -c $<
$(OCAMLC) $(OCAMLFLAGS) -c $<
%.cmx: %.ml
ocamlopt $(OCAMLFLAGS) -c $<
$(OCAMLOPT) $(OCAMLFLAGS) -c $<
%.cmi: %.mli
ocamlc $(OCAMLFLAGS) -c $<
$(OCAMLC) $(OCAMLFLAGS) -c $<
%.cma:
$(OCAMLC) -a $(OCAMLFLAGS) -o $@ $^
%$(OCAMLLEX_POST).ml: %.mll
$(OCAMLLEX) -o "$*$(OCAMLLEX_POST).ml" $(OCAMLEX_FLAGS) $<
%$(OCAMLYACC_POST).mli %$(OCAMLYACC_POST).ml: %.mly
$(OCAMLYACC) -b "$*$(OCAMLYACC_POST)" $(OCAMLYACC_FLAGS) $<
Makefile.d:
$(OCAMLDEP) $^ > $@
HTTPLex.ml
HTTPParse.ml
HTTPParse.mli
http.cma
test
......@@ -87,15 +87,16 @@ module HTTPBase (R : HTTPBase_intf) : (HTTPBase with type t = R.t) = struct
let set_header r n v =
Header.set_header (R.headers r) n v |> R._sheaders r
let from_string s =
let reader pos len = String.sub s (pos + 1) len in
(Lexing.from_string s |> R._read) reader ;;
let _string_reader s pos len =
if len = 0 then "" else String.sub s (pos + 1) len;;
let write oc r = R.as_string r |> output_string oc;;
let read ic =
let _channel_reader ic pos len =
let body = "" in
let reader pos len = really_input ic body 0 len; body in
(Lexing.from_channel ic |> R._read) reader;;
if len = 0 then "" else (really_input ic body 0 len; body);;
let from_string s = (Lexing.from_string s |> R._read) (_string_reader s);;
let read ic = (Lexing.from_channel ic |> R._read) (_channel_reader ic);;
let write oc r = R.as_string r |> output_string oc;;
end;;
module RequestBase = struct
......@@ -120,7 +121,7 @@ module RequestBase = struct
let body = reader buf.lex_curr_p.pos_cnum length in
{ meth = m; uri = p; version = v; headers = hdrs; body = body}
with
| Parse_error -> raise (Bad_request "Couldn't parse request.")
| Parse_error | Failure _ -> raise (Bad_request "Couldn't parse request.")
| Header_not_found -> raise (Bad_request "No Content-Length header.")
| End_of_file -> raise (Bad_request "Connection terminated early.");;
......@@ -153,7 +154,7 @@ module ResponseBase = struct
let body = reader buf.lex_curr_p.pos_cnum length in
{ version = v; code = c; reason = r; headers = hdrs; body = body}
with
| Parse_error -> raise (Bad_response "Couldn't parse response.")
| Parse_error | Failure _ -> raise (Bad_response "Couldn't parse response.")
| Header_not_found -> raise (Bad_response "No Content-Length header.")
| End_of_file -> raise (Bad_response "Connection terminated early.");;
......
......@@ -4,41 +4,40 @@ OCAMLLEX_FLAGS =
OCAMLYACC_FLAGS =
OCAMLFLAGS =
bin = test
lib = http.cma
OCAMLLEX_POST = Lex
OCAMLYACC_POST = Yacc
par_prefix = HTTP
lex_prefix = $(par_prefix)Lex
yacc_prefix = $(par_prefix)Parse
lexp = $(par_prefix)$(OCAMLLEX_POST)
yaccp = $(par_prefix)$(OCAMLYACC_POST)
libs = unix
interfaces = $(yacc_prefix).mli $(wildcard *.mli)
sources = $(yacc_prefix).ml $(lex_prefix).ml
sources += HTTPTypes.ml HTTP.ml Test.ml
interfaces = $(yaccp).mli $(wildcard *.mli)
sources = $(yaccp).ml $(lexp).ml
sources += HTTPTypes.ml HTTP.ml
objects = $(patsubst %.ml,%.cmo,$(sources))
depfile = Makefile.d
default: $(bin)
$(depfile): $(yacc_prefix).mli $(yacc_prefix).ml $(lex_prefix).ml
$(lex_prefix).ml: $(yacc_prefix).mli
$(bin): $(objects)
ocamlc $(OCAMLFLAGS) -o $@ $(addsuffix .cma,$(libs)) $^
$(lib): $(objects)
$(lex_prefix).ml: $(par_prefix).mll
ocamllex $(OCAMLLEX_FLAGS) -o $@ $<
$(depfile): $(yaccp).mli $(yaccp).ml $(lexp).ml
$(depfile): $(sources) $(interfaces)
$(yacc_prefix).ml $(yacc_prefix).mli: $(par_prefix).mly
ocamlyacc $(OCAMLYACC_FLAGS) -b$(yacc_prefix) $<
$(lexp).ml: $(yaccp).mli
$(depfile): $(sources) $(interfaces)
ocamldep $^ > $@
#$(lex_prefix).ml: $(par_prefix).mll
# ocamllex $(OCAMLLEX_FLAGS) -o $@ $<
#
#$(yacc_prefix).ml $(yacc_prefix).mli: $(par_prefix).mly
# ocamlyacc $(OCAMLYACC_FLAGS) -b$(yacc_prefix) $<
clean:
-rm -f $(bin) *.cmo *.cmi *.o *.cmx
-rm -f $(lex_prefix).ml $(wildcard $(yacc_prefix).ml*)
-rm -f $(lexp).ml $(wildcard $(yaccp).ml*)
-rm -f $(depfile)
include ../common/Makefile.implicit
......
open HTTP
open Printf
let req_test =
"GET /this/is/a/path HTTP/1.1\r
Host: localhost\r
\r\n";;
let resp_test =
"HTTP/1.1 200 OK\r
This: Works\r
Content-Length: 4\r
\r\n
Boo!";;
(* Lexing.from_string req_test |> HTTPParse.request HTTPLex.http_token;; *)
let resp = Response.from_string resp_test in
Response.body resp |> printf "Body:\n'%s'";;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment