stadly/http

A PHP library for handling HTTP headers.

v1.2.0 2023-09-22 08:52 UTC

This package is auto-updated.

Last update: 2024-12-22 11:40:04 UTC


README

Latest Version on Packagist Software License Coverage Status Quality Score Total Downloads

A PHP library for parsing and building HTTP headers.

Install

Via Composer

$ composer require stadly/http

Usage

Parsing HTTP headers

Header values can be parsed using fromValue on each header class:

use Stadly\Http\Header\Common\ContentType;
use Stadly\Http\Header\Request\IfNoneMatch;
use Stadly\Http\Header\Response\ContentDisposition;

$contentType = ContentType::fromValue($_SERVER['HTTP_CONTENT_TYPE']);
$ifNoneMatch = IfNoneMatch::fromValue($_SERVER['HTTP_IF_NONE_MATCH']);
$contentDisposition = ContentDisposition::fromValue($_SERVER['HTTP_CONTENT_DISPOSITION']);

Header strings can be parsed using HeaderFactory::fromString:

use Stadly\Http\Header\Request\HeaderFactory as RequestHeaderFactory;
use Stadly\Http\Header\Response\HeaderFactory as ResponseHeaderFactory;

$requestHeaders = [
    'Content-Type: text/html; charset=UTF-8',
    'If-Match: "67ab43", "54ed21", W/"7892dd"',
    'Range: bytes=10-100, 200-300',
];
foreach ($requestHeaders as $headerString) {
    $header = RequestHeaderFactory::fromString($headerString);
}

$responseHeaders = [
    'Content-Type: multipart/form-data; boundary="abc def"',
    'Cache-Control: no-cache="foo, bar", max-age=120, must-revalidate',
    "Content-Disposition: attachment; filename=unicorn.jpg; filename*=UTF-8''%F0%9F%A6%84.jpg",
];
foreach ($responseHeaders as $headerString) {
    $header = ResponseHeaderFactory::fromString($headerString);
}

Note that header strings include the header name, while header values do not. The following results in identical headers:

use Stadly\Http\Header\Response\ContentDisposition;
use Stadly\Http\Header\Response\HeaderFactory;

$header1 = ContentDisposition::fromValue('inline; filename=image.jpg');
$header2 = HeaderFactory::fromString('Content-Disposition: inline; filename=image.jpg');

Example usage

Example parsing the If-None-Match request header and using it to determine whether to serve a file. The response headers Content-Disposition and ETag are built for serving the file.

use Stadly\Http\Header\Request\IfNoneMatch;
use Stadly\Http\Header\Response\ContentDisposition;
use Stadly\Http\Header\Response\ETag;
use Stadly\Http\Header\Value\EntityTag\EntityTag;

$entityTag = new EntityTag(md5($filename));
$ifNoneMatch = IfNoneMatch::fromValue($_SERVER['HTTP_IF_NONE_MATCH']);

if ($ifNoneMatch->evaluate($entityTag)) {
    // Serve file.
    $contentDisposition = new ContentDisposition('attachment');
    $contentDisposition->setFilename(basename($filename));
    header((string)$contentDisposition);

    $eTag = new ETag($entityTag);
    header((string)$eTag);

    readfile($filename);
} else {
    // 304 Not modified.
    http_response_code(304);
}

Header fields overview

The checked header fields have been implemented.

Common header fields

Representation Metadata

Payload Semantics

Request header fields

Controls

Conditionals

Content Negotiation

Authentication Credentials

Request Context

Response header fields

Control Data

Validator Header Fields

Authentication Challenges

Response Context

Other

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email magnar@myrtveit.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.