Skip to content

Commit

Permalink
cohttp-eio: Request.Make() functor, R module with default headers Req…
Browse files Browse the repository at this point in the history
…uest functionality
  • Loading branch information
bikallem committed Jan 11, 2023
1 parent aaf6494 commit e04ceb2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 35 deletions.
6 changes: 1 addition & 5 deletions cohttp-eio/src/header.ml
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,7 @@ with type 'a key = 'a Header.t = struct
type binding = B : 'a key * 'a -> binding
type mapper = { f : 'a. 'a key -> 'a -> 'a }

module M = Map.Make (struct
type t = string

let compare = String.compare
end)
module M = Map.Make (String)

type t = v M.t

Expand Down
62 changes: 38 additions & 24 deletions cohttp-eio/src/request.ml
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
type host = string * int option
type resource_path = string

type 'a Header.header +=
| Content_length = Header.Content_length
| Transfer_encoding = Header.Transfer_encoding
| Hdr = Header.Hdr
| Host : host Header.header
| User_agent : string Header.header

module Header = Header.Make (struct
module Request_header = struct
type 'a Header.header +=
| Host : host Header.header
| User_agent : string Header.header

type 'a t = 'a Header.header

let equal : type a b. a t -> b t -> (a, b) Header.eq option =
Expand All @@ -31,19 +28,36 @@ module Header = Header.Make (struct

let decoder _hdr = None
let encoder _hdr = None
end)

type t = {
headers : Header.t;
meth : Http.Method.t;
version : Http.Version.t;
resource_path : resource_path;
}

let make ?(meth = `GET) ?(version = `HTTP_1_1) ?(headers = Header.empty) _host
resource_path =
{ headers; meth; version; resource_path }

let meth t = t.meth
let version t = t.version
let resource_path t = t.resource_path
end

module Make (H : Header.HEADER) = struct
module Header = Header.Make (H)

type t = {
headers : Header.t;
meth : Http.Method.t;
version : Http.Version.t;
resource_path : resource_path;
}

let make ?(meth = `GET) ?(version = `HTTP_1_1) ?(headers = Header.empty) _host
resource_path =
{ headers; meth; version; resource_path }

let meth t = t.meth
let version t = t.version
let resource_path t = t.resource_path
let headers t = t.headers
end

(** Default request with *)
module R = struct
type 'a Header.header +=
| Content_length = Header.Content_length
| Transfer_encoding = Header.Transfer_encoding
| Hdr = Header.Hdr
| Host = Request_header.Host
| User_agent = Request_header.User_agent

include Make (Request_header)
end
10 changes: 4 additions & 6 deletions cohttp-eio/tests/header.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# Setup

```ocaml
# module H = Cohttp_eio.Request.Header ;;
module H = Cohttp_eio.Request.Header
# module R = Cohttp_eio.Request ;;
module R = Cohttp_eio.Request
# module R = Cohttp_eio.Request.R ;;
module R = Cohttp_eio.Request.R
# module H = R.Header ;;
module H = R.Header
```

Add header key values using type-safe API - add, add_lazy.
Expand Down Expand Up @@ -42,5 +41,4 @@ val h : H.t = <abstr>
# H.find_opt R.Content_length h ;;
- : int option = Some 100
```

0 comments on commit e04ceb2

Please sign in to comment.