示例#1
0
文件: Slim.php 项目: nhp/shopware-4
    /**
     * Set ETag HTTP Response Header
     *
     * Set the etag header and stop if the conditional GET request matches.
     * The `value` argument is a unique identifier for the current resource.
     * The `type` argument indicates whether the etag should be used as a strong or
     * weak cache validator.
     *
     * When the current request includes an 'If-None-Match' header with
     * a matching etag, execution is immediately stopped. If the request
     * method is GET or HEAD, a '304 Not Modified' response is sent.
     *
     * @param   string                      $value  The etag value
     * @param   string                      $type   The type of etag to create; either "strong" or "weak"
     * @throws  InvalidArgumentException            If provided type is invalid
     * @return  void
     */
    public function etag( $value, $type = 'strong' ) {
        //Ensure type is correct
        if ( !in_array($type, array('strong', 'weak')) ) {
            throw new InvalidArgumentException('Invalid Slim::etag type. Expected "strong" or "weak".');
        }

        //Set etag value
        $value = '"' . $value . '"';
        if ( $type === 'weak' ) $value = 'W/'.$value;
        $this->response['ETag'] = $value;

        //Check conditional GET
        if ( $etagsHeader = $this->request->headers('IF_NONE_MATCH')) {
            $etags = preg_split('@\s*,\s*@', $etagsHeader);
            if ( in_array($value, $etags) || in_array('*', $etags) ) $this->halt(304);
        }
    }
示例#2
0
 /**
  * Test accurately removes HTTP_ prefix from input header name
  */
 public function testHeaderRemovesHttpPrefix()
 {
     $env = Slim_Environment::mock(array('X_HTTP_METHOD_OVERRIDE' => 'PUT', 'CONTENT_TYPE' => 'application/json'));
     //fwrite(fopen('php://stdout', 'w'), print_r($env, true));
     $req = new Slim_Http_Request($env);
     $this->assertEquals('PUT', $req->headers('X_HTTP_METHOD_OVERRIDE'));
     $this->assertNull($req->headers('X_METHOD_OVERRIDE'));
     //<-- Ensures `HTTP_` is not removed if not prefix
     $this->assertEquals('application/json', $req->headers('HTTP_CONTENT_TYPE'));
     //<-- Ensures `HTTP_` is removed if prefix
 }
示例#3
0
 /**
  * Test headers
  */
 public function testHeaders()
 {
     Slim_Environment::mock(array('REQUEST_METHOD' => 'PUT', 'REMOTE_ADDR' => '127.0.0.1', 'SCRIPT_NAME' => '/foo/index.php', 'PATH_INFO' => '/bar/xyz', 'QUERY_STRING' => 'one=1&two=2&three=3', 'SERVER_NAME' => 'slim', 'SERVER_PORT' => 80, 'slim.url_scheme' => 'http', 'slim.input' => '', 'slim.errors' => fopen('php://stderr', 'w'), 'CONTENT_TYPE' => 'application/x-www-form-urlencoded', 'HTTP_ACCEPT_ENCODING' => 'gzip'));
     $env = Slim_Environment::getInstance();
     $req = new Slim_Http_Request($env);
     $headers = $req->headers();
     $this->assertTrue(is_array($headers));
     $this->assertArrayHasKey('HTTP_ACCEPT_ENCODING', $headers);
     $this->assertFalse(isset($headers['CONTENT_TYPE']));
     $this->assertEquals('gzip', $req->headers('HTTP_ACCEPT_ENCODING'));
     $this->assertEquals('gzip', $req->headers('HTTP-ACCEPT-ENCODING'));
     $this->assertEquals('gzip', $req->headers('http_accept_encoding'));
     $this->assertEquals('gzip', $req->headers('http-accept-encoding'));
     $this->assertEquals('gzip', $req->headers('ACCEPT_ENCODING'));
     $this->assertEquals('gzip', $req->headers('ACCEPT-ENCODING'));
     $this->assertEquals('gzip', $req->headers('accept_encoding'));
     $this->assertEquals('gzip', $req->headers('accept-encoding'));
     $this->assertNull($req->headers('foo'));
 }
示例#4
0
 public function testHeaders()
 {
     //Case A
     $_SERVER['X_REQUESTED_WITH'] = 'XMLHttpRequest';
     $r = new Slim_Http_Request();
     $this->assertEquals('slim', $r->headers('HOST'));
     $this->assertEquals('XMLHttpRequest', $r->headers('X_REQUESTED_WITH'));
     $this->assertTrue(is_array($r->headers()));
     //Case B - HTTP headers may be case insensitive
     $_SERVER['x-requested-with'] = 'XMLHttpRequest';
     $r = new Slim_Http_Request();
     $this->assertEquals('XMLHttpRequest', $r->headers('X_REQUESTED_WITH'));
     //Case C - HTTP headers may be case insensitive
     $_SERVER['X-Requested-With'] = 'XMLHttpRequest';
     $r = new Slim_Http_Request();
     $this->assertEquals('XMLHttpRequest', $r->headers('X_REQUESTED_WITH'));
 }