-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patht-ev.sml
85 lines (68 loc) · 2.54 KB
/
t-ev.sml
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
open CurlHTTP
fun main_handle () = Curl.withCurl ( fn () =>
let
val _ = print ((Curl.version ()) ^ "\n")
open Curl
open Ev
val ev = evInit ()
val ev_continue = ref 0
val timer_id = evTimerNew ev
val _ = evTimerAdd ev (timer_id, Time.fromSeconds(5 * 60), fn () => (print "END timeout\n" ; ev_continue := 0 ));
val multi = Multi.init () (* ToDo val withCurlMulti : (multi -> unit) -> init *)
val curl_ev = CurlEv.curl_ev ev multi
val urls = ref [
"https://www.google.com/",
"http://sml-family.org/",
"http://www.polyml.org/",
"http://www.mlton.org/",
"http://melsman.github.io/mlkit",
"http://www.mpi-sws.org/~rossberg/hamlet/",
"http://www.smlnj.org/",
"http://www.pllab.riec.tohoku.ac.jp/smlsharp/",
"http://manticore.cs.uchicago.edu/"
]
fun doUrl 0 = []
| doUrl n = "https://edition.cnn.com/"::"https://www.bbc.com/"::(doUrl (n - 1))
(* val urls = ref (doUrl 3) *)
val opt = [
(* (HttpVerbose true), *)
(HttpFollowLocation true),
(HttpSSLVerifyPeer false),
(HttpSSLVerifyHost false),
(HttpMaxRedirs 5),
(HttpCookieFile ""),
(HttpUserAgent "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"),
(* (HttpProxy "127.0.0.1:3128"), *)
(HttpTimeout 30),
(HttpAcceptEncoding ""),
(HttpHeaders [("Accept", "*/*"), ("Accept-Language", "*")])
]
fun cb add_request url (is_success, status, reason, new_url, body, headers, redirects) =
(
ev_continue := (!ev_continue) - 1;
(
if is_success
then print ("Answer: " ^ (Int.toString status) ^ " (" ^ reason ^ ") " ^ new_url ^ "\n")
else print ("Answer: " ^ (Int.toString status) ^ " (" ^ reason ^ ") " ^ url ^ "\n")
);
add_request ()
)
fun add_request () =
case (!urls) of
[] => ()
| (u::us) => (
urls := us;
ev_continue := (!ev_continue) + 1;
print ("Request: " ^ u ^ "\n");
doHttpEv curl_ev u opt (cb add_request u)
)
fun doN f 0 = ()
| doN f n = ( f (); doN f (n - 1) )
in
doN add_request 5;
while (!ev_continue) > 0 do evWait ev (SOME (Time.fromSeconds 3));
Multi.cleanup multi;
()
end
)
fun main () = main_handle () handle exc => print ("function main raised an exception: " ^ exnMessage exc ^ "\n")