Skip to content

Commit

Permalink
Compile against OCaml 4.06 & Cohttp 1.0.x
Browse files Browse the repository at this point in the history
  • Loading branch information
vasilisp committed Feb 6, 2018
1 parent 548353f commit da2423b
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/baselib/ocsigen_stream.ml
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ let of_file filename =
if n = 0 then empty None else
(* Streams should be immutable, thus we always make a copy
of the buffer *)
cont (Bytes.sub buf 0 n) aux
cont (Bytes.sub_string buf 0 n) aux
in make ~finalize:(fun _ -> Lwt_unix.close fd) aux

let of_string s =
Expand Down
29 changes: 19 additions & 10 deletions src/extensions/deflatemod.ml
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,18 @@ let buffer_size =
(* Minimal header, by X. Leroy *)
let gzip_header_length = 10
let gzip_header = Bytes.make gzip_header_length (Char.chr 0)
let () =
let gzip_header =
Bytes.set gzip_header 0 @@ Char.chr 0x1F;
Bytes.set gzip_header 1 @@ Char.chr 0x8B;
Bytes.set gzip_header 2 @@ Char.chr 8;
Bytes.set gzip_header 9 @@ Char.chr 0xFF
Bytes.set gzip_header 9 @@ Char.chr 0xFF;
Bytes.unsafe_to_string gzip_header

(* inspired by an auxiliary function from camlzip, by Xavier Leroy *)
type output_buffer =
{
stream: Zlib.stream;
buf: string;
buf: bytes;
mutable pos: int;
mutable avail: int;
mutable size : int32;
Expand Down Expand Up @@ -99,15 +100,16 @@ let rec output oz f buf pos len =
let (_, used_in, used_out) =
try
Zlib.deflate
oz.stream buf pos len oz.buf oz.pos oz.avail Zlib.Z_NO_FLUSH
oz.stream (Bytes.unsafe_of_string buf)
pos len oz.buf oz.pos oz.avail Zlib.Z_NO_FLUSH
with Zlib.Error(s, s') ->
raise
(Ocsigen_stream.Stream_error("Error during compression: "^s^" "^s'))
in
oz.pos <- oz.pos + used_out;
oz.avail <- oz.avail - used_out;
oz.size <- Int32.add oz.size (Int32.of_int used_in);
oz.crc <- Zlib.update_crc oz.crc buf pos used_in;
oz.crc <- Zlib.update_crc_string oz.crc buf pos used_in;
output oz f buf (pos + used_in) (len - used_in)
end

Expand All @@ -119,7 +121,12 @@ and flush oz cont =
cont ()
else begin
let buf_len = Bytes.length oz.buf in
let s = if len = buf_len then oz.buf else Bytes.sub oz.buf 0 len in
let s =
if len = buf_len then
Bytes.to_string oz.buf
else
Bytes.sub_string oz.buf 0 len
in
Lwt_log.ign_info ~section "Flushing!";
oz.pos <- 0 ;
oz.avail <- buf_len;
Expand All @@ -140,7 +147,9 @@ and next_cont oz stream =
(* no more input, deflates only what were left because output buffer
* was full *)
let (finished, _, used_out) =
Zlib.deflate oz.stream oz.buf 0 0 oz.buf oz.pos oz.avail Zlib.Z_FINISH
Zlib.deflate
oz.stream oz.buf
0 0 oz.buf oz.pos oz.avail Zlib.Z_FINISH
in
oz.pos <- oz.pos + used_out;
oz.avail <- oz.avail - used_out;
Expand All @@ -166,7 +175,7 @@ and next_cont oz stream =
output oz f s 0 (String.length s)

(* deflate param : true = deflate ; false = gzip (no header in this case) *)
let compress deflate stream =
let compress deflate stream : string Ocsigen_stream.t =
let zstream =
Zlib.deflate_init
(Ocsigen_lib.Option.get' 6
Expand Down Expand Up @@ -286,11 +295,11 @@ let stream_filter contentencoding url deflate choice res =
Cohttp.Response.encoding = Cohttp.Transfer.Chunked
}
and body =
Cohttp_lwt_body.to_stream body
Cohttp_lwt.Body.to_stream body
|> Ocsigen_stream.of_lwt_stream
|> compress deflate
|> Ocsigen_stream.to_lwt_stream
|> Cohttp_lwt_body.of_stream
|> Cohttp_lwt.Body.of_stream
in
Lwt.return (Ocsigen_response.update res ~body ~response)
| _ ->
Expand Down
2 changes: 1 addition & 1 deletion src/server/ocsigen_cohttp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ let service ?ssl ~address ~port ~connector () =
Conduit_lwt_unix.init
~src:(Ocsigen_config.Socket_type.to_string address)
~tls_server_key () >>= fun conduit_ctx ->
Lwt.return (Cohttp_lwt_unix_net.init ~ctx:conduit_ctx ()) >>= fun ctx ->
Lwt.return (Cohttp_lwt_unix.Net.init ~ctx:conduit_ctx ()) >>= fun ctx ->
(* We catch the INET_ADDR of the server *)
let callback =
let address = Ocsigen_config.Socket_type.to_inet_addr address
Expand Down
6 changes: 3 additions & 3 deletions src/server/ocsigen_multipart.ml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ let scan_header
if i' > end_pos then raise (Multipart_error "Mimestring.scan_header");
let name =
if downcase then
String.lowercase (S.matched_group r 1 parstr)
String.lowercase_ascii (S.matched_group r 1 parstr)
else
S.matched_group r 1 parstr
in
Expand Down Expand Up @@ -343,7 +343,7 @@ let post_params_multipart_form_data body_gen ctparams upload_dir max_size =
Lwt.return ()
| _, `Some_file (_, _, wh, _) ->
let len = String.length s in
let r = Unix.write wh s 0 len in
let r = Unix.write_substring wh s 0 len in
if r < len then
(*XXXX Inefficient if s is long *)
add p (String.sub s r (len - r))
Expand Down Expand Up @@ -413,7 +413,7 @@ let post_params_multipart_form_data body_gen ctparams upload_dir max_size =

let post_params ~content_type body_gen =
let (ct, cst), ctparams = content_type in
match String.lowercase ct, String.lowercase cst with
match String.lowercase_ascii ct, String.lowercase_ascii cst with
| "application", "x-www-form-urlencoded" ->
Some (post_params_form_urlencoded body_gen)
| "multipart", "form-data" ->
Expand Down
4 changes: 2 additions & 2 deletions src/server/ocsigen_request.ml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
open Lwt.Infix

let post_data_of_body ~content_type b =
Cohttp_lwt_body.to_stream b
Cohttp_lwt.Body.to_stream b
|> Ocsigen_stream.of_lwt_stream
|> Ocsigen_multipart.post_params ~content_type

Expand All @@ -17,7 +17,7 @@ type file_info = Ocsigen_multipart.file_info = {
type post_data = Ocsigen_multipart.post_data

type body = [
| `Unparsed of Cohttp_lwt_body.t
| `Unparsed of Cohttp_lwt.Body.t
| `Parsed of post_data Lwt.t option
]

Expand Down
4 changes: 2 additions & 2 deletions src/server/ocsigen_request.mli
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ val make :
ssl : bool ->
filenames : string list ref ->
sockaddr : Lwt_unix.sockaddr ->
body : Cohttp_lwt_body.t ->
body : Cohttp_lwt.Body.t ->
connection_closed : unit Lwt.t ->
Cohttp.Request.t ->
t
Expand All @@ -45,7 +45,7 @@ val to_cohttp : t -> Cohttp.Request.t

val uri : t -> Uri.t

val body : t -> Cohttp_lwt_body.t
val body : t -> Cohttp_lwt.Body.t

val address : t -> Unix.inet_addr

Expand Down
4 changes: 2 additions & 2 deletions src/server/ocsigen_response.ml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
type t = {
a_response : Cohttp.Response.t ;
a_body : Cohttp_lwt_body.t ;
a_body : Cohttp_lwt.Body.t ;
a_cookies : Ocsigen_cookies.cookieset
}

let make
?(body = Cohttp_lwt_body.empty)
?(body = Cohttp_lwt.Body.empty)
?(cookies = Ocsigen_cookies.empty_cookieset)
a_response =
{ a_response ; a_body = body ; a_cookies = cookies }
Expand Down
8 changes: 4 additions & 4 deletions src/server/ocsigen_response.mli
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
type t

val make :
?body : Cohttp_lwt_body.t ->
?body : Cohttp_lwt.Body.t ->
?cookies : Ocsigen_cookies.cookieset ->
Cohttp.Response.t ->
t

val update :
?response : Cohttp.Response.t ->
?body : Cohttp_lwt_body.t ->
?body : Cohttp_lwt.Body.t ->
?cookies : Ocsigen_cookies.cookieset ->
t ->
t

val of_cohttp :
?cookies : Ocsigen_cookies.cookieset ->
(Cohttp.Response.t * Cohttp_lwt_body.t) ->
(Cohttp.Response.t * Cohttp_lwt.Body.t) ->
t

val to_cohttp : t -> Cohttp.Response.t * Cohttp_lwt_body.t
val to_cohttp : t -> Cohttp.Response.t * Cohttp_lwt.Body.t

val status : t -> Cohttp.Code.status

Expand Down
2 changes: 1 addition & 1 deletion src/server/ocsigen_server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ let start ?config () =
Unix.openfile
p
[Unix.O_WRONLY; Unix.O_CREAT; Unix.O_TRUNC] 0o640 in
ignore (Unix.write f spid 0 len);
ignore (Unix.write_substring f spid 0 len);
Unix.close f
in

Expand Down

0 comments on commit da2423b

Please sign in to comment.