Skip to content

Instantly share code, notes, and snippets.

@pdonadeo
Created January 8, 2017 16:33
Show Gist options
  • Save pdonadeo/b56688193e5406d5d59d83487376e902 to your computer and use it in GitHub Desktop.
Save pdonadeo/b56688193e5406d5d59d83487376e902 to your computer and use it in GitHub Desktop.
A non SSL program to test everything
open Core.Std
open Async.Std
open Log.Global
open Re2.Std
(* ocamlbuild -use-ocamlfind -pkgs re2,async src/test.native *)
let request qs = Printf.sprintf "GET /u/%s HTTP/1.1
Host: www.donadeo.net
Connection: close
Pragma: no-cache
Cache-Control: no-cache
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: en-US,en;q=0.8,it;q=0.6\n\n" qs
let rand_string n =
let rand_string = String.make n '_' in
for i = 0 to (n - 1) do
rand_string.[i] <- Char.of_int_exn ((Random.int 25) + 97);
done;
rand_string
let http_first_line_regex = Re2.create_exn "^HTTP/1\\.1\\s+(\\d+)\\s+(.*)$"
let get () =
(* Connect the socket *)
Tcp.connect (Tcp.to_host_and_port "donadeo.net" 80) >>= fun (socket, app_rd, app_wr) ->
(* Send the request *)
Writer.write app_wr (request (rand_string 16));
Writer.flushed app_wr >>= fun () ->
(* Read the response *)
Reader.contents app_rd >>= fun response_str ->
(* Parse response code *)
let first_line = String.split_lines response_str |> List.hd_exn |> String.strip in
let tokens = Re2.find_submatches_exn http_first_line_regex first_line in
let code = Option.value_exn (tokens.(1)) |> Int.of_string in
let message = Option.value_exn (tokens.(2)) in
(* Close *)
don't_wait_for (
Writer.close app_wr >>= fun () ->
Reader.close app_rd
);
return (code, message)
let gc_loop () =
let rec loop () =
info "Gc.compact ()";
Gc.compact ();
(after (sec 60.)) >>= loop in
loop ()
let rec loop ?(i=1) ?(errors=0.0) () =
info "Call number %06d" i;
get () >>= fun (code, message) ->
let errors = if code = 200 then 0.0 else errors +. 1.0 in
info "Server replied: %d \"%s\"" code message;
(after (sec 1.0)) >>= fun () ->
loop ~i:(i+1) ~errors ()
let main () =
loop () |> don't_wait_for;
gc_loop () |> don't_wait_for;
(* never_returns (Scheduler.go ()) *)
Deferred.never ()
let () = Command.(async ~summary:"NON SSL test" Spec.empty main |> run)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment