package bitcoin-ocurl

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file bitcoin_ocurl.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
(********************************************************************************)
(*  Bitcoin_ocurl.ml
    Copyright (c) 2013 Vincent Bernardoff <vb@luminar.eu.org>
*)
(********************************************************************************)

(** Offers an implementation of a {!Bitcoin.HTTPCLIENT} using OCurl's
    [Http_client].
*)

(********************************************************************************)
(** {1 Public modules}                                                          *)
(********************************************************************************)

module Httpclient : Bitcoin.HTTPCLIENT with type 'a Monad.t = 'a = struct
  module Monad = struct
    type 'a t = 'a

    let return x = x
    let fail x = raise x
    let bind t f = f t

    let catch t f =
      try t () with
      | exc -> f exc
  end

  let buf = Buffer.create 100

  let post_string ~headers ~inet_addr:_ ~host ~port ~uri request =
    let open Curl in
    let h = new handle in
    let dst = "http://" ^ host ^ ":" ^ string_of_int port ^ uri in
    try
      h#set_post true;
      h#set_postfields request;
      h#set_postfieldsize (String.length request);
      h#set_url dst;
      h#set_writefunction (fun s ->
          Buffer.add_string buf s;
          String.length s);
      h#set_httpheader (List.map (fun (k, v) -> k ^ ": " ^ v) headers);
      h#perform;
      h#cleanup;
      let contents = Buffer.contents buf in
      Buffer.reset buf;
      contents
    with
    | CurlException (_, i, s) as exn ->
      Printf.eprintf "EXN: %d %s\n%!" i s;
      raise exn
end
OCaml

Innovation. Community. Security.