PcapFileEx.Flows.ProtocolDetector (pcap_file_ex v0.5.5)
View SourceDetects HTTP protocol version from TCP flow data.
Inspects the initial bytes of a TCP flow to determine whether it's HTTP/2 (h2c prior-knowledge), HTTP/1.x, or unknown.
Detection Strategy
- HTTP/2: Match the connection preface
"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" - HTTP/1: Match request methods (
GET,POST, etc.) or response (HTTP/) - Unknown: Any other content
Example
data = "GET /index.html HTTP/1.1\r\nHost: example.com\r\n\r\n"
:http1 = ProtocolDetector.detect(data)
data = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" <> settings_frame
:http2 = ProtocolDetector.detect(data)
data = <<0x16, 0x03, 0x01, ...>> # TLS handshake
:unknown = ProtocolDetector.detect(data)
Summary
Functions
Detects the HTTP protocol version from flow data.
Checks if data looks like HTTP/1.x request or response.
Checks if data starts with HTTP/2 connection preface.
Returns the HTTP/2 connection preface.
Returns the size of the HTTP/2 connection preface in bytes.
Types
Functions
Detects the HTTP protocol version from flow data.
Examines the beginning of the data to identify the protocol.
Parameters
data- Binary data from the start of a TCP flow
Returns
:http2- HTTP/2 connection preface detected:http1- HTTP/1.x request or response detected:unknown- Neither HTTP/1 nor HTTP/2 detected
Examples
iex> PcapFileEx.Flows.ProtocolDetector.detect("GET / HTTP/1.1\r\n")
:http1
iex> PcapFileEx.Flows.ProtocolDetector.detect("HTTP/1.1 200 OK\r\n")
:http1
iex> PcapFileEx.Flows.ProtocolDetector.detect("PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n")
:http2
iex> PcapFileEx.Flows.ProtocolDetector.detect(<<0, 1, 2, 3>>)
:unknown
Checks if data looks like HTTP/1.x request or response.
Examples
iex> PcapFileEx.Flows.ProtocolDetector.http1?("GET / HTTP/1.1\r\n")
true
iex> PcapFileEx.Flows.ProtocolDetector.http1?("HTTP/1.1 200 OK\r\n")
true
iex> PcapFileEx.Flows.ProtocolDetector.http1?("PRI * HTTP/2.0")
false
Checks if data starts with HTTP/2 connection preface.
Examples
iex> PcapFileEx.Flows.ProtocolDetector.http2?("PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n")
true
iex> PcapFileEx.Flows.ProtocolDetector.http2?("GET / HTTP/1.1")
false
@spec http2_preface() :: binary()
Returns the HTTP/2 connection preface.
@spec http2_preface_size() :: non_neg_integer()
Returns the size of the HTTP/2 connection preface in bytes.