I am trying out cohttp.
I am writing the following code based on the server-side code in the README-basic-server-tutorial, but the scheme is always None.
htttp_server.ml
let server =
let open Lwt in
let callback _conn req body =
(* Scheme is always None... *)
let scheme = req |> Cohttp.Request.scheme |> (fun s -> match s with | Some x -> x | None -> "") in
let uri = req |> Cohttp.Request.uri |> Uri.to_string in
let meth = req |> Cohttp.Request.meth |> Cohttp.Code.string_of_method in
let headers = req |> Cohttp.Request.headers |> Cohttp.Header.to_string in
let version = req |> Cohttp.Request.version |> Cohttp.Code.string_of_version in
match (uri, meth) with
(_, _) -> body |> Cohttp_lwt.Body.to_string >|= (fun body ->
Printf.sprintf "Uri: %s:%s\nMethod: %s\nVersion: %s\nHeaders:---\n%s\nBody:---\n%s\n"
scheme uri meth version headers body)
>>= fun body -> Cohttp_lwt_unix.Server.respond_string ~status:`OK ~body ()
in
Cohttp_lwt_unix.Server.create ~mode:(`TCP (`Port 8080)) (Cohttp_lwt_unix.Server.make ~callback ())
let () = ignore (Lwt_main.run server)
Build:
ocamlfind ocamlopt -package cohttp-lwt-unix -linkpkg -thread http_server.ml
Run the server:
./a.out
Request:
curl http://localhost:8080
Result: (Missing scheme)
Uri: ://localhost:8080/
Method: GET
Version: HTTP/1.1
Headers:---
accept: */*
host: localhost:8080
user-agent: curl/7.68.0
Body:---
Environment:
# OCaml version
ocaml --version
The OCaml toplevel, version 4.12.0
# OS
cat /etc/issue
Linux Mint 20.2 Uma \n \l
What is the correct way to get a scheme please?