|
| 1 | +# Getting Started |
| 2 | + |
| 3 | +This guide explains how to get started with `protocol-http1`, a low-level implementation of the HTTP/1 protocol for building HTTP clients and servers. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Add the gem to your project: |
| 8 | + |
| 9 | +```bash |
| 10 | +$ bundle add protocol-http1 |
| 11 | +``` |
| 12 | + |
| 13 | +## Core Concepts |
| 14 | + |
| 15 | +`protocol-http1` provides a low-level implementation of the HTTP/1 protocol with several core concepts: |
| 16 | + |
| 17 | +- A {ruby Protocol::HTTP1::Connection} which represents the main entry point for creating HTTP/1.1 clients and servers. |
| 18 | +- Integration with the `Protocol::HTTP::Body` classes for handling request and response bodies. |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +`protocol-http1` can be used to build both HTTP clients and servers. |
| 23 | + |
| 24 | +### HTTP Server |
| 25 | + |
| 26 | +Here's a simple HTTP/1.1 server that responds to all requests with "Hello World": |
| 27 | + |
| 28 | +```ruby |
| 29 | +#!/usr/bin/env ruby |
| 30 | + |
| 31 | +require "socket" |
| 32 | +require "protocol/http1/connection" |
| 33 | +require "protocol/http/body/buffered" |
| 34 | + |
| 35 | +# Test with: curl http://localhost:8080/ |
| 36 | + |
| 37 | +Addrinfo.tcp("0.0.0.0", 8080).listen do |server| |
| 38 | + loop do |
| 39 | + client, address = server.accept |
| 40 | + connection = Protocol::HTTP1::Connection.new(client) |
| 41 | + |
| 42 | + # Read request: |
| 43 | + while request = connection.read_request |
| 44 | + authority, method, path, version, headers, body = request |
| 45 | + |
| 46 | + # Write response: |
| 47 | + connection.write_response(version, 200, [["content-type", "text/plain"]]) |
| 48 | + connection.write_body(version, Protocol::HTTP::Body::Buffered.wrap(["Hello World"])) |
| 49 | + |
| 50 | + break unless connection.persistent |
| 51 | + end |
| 52 | + end |
| 53 | +end |
| 54 | +``` |
| 55 | + |
| 56 | +The server: |
| 57 | + |
| 58 | +1. Creates a new {ruby Protocol::HTTP1::Connection} for each client connection. |
| 59 | +2. Reads incoming requests using `read_request`. |
| 60 | +3. Sends responses using `write_response` and `write_body`. |
| 61 | +4. Supports persistent connections by checking `connection.persistent`. |
| 62 | + |
| 63 | +### HTTP Client |
| 64 | + |
| 65 | +Here's a simple HTTP/1.1 client that makes multiple requests: |
| 66 | + |
| 67 | +```ruby |
| 68 | +#!/usr/bin/env ruby |
| 69 | + |
| 70 | +require "async" |
| 71 | +require "async/http/endpoint" |
| 72 | +require "protocol/http1/connection" |
| 73 | + |
| 74 | +Async do |
| 75 | + endpoint = Async::HTTP::Endpoint.parse("http://localhost:8080") |
| 76 | + |
| 77 | + peer = endpoint.connect |
| 78 | + |
| 79 | + puts "Connected to #{peer} #{peer.remote_address.inspect}" |
| 80 | + |
| 81 | + # IO Buffering... |
| 82 | + client = Protocol::HTTP1::Connection.new(peer) |
| 83 | + |
| 84 | + puts "Writing request..." |
| 85 | + 3.times do |
| 86 | + client.write_request("localhost", "GET", "/", "HTTP/1.1", [["Accept", "*/*"]]) |
| 87 | + client.write_body("HTTP/1.1", nil) |
| 88 | + |
| 89 | + puts "Reading response..." |
| 90 | + response = client.read_response("GET") |
| 91 | + version, status, reason, headers, body = response |
| 92 | + |
| 93 | + puts "Got response: #{response.inspect}" |
| 94 | + puts body&.read |
| 95 | + end |
| 96 | + |
| 97 | + puts "Closing client..." |
| 98 | + client.close |
| 99 | +end |
| 100 | +``` |
| 101 | + |
| 102 | +The client: |
| 103 | + |
| 104 | +1. Creates a connection to a server using `Async::HTTP::Endpoint`. |
| 105 | +2. Creates a {ruby Protocol::HTTP1::Connection} wrapper around the socket. |
| 106 | +3. Sends requests using `write_request` and `write_body`. |
| 107 | +4. Reads responses using `read_response`. |
| 108 | +5. Properly closes the connection when done. |
| 109 | + |
| 110 | +### Connection Management |
| 111 | + |
| 112 | +The {ruby Protocol::HTTP1::Connection} handles: |
| 113 | + |
| 114 | +- **Request/Response Parsing**: Automatically parses HTTP/1.1 request and response formats. |
| 115 | +- **Persistent Connections**: Supports HTTP/1.1 keep-alive for multiple requests over one connection. |
| 116 | +- **Body Handling**: Integrates with `Protocol::HTTP::Body` classes for streaming and buffered content. |
| 117 | +- **Header Management**: Properly handles HTTP headers as arrays of key-value pairs. |
0 commit comments