class HTTP

A class with HTTP-related helpers.

Like Debug, this is more a bundle of methods than a class ;-)

Config options

cache_ajax_requests
disable_http_cache bool
MimeTypes array Mapping of extension to mime types
cache_control array List of names to add to the Cache-Control header.
vary string|null Vary string; A comma separated list of var header names

Methods

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, string|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 $currentURL = null, String $separator = '&')

Will try to include a GET parameter for an existing URL, preserving existing parameters and fragments.

static 
RAW_setGetVar($varname, $varvalue, $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 
getLinksIn($content)

No description

static 
getImagesIn($content)

No description

static string
get_mime_type(string $filename)

Get the MIME type based on a file's extension.

static 
set_cache_age(int $age)

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

static 
register_modification_date($dateString)

No description

static 
register_modification_timestamp($timestamp)

No description

static 
register_etag($etag)

No description

static 
add_cache_headers(SS_HTTPResponse $body = null)

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

static string
gmt_date(int $timestamp)

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

at line 71
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

at line 104
static string absoluteURLs(string $html)

Turn all relative URLs in the content to absolute URLs

Parameters

string $html

Return Value

string

at line 141
static string urlRewriter(string $content, string|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 Some example code:

  • '"../../" . $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 URL. e.g.

function($url) { return Director::absoluteURL($url, true); }

Parameters

string $content The HTML to search for links to rewrite
string|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

at line 206
static String setGetVar(String $varname, String $varvalue, String $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 $currentURL Relative or absolute URL (Optional).
String $separator Separator for http_build_query(). (Optional).

Return Value

String Absolute URL

at line 254
static RAW_setGetVar($varname, $varvalue, $currentURL = null)

Parameters

$varname
$varvalue
$currentURL

at line 266
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

at line 285
static getLinksIn($content)

Parameters

$content

at line 289
static getImagesIn($content)

Parameters

$content

at line 303
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 actually exists, then use that extension, otherwise fallback to a list of commonly known MIME types.

Parameters

string $filename Relative path to filename from project root, e.g. "mysite/tests/file.csv"

Return Value

string MIME type

at line 330
static set_cache_age(int $age)

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

Parameters

int $age

at line 335
static register_modification_date($dateString)

Parameters

$dateString

at line 341
static register_modification_timestamp($timestamp)

Parameters

$timestamp

at line 346
static register_etag($etag)

Parameters

$etag

at line 362
static add_cache_headers(SS_HTTPResponse $body = null)

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.

Parameters

SS_HTTPResponse $body The SS_HTTPResponse object to augment. Omitted the argument or passing a string is deprecated; in these cases, the headers are output directly.

at line 536
static string gmt_date(int $timestamp)

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

at line 545
static int get_cache_age()

Return static variable cache_age in second

Return Value

int