<commit_before>
open Lwt

let o f g x = f (g x)

let rec unlines = function
  | []    -> ""
  | [x]   -> x
  | x::xs -> x ^ "\r\n" ^ unlines xs

let cs_of_lines = o Cstruct.of_string unlines

let http_client host port =
  lwt sock = Tls_lwt.connect host port in
  let req  = cs_of_lines [
    "GET / HTTP/1.1" ; "Host: " ^ host ; "" ; "" ;
  ] in
  Tls_lwt.write sock req >> Tls_lwt.read sock >>= o Lwt_io.print Cstruct.to_string

let yap ~tag msg = Lwt_io.printf "[%s] %s\n%!" tag msg

let serve_ssl port callback =

  let server_s =
    let open Lwt_unix in
    let s = socket PF_INET SOCK_STREAM 0 in
    bind s (ADDR_INET (Unix.inet_addr_any, port)) ;
    listen s 10 ;
    s in

  let rec loop () =
    lwt (socket, addr) = Tls_lwt.accept server_s in
    yap ~tag:"server" "-> connect" >>
    let _ =
      try_lwt callback socket addr with
      | exn -> yap ~tag:"server" "+ handler error" in
    loop () in
  yap ~tag:"server" ("-> start @ " ^ string_of_int port) >>
  loop ()


let echo_server port =
  serve_ssl port @@ fun socket addr ->
    yap ~tag:"handler" "-> incoming" >>
    let rec loop () =
      try_lwt
        lwt data = Tls_lwt.read socket in
        yap ~tag:"handler" ("recv: " ^ Cstruct.to_string data) >>
        Tls_lwt.write socket data >>
        loop ()
      with End_of_file -> yap ~tag:"handler" "eof."
    in
    loop ()

let http_client_main () =
  match Sys.argv with
  | [| _ ; host ; port |] -> Lwt_main.run (http_client host port)
  | [| _ ; host |]        -> Lwt_main.run (http_client host "443")
  | args                  -> Printf.eprintf "%s <host> <port>\n%!" args.(0)

let echo_server_main () =
  let port =<commit_msg>switch the example<commit_after>
open Lwt

let o f g x = f (g x)

let rec unlines = function
  | []    -> ""
  | [x]   -> x
  | x::xs -> x ^ "\r\n" ^ unlines xs

let cs_of_lines = o Cstruct.of_string unlines

let http_client host port =
  lwt sock = Tls_lwt.connect host port in
  let req  = cs_of_lines [
    "GET / HTTP/1.1" ; "Host: " ^ host ; "" ; "" ;
  ] in
  Tls_lwt.write sock req >> Tls_lwt.read sock >>= o Lwt_io.print Cstruct.to_string

let yap ~tag msg = Lwt_io.printf "[%s] %s\n%!" tag msg

let serve_ssl port callback =

  lwt cert =
    X509_lwt.cert_of_pems ~cert:"server.pem" ~priv_key:"server.key"
  in

  let server_s =
    let open Lwt_unix in
    let s = socket PF_INET SOCK_STREAM 0 in
    bind s (ADDR_INET (Unix.inet_addr_any, port)) ;
    listen s 10 ;
    s in

  let rec loop () =
    lwt (socket, addr) = Tls_lwt.accept ~cert server_s in
    yap ~tag:"server" "-> connect" >>
    let _ =
      try_lwt callback socket addr with
      | exn -> yap ~tag:"server" "+ handler error" in
    loop () in
  yap ~tag:"server" ("-> start @ " ^ string_of_int port) >>
  loop ()


let echo_server port =
  serve_ssl port @@ fun socket addr ->
    yap ~tag:"handler" "-> incoming" >>
    let rec loop () =
      try_lwt
        lwt data = Tls_lwt.read socket in
        yap ~tag:"handler" ("recv: " ^ Cstruct.to_string data) >>
        Tls_lwt.write socket data >>
        loop ()
      with End_of_file -> yap ~tag:"handler" "eof."
    in
    loop ()

let http_client_main () =
  match Sys.argv with
  | [| _ ; host ; port |] -> Lwt_main.run (http_client host port)
  | [| _ ; host |]        -> Lwt_main.run (http_client host "443")
  | args                  -> Printf.eprintf "%s <host> <port>\n%!" args.(0)

let echo_server_main () =
  let port =
{'fraction_non_alphanumeric': 0.13965014577259474, 'fraction_numerical': 0.006122448979591836, 'mean_word_length': 2.3936696340257173, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1305769', 'n_tokens_mistral': 1336, 'n_tokens_neox': 1304, 'n_words': 375}