Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example in README does not work out of the box #220

Open
ukd1 opened this issue Aug 8, 2022 · 1 comment
Open

Example in README does not work out of the box #220

ukd1 opened this issue Aug 8, 2022 · 1 comment

Comments

@ukd1
Copy link

ukd1 commented Aug 8, 2022

Assuming a fresh install, and running:

dune init proj helloworld
opam install httpaf

add lib to the dune thing

(executable
 (public_name helloworld)
 (name main)
 (libraries helloworld httpaf))

the example in the readme does not compile........

open Httpaf
module String = Caml.String

let invalid_request reqd status body =
  (* Responses without an explicit length or transfer-encoding are
     close-delimited. *)
  let headers = Headers.of_list [ "Connection", "close" ] in
  Reqd.respond_with_string reqd (Response.create ~headers status) body
;;

let request_handler reqd =
  let { Request.meth; target; _ } = Reqd.request reqd in
  match meth with
  | `GET ->
    begin match String.split_on_char '/' target with
    | "" :: "hello" :: rest ->
      let who =
        match rest with
        | [] -> "world"
        | who :: _ -> who
      in
      let response_body = Printf.sprintf "Hello, %s!\n" who in
      (* Specify the length of the response. *)
      let headers =
        Headers.of_list
          [ "Content-length", string_of_int (String.length response_body) ]
      in
      Reqd.respond_with_string reqd (Response.create ~headers `OK) response_body
    | _ ->
      let response_body = Printf.sprintf "%S not found\n" target in
      invalid_request reqd `Not_found response_body
    end
  | meth ->
    let response_body =
      Printf.sprintf "%s is not an allowed method\n" (Method.to_string meth)
    in
    invalid_request reqd `Method_not_allowed response_body
;;
% dune build               
File "bin/main.ml", line 4, characters 16-27:
4 | module String = Caml.String
                    ^^^^^^^^^^^
Error: Unbound module Caml
@dpatti
Copy link
Collaborator

dpatti commented Aug 14, 2022

I think that Caml is part of base specifically, so you're either meant to open Base or more simply elide the module String = Caml.String line. Not sure if something changed about the compiler at some point but I'm guessing the latter is the easier solution here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants