Skip to content

Latest commit

 

History

23 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Minimal HTTP/2 Examples in Pure PHP

Minimal HTTP/2 client and server examples implemented in pure PHP using ReactPHP Socket.

The goal of this repository is educational: to demonstrate the HTTP/2 connection flow and frame-level behavior without relying on external HTTP/2 libraries.

Instead of using full implementations such as nghttp2, these examples show how HTTP/2 works internally by implementing a very small subset of the protocol.

⚠️Important

These examples are intentionally minimal and incomplete.

They are designed for learning and experimentation only.

They do NOT implement the full HTTP/2 specification and must not be used in production.

Limitations include:

  • single request/response flow
  • minimal frame handling
  • no HPACK implementation
  • no advanced flow control
  • no error handling for many edge cases

What this repository demonstrates

  • HTTP/2 connection preface
  • SETTINGS exchange
  • HEADERS and DATA frame flow
  • minimal HTTP/2 request/response lifecycle
  • cleartext HTTP/2 (h2c, prior knowledge)
  • TLS HTTP/2 with ALPN

This repository is useful if you want to:

  • understand HTTP/2 at the frame level
  • experiment with HTTP/2 without large dependencies
  • build educational tools or protocol experiments

HTTP/2 connection flow

Client                              Server
  |                                   |
  | connection preface                |
  | SETTINGS -----------------------> |
  |                                   |
  |                        SETTINGS   |
  | <-------------------------------  |
  | request HEADERS / DATA ---------> |
  |                                   |
  |                        response HEADERS
  | <-------------------------------  |
  |                        response DATA
  | <-------------------------------  |

Repository Structure

http2-client.php
http2-server.php

docs/
    client-explained.md
    server-explained.md

Requirements

PHP 7.1 or later

Composer

ReactPHP

Install dependencies:

git clone https://github.com/masakielastic/http2-minimal-examples
cd http2-minimal-examples
composer install

Quick start

1. Run HTTP/2 server (cleartext / h2c)

php http2-server.php 8080

Test with curl:

curl --http2-prior-knowledge http://127.0.0.1:8080/

Explanation:

  • --http2-prior-knowledge skips HTTP/1.1 upgrade
  • the client immediately starts HTTP/2

2. Run HTTP/2 server with TLS + ALPN

php http2-server.php 8443 server.key server.crt

Test with curl:

curl -k -v --http2 https://127.0.0.1:8443/

Options explained:

  • --http2 enables HTTP/2 negotiation
  • -k ignores certificate verification (for local testing)

3. Run the minimal HTTP/2 client

php http2-client.php http://127.0.0.1:8080/

Documentation

Step-by-step explanations of the HTTP/2 protocol flow implemented in this repository.

Related Concepts

If you want to explore further:

  • HTTP/2 specification (RFC 9113)
  • HPACK header compression
  • flow control
  • stream multiplexing

These examples intentionally omit many of these features to keep the code readable.

Summary

This repository provides a small, readable reference implementation that demonstrates the core mechanics of HTTP/2.

It is particularly useful for:

  • protocol learners
  • library authors
  • developers experimenting with HTTP/2 internals

About

Minimal HTTP/2 client and server examples in pure PHP. Demonstrates HTTP/2 frame-level behavior using ReactPHP Socket for educational purposes.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages