package mechaml

  1. Overview
  2. Docs

Source file agent.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
(*{{{ Copyright (C) 2016, Yann Hamdaoui <yann.hamdaoui@centraliens.net>
  Permission to use, copy, modify, and/or distribute this software for any
  purpose with or without fee is hereby granted, provided that the above
  copyright notice and this permission notice appear in all copies.

  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
  THIS SOFTWARE.
}}}*)

let (>>=) = Lwt.(>>=)
let (>|=) = Lwt.(>|=)

open Cohttp
open Cohttp_lwt_unix

type http_status_code = Code.status_code
type http_headers = Header.t

module HttpResponse = struct
  type t = {
    location : Uri.t;
    cohttp_response : Response.t;
    content : string
  }

  let location r = r.location

  let status r =
    Response.status r.cohttp_response

  let status_code r =
    r.cohttp_response
    |> Response.status
    |> Code.code_of_status

  let headers r =
    Response.headers r.cohttp_response

  let content r = r.content

  let page r =
    content r
    |> Page.from_string ~location:r.location

  let cohttp_response r = r.cohttp_response

  let make ~location ~cohttp_response ~content = {location; cohttp_response;
    content}
end

type t = {
  cookie_jar : Cookiejar.t;
  client_headers : Header.t;
  max_redirect : int;
  redirect : int
}

type result = t * HttpResponse.t

let default_max_redirect = 5

let init ?(max_redirect = default_max_redirect) _ =
  { cookie_jar = Cookiejar.empty;
    client_headers = Header.init ();
    max_redirect;
    redirect = 0}

let rec redirect (agent,r) =
  match r |> HttpResponse.cohttp_response |> Response.status with
    | `Moved_permanently
    | `Found ->
      (match Header.get (HttpResponse.headers r) "Location" with
        | Some loc ->
          { agent with redirect = succ agent.redirect}
          |> get loc
        | None -> Lwt.return ({ agent with redirect = 0 },r) )
    | _ -> Lwt.return ({ agent with redirect = 0 },r)

and update_agent location agent (cohttp_response,body) =
  let headers = Response.headers cohttp_response in
  let agent =
    {agent with cookie_jar =
      Cookiejar.add_from_headers location headers agent.cookie_jar}
  in
  body
  |> Cohttp_lwt.Body.to_string
  >>= (function content ->
    if agent.redirect < agent.max_redirect then
      redirect (agent, HttpResponse.make ~location ~cohttp_response ~content)
    else
      Lwt.return ({ agent with redirect=0 }, HttpResponse.make ~location
        ~cohttp_response ~content))

and get_uri uri agent =
  let headers = agent.cookie_jar
    |> Cookiejar.add_to_headers uri agent.client_headers in
  Client.get ~headers uri
  >>= update_agent uri agent

and get uri_string agent =
  get_uri (Uri.of_string uri_string) agent

let click link = link |> Page.Link.uri |> get_uri

let post_uri uri content agent =
  let headers = agent.cookie_jar
    |> Cookiejar.add_to_headers uri agent.client_headers in
  Client.post ~headers:headers ~body:(Cohttp_lwt.Body.of_string content) uri
  >>= update_agent uri agent

let post uri_string content agent =
  post_uri (Uri.of_string uri_string) content agent

let submit form agent =
  let uri = Page.Form.uri form in
  let params = Page.Form.values form in
  let headers = agent.cookie_jar
    |> Cookiejar.add_to_headers uri agent.client_headers in
  match Page.Form.meth form with
    | `POST ->
      Client.post_form ~headers:headers ~params:params uri
      >>= update_agent uri agent
    | `GET ->
      let target = Uri.with_query uri params in
      get_uri target agent

let save_content file data =
  Lwt_io.open_file ~mode:Lwt_io.output file
  >>= (fun out ->
    Lwt_io.write out data
    |> ignore;
    Lwt_io.close out)

let save_image file image agent =
  let uri = Page.Image.uri image in
  agent
  |> get_uri uri
  >>= (function (agent,response) ->
    save_content file (HttpResponse.content response)
    >|= fun _ -> (agent,response))

let cookie_jar agent = agent.cookie_jar
let set_cookie_jar cookie_jar agent = {agent with cookie_jar = cookie_jar}
let add_cookie cookie agent =
  {agent with cookie_jar = Cookiejar.add cookie agent.cookie_jar}
let remove_cookie cookie agent =
  {agent with cookie_jar = Cookiejar.remove cookie agent.cookie_jar}

let client_headers agent = agent.client_headers
let set_client_headers headers agent = {agent with client_headers = headers}
let add_client_header header value agent =
  {agent with client_headers = Header.add agent.client_headers header value}
let remove_client_header header agent =
  {agent with client_headers = Header.remove agent.client_headers header}

let set_max_redirect max_redirect agent = {agent with max_redirect }

module Monad = struct
  type 'a m = t -> (t * 'a) Lwt.t

  let bind x f =
    fun agent ->
      Lwt.bind (x agent) (fun (agent,result) ->
        f result agent)

  let return x =
    fun agent -> Lwt.return (agent,x)

  let map f x =
    bind x (function y ->
      f y
      |> return)

  let return_from_lwt x =
    fun agent ->
      Lwt.bind x (fun y ->
        Lwt.return (agent,y))

  let run agent x =
    Lwt_main.run (x agent)

  let fail e = Lwt.fail e |> return_from_lwt

  let fail_with s = Lwt.fail_with s |> return_from_lwt

  let catch x c =
    fun agent ->
      let try_lwt = fun _ -> x () agent in
      let catch_lwt = fun e -> c e agent in
      Lwt.catch try_lwt catch_lwt

  let try_bind x f c =
    catch (fun _ -> bind (x ()) f) c

  module Infix = struct
    let (>>=) = bind

    let (=<<) f x = x >>= f

    let (>>) x y = x >>= (fun _ -> y)

    let (<<) y x = x >> y

    let (>|=) x f = x |> map f

    let (=|<) f x = x |> map f
  end

  module Syntax = struct
    let (let*) = bind

    let (and*) x y =
      fun agent ->
        let x' = x agent
        and y' = y agent in
        Lwt.bind x' (fun (_,x'') ->
          Lwt.bind y' (fun (_,y'') ->
            Lwt.return (agent,(x'',y''))))

    let (let+) x f = x |> map f

    let (and+) = (and*)
  end

  module List = struct
    let iter_s f l =
      l
      |> List.map f
      |> List.fold_left Infix.(>>) (return ())

    let iter_p f l =
      fun agent ->
        let it x =
          f x agent
          >|= fun _ -> () in
        l
        |> Lwt_list.iter_p it
        >|= fun _ -> (agent,())

    let iteri_s f l =
      l
      |> List.mapi f
      |> List.fold_left Infix.(>>) (return ())

    let iteri_p f l =
      fun agent ->
        let it i x =
          f i x agent
          >|= fun _ -> () in
        l
        |> Lwt_list.iteri_p it
        >|= fun _ -> (agent,())

    let appendM listM xM =
      let open Infix in
      listM >>= fun l ->
      xM >|= fun x ->
        x::l

    let map_s f l =
      l
      |> List.map f
      |> List.fold_left appendM (return [])

    let map_p f l =
      fun agent ->
        let f' x =
          f x agent
          >|= snd in
        l
        |> Lwt_list.map_p f'
        >|= fun l ->
          (agent,l)

    let mapi_s f l =
      l
      |> List.mapi f
      |> List.fold_left appendM (return [])

    let mapi_p f l =
      fun agent ->
        let f' i x =
          f i x agent
          >|= snd in
        l
        |> Lwt_list.mapi_p f'
        >|= fun l ->
          (agent,l)

    let fold_left_s f e l =
      let f' accuM x =
        let open Infix in
        accuM >>= fun accu ->
        f accu x in
      List.fold_left f' (return e) l

    let fold_right_s f l e =
      let f' x accuM =
        let open Infix in
        accuM >>= fun accu ->
        f x accu in
      List.fold_right f' l (return e)
  end

  let set new_agent _ =
    Lwt.return (new_agent,())

  let get agent =
    Lwt.return (agent,agent)

  let save_content data file =
    save_content data file
    |> return_from_lwt

  let monadic_get g =
    fun agent ->
      Lwt.return (agent, g agent)

  let monadic_set s =
    fun agent ->
      Lwt.return (s agent, ())

  let cookie_jar = monadic_get cookie_jar

  let set_cookie_jar jar =
    set_cookie_jar jar
    |> monadic_set

  let add_cookie cookie =
    add_cookie cookie
    |> monadic_set

  let remove_cookie cookie =
    remove_cookie cookie
    |> monadic_set

  let client_headers = monadic_get client_headers

  let set_client_headers headers =
    set_client_headers headers
    |> monadic_set

  let add_client_header key value =
    add_client_header key value
    |> monadic_set

  let remove_client_header key =
    remove_client_header key
    |> monadic_set

  let set_max_redirect n =
    set_max_redirect n
    |> monadic_set
end
OCaml

Innovation. Community. Security.