All Projects → posener → H2conn

posener / H2conn

Licence: apache-2.0
HTTP2 client-server full-duplex connection

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Labels

Projects that are alternatives of or similar to H2conn

Shgf
Simple HTTP golang framework
Stars: ✭ 13 (-81.69%)
Mutual labels:  http2
Nico
A HTTP2 web server for reverse proxy and single page application, automatically apply for ssl certificate, Zero-Configuration.
Stars: ✭ 43 (-39.44%)
Mutual labels:  http2
Httpie Http2
Experimental HTTP/2 plugin for HTTPie
Stars: ✭ 61 (-14.08%)
Mutual labels:  http2
H2o.docker
docker files for h2o http2 webserver
Stars: ✭ 33 (-53.52%)
Mutual labels:  http2
Katwebx
An extremely fast static web server and reverse proxy for the modern web.
Stars: ✭ 39 (-45.07%)
Mutual labels:  http2
Siler
⚡ Flat-files and plain-old PHP functions rockin'on as a set of general purpose high-level abstractions.
Stars: ✭ 1,056 (+1387.32%)
Mutual labels:  http2
Grpc
An Elixir implementation of gRPC
Stars: ✭ 858 (+1108.45%)
Mutual labels:  http2
Esa Restlight
ESA Restlight is a lightweight and rest-oriented web framework.
Stars: ✭ 67 (-5.63%)
Mutual labels:  http2
Lib.web.mvc
Lib.Web.Mvc is a library which contains some helper classes for ASP.NET MVC such as strongly typed jqGrid helper, attribute and helper providing support for HTTP/2 Server Push with Cache Digest, attribute and helpers providing support for Content Security Policy Level 2, FileResult providing support for Range Requests, action result and helper providing support for XSL transformation and more.
Stars: ✭ 42 (-40.85%)
Mutual labels:  http2
Cloudflare Worker Preact Pwa
Cloudflare worker running a Preact Progressive Web App
Stars: ✭ 57 (-19.72%)
Mutual labels:  http2
Docker Swift Apns
A collection of Docker images to build APNS providers in Swift
Stars: ✭ 34 (-52.11%)
Mutual labels:  http2
Express Security
nodejs + express security and performance boilerplate.
Stars: ✭ 37 (-47.89%)
Mutual labels:  http2
Go Apns2
Go package for HTTP/2 Apple Push Notification Service.
Stars: ✭ 53 (-25.35%)
Mutual labels:  http2
H2
HTTP 2.0 client & server implementation for Rust.
Stars: ✭ 886 (+1147.89%)
Mutual labels:  http2
Setup Nginx Webserver
🚀Setup a perfect webserver on CentOS/Redhat 7.x guide with understanding.
Stars: ✭ 65 (-8.45%)
Mutual labels:  http2
Summary
个人总结 持续更新 欢迎提出各种issues
Stars: ✭ 12 (-83.1%)
Mutual labels:  http2
Finagle
A fault tolerant, protocol-agnostic RPC system
Stars: ✭ 8,126 (+11345.07%)
Mutual labels:  http2
Akka Http
The Streaming-first HTTP server/module of Akka
Stars: ✭ 1,163 (+1538.03%)
Mutual labels:  http2
Grpc Rust
Rust implementation of gRPC
Stars: ✭ 1,139 (+1504.23%)
Mutual labels:  http2
Grpcc
A gRPC cli interface for easy testing against gRPC servers
Stars: ✭ 1,078 (+1418.31%)
Mutual labels:  http2

h2conn

h2conn provides HTTP2 client-server full-duplex communication connection.

Build Status codecov GoDoc Go Report Card

Motivation

Go has a wonderful HTTP2 support that is integrated seamlessly into the HTTP1.1 implementation. There is a nice demo on https://http2.golang.org in which you can see it in action. The code for the demo is available here.

I became interested how HTTP2 can work with full-duplex communication, and saw the echo handler implementation, and a suggested client side implementation for this handler in this Github issue.

This library provides a simpler API for the same sort of "advanced usage" / "low level" / "hard core" implementation.

Examples

Check out the example directory.

Server

On the server side, in an implementation of http.Handler, the h2conn.Accept function can be used to get a full-duplex connection to the client.

type handler struct{}

func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	conn, err := h2conn.Accept(w, r)
	if err != nil {
		log.Printf("Failed creating connection from %s: %s", r.RemoteAddr, err)
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return
	}
	defer conn.Close() 
	
	// [ Use conn ... ]
	// The connection will be left open until this function will return.
	// If there is a need to wait to the client to close the connection,
	// we can wait on the request context: `<-r.Context().Done()`.
}

Client

On the client side, the h2conn.Connect function can be used in order to connect to an HTTP2 server with full-duplex communication.

func main() {
    conn, resp, err := h2conn.Connect(ctx, url, nil)
	if err != nil {
		log.Fatalf("Initiate conn: %s", err)
	}
	defer conn.Close()

	// Check server status code
	if resp.StatusCode != http.StatusOK {
		log.Fatalf("Bad status code: %d", resp.StatusCode)
	}

	// [ Use conn ... ]
}

Using the Connection

The server and the client need to decide on message format. Here are few examples that demonstrate how the client and server can communicate over the created pipe.

1. JSON

Sending and receiving JSON format is a very common thing to do.

import "encoding/json"

func main() {
	// [ Create a connection ... ]
	
	// Create an encoder and decoder from the connection
	var in, out = json.NewDecoder(conn), json.NewEncoder(conn)
	
	// Sending data into the connection using the out encoder.	
	// Any type can be sent - the important thing is that the other side will read with a
	// variable of the same type.
	// In this case, we just use a simple string.
	err = out.Encode("hello")
	// [ handle err ... ]
	
	// Receiving data from the connection using the in decoder and a variable.
	// Any type can be received - the important thing is that the other side will write data
	// to the connection of the same type.
	// In this case we assume that the other side sent us a string.
	var resp string
	err = in.Decode(&resp)	
	// [ handle err, use resp ... ]
}

2. GOB

GOB is more efficient message format, but requires both client and server to be written in Go. The example is exactly the same as in the json encoding, just switch json with gob.

import "encoding/gob"

func main() {
	// [ Create a connection ... ]
	
	var in, out = gob.NewDecoder(conn), gob.NewEncoder(conn)
	
	// Sending data into the connection using the out encoder.	
	// Any type can be sent - the important thing is that the other side will read with a
	// variable of the same type.
	// In this case, we just use a simple string.
	err = out.Encode("hello")
	// [ handle err ... ]
	
	// Receiving data from the connection using the in decoder and a variable.
	// Any type can be received - the important thing is that the other side will write data
	// to the connection of the same type.
	// In this case we assume that the other side sent us a string.
	var resp string
	err = in.Decode(&resp)	
	// [ handle err, use resp ... ]
}

3. Constant Buffer Size

// Create constant size buffer
const bufSize = 10

func main () {
	// [ Create a connection ... ]
	
	buf := make([]byte, bufSize)

	// Write to the connection:
	// [ Write something to buf... ]
	_, err = conn.Write(buf)

	// Read from the connection:
	_, err = conn.Read(buf)
	// [ Use buf... ]
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].