Click the Castor logo or press Ctrl Alt T to change theme.
# Protocol versions `Castor\Net\Http\Version` represents an HTTP protocol version as a major and minor number. Requests and responses both carry a `Version` value. ## Default version Manually created requests and responses default to HTTP/1.1: ```php <?php use Castor\Net\Http\Version; $version = Version::default(); $version->major; // 1 $version->minor; // 1 $version->toString(); // "HTTP/1.1" ``` `Request::create` and `Response::create` both use this default. ## Constructing versions Create a version directly when you already know the numeric parts: ```php $http2 = new Version(2, 0); $http2->toString(); // "HTTP/2.0" $http2->toNumericString(); // "2.0" $http2->toFloat(); // 2.0 ``` ## Parsing versions Use `Version::parse` for strings in HTTP version format: ```php $version = Version::parse('HTTP/1.1'); ``` The parser accepts a major version and an optional minor version. If the string cannot be parsed, it throws `InvalidArgumentException`. ```php Version::parse('HTTP/3'); // major 3, minor 0 Version::parse('HTTP/2.0'); // major 2, minor 0 ``` ## Copying messages with a different version Requests and responses are readonly value objects, so change the version by creating a copy: ```php $request = $request->copyWith(version: new Version(2, 0)); $response = $response->copyWith(version: 'HTTP/2.0'); ``` `Request::copyWith` accepts a `Version` instance. `Response::copyWith` accepts either a `Version` instance or a string that is parsed with `Version::parse`. ## Serialization When `Response::writeTo` serializes a response, it uses `Version::toString()` in the status line: ```text HTTP/1.1 200 OK Header: value body ``` For request or response logging, prefer `toString()` when you need the wire representation and `toNumericString()` when you only need the numeric part.
Castor ecosystem