File: client.rb

package info (click to toggle)
ruby-protocol-http1 0.35.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 376 kB
  • sloc: ruby: 2,367; makefile: 4
file content (40 lines) | stat: -rwxr-xr-x 923 bytes parent folder | download
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
#!/usr/bin/env ruby
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2019-2025, by Samuel Williams.

$LOAD_PATH.unshift File.expand_path("../../../lib", __dir__)

require "async"
require "async/http/endpoint"
require "protocol/http1/connection"

Async do
	endpoint = Async::HTTP::Endpoint.parse("http://localhost:8080")
	
	peer = endpoint.connect
	
	puts "Connected to #{peer} #{peer.remote_address.inspect}"
	
	# IO Buffering...
	client = Protocol::HTTP1::Connection.new(peer)
	
	puts "Writing request..."
	3.times do
		client.write_request("localhost", "GET", "/", "HTTP/1.1", [["Accept", "*/*"]])
		client.write_body("HTTP/1.1", nil)
		
		puts "Reading response..."
		response = client.read_response("GET")
		version, status, reason, headers, body = response
		
		puts "Got response: #{response.inspect}"
		puts body&.read
	end
	
	puts "Closing client..."
	client.close
end

puts "Exiting."