mirror of
https://github.com/c-cube/ezcurl.git
synced 2026-01-28 12:24:51 -05:00
Provide seek function when uploading from string
This prevents CURLE_SEND_FAIL_REWIND (65) When doing a send operation curl had to rewind the data to retransmit, but the rewinding operation failed. on redirections
This commit is contained in:
parent
a0a5b989b7
commit
0c1ff862dd
1 changed files with 17 additions and 4 deletions
|
|
@ -358,21 +358,32 @@ module Make (IO : IO) : S with type 'a io = 'a IO.t = struct
|
||||||
|
|
||||||
type 'a io = 'a IO.t
|
type 'a io = 'a IO.t
|
||||||
|
|
||||||
let content_read_fun_ content =
|
let content_read_funs_ content =
|
||||||
match content with
|
match content with
|
||||||
| `String s ->
|
| `String s ->
|
||||||
let n = ref 0 in
|
let n = ref 0 in
|
||||||
fun i ->
|
let read i =
|
||||||
let len = min i (String.length s - !n) in
|
let len = min i (String.length s - !n) in
|
||||||
let r = String.sub s !n len in
|
let r = String.sub s !n len in
|
||||||
n := !n + len;
|
n := !n + len;
|
||||||
r
|
r
|
||||||
|
and seek i o =
|
||||||
|
begin match o with
|
||||||
|
| Curl.SEEK_SET -> n := Int64.to_int i
|
||||||
|
| SEEK_END -> n := String.length s + Int64.to_int i
|
||||||
|
| SEEK_CUR -> n := !n + Int64.to_int i
|
||||||
|
end;
|
||||||
|
Curl.SEEKFUNC_OK
|
||||||
|
in read, seek
|
||||||
| `Write f ->
|
| `Write f ->
|
||||||
let buf = Bytes.create 1024 in
|
let buf = Bytes.create 1024 in
|
||||||
fun i ->
|
let read i =
|
||||||
let len = min i (Bytes.length buf) in
|
let len = min i (Bytes.length buf) in
|
||||||
let n = f buf len in
|
let n = f buf len in
|
||||||
Bytes.sub_string buf i n
|
Bytes.sub_string buf i n
|
||||||
|
and seek _ _ =
|
||||||
|
Curl.SEEKFUNC_CANTSEEK
|
||||||
|
in read, seek
|
||||||
|
|
||||||
let content_size_ = function
|
let content_size_ = function
|
||||||
| `String s -> Some (String.length s)
|
| `String s -> Some (String.length s)
|
||||||
|
|
@ -403,7 +414,9 @@ module Make (IO : IO) : S with type 'a io = 'a IO.t = struct
|
||||||
|
|
||||||
(* TODO: ability to make content a stream with a `read` function *)
|
(* TODO: ability to make content a stream with a `read` function *)
|
||||||
opt_iter content ~f:(fun content ->
|
opt_iter content ~f:(fun content ->
|
||||||
Curl.set_readfunction self.curl (content_read_fun_ content);
|
let read, seek = content_read_funs_ content in
|
||||||
|
Curl.set_readfunction self.curl read;
|
||||||
|
Curl.set_seekfunction self.curl seek;
|
||||||
(* also set size if known *)
|
(* also set size if known *)
|
||||||
match content_size_ content, meth with
|
match content_size_ content, meth with
|
||||||
| None, _ ->
|
| None, _ ->
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue