frame
import std::net::http2::frame; · source
HTTP/2 framing, RFC 7540 sections 4 and 6. Every frame is a 9-octet header — a 24-bit length, an 8-bit type, an 8-bit flags field, and a 31-bit stream identifier whose reserved top bit is cleared — followed by length octets of payload. Http2Connection drives all of this; the page is for code that speaks the wire format itself.
Types
Frame
type struct Frame {
ftype: u8;
flags: u8;
stream_id: u32;
payload: Array<u8>;
}
One decoded frame, owning its payload bytes, as read_http2_frame returns it; the type itself is internal to the library. has_flag(flag) tests a flag bit; a flag's meaning depends on the frame type — FLAG_ACK and FLAG_END_STREAM are the same bit — so check ftype first.
Properties
| Property | Type |
|---|---|
ftype | u8 |
flags | u8 |
stream_id | u32 |
payload | Array<u8> |
Methods
has_flag(&this, flag: u8) -> boolean;
has_flag(&this, flag: u8) -> boolean;Whether any bit of flag is set in the frame's flags. A flag's meaning depends on the frame type: FLAG_ACK and FLAG_END_STREAM are the same bit, so check ftype first.
Reading and queuing frames
implement struct BufferedStream<S> {
queue_http2_frame(mut &this, ftype: u8, flags: u8, stream_id: u32, payload: &Slice<u8>) -> Result<(), IoError>;
}
Both directions are methods on BufferedStream<S>, beside HTTP/1.1's read_request and WebSocket's read_frame, and for the same reasons. Reading is a method because only the receiver is re-supplied on every poll (E0455). Queuing is synchronous: queue_http2_frame builds the header and payload into pending() with no suspension point inside it, and the caller suspends once, in flush — which is what lets a HEADERS block and its CONTINUATION fragments go out in one transport operation. The names carry http2 because BufferedStream<S> hosts the framing for three protocols, and WebSocket's ws::frame, internal to the library, already owns read_frame and queue_frame.
read_http2_frame(max_payload) rejects a payload longer than max_payload — the negotiated SETTINGS_MAX_FRAME_SIZE — or a fixed hard backstop, whichever is smaller.
Constants
The frame types (FRAME_*), the flag bits (FLAG_*), and the error codes a RST_STREAM or GOAWAY frame carries.
const FRAME_HEADER_LENGTH: i64 = 9;
const FRAME_DATA: u8 = 0x0;
const FRAME_HEADERS: u8 = 0x1;
const FRAME_PRIORITY: u8 = 0x2;
const FRAME_RST_STREAM: u8 = 0x3;
const FRAME_SETTINGS: u8 = 0x4;
const FRAME_PUSH_PROMISE: u8 = 0x5;
const FRAME_PING: u8 = 0x6;
const FRAME_GOAWAY: u8 = 0x7;
const FRAME_WINDOW_UPDATE: u8 = 0x8;
const FRAME_CONTINUATION: u8 = 0x9;
const FLAG_ACK: u8 = 1 << 0;
const FLAG_END_STREAM: u8 = 1 << 0;
const FLAG_END_HEADERS: u8 = 1 << 2;
const FLAG_PADDED: u8 = 1 << 3;
const FLAG_PRIORITY: u8 = 1 << 5;
const NO_ERROR: u32 = 0x0;
const PROTOCOL_ERROR: u32 = 0x1;
const INTERNAL_ERROR: u32 = 0x2;
const FLOW_CONTROL_ERROR: u32 = 0x3;
const SETTINGS_TIMEOUT: u32 = 0x4;
const STREAM_CLOSED: u32 = 0x5;
const FRAME_SIZE_ERROR: u32 = 0x6;
const REFUSED_STREAM: u32 = 0x7;
const CANCEL: u32 = 0x8;
const COMPRESSION_ERROR: u32 = 0x9;
const CONNECT_ERROR: u32 = 0xa;
const ENHANCE_YOUR_CALM: u32 = 0xb;
const INADEQUATE_SECURITY: u32 = 0xc;
const HTTP_1_1_REQUIRED: u32 = 0xd;type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct FrameFrame.ftype: u8Frame.flags: u8Frame.stream_id: u32Frame.payload: Array<u8>type struct Array<T, A = GlobalAllocator> where A: Allocator {}A growable sequence of Ts stored contiguously, owning its elements; A is the allocator its storage comes from. Indexing out of range panics; get answers None instead.
implement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement struct Name { ... }Names the struct an inherent block extends. The methods inside are interchangeable with ones declared in the struct body.
language referencetype struct BufferedStream<S> {}A buffered connection for asynchronous code: it owns its transport S (a socket or a TLS session) and buffers both directions, so many small reads and writes become few large ones. It implements AsyncRead and AsyncWrite; make one with of.
type parameter SBufferedStream::queue_http2_frame(mut &this, ftype: u8, flags: u8, stream_id: u32, payload: &Slice<u8>) -> Result<(), IoError>Queue one frame into the outgoing buffer: the 9-byte header, then the payload. The reserved high bit of the stream identifier is always sent as 0. The payload is not consumed. Nothing is sent until the caller flushes.
mut &this
mut &TypeAn exclusive, mutating borrow. As a receiver, mut &this lets the method modify the fields - and callers can see that from the signature alone. As a parameter type, mut &T lets the callee mutate through the reference while the caller keeps ownership.
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
(parameter) ftype: u8(parameter) flags: u8(parameter) stream_id: u32(parameter) payload: &Slice<u8>type struct Slice<T> {}The elements are reached through ptr, declared read-only: through a mut &Slice<T> it reads as a writable pointer, through a &Slice<T> as the read-only one it is.
type enum Result<T, E> with [Eq, Clone, Debug] {
Ok(T);
Err(E);
}The outcome of an operation that can fail: Ok(value) on success, Err(error) on failure.
primitive ()The unit type: a value that carries no information, and the empty tuple. Distinct from void, which is the absence of a value - Result<(), E> succeeds with nothing to return. 0 bytes.
type struct IoError {
kind: IoErrorKind;
os_code: i32;
}The error of a failed I/O operation: its kind, the operating system's own code, and, when the operation named one, what it was doing and the path. It prints as could not open config.toml: not found.
const NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const FRAME_HEADER_LENGTH: i64 = 9;const FRAME_DATA: u8 = 0x0;const FRAME_HEADERS: u8 = 0x1;const FRAME_PRIORITY: u8 = 0x2;const FRAME_RST_STREAM: u8 = 0x3;const FRAME_SETTINGS: u8 = 0x4;const FRAME_PUSH_PROMISE: u8 = 0x5;const FRAME_PING: u8 = 0x6;const FRAME_GOAWAY: u8 = 0x7;const FRAME_WINDOW_UPDATE: u8 = 0x8;const FRAME_CONTINUATION: u8 = 0x9;const FLAG_ACK: u8 = 1 << 0;const FLAG_END_STREAM: u8 = 1 << 0;const FLAG_END_HEADERS: u8 = 1 << 2;const FLAG_PADDED: u8 = 1 << 3;const FLAG_PRIORITY: u8 = 1 << 5;const NO_ERROR: u32 = 0x0;const PROTOCOL_ERROR: u32 = 0x1;const INTERNAL_ERROR: u32 = 0x2;const FLOW_CONTROL_ERROR: u32 = 0x3;const SETTINGS_TIMEOUT: u32 = 0x4;const STREAM_CLOSED: u32 = 0x5;const FRAME_SIZE_ERROR: u32 = 0x6;const REFUSED_STREAM: u32 = 0x7;const CANCEL: u32 = 0x8;const COMPRESSION_ERROR: u32 = 0x9;const CONNECT_ERROR: u32 = 0xa;const ENHANCE_YOUR_CALM: u32 = 0xb;const INADEQUATE_SECURITY: u32 = 0xc;const HTTP_1_1_REQUIRED: u32 = 0xd;import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
namespace std::net::http2;net::http2: HTTP/2 (RFC 7540) with HPACK header compression (RFC 7541).
module pageconst FLAG_ACK: u8 = 1 << 0;const FLAG_END_STREAM: u8 = 1 << 0;(parameter) flag: u8