ocaml-webmachine icon indicating copy to clipboard operation
ocaml-webmachine copied to clipboard

Redirect does not set the HTTP code

Open Leonidas-from-XIV opened this issue 9 years ago • 1 comments

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.

Leonidas-from-XIV avatar Nov 26 '16 20:11 Leonidas-from-XIV

Thanks for reporting this. Can you provide the path that the resource reported (fourth return value)?

Possibly related to #34.

-Spiros E.

i sent this from my Phone.

On Nov 26, 2016, at 3:55 PM, Marek Kubica [email protected] wrote:

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.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

seliopou avatar Nov 27 '16 19:11 seliopou