Skip to content

Commit 491a11b

Browse files
committed
feature: cohttp_curl_async
1 parent c7187ad commit 491a11b

File tree

7 files changed

+465
-0
lines changed

7 files changed

+465
-0
lines changed

cohttp-curl-async.opam

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# This file is generated by dune, edit dune-project instead
2+
opam-version: "2.0"
3+
synopsis: "Cohttp client using Curl & Async as the backend"
4+
description: """
5+
An HTTP client that relies on Curl + Async for the backend. Does not require
6+
conduit for SSL."""
7+
maintainer: ["Anil Madhavapeddy <[email protected]>"]
8+
authors: [
9+
"Anil Madhavapeddy"
10+
"Stefano Zacchiroli"
11+
"David Sheets"
12+
"Thomas Gazagnaire"
13+
"David Scott"
14+
"Rudi Grinberg"
15+
"Andy Ray"
16+
]
17+
license: "ISC"
18+
homepage: "https://github.com/mirage/ocaml-cohttp"
19+
doc: "https://mirage.github.io/ocaml-cohttp/"
20+
bug-reports: "https://github.com/mirage/ocaml-cohttp/issues"
21+
depends: [
22+
"dune" {>= "2.9"}
23+
"ocurl"
24+
"stringext"
25+
"async_kernel"
26+
"async_unix"
27+
"async"
28+
"http" {= version}
29+
"odoc" {with-doc}
30+
]
31+
build: [
32+
["dune" "subst"] {dev}
33+
[
34+
"dune"
35+
"build"
36+
"-p"
37+
name
38+
"-j"
39+
jobs
40+
"--promote-install-files=false"
41+
"@install"
42+
"@runtest" {with-test}
43+
"@doc" {with-doc}
44+
]
45+
["dune" "install" "-p" name "--create-install-files" name]
46+
]
47+
dev-repo: "git+https://github.com/mirage/ocaml-cohttp.git"

cohttp-curl-async/bin/curl.ml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
open Cohttp
2+
module Curl = Cohttp_curl_async
3+
module Sexp = Sexplib0.Sexp
4+
open Async_kernel
5+
module Writer = Async_unix.Writer
6+
7+
let ( let* ) x f = Deferred.bind x ~f
8+
let ( let+ ) x f = Deferred.map x ~f
9+
10+
let client uri meth' () =
11+
let meth = Cohttp.Code.method_of_string meth' in
12+
let reply =
13+
let context = Curl.Context.create () in
14+
let request =
15+
Curl.Request.create ~timeout_ms:5000 meth ~uri ~input:Curl.Source.empty
16+
~output:Curl.Sink.string
17+
in
18+
Curl.submit context request
19+
in
20+
let* resp, response_body =
21+
Deferred.both (Curl.Response.response reply) (Curl.Response.body reply)
22+
in
23+
Format.eprintf "response:%a@.%!" Sexp.pp_hum (Response.sexp_of_t resp);
24+
let status = Response.status resp in
25+
(match Code.is_success (Code.code_of_status status) with
26+
| false -> prerr_endline (Code.string_of_status status)
27+
| true -> ());
28+
let output_body c =
29+
Writer.write c response_body;
30+
Writer.flushed c
31+
in
32+
output_body (Lazy.force Writer.stdout)
33+
34+
let debug =
35+
match Sys.getenv_opt "COHTTP_CURL_DEBUG" with None -> false | Some _ -> true
36+
37+
let _ =
38+
let open Async_command in
39+
async_spec ~summary:"Fetch URL and print it"
40+
Spec.(
41+
empty
42+
+> anon ("url" %: string)
43+
+> flag "-X" (optional_with_default "GET" string) ~doc:" Set HTTP method")
44+
client
45+
|> Command_unix.run

cohttp-curl-async/bin/dune

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
(executable
2+
(name curl)
3+
(libraries
4+
sexplib0
5+
cohttp
6+
cohttp_curl_async
7+
async_unix
8+
async_kernel
9+
async.async_command
10+
core_unix.command_unix
11+
logs
12+
logs.cli
13+
fmt.tty))

0 commit comments

Comments
 (0)