-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for persistent connections in the client (#5)
- Loading branch information
1 parent
6229a06
commit f9339f3
Showing
24 changed files
with
881 additions
and
258 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
open! Core | ||
open Async | ||
|
||
open Httpaf | ||
open Httpaf_async | ||
|
||
let error_handler _ = assert false | ||
|
||
let main port host () = | ||
let where_to_connect = Tcp.Where_to_connect.of_host_and_port { host; port } in | ||
Tcp.connect_sock where_to_connect | ||
>>= fun socket -> | ||
let finished = Ivar.create () in | ||
let response_handler = Httpaf_examples.Client.print ~on_eof:(Ivar.fill finished) in | ||
let request_headers = | ||
Request.create ~headers:(Headers.of_list [ "host", host ]) `GET "/" | ||
in | ||
let connection = Client.create_connection socket in | ||
let request_body = | ||
Client.request | ||
connection | ||
~response_handler | ||
~error_handler | ||
request_headers | ||
in | ||
let finished' = Ivar.create () in | ||
let response_handler' = | ||
Httpaf_examples.Client.print ~on_eof:(Ivar.fill finished') | ||
in | ||
let request_body' = | ||
Client.request | ||
connection | ||
~response_handler:response_handler' | ||
~error_handler | ||
request_headers | ||
in | ||
Body.close_writer request_body'; | ||
Body.close_writer request_body; | ||
Async.Deferred.all_unit [Ivar.read finished; Ivar.read finished'] >>| fun () -> | ||
Client.shutdown connection | ||
;; | ||
|
||
let () = | ||
Command.async | ||
~summary:"Start a hello world Async client" | ||
Command.Param.( | ||
map (both | ||
(flag "-p" (optional_with_default 80 int) | ||
~doc:"int destination port") | ||
(anon ("host" %: string))) | ||
~f:(fun (port, host) -> | ||
(fun () -> main port host ()))) | ||
|> Command.run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
(executables | ||
(libraries httpaf httpaf-async httpaf_examples async core) | ||
(names async_echo_post async_get async_post)) | ||
(names async_echo_post async_get async_get_pipelined async_post)) | ||
|
||
(alias | ||
(name examples) | ||
(deps (glob_files *.exe))) | ||
(deps | ||
(glob_files *.exe))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
open Base | ||
open Lwt.Infix | ||
module Arg = Caml.Arg | ||
|
||
open Httpaf | ||
open Httpaf_lwt_unix | ||
|
||
let error_handler _ = assert false | ||
|
||
let main port host = | ||
Lwt_unix.getaddrinfo host (Int.to_string port) [Unix.(AI_FAMILY PF_INET)] | ||
>>= fun addresses -> | ||
let socket = Lwt_unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in | ||
Lwt_unix.connect socket (List.hd_exn addresses).Unix.ai_addr | ||
>>= fun () -> | ||
let finished, notify_finished = Lwt.wait () in | ||
let response_handler = | ||
Httpaf_examples.Client.print ~on_eof:(Lwt.wakeup_later notify_finished) | ||
in | ||
let request_headers = | ||
Request.create ~headers:(Headers.of_list [ "host", host ]) `GET "/" | ||
in | ||
Client.create_connection socket >>= fun connection -> | ||
let request_body = | ||
Client.request | ||
connection | ||
~response_handler | ||
~error_handler | ||
request_headers | ||
in | ||
let finished', notify_finished' = Lwt.wait () in | ||
let response_handler' = | ||
Httpaf_examples.Client.print ~on_eof:(Lwt.wakeup_later notify_finished') | ||
in | ||
let request_body' = | ||
Client.request | ||
connection | ||
~response_handler:response_handler' | ||
~error_handler | ||
request_headers | ||
in | ||
Body.close_writer request_body'; | ||
Body.close_writer request_body; | ||
Lwt.join [finished; finished'] >|= fun () -> | ||
Client.shutdown connection | ||
;; | ||
|
||
let () = | ||
let host = ref None in | ||
let port = ref 80 in | ||
Arg.parse | ||
["-p", Set_int port, " Port number (80 by default)"] | ||
(fun host_argument -> host := Some host_argument) | ||
"lwt_get.exe [-p N] HOST"; | ||
let host = | ||
match !host with | ||
| None -> failwith "No hostname provided" | ||
| Some host -> host | ||
in | ||
Lwt_main.run (main !port host) | ||
;; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.