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

Redirect does not set the HTTP code #62

Open
Leonidas-from-XIV opened this issue Nov 26, 2016 · 1 comment
Open

Redirect does not set the HTTP code #62

Leonidas-from-XIV opened this issue Nov 26, 2016 · 1 comment

Comments

@Leonidas-from-XIV
Copy link

When calling the redirect function, the location header is set, but the return code stays 200, which is (at least for me, possibly others) confusing.

The following fairly minimal example illustrates the issue:

open Cohttp_lwt_unix
open Lwt.Infix

module Wm = struct
  module Rd = Webmachine.Rd
  include Webmachine.Make(Cohttp_lwt_unix_io)
end

class redirect () = object(self)
  inherit [Cohttp_lwt_body.t] Wm.resource

  method private to_json rd =
    Wm.Rd.redirect "https://github.com/inhabitedtype/ocaml-webmachine/issues/62" rd
    |> Wm.continue `Empty

  method content_types_provided rd =
    Wm.continue [
      "application/json", self#to_json
    ] rd

  method content_types_accepted rd =
    Wm.continue [] rd
end

let main () =
  let port = 8080 in
  let routes = [
    ("/", fun () -> new redirect ()) ;
  ] in
  let callback (ch,conn) request body =
    let open Cohttp in
    Wm.dispatch' routes ~body ~request
    >|= (function
      | None        -> (`Not_found, Header.init (), `String "Not found", [])
      | Some result -> result)
    >>= fun (status, headers, body, path) ->
      Server.respond ~headers ~body ~status ()
  in
  let config = Server.make ~callback () in
  Server.create ~mode:(`TCP(`Port port)) config
  >>= fun () -> Lwt.return_unit

let () = Lwt_main.run @@ main ()

Request from the running server via curl -v localhost:8080/ and see that the location header is sent but the return code stays 200.

@seliopou
Copy link
Member

seliopou commented Nov 27, 2016 via email

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