mirror of
https://github.com/c-cube/ezcurl.git
synced 2026-03-13 17:16:16 -04:00
Compare commits
1 commit
def9411b72
...
aa5474167f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aa5474167f |
5 changed files with 46 additions and 51 deletions
|
|
@ -23,7 +23,6 @@
|
||||||
(ocurl
|
(ocurl
|
||||||
(>= 0.8))
|
(>= 0.8))
|
||||||
(odoc :with-doc)
|
(odoc :with-doc)
|
||||||
(tiny_httpd (and (>= 0.19) :with-test))
|
|
||||||
(ocaml
|
(ocaml
|
||||||
(>= 4.03))))
|
(>= 4.03))))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,6 @@ depends: [
|
||||||
"dune" {>= "3.0"}
|
"dune" {>= "3.0"}
|
||||||
"ocurl" {>= "0.8"}
|
"ocurl" {>= "0.8"}
|
||||||
"odoc" {with-doc}
|
"odoc" {with-doc}
|
||||||
"tiny_httpd" {>= "0.19" & with-test}
|
|
||||||
"ocaml" {>= "4.03"}
|
"ocaml" {>= "4.03"}
|
||||||
]
|
]
|
||||||
build: [
|
build: [
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,39 @@
|
||||||
get: OK
|
get: OK
|
||||||
code=200,body=```
|
body=```
|
||||||
hello jeanjacques
|
version = 0.27.0
|
||||||
|
profile=conventional
|
||||||
|
margin=80
|
||||||
|
if-then-else=k-r
|
||||||
|
parens-ite=true
|
||||||
|
parens-tuple=multi-line-only
|
||||||
|
sequence-style=terminator
|
||||||
|
type-decl=sparse
|
||||||
|
break-cases=toplevel
|
||||||
|
cases-exp-indent=2
|
||||||
|
field-space=tight-decl
|
||||||
|
leading-nested-match-parens=true
|
||||||
|
module-item-spacing=compact
|
||||||
|
quiet=true
|
||||||
|
ocaml-version=4.08.0
|
||||||
|
|
||||||
```
|
```
|
||||||
streaming get: OK
|
streaming get: OK
|
||||||
code=200, body=```
|
body=```
|
||||||
hello reineclaude
|
version = 0.27.0
|
||||||
|
profile=conventional
|
||||||
|
margin=80
|
||||||
|
if-then-else=k-r
|
||||||
|
parens-ite=true
|
||||||
|
parens-tuple=multi-line-only
|
||||||
|
sequence-style=terminator
|
||||||
|
type-decl=sparse
|
||||||
|
break-cases=toplevel
|
||||||
|
cases-exp-indent=2
|
||||||
|
field-space=tight-decl
|
||||||
|
leading-nested-match-parens=true
|
||||||
|
module-item-spacing=compact
|
||||||
|
quiet=true
|
||||||
|
ocaml-version=4.08.0
|
||||||
|
|
||||||
```
|
```
|
||||||
|
same buf? true
|
||||||
|
|
|
||||||
|
|
@ -1,44 +1,17 @@
|
||||||
module H = Tiny_httpd
|
let body = ref ""
|
||||||
|
|
||||||
(** Start server, return its port and a thread *)
|
let url =
|
||||||
let start_server () : int * Thread.t =
|
"https://raw.githubusercontent.com/c-cube/ezcurl/refs/heads/main/.ocamlformat"
|
||||||
let port = ref (-1) in
|
|
||||||
let cond = Condition.create () in
|
|
||||||
let mutex = Mutex.create () in
|
|
||||||
|
|
||||||
let server = H.create ~masksigpipe:true ~addr:"127.0.0.1" ~port:0 () in
|
let () =
|
||||||
H.add_route_handler server
|
|
||||||
H.Route.(exact "test" @/ string @/ return)
|
|
||||||
(fun str _req ->
|
|
||||||
H.Response.make_string ~code:200 @@ Ok (Printf.sprintf "hello %s" str));
|
|
||||||
let th =
|
|
||||||
Thread.create
|
|
||||||
(H.run_exn ~after_init:(fun () ->
|
|
||||||
port := H.port server;
|
|
||||||
Condition.broadcast cond))
|
|
||||||
server
|
|
||||||
in
|
|
||||||
|
|
||||||
(* wait for server to start *)
|
|
||||||
while !port < 0 do
|
|
||||||
Mutex.lock mutex;
|
|
||||||
Condition.wait cond mutex;
|
|
||||||
Mutex.unlock mutex
|
|
||||||
done;
|
|
||||||
!port, th
|
|
||||||
|
|
||||||
let test1 ~port () =
|
|
||||||
let name = "jeanjacques" in
|
|
||||||
let url = Printf.sprintf "http://127.0.0.1:%d/test/%s" port name in
|
|
||||||
match Ezcurl.get ~url () with
|
match Ezcurl.get ~url () with
|
||||||
| Error (code, msg) ->
|
| Error (code, msg) ->
|
||||||
Format.eprintf "curl error: code `%s` (%s)@." (Curl.strerror code) msg
|
Format.eprintf "curl error: code `%s` (%s)@." (Curl.strerror code) msg
|
||||||
| Ok res ->
|
| Ok res ->
|
||||||
Format.printf "get: OK@.code=%d,body=```@.%s@.```@." res.code res.body
|
body := res.body;
|
||||||
|
Format.printf "get: OK@.body=```@.%s@.```@." !body
|
||||||
|
|
||||||
let test2 ~port () =
|
let () =
|
||||||
let name = "reineclaude" in
|
|
||||||
let url = Printf.sprintf "http://127.0.0.1:%d/test/%s" port name in
|
|
||||||
let buf = Buffer.create 32 in
|
let buf = Buffer.create 32 in
|
||||||
match
|
match
|
||||||
Ezcurl.http_stream ~meth:GET ~url
|
Ezcurl.http_stream ~meth:GET ~url
|
||||||
|
|
@ -51,13 +24,7 @@ let test2 ~port () =
|
||||||
with
|
with
|
||||||
| Error (code, msg) ->
|
| Error (code, msg) ->
|
||||||
Format.eprintf "curl error: code `%s` (%s)@." (Curl.strerror code) msg
|
Format.eprintf "curl error: code `%s` (%s)@." (Curl.strerror code) msg
|
||||||
| Ok res ->
|
| Ok _res ->
|
||||||
let new_body = Buffer.contents buf in
|
let new_body = Buffer.contents buf in
|
||||||
Format.printf "streaming get: OK@.code=%d, body=```@.%s@.```@." res.code
|
Format.printf "streaming get: OK@.body=```@.%s@.```@." new_body;
|
||||||
new_body
|
Format.printf "same buf? %b@." (new_body = !body)
|
||||||
|
|
||||||
let () =
|
|
||||||
let port, _th = start_server () in
|
|
||||||
test1 ~port ();
|
|
||||||
test2 ~port ();
|
|
||||||
exit 0
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
(test
|
(test
|
||||||
(name basic_test)
|
(name basic_test)
|
||||||
(package ezcurl)
|
(libraries ezcurl))
|
||||||
(libraries ezcurl tiny_httpd threads.posix))
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue