class HTTP (View source)

A class with HTTP-related helpers. Like Debug, this is more a bundle of methods than a class.

Traits

Provides extensions to this object to integrate it with standard config API methods.

Properties

static private bool $cache_ajax_requests
static private bool $disable_http_cache
static private array $MimeTypes

Mapping of extension to mime types

static private array $cache_control

List of names to add to the Cache-Control header.

static private string|null $vary

Vary string; A comma separated list of var header names

Methods

static Config_ForClass
config()

Get a configuration accessor for this class. Short hand for Config::inst()->get($this->class, .....).

mixed
stat(string $name) deprecated

Get inherited config value

mixed
uninherited(string $name)

Gets the uninherited value for the given config option

$this
set_stat(string $name, mixed $value) deprecated

Update the config value for a given property

static string
filename2url(string $filename)

Turns a local system filename into a URL by comparing it to the script filename.

static string
absoluteURLs(string $html)

Turn all relative URLs in the content to absolute URLs.

static string
urlRewriter(string $content, callable $code)

Rewrite all the URLs in the given content, evaluating the given string as PHP code.

static string
setGetVar(string $varname, string $varvalue, string|null $currentURL = null, string $separator = '&')

Will try to include a GET parameter for an existing URL, preserving existing parameters and fragments. If no URL is given, falls back to $_SERVER['REQUEST_URI']. Uses parse_url() to dissect the URL, and http_build_query() to reconstruct it with the additional parameter.

static string
RAW_setGetVar(string $varname, string $varvalue, null|string $currentURL = null)

No description

static array
findByTagAndAttribute(string $content, array $attributes)

Search for all tags with a specific attribute, then return the value of that attribute in a flat array.

static array
getLinksIn(string $content)

No description

static array
getImagesIn(string $content)

No description

static string
get_mime_type(string $filename)

Get the MIME type based on a file's extension. If the finfo class exists in PHP, and the file exists relative to the project root, then use that extension, otherwise fallback to a list of commonly known MIME types.

static 
set_cache_age(int $age) deprecated

Set the maximum age of this page in web caches, in seconds.

static 
register_modification_date(string $dateString) deprecated

No description

static 
register_modification_timestamp(int $timestamp) deprecated

No description

static 
register_etag(string $etag) deprecated

No description

static 
add_cache_headers(HTTPResponse $response = null) deprecated

Add the appropriate caching headers to the response, including If-Modified-Since / 304 handling.

static 
augmentState(HTTPRequest $request, HTTPResponse $response) deprecated

Ensure that all deprecated HTTP cache settings are respected

static string
gmt_date(int $timestamp) deprecated

Return an {@link http://www.faqs.org/rfcs/rfc2822 RFC 2822} date in the GMT timezone (a timestamp is always in GMT: the number of seconds since January 1 1970 00:00:00 GMT)

static int
get_cache_age()

Return static variable cache_age in second

Details

static Config_ForClass config()

Get a configuration accessor for this class. Short hand for Config::inst()->get($this->class, .....).

Return Value

Config_ForClass

mixed stat(string $name) deprecated

deprecated 5.0 Use ->config()->get() instead

Get inherited config value

Parameters

string $name

Return Value

mixed

mixed uninherited(string $name)

Gets the uninherited value for the given config option

Parameters

string $name

Return Value

mixed

$this set_stat(string $name, mixed $value) deprecated

deprecated 5.0 Use ->config()->set() instead

Update the config value for a given property

Parameters

string $name
mixed $value

Return Value

$this

static string filename2url(string $filename)

Turns a local system filename into a URL by comparing it to the script filename.

Parameters

string $filename

Return Value

string

static string absoluteURLs(string $html)

Turn all relative URLs in the content to absolute URLs.

Parameters

string $html

Return Value

string

static string urlRewriter(string $content, callable $code)

Rewrite all the URLs in the given content, evaluating the given string as PHP code.

Put $URL where you want the URL to appear, however, you can't embed $URL in strings, for example:

  • '"../../" . $URL'
  • 'myRewriter($URL)'
  • '(substr($URL, 0, 1)=="/") ? "../" . substr($URL, 1) : $URL'

As of 3.2 $code should be a callable which takes a single parameter and returns the rewritten, for example: function($url) { return Director::absoluteURL($url, true); }

Parameters

string $content

The HTML to search for links to rewrite.

callable $code

Either a string that can evaluate to an expression to rewrite links (depreciated), or a callable that takes a single parameter and returns the rewritten URL.

Return Value

string

The content with all links rewritten as per the logic specified in $code.

static string setGetVar(string $varname, string $varvalue, string|null $currentURL = null, string $separator = '&')

Will try to include a GET parameter for an existing URL, preserving existing parameters and fragments. If no URL is given, falls back to $_SERVER['REQUEST_URI']. Uses parse_url() to dissect the URL, and http_build_query() to reconstruct it with the additional parameter.

Converts any '&' (ampersand) URL parameter separators to the more XHTML compliant '&'.

CAUTION: If the URL is determined to be relative, it is prepended with Director::absoluteBaseURL(). This method will always return an absolute URL because Director::makeRelative() can lead to inconsistent results.

Parameters

string $varname
string $varvalue
string|null $currentURL

Relative or absolute URL, or HTTPRequest to get url from

string $separator

Separator for http_build_query().

Return Value

string

static string RAW_setGetVar(string $varname, string $varvalue, null|string $currentURL = null)

Parameters

string $varname
string $varvalue
null|string $currentURL

Return Value

string

static array findByTagAndAttribute(string $content, array $attributes)

Search for all tags with a specific attribute, then return the value of that attribute in a flat array.

Parameters

string $content
array $attributes

An array of tags to attributes, for example "[a] => 'href', [div] => 'id'"

Return Value

array

static array getLinksIn(string $content)

Parameters

string $content

Return Value

array

static array getImagesIn(string $content)

Parameters

string $content

Return Value

array

static string get_mime_type(string $filename)

Get the MIME type based on a file's extension. If the finfo class exists in PHP, and the file exists relative to the project root, then use that extension, otherwise fallback to a list of commonly known MIME types.

Parameters

string $filename

Return Value

string

static set_cache_age(int $age) deprecated

deprecated 4.2.0:5.0.0 Use HTTPCacheControlMiddleware::singleton()->setMaxAge($age) instead

Set the maximum age of this page in web caches, in seconds.

Parameters

int $age

static register_modification_date(string $dateString) deprecated

deprecated 4.2.0:5.0.0 Use HTTPCacheControlMiddleware::registerModificationDate() instead

Parameters

string $dateString

static register_modification_timestamp(int $timestamp) deprecated

deprecated 4.2.0:5.0.0 Use HTTPCacheControlMiddleware::registerModificationDate() instead

Parameters

int $timestamp

static register_etag(string $etag) deprecated

deprecated 4.2.0:5.0.0 Use ChangeDetectionMiddleware instead

Parameters

string $etag

static add_cache_headers(HTTPResponse $response = null) deprecated

deprecated 4.2.0:5.0.0 Headers are added automatically by HTTPCacheControlMiddleware instead.

Add the appropriate caching headers to the response, including If-Modified-Since / 304 handling.

Note that setting HTTP::$cache_age will overrule any cache headers set by PHP's session_cache_limiter functionality. It is your responsibility to ensure only cacheable data is in fact cached, and HTTP::$cache_age isn't set when the HTTP body contains session-specific content.

Omitting the $body argument or passing a string is deprecated; in these cases, the headers are output directly.

Parameters

HTTPResponse $response

static augmentState(HTTPRequest $request, HTTPResponse $response) deprecated

deprecated 4.2.0:5.0.0 Use HTTPCacheControlMiddleware instead

Ensure that all deprecated HTTP cache settings are respected

Parameters

HTTPRequest $request
HTTPResponse $response

Exceptions

LogicException

static string gmt_date(int $timestamp) deprecated

deprecated 4.2.0:5.0.0 Inline if you need this

Return an {@link http://www.faqs.org/rfcs/rfc2822 RFC 2822} date in the GMT timezone (a timestamp is always in GMT: the number of seconds since January 1 1970 00:00:00 GMT)

Parameters

int $timestamp

Return Value

string

static int get_cache_age()

Return static variable cache_age in second

Return Value

int