diff --git a/cohttp-lwt-jsoo/src/cohttp_lwt_jsoo.ml b/cohttp-lwt-jsoo/src/cohttp_lwt_jsoo.ml index 0cc0719660..94eb6e34b3 100644 --- a/cohttp-lwt-jsoo/src/cohttp_lwt_jsoo.ml +++ b/cohttp-lwt-jsoo/src/cohttp_lwt_jsoo.ml @@ -46,6 +46,10 @@ let binary_string str = let string_of_uint8array u8a offset len = String.init len (fun i -> Char.chr (Typed_array.unsafe_get u8a (offset + i))) +module String_io = Cohttp__String_io +module IO = Cohttp_lwt__String_io +module Header_io = Cohttp__Header_io.Make (IO) + module Body_builder (P : Params) = struct (* perform the body transfer in chunks from string. *) let chunked_body_str text = @@ -94,6 +98,32 @@ module Body_builder (P : Params) = struct else let u8a = new%js Typed_array.uint8Array_fromBuffer ab in CLB.of_string (string_of_uint8array u8a 0 ab##.byteLength) + + let construct_body xml = + (* construct body *) + let b = + let respText () = + Js.Opt.case xml##.responseText + (fun () -> `String (Js.string "")) + (fun s -> `String s) + in + if xhr_response_supported then + if Js.Opt.return xml##.response == Js.null then ( + Firebug.console##log + (Js.string "XHR Response is null; using empty string"); + `String (Js.string "")) + else + Js.Opt.case + (File.CoerceTo.arrayBuffer xml##.response) + (fun () -> + Firebug.console##log + (Js.string + "XHR Response is not an arrayBuffer; using responseText"); + respText ()) + (fun ab -> `ArrayBuffer ab) + else respText () + in + get b end module Make_api (X : sig @@ -151,10 +181,6 @@ struct (* ??? *) end -module String_io = Cohttp__String_io -module IO = Cohttp_lwt__String_io -module Header_io = Cohttp__Header_io.Make (IO) - module Make_client_async (P : Params) = Make_api (struct module Response = Cohttp.Response module Request = Cohttp.Request @@ -193,43 +219,22 @@ module Make_client_async (P : Params) = Make_api (struct match xml##.readyState with | XmlHttpRequest.DONE -> ( try - (* construct body *) - let body = - let b = - let respText () = - Js.Opt.case xml##.responseText - (fun () -> `String (Js.string "")) - (fun s -> `String s) - in - if xhr_response_supported then - Js.Opt.case - (File.CoerceTo.arrayBuffer xml##.response) - (fun () -> - Firebug.console##log - (Js.string - "XHR Response is not an arrayBuffer; using \ - responseText"); - respText ()) - (fun ab -> `ArrayBuffer ab) - else respText () - in - Bb.get b - in + let body = Bb.construct_body xml in + (* Note; a type checker subversion seems to be possible here (4.01.0). + * Remove the type constraint on Lwt.task above and return any old + * guff here. It'll compile and crash in the browser! *) (* (re-)construct the response *) + let resp_headers = Js.to_string xml##getAllResponseHeaders in + let channel = String_io.open_in resp_headers in let response = - let resp_headers = Js.to_string xml##getAllResponseHeaders in - let channel = String_io.open_in resp_headers in Lwt.( Header_io.parse channel >|= fun resp_headers -> - Response.make ~version:`HTTP_1_1 + Cohttp.Response.make ~version:`HTTP_1_1 ~status:(C.Code.status_of_code xml##.status) ~flush:false (* ??? *) ~encoding:(CLB.transfer_encoding body) ~headers:resp_headers ()) in - (* Note; a type checker subversion seems to be possible here (4.01.0). - * Remove the type constraint on Lwt.task above and return any old - * guff here. It'll compile and crash in the browser! *) Lwt.wakeup wake (response, body) with | e @@ -260,7 +265,6 @@ module Make_client_async (P : Params) = Make_api (struct Lwt.return (xml##send (Js.Opt.return (Obj.magic bs)))) >>= fun () -> Lwt.on_cancel res (fun () -> xml##abort); - (* unwrap the response *) Lwt.( res >>= fun (r, b) -> @@ -307,29 +311,7 @@ module Make_client_sync (P : Params) = Make_api (struct let bs = binary_string body in xml ## (send (Js.Opt.return (Obj.magic bs)))) >>= fun _body -> - (* TODO: FIXME: looks like an indenting or cut-and-pasto here. Check this - avsm *) - (* construct body *) - let body = - let b = - let respText () = - Js.Opt.case xml##.responseText - (fun () -> `String (Js.string "")) - (fun s -> `String s) - in - if xhr_response_supported then - Js.Opt.case - (File.CoerceTo.arrayBuffer xml##.response) - (fun () -> - Firebug.console##log - (Js.string - "XHR Response is not an arrayBuffer; using responseText"); - respText ()) - (fun ab -> `ArrayBuffer ab) - else respText () - in - Bb.get b - in - + let body = Bb.construct_body xml in (* (re-)construct the response *) let resp_headers = Js.to_string xml##getAllResponseHeaders in Header_io.parse (String_io.open_in resp_headers) >>= fun resp_headers ->