• Stars
    star
    140
  • Rank 261,473 (Top 6 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

NetHttp2 is an HTTP/2 client for Ruby.

Code Climate Gem Version

NetHttp2

NetHttp2 is an HTTP/2 client for Ruby.

Installation

Just install the gem:

$ gem install net-http2

Or add it to your Gemfile:

gem 'net-http2'

Usage

NetHttp2 can perform sync and async calls. Sync calls are very similar to the HTTP/1 calls, while async calls take advantage of the streaming properties of HTTP/2.

To perform a sync call:

require 'net-http2'

# create a client
client = NetHttp2::Client.new("http://nghttp2.org")

# send request
response = client.call(:get, '/')

# read the response
response.ok?      # => true
response.status   # => '200'
response.headers  # => {":status"=>"200"}
response.body     # => "A body"

# close the connection
client.close

To perform an async call:

require 'net-http2'

# create a client
client = NetHttp2::Client.new("http://nghttp2.org")

# prepare request
request = client.prepare_request(:get, '/')
request.on(:headers) { |headers| p headers }
request.on(:body_chunk) { |chunk| p chunk }
request.on(:close) { puts "request completed!" }

# send
client.call_async(request)

# Wait for all outgoing stream to be closed
client.join(timeout: 5)

# close the connection
client.close

Objects

NetHttp2::Client

Methods

  • new(url, options={}) β†’ NetHttp2::Client

Returns a new client. url is a string such as http://nghttp2.org. The current options are:

  • :connect_timeout, specifies the max connect timeout in seconds (defaults to 60).
  • :ssl_context, in case the url has an https scheme and you want your SSL client to use a custom context.
  • :proxy_addr, :proxy_port, :proxy_user, :proxy_pass, specify Proxy connection parameters.

To create a new client:

NetHttp2::Client.new("http://nghttp2.org")

To create a new client with a custom SSL context:

certificate = File.read("cert.pem")
ctx         = OpenSSL::SSL::SSLContext.new
ctx.key     = OpenSSL::PKey::RSA.new(certificate, "cert_password")
ctx.cert    = OpenSSL::X509::Certificate.new(certificate)

NetHttp2::Client.new("https://nghttp2.org", ssl_context: ctx)
  • on(event, &block)

Allows to set a callback for the client. The only available event is :error, which allows to set a callback when an error is raised at socket level, hence in the underlying socket thread.

client.on(:error) { |exception| puts "Exception has been raised: #{exception}" }

It is RECOMMENDED to set the :error callback: if none is defined, the underlying socket thread may raise an error in the main thread at unexpected execution times.

  • uri β†’ URI

Returns the URI of the endpoint.

Blocking calls

These behave similarly to HTTP/1 calls.

  • call(method, path, options={}) β†’ NetHttp2::Response or nil

Sends a request. Returns nil in case a timeout occurs.

method is a symbol that specifies the :method header (:get, :post, :put, :patch, :delete, :options). The body, headers and query-string params of the request can be specified in the options, together with the timeout.

response_1 = client.call(:get, '/path1')
response_2 = client.call(:get, '/path2', headers: { 'x-custom' => 'custom' })
response_3 = client.call(:post, '/path3', body: "the request body", timeout: 1)
response_3 = client.call(:post, '/path4', params: { page: 4 })
Non-blocking calls

The real benefit of HTTP/2 is being able to receive body and header streams. Instead of buffering the whole response, you might want to react immediately upon receiving those streams. This is what non-blocking calls are for.

  • prepare_request(method, path, options={}) β†’ NetHttp2::Request

Prepares an async request. Arguments are the same as the call method, with the difference that the :timeout option will be ignored. In an async call, you will need to write your own logic for timeouts.

request = client.prepare_request(:get, '/path', headers: { 'x-custom-header' => 'custom' })
  • call_async(request)

Calls the server with the async request.

  • join(timeout:)

Wait for all outstanding requests to be completed, optionally with a timeout for this condition to be met. Raises NetHttp2::AsyncRequestTimeout if the timeout is reached.

NetHttp2::Request

Methods

  • method β†’ symbol

The request's method.

  • uri β†’ URI

The request's URI.

  • path β†’ string

The request's path.

  • params β†’ hash

The query string params in hash format, for example {one: 1, two: 2}. These will be encoded and appended to path.

  • body β†’ string

The request's body.

  • timeout β†’ integer

The request's timeout.

  • on(event, &block)

Allows to set a callback for the request. Available events are:

  • :headers: triggered when headers are received (called once).
  • :body_chunk: triggered when body chunks are received (may be called multiple times).
  • :close: triggered when the request has been completed (called once).

Even if NetHttp2 is thread-safe, the async callbacks will be executed in a different thread, so ensure that your code in the callbacks is thread-safe.

request.on(:headers) { |headers| p headers }
request.on(:body_chunk) { |chunk| p chunk }
request.on(:close) { puts "request completed!" }

NetHttp2::Response

Methods

  • ok? β†’ boolean

Returns if the request was successful.

  • headers β†’ hash

Returns a Hash containing the Headers of the response.

  • status β†’ string

Returns the status code.

  • body β†’ string

Returns the RAW body of the response.

Thread-Safety

NetHttp2 is thread-safe. However, some caution is imperative:

  • The async callbacks will be executed in a different thread, so ensure that your code in the callbacks is thread-safe.
  • Errors in the underlying socket loop thread will be raised in the main thread at unexpected execution times, unless you specify the :error callback on the Client (recommended).

Contributing

So you want to contribute? That's great! Please follow the guidelines below. It will make it easier to get merged in.

Before implementing a new feature, please submit a ticket to discuss what you intend to do. Your feature might already be in the works, or an alternative implementation might have already been discussed.

Do not commit to master in your fork. Provide a clean branch without merge commits. Every pull request should have its own topic branch. In this way, every additional adjustments to the original pull request might be done easily, and squashed with git rebase -i. The updated branch will be visible in the same pull request, so there will be no need to open new pull requests when there are changes to be applied.

Ensure to include proper testing. To run tests you simply have to be in the project's root directory and run:

$ rake

More Repositories

1

misultin

Misultin (pronounced mee-sool-tΓ©en) is an Erlang library for building fast lightweight HTTP(S) servers, which also supports websockets.
Erlang
649
star
2

syn

A scalable global Process Registry and Process Group manager for Erlang and Elixir.
Erlang
622
star
3

apnotic

A Ruby APNs HTTP/2 gem able to provide instant feedback.
Ruby
477
star
4

gin

A LUA fast, low-latency, low-memory footprint, web JSON-API framework with Test Driven Development helpers and patterns.
Lua
238
star
5

SublimErl

An Erlang Plugin for Sublime Text 2, which enables code completion and allows you to run tests within the editor itself.
Python
190
star
6

ram

A distributed KV store for Erlang and Elixir.
Erlang
119
star
7

pgpool

A PosgreSQL client that automatically uses connection pools and handles reconnections in case of errors.
Erlang
42
star
8

cowbell

An Erlang node connection manager.
Erlang
26
star
9

erlcassa

A Cassandra CQL client.
Erlang
18
star
10

bisbino

An Erlang HTTP server with FastCGI as backend.
Erlang
18
star
11

rebar3_vendor

Rebar3 Vendor Plugin.
Erlang
14
star
12

SCAR_UCM

Utilities Construction Mod for Arma 3.
SQF
11
star
13

PyAES256

An AES-256 cryptographic module for Python.
C
9
star
14

erlgate

An alternative transport for Erlang messages across nodes.
Erlang
8
star
15

lager_airbrake

An Airbrake lager backend.
Erlang
7
star
16

gin-demo

The working demo code of the Gin tutorial on gin.io.
Lua
4
star
17

origami

My standard bootstrap project for distributed TCP applications.
Erlang
3
star
18

SCAR_E2TB

A mod for Arma 3 that allows you to export data to Terrain Builder, from EDEN and in game.
SQF
3
star
19

phpass

A simple implementation of PHPass’ Portable Hash.
Erlang
3
star
20

mochiweb_reloader

Erlang
2
star
21

contextual-git-prompt

Display git information in OSX terminal's prompt.
Shell
2
star
22

pyopenspime

Automatically exported from code.google.com/p/pyopenspime
Python
2
star