{"id":13484142,"url":"https://github.com/igrigorik/http-2","last_synced_at":"2025-05-13T18:14:31.574Z","repository":{"id":55045277,"uuid":"12289624","full_name":"igrigorik/http-2","owner":"igrigorik","description":"Pure Ruby implementation of HTTP/2 protocol","archived":false,"fork":false,"pushed_at":"2025-04-16T00:09:02.000Z","size":15695,"stargazers_count":901,"open_issues_count":0,"forks_count":63,"subscribers_count":46,"default_branch":"main","last_synced_at":"2025-04-25T17:55:29.364Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://httpwg.github.io/specs/rfc7540.html","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/igrigorik.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2013-08-22T06:03:57.000Z","updated_at":"2025-04-17T03:07:00.000Z","dependencies_parsed_at":"2024-06-17T10:54:11.210Z","dependency_job_id":"9f130064-4aa6-440a-b13d-4092be915e2a","html_url":"https://github.com/igrigorik/http-2","commit_stats":{"total_commits":318,"total_committers":34,"mean_commits":9.352941176470589,"dds":0.449685534591195,"last_synced_commit":"059ec0cc3a3647e542bf4ff93fd1216f26fd8844"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/igrigorik%2Fhttp-2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/igrigorik%2Fhttp-2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/igrigorik%2Fhttp-2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/igrigorik%2Fhttp-2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/igrigorik","download_url":"https://codeload.github.com/igrigorik/http-2/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254000885,"owners_count":21997443,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T17:01:19.845Z","updated_at":"2025-05-13T18:14:31.545Z","avatar_url":"https://github.com/igrigorik.png","language":"Ruby","readme":"# HTTP-2\n\n[![Gem Version](https://badge.fury.io/rb/http-2.svg)](http://rubygems.org/gems/http-2)\n[![Build status](https://github.com/igrigorik/http-2/actions/workflows/ci.yml/badge.svg)](https://github.com/igrigorik/http-2)\n\nPure Ruby, framework and transport agnostic, implementation of HTTP/2 protocol and HPACK header compression with support for:\n\n\n* [Binary framing](https://hpbn.co/http2/#binary-framing-layer) parsing and encoding\n* [Stream multiplexing](https://hpbn.co/http2/#streams-messages-and-frames) and [prioritization](https://hpbn.co/http2/#stream-prioritization)\n* Connection and stream [flow control](https://hpbn.co/http2/#flow-control)\n* [Header compression](https://hpbn.co/http2/#header-compression) and [server push](https://hpbn.co/http2/#server-push)\n* Connection and stream management\n* And more... see [API docs](https://www.rubydoc.info/gems/http-2)\n\nProtocol specifications:\n\n* [Hypertext Transfer Protocol Version 2 (RFC 7540)](https://httpwg.github.io/specs/rfc7540.html)\n* [HPACK: Header Compression for HTTP/2 (RFC 7541)](https://httpwg.github.io/specs/rfc7541.html)\n\n\n## Getting started\n\n```bash\n$\u003e gem install http-2\n```\n\nThis implementation makes no assumptions as how the data is delivered: it could be a regular Ruby TCP socket, your custom eventloop, or whatever other transport you wish to use - e.g. ZeroMQ, [avian carriers](http://www.ietf.org/rfc/rfc1149.txt), etc.\n\nYour code is responsible for feeding data into the parser, which performs all of the necessary HTTP/2 decoding, state management and the rest, and vice versa, the parser will emit bytes (encoded HTTP/2 frames) that you can then route to the destination. Roughly, this works as follows:\n\n```ruby\nrequire 'http/2'\n\nsocket = YourTransport.new\n\nconn = HTTP2::Client.new\nconn.on(:frame) {|bytes| socket \u003c\u003c bytes }\n\nwhile bytes = socket.read\n conn \u003c\u003c bytes\nend\n```\n\nCheckout provided [client](example/client.rb) and [server](example/server.rb) implementations for basic examples.\n\n\n### Connection lifecycle management\n\nDepending on the role of the endpoint you must initialize either a [Client](lib/http/2/client.rb) or a [Server](lib/http/2/server.rb) object. Doing so picks the appropriate header compression / decompression algorithms and stream management logic. From there, you can subscribe to connection level events, or invoke appropriate APIs to allocate new streams and manage the lifecycle. For example:\n\n```ruby\n# - Server ---------------\nserver = HTTP2::Server.new\n\nserver.on(:stream) { |stream| ... } # process inbound stream\nserver.on(:frame)  { |bytes| ... }  # encoded HTTP/2 frames\n\nserver.ping { ... } # run liveness check, process pong response\nserver.goaway # send goaway frame to the client\n\n# - Client ---------------\nclient = HTTP2::Client.new\nclient.on(:promise) { |stream| ... } # process push promise\n\nstream = client.new_stream # allocate new stream\nstream.headers({':method' =\u003e 'post', ...}, end_stream: false)\nstream.data(payload, end_stream: true)\n```\n\nEvents emitted by the connection object:\n\n\u003ctable\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003cb\u003e:promise\u003c/b\u003e\u003c/td\u003e\n    \u003ctd\u003eclient role only, fires once for each new push promise\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003cb\u003e:stream\u003c/b\u003e\u003c/td\u003e\n    \u003ctd\u003eserver role only, fires once for each new client stream\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003cb\u003e:frame\u003c/b\u003e\u003c/td\u003e\n    \u003ctd\u003efires once for every encoded HTTP/2 frame that needs to be sent to the peer\u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\n\n### Stream lifecycle management\n\nA single HTTP/2 connection can [multiplex multiple streams](https://hpbn.co/http2/#request-and-response-multiplexing) in parallel: multiple requests and responses can be in flight simultaneously and stream data can be interleaved and prioritized. Further, the specification provides a well-defined lifecycle for each stream (see below).\n\nThe good news is, all of the stream management, and state transitions, and error checking is handled by the library. All you have to do is subscribe to appropriate events (marked with \":\" prefix in diagram below) and provide your application logic to handle request and response processing.\n\n```\n                      +--------+\n                 PP   |        |   PP\n             ,--------|  idle  |--------.\n            /         |        |         \\\n           v          +--------+          v\n    +----------+          |           +----------+\n    |          |          | H         |          |\n,---|:reserved |          |           |:reserved |---.\n|   | (local)  |          v           | (remote) |   |\n|   +----------+      +--------+      +----------+   |\n|      | :active      |        |      :active |      |\n|      |      ,-------|:active |-------.      |      |\n|      | H   /   ES   |        |   ES   \\   H |      |\n|      v    v         +--------+         v    v      |\n|   +-----------+          |          +-----------+  |\n|   |:half_close|          |          |:half_close|  |\n|   |  (remote) |          |          |  (local)  |  |\n|   +-----------+          |          +-----------+  |\n|        |                 v                |        |\n|        |    ES/R    +--------+    ES/R    |        |\n|        `-----------\u003e|        |\u003c-----------'        |\n| R                   | :close |                   R |\n`--------------------\u003e|        |\u003c--------------------'\n                      +--------+\n```\n\nFor sake of example, let's take a look at a simple server implementation:\n\n```ruby\nconn = HTTP2::Server.new\n\n# emits new streams opened by the client\nconn.on(:stream) do |stream|\n  stream.on(:active) { } # fires when stream transitions to open state\n  stream.on(:close)  { } # stream is closed by client and server\n\n  stream.on(:headers) { |head| ... } # header callback\n  stream.on(:data) { |chunk| ... }   # body payload callback\n\n  # fires when client terminates its request (i.e. request finished)\n  stream.on(:half_close) do\n\n    # ... generate_response\n\n    # send response\n    stream.headers({\n      \":status\" =\u003e 200,\n      \"content-type\" =\u003e \"text/plain\"\n    })\n\n    # split response between multiple DATA frames\n    stream.data(response_chunk, end_stream: false)\n    stream.data(last_chunk)\n  end\nend\n```\n\nEvents emitted by the [Stream object](lib/http/2/stream.rb):\n\n\u003ctable\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003cb\u003e:reserved\u003c/b\u003e\u003c/td\u003e\n    \u003ctd\u003efires exactly once when a push stream is initialized\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003cb\u003e:active\u003c/b\u003e\u003c/td\u003e\n    \u003ctd\u003efires exactly once when the stream become active and is counted towards the open stream limit\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003cb\u003e:headers\u003c/b\u003e\u003c/td\u003e\n    \u003ctd\u003efires once for each received header block (multi-frame blocks are reassembled before emitting this event)\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003cb\u003e:data\u003c/b\u003e\u003c/td\u003e\n    \u003ctd\u003efires once for every DATA frame (no buffering)\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003cb\u003e:half_close\u003c/b\u003e\u003c/td\u003e\n    \u003ctd\u003efires exactly once when the opposing peer closes its end of connection (e.g. client indicating that request is finished, or server indicating that response is finished)\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003cb\u003e:close\u003c/b\u003e\u003c/td\u003e\n    \u003ctd\u003efires exactly once when both peers close the stream, or if the stream is reset\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003cb\u003e:priority\u003c/b\u003e\u003c/td\u003e\n    \u003ctd\u003efires once for each received priority update (server only)\u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\n\n### Prioritization\n\nEach HTTP/2 [stream has a priority value](https://hpbn.co/http2/#stream-prioritization) that can be sent when the new stream is initialized, and optionally reprioritized later:\n\n```ruby\nclient = HTTP2::Client.new\n\ndefault_priority_stream = client.new_stream\ncustom_priority_stream = client.new_stream(priority: 42)\n\n# sometime later: change priority value\ncustom_priority_stream.reprioritize(32000) # emits PRIORITY frame\n```\n\nOn the opposite side, the server can optimize its stream processing order or resource allocation by accessing the stream priority value (`stream.priority`).\n\n\n### Flow control\n\nMultiplexing multiple streams over the same TCP connection introduces contention for shared bandwidth resources. Stream priorities can help determine the relative order of delivery, but priorities alone are insufficient to control how the resource allocation is performed between multiple streams. To address this, HTTP/2 provides a simple mechanism for [stream and connection flow control](https://hpbn.co/http2/#flow-control).\n\nConnection and stream flow control is handled by the library: all streams are initialized with the default window size (64KB), and send/receive window updates are automatically processed - i.e. window is decremented on outgoing data transfers, and incremented on receipt of window frames. Similarly, if the window is exceeded, then data frames are automatically buffered until window is updated.\n\nThe only thing left is for your application to specify the logic as to when to emit window updates:\n\n```ruby\nconn.buffered_amount     # check amount of buffered data\nconn.window              # check current window size\nconn.window_update(1024) # increment connection window by 1024 bytes\n\nstream.buffered_amount     # check amount of buffered data\nstream.window              # check current window size\nstream.window_update(2048) # increment stream window by 2048 bytes\n```\n\n\n### Server push\n\nAn HTTP/2 server can [send multiple replies](https://hpbn.co/http2/#server-push) to a single client request. To do so, first it emits a \"push promise\" frame which contains the headers of the promised resource, followed by the response to the original request, as well as promised resource payloads (which may be interleaved). A simple example is in order:\n\n```ruby\nconn = HTTP2::Server.new\n\nconn.on(:stream) do |stream|\n  stream.on(:headers) { |head| ... }\n  stream.on(:data) { |chunk| ... }\n\n  # fires when client terminates its request (i.e. request finished)\n  stream.on(:half_close) do\n    promise_header = { ':method' =\u003e 'GET',\n                       ':authority' =\u003e 'localhost',\n                       ':scheme' =\u003e 'https',\n                       ':path' =\u003e \"/other_resource\" }\n\n    # initiate server push stream\n    push_stream = nil\n    stream.promise(promise_header) do |push|\n      push.headers({...})\n      push_stream = push\n    end\n\n    # send response\n    stream.headers({\n      \":status\" =\u003e 200,\n      \"content-type\" =\u003e \"text/plain\"\n    })\n\n    # split response between multiple DATA frames\n    stream.data(response_chunk, end_stream: false)\n    stream.data(last_chunk)\n\n    # now send the previously promised data\n    push_stream.data(push_data)\n  end\nend\n```\n\nWhen a new push promise stream is sent by the server, the client is notified via the `:promise` event:\n\n```ruby\nconn = HTTP2::Client.new\nconn.on(:promise) do |push|\n  # process push stream\nend\n```\n\nThe client can cancel any given push stream (via `.close`), or disable server push entirely by sending the appropriate settings frame:\n\n```ruby\nclient.settings(settings_enable_push: 0)\n```\n### Specs\n\nTo run specs:\n\n```ruby\nrake\n```\n\n### License\n\n(MIT License) - Copyright (c) 2013-2019 Ilya Grigorik ![GA](https://www.google-analytics.com/__utm.gif?utmac=UA-71196-9\u0026utmhn=github.com\u0026utmdt=HTTP2\u0026utmp=/http-2/readme)\n(MIT License) - Copyright (c) 2019 Tiago Cardoso ![GA](https://www.google-analytics.com/__utm.gif?utmac=UA-71196-9\u0026utmhn=github.com\u0026utmdt=HTTP2\u0026utmp=/http-2/readme)\n","funding_links":[],"categories":["HTTP Clients and tools","Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Figrigorik%2Fhttp-2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Figrigorik%2Fhttp-2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Figrigorik%2Fhttp-2/lists"}