set() public method

DateTime objects will be converted to a string representation internally but will be returned as DateTime objects on calling get(). Please note that dates are normalized to GMT internally, so that get() will return the same point in time, but not necessarily in the same timezone, if it was not GMT previously. GMT is used synonymously with UTC as per RFC 2616 3.3.1.
public set ( string $name, array | string | DateTime $values, boolean $replaceExistingHeader = true ) : void
$name string Name of the header, for example "Location", "Content-Description" etc.
$values array | string | DateTime An array of values or a single value for the specified header field
$replaceExistingHeader boolean If a header with the same name should be replaced. Default is TRUE.
return void
 /**
  * Sets the specified HTTP header
  *
  * DateTime objects will be converted to a string representation internally but
  * will be returned as DateTime objects on calling getHeader().
  *
  * Please note that dates are normalized to GMT internally, so that getHeader() will return
  * the same point in time, but not necessarily in the same timezone, if it was not
  * GMT previously. GMT is used synonymously with UTC as per RFC 2616 3.3.1.
  *
  * @param string $name Name of the header, for example "Location", "Content-Description" etc.
  * @param array|string|\DateTime $values An array of values or a single value for the specified header field
  * @param boolean $replaceExistingHeader If a header with the same name should be replaced. Default is TRUE.
  * @return self This message, for method chaining
  * @throws \InvalidArgumentException
  * @api
  */
 public function setHeader($name, $values, $replaceExistingHeader = true)
 {
     switch ($name) {
         case 'Content-Type':
             if (is_array($values)) {
                 if (count($values) !== 1) {
                     throw new \InvalidArgumentException('The "Content-Type" header must be unique and thus only one field value may be specified.', 1454949291);
                 }
                 $values = (string) $values[0];
             }
             if (stripos($values, 'charset') === false && stripos($values, 'text/') === 0) {
                 $values .= '; charset=' . $this->charset;
             }
             break;
     }
     $this->headers->set($name, $values, $replaceExistingHeader);
     return $this;
 }
Exemplo n.º 2
0
 /**
  * Creates a response from the given raw, that is plain text, HTTP response.
  *
  * @param string $rawResponse
  * @param Response $parentResponse Parent response, if called recursively
  *
  * @throws \InvalidArgumentException
  * @return Response
  */
 public static function createFromRaw($rawResponse, Response $parentResponse = null)
 {
     $response = new static($parentResponse);
     $lines = explode(chr(10), $rawResponse);
     $statusLine = array_shift($lines);
     if (substr($statusLine, 0, 5) !== 'HTTP/') {
         throw new \InvalidArgumentException('The given raw HTTP message is not a valid response.', 1335175601);
     }
     list($version, $statusCode, $reasonPhrase) = explode(' ', $statusLine, 3);
     $response->setVersion($version);
     $response->setStatus((int) $statusCode, trim($reasonPhrase));
     $parsingHeader = true;
     $contentLines = [];
     $headers = new Headers();
     foreach ($lines as $line) {
         if ($parsingHeader) {
             if (trim($line) === '') {
                 $parsingHeader = false;
                 continue;
             }
             $fieldName = trim(substr($line, 0, strpos($line, ':')));
             $fieldValue = trim(substr($line, strlen($fieldName) + 1));
             if (strtoupper(substr($fieldName, 0, 10)) === 'SET-COOKIE') {
                 $cookie = Cookie::createFromRawSetCookieHeader($fieldValue);
                 if ($cookie !== null) {
                     $headers->setCookie($cookie);
                 }
             } else {
                 $headers->set($fieldName, $fieldValue, false);
             }
         } else {
             $contentLines[] = $line;
         }
     }
     $content = implode(chr(10), $contentLines);
     $response->setHeaders($headers);
     $response->setContent($content);
     return $response;
 }
 /**
  * @test
  *
  * Note: This is a fix for https://jira.neos.io/browse/FLOW-324 (see https://code.google.com/p/chromium/issues/detail?id=501095)
  */
 public function setExceptsHttpsHeaders()
 {
     $headers = new Headers();
     $headers->set('HTTPS', 1);
     // dummy assertion to suppress PHPUnit warning
     $this->assertTrue(true);
 }
Exemplo n.º 4
0
 /**
  * Allows to add headers to be sent with every request the browser executes.
  *
  * @param string $name Name of the header, for example "Location", "Content-Description" etc.
  * @param array|string|\DateTime $values An array of values or a single value for the specified header field
  * @return void
  * @see Message::setHeader()
  */
 public function addAutomaticRequestHeader($name, $values)
 {
     $this->automaticRequestHeaders->set($name, $values, true);
 }