Skip to content

Commit 535a779

Browse files
author
Vincent Balat
committed
Fix bug in Ocsigen_range
(was broken for big files with range > 8192 bytes)
1 parent a9abb2f commit 535a779

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

src/server/ocsigen_range.ml

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,17 @@ let select_range length beg endopt skipfun stream =
4444
then Ocsigen_stream.empty None
4545
else
4646
(match step with
47-
| Ocsigen_stream.Finished _ ->
47+
| Ocsigen_stream.Finished _ ->
4848
Lwt.fail Ocsigen_stream.Stream_too_small
4949
| Ocsigen_stream.Cont (c, f) -> Lwt.return (c, f))
5050
>>= fun (buf, nextstream) ->
5151
let buflen = String.length buf in
5252
let buflen64 = Int64.of_int buflen in
5353
if (Int64.compare buflen64 num) <= 0
54-
then
55-
Ocsigen_stream.next nextstream >>= fun next ->
56-
Ocsigen_stream.cont buf (aux next (Int64.sub num buflen64))
54+
then
55+
Ocsigen_stream.cont buf (fun () ->
56+
Ocsigen_stream.next nextstream >>= fun next ->
57+
aux next (Int64.sub num buflen64) ())
5758
else
5859
Ocsigen_stream.cont (String.sub buf 0 (Int64.to_int num))
5960
(fun () -> Ocsigen_stream.empty None)
@@ -63,12 +64,12 @@ let select_range length beg endopt skipfun stream =
6364
skipfun stream beg >>= fun new_s ->
6465
Lwt.return
6566
(match endopt with
66-
| None ->
67+
| None ->
6768
Ocsigen_stream.make
6869
~finalize:
6970
(fun status -> Ocsigen_stream.finalize stream status)
7071
(fun () -> Lwt.return new_s)
71-
| Some endc ->
72+
| Some endc ->
7273
Ocsigen_stream.make
7374
~finalize:
7475
(fun status -> Ocsigen_stream.finalize stream status)
@@ -93,14 +94,14 @@ let compute_range ri res =
9394
else begin
9495
let res = {res with
9596
Ocsigen_http_frame.res_headers =
96-
Http_headers.replace
97+
Http_headers.replace
9798
Http_headers.accept_ranges "bytes"
9899
res.Ocsigen_http_frame.res_headers;
99100
}
100101
in
101102
match change_range (Lazy.force ri.Ocsigen_extensions.ri_range) with
102103
| None -> Lwt.return res
103-
| Some (_, _, Ocsigen_extensions.IR_ifmatch etag)
104+
| Some (_, _, Ocsigen_extensions.IR_ifmatch etag)
104105
when (match res.Ocsigen_http_frame.res_etag with
105106
| None -> true
106107
| Some resetag -> String.compare etag resetag <> 0) ->
@@ -118,7 +119,7 @@ let compute_range ri res =
118119
(if Int64.compare cl beg <= 0
119120
then Lwt.fail Range_416
120121
else Lwt.return ()) >>= fun () ->
121-
122+
122123
let endc, length = match endopt with
123124
| None -> (Int64.sub cl 1L, Int64.sub cl beg)
124125
| Some e -> (e, Int64.add (Int64.sub e beg) 1L)
@@ -128,25 +129,25 @@ let compute_range ri res =
128129
res.Ocsigen_http_frame.res_stream
129130
in
130131
(* stream transform *)
131-
let skipfun =
132+
let skipfun =
132133
match skipfun with
133-
| None ->
134+
| None ->
134135
(fun stream beg ->
135-
(Ocsigen_stream.next
136+
(Ocsigen_stream.next
136137
(Ocsigen_stream.get stream) >>= fun s ->
137138
Ocsigen_stream.skip s beg))
138139
| Some f -> f
139140
in
140-
select_range
141+
select_range
141142
length beg endopt skipfun
142143
resstream
143144
>>= fun new_s ->
144-
Lwt.return
145+
Lwt.return
145146
{res with
146147
Ocsigen_http_frame.res_stream = (new_s, None);
147148
Ocsigen_http_frame.res_code = 206;
148149
Ocsigen_http_frame.res_headers =
149-
Http_headers.replace
150+
Http_headers.replace
150151
Http_headers.content_range
151152
("bytes "^Int64.to_string beg^"-"^
152153
Int64.to_string endc^"/"^
@@ -163,7 +164,7 @@ let compute_range ri res =
163164
{dr with
164165
Ocsigen_http_frame.res_code = 416;
165166
Ocsigen_http_frame.res_headers =
166-
Http_headers.replace
167+
Http_headers.replace
167168
Http_headers.content_range
168169
("bytes */"^Int64.to_string cl)
169170
res.Ocsigen_http_frame.res_headers;
@@ -180,7 +181,7 @@ let get_range http_frame =
180181
Http_headers.range
181182
in
182183

183-
let decode_int index d e =
184+
let decode_int index d e =
184185
let a = Int64.of_string d in
185186
let b = Int64.of_string e in
186187
assert (Int64.compare index a < 0);
@@ -196,7 +197,7 @@ let get_range http_frame =
196197
let l = String.split ',' b in
197198
let rec f index = function
198199
| [] -> [], None
199-
| [a] ->
200+
| [a] ->
200201
let d, e = String.sep '-' a in
201202
if e = ""
202203
then [], Some (Int64.of_string d)
@@ -210,8 +211,8 @@ let get_range http_frame =
210211
f (-1L) l
211212
in
212213

213-
let ifrange =
214-
try
214+
let ifrange =
215+
try
215216
let ifrangeheader = Ocsigen_http_frame.Http_header.get_headers_value
216217
http_frame.Ocsigen_http_frame.frame_header
217218
Http_headers.if_range

0 commit comments

Comments
 (0)