示例#1
0
 public function handle(RequestInterface $request, ResponseInterface $response, FilterChainInterface $filterChain)
 {
     $signature = null;
     if ($request->hasHeader('Cookie')) {
         $cookies = Cookie::parseList($request->getHeader('Cookie'));
         foreach ($cookies as $cookie) {
             if ($cookie->getName() == self::COOKIE_NAME) {
                 $data = $cookie->getValue();
                 $parts = explode('.', $data, 2);
                 $payload = isset($parts[0]) ? $parts[0] : null;
                 $signature = isset($parts[1]) ? $parts[1] : null;
                 if (strcmp($signature, $this->generateSignature($payload)) === 0) {
                     $request->setAttribute(self::COOKIE_NAME, $this->unserializeData($payload));
                 } else {
                     // invalid signature
                 }
                 break;
             }
         }
     }
     $filterChain->handle($request, $response);
     $data = $request->getAttribute(self::COOKIE_NAME);
     if (!empty($data)) {
         $payload = $this->serializeData($data);
         $newSignature = $this->generateSignature($payload);
         // send only a new cookie if the data has changed
         if ($newSignature != $signature) {
             $response->addHeader('Set-Cookie', self::COOKIE_NAME . '=' . $payload . '.' . $newSignature);
         }
     }
 }
示例#2
0
文件: ContentMd5.php 项目: seytar/psx
 public function handle(RequestInterface $request, ResponseInterface $response, FilterChainInterface $filterChain)
 {
     $filterChain->handle($request, $response, $filterChain);
     if (!$response->hasHeader('Content-MD5')) {
         $response->setHeader('Content-MD5', md5(Util::toString($response->getBody())));
     }
 }
示例#3
0
 public function handle(RequestInterface $request, ResponseInterface $response, FilterChainInterface $filterChain)
 {
     $this->controller->onLoad();
     switch ($request->getMethod()) {
         case 'DELETE':
             $this->controller->onDelete();
             break;
         case 'GET':
             $this->controller->onGet();
             break;
         case 'HEAD':
             $this->controller->onHead();
             break;
         case 'OPTIONS':
             $this->controller->onOptions();
             break;
         case 'POST':
             $this->controller->onPost();
             break;
         case 'PUT':
             $this->controller->onPut();
             break;
         case 'TRACE':
             $this->controller->onTrace();
             break;
     }
     $method = $this->context->get(Context::KEY_METHOD);
     if (!empty($method) && is_callable([$this->controller, $method])) {
         call_user_func_array([$this->controller, $method], array());
     }
     $this->controller->processResponse();
     $filterChain->handle($request, $response);
 }
示例#4
0
 public function handle(RequestInterface $request, ResponseInterface $response, FilterChainInterface $filterChain)
 {
     $authorization = $request->getHeader('Authorization');
     if (!empty($authorization)) {
         $parts = explode(' ', $authorization, 2);
         $type = isset($parts[0]) ? $parts[0] : null;
         $data = isset($parts[1]) ? $parts[1] : null;
         if ($type == 'Basic' && !empty($data)) {
             $data = base64_decode($data);
             $parts = explode(':', $data, 2);
             $username = isset($parts[0]) ? $parts[0] : null;
             $password = isset($parts[1]) ? $parts[1] : null;
             $result = call_user_func_array($this->isValidCallback, array($username, $password));
             if ($result === true) {
                 $this->callSuccess($response);
                 $filterChain->handle($request, $response);
             } else {
                 $this->callFailure($response);
             }
         } else {
             $this->callMissing($response);
         }
     } else {
         $this->callMissing($response);
     }
 }
示例#5
0
 public function handle(RequestInterface $request, ResponseInterface $response, FilterChainInterface $filterChain)
 {
     $userAgent = $request->getHeader('User-Agent');
     if (!empty($userAgent)) {
         $filterChain->handle($request, $response);
     } else {
         throw new BadRequestException('Request must contain an User-Agent header');
     }
 }
示例#6
0
文件: IpFirewall.php 项目: seytar/psx
 public function handle(RequestInterface $request, ResponseInterface $response, FilterChainInterface $filterChain)
 {
     $ip = $this->getIp();
     if ($ip === null || in_array($ip, $this->allowedIps)) {
         $filterChain->handle($request, $response);
     } else {
         throw new ForbiddenException('Access not allowed');
     }
 }
示例#7
0
文件: Backstage.php 项目: seytar/psx
 public function handle(RequestInterface $request, ResponseInterface $response, FilterChainInterface $filterChain)
 {
     $accept = $request->getHeader('Accept');
     if (stripos($accept, 'text/html') !== false && is_file($this->file)) {
         $response->setHeader('Content-Type', 'text/html');
         $response->getBody()->write(file_get_contents($this->file));
     } else {
         $filterChain->handle($request, $response);
     }
 }
示例#8
0
文件: GzipEncode.php 项目: seytar/psx
 public function handle(RequestInterface $request, ResponseInterface $response, FilterChainInterface $filterChain)
 {
     if ($request->hasHeader('Accept-Encoding')) {
         $acceptEncoding = $request->getHeader('Accept-Encoding');
         if (strpos($acceptEncoding, 'gzip') !== false) {
             // the sender will compress the response if the content encoding
             // header is available
             $response->setHeader('Content-Encoding', 'gzip');
         }
     }
     $filterChain->handle($request, $response);
 }
示例#9
0
 public function handle(RequestInterface $request, ResponseInterface $response)
 {
     $filter = array_shift($this->filters);
     if ($filter === null) {
         // if we have no filters check whether we have another filter chain
         // which should be called next
         if ($this->filterChain !== null) {
             $this->filterChain->handle($request, $response, $this->filterChain);
         }
     } elseif ($filter instanceof FilterInterface) {
         if ($this->logger !== null) {
             $this->logger->info('Filter execute ' . get_class($filter));
         }
         $filter->handle($request, $response, $this);
     } elseif (is_callable($filter)) {
         call_user_func_array($filter, array($request, $response, $this));
     } else {
         throw new RuntimeException('Invalid filter value');
     }
 }
示例#10
0
 public function handle(RequestInterface $request, ResponseInterface $response, FilterChainInterface $filterChain)
 {
     $authorization = $request->getHeader('Authorization');
     if (!empty($authorization)) {
         $parts = explode(' ', $authorization, 2);
         $type = isset($parts[0]) ? $parts[0] : null;
         $data = isset($parts[1]) ? $parts[1] : null;
         if ($type == 'OAuth' && !empty($data)) {
             $params = Authentication::decodeParameters($data);
             $params = array_map(array('\\PSX\\Oauth', 'urlDecode'), $params);
             // realm is not used in the base string
             unset($params['realm']);
             if (!isset($params['oauth_consumer_key'])) {
                 throw new BadRequestException('Consumer key not set');
             }
             if (!isset($params['oauth_token'])) {
                 throw new BadRequestException('Token not set');
             }
             if (!isset($params['oauth_signature_method'])) {
                 throw new BadRequestException('Signature method not set');
             }
             if (!isset($params['oauth_signature'])) {
                 throw new BadRequestException('Signature not set');
             }
             $consumer = call_user_func_array($this->consumerCallback, array($params['oauth_consumer_key'], $params['oauth_token']));
             if ($consumer instanceof Consumer) {
                 $signature = Oauth::getSignature($params['oauth_signature_method']);
                 $method = $request->getMethod();
                 $url = $request->getUri();
                 $params = array_merge($params, $request->getUri()->getParameters());
                 if (strpos($request->getHeader('Content-Type'), 'application/x-www-form-urlencoded') !== false) {
                     $body = (string) $request->getBody();
                     $data = array();
                     parse_str($body, $data);
                     $params = array_merge($params, $data);
                 }
                 $baseString = Oauth::buildBasestring($method, $url, $params);
                 if ($signature->verify($baseString, $consumer->getConsumerSecret(), $consumer->getTokenSecret(), $params['oauth_signature']) !== false) {
                     $this->callSuccess($response);
                     $filterChain->handle($request, $response);
                 } else {
                     $this->callFailure($response);
                 }
             } else {
                 $this->callFailure($response);
             }
         } else {
             $this->callMissing($response);
         }
     } else {
         $this->callMissing($response);
     }
 }
示例#11
0
 public function handle(RequestInterface $request, ResponseInterface $response, FilterChainInterface $filterChain)
 {
     $authorization = $request->getHeader('Authorization');
     if (!empty($authorization)) {
         $parts = explode(' ', $authorization, 2);
         $type = isset($parts[0]) ? $parts[0] : null;
         $accessToken = isset($parts[1]) ? $parts[1] : null;
         if ($type == 'Bearer' && !empty($accessToken)) {
             $result = call_user_func_array($this->accessCallback, array($accessToken));
             if ($result === true) {
                 $this->callSuccess($response);
                 $filterChain->handle($request, $response);
             } else {
                 $this->callFailure($response);
             }
         } else {
             $this->callMissing($response);
         }
     } else {
         $this->callMissing($response);
     }
 }
示例#12
0
 public function handle(RequestInterface $request, ResponseInterface $response, FilterChainInterface $filterChain)
 {
     $key = $this->getCacheKey($request);
     if (!empty($key)) {
         $item = $this->cache->getItem($key);
         if ($item->isHit()) {
             // serve cache response
             $resp = $item->get();
             $response->setHeaders($resp['headers']);
             $response->getBody()->write($resp['body']);
         } else {
             $filterChain->handle($request, $response);
             // save response
             $resp = array('headers' => $this->getCacheHeaders($response), 'body' => Util::toString($response->getBody()));
             $item->set($resp, $this->ttl);
             $this->cache->save($item);
         }
     } else {
         // if we have no key we can not use a cache
         $filterChain->handle($request, $response);
     }
 }
示例#13
0
 public function handle(RequestInterface $request, ResponseInterface $response, FilterChainInterface $filterChain)
 {
     $cacheControl = array();
     if ($this->flags & self::TYPE_PUBLIC) {
         $cacheControl[] = 'public';
     }
     if ($this->flags & self::TYPE_PRIVATE) {
         $cacheControl[] = 'private';
     }
     if ($this->flags & self::NO_CACHE) {
         $cacheControl[] = 'no-cache';
     }
     if ($this->flags & self::NO_STORE) {
         $cacheControl[] = 'no-store';
     }
     if ($this->flags & self::NO_TRANSFORM) {
         $cacheControl[] = 'no-transform';
     }
     if ($this->flags & self::MUST_REVALIDATE) {
         $cacheControl[] = 'must-revalidate';
     }
     if ($this->flags & self::PROXY_REVALIDATE) {
         $cacheControl[] = 'proxy-revalidate';
     }
     if ($this->maxAge !== null) {
         $cacheControl[] = 'max-age=' . intval($this->maxAge);
     }
     if ($this->sMaxAge !== null) {
         $cacheControl[] = 's-maxage=' . intval($this->sMaxAge);
     }
     if (!empty($cacheControl)) {
         $response->setHeader('Cache-Control', implode(', ', $cacheControl));
     }
     if ($this->expires !== null) {
         $response->setHeader('Expires', $this->expires->format(DateTime::HTTP));
     }
     $filterChain->handle($request, $response);
 }
示例#14
0
 public function handle(RequestInterface $request, ResponseInterface $response, FilterChainInterface $filterChain)
 {
     $authorization = $request->getHeader('Authorization');
     if (!empty($authorization)) {
         $parts = explode(' ', $authorization, 2);
         $type = isset($parts[0]) ? $parts[0] : null;
         $data = isset($parts[1]) ? $parts[1] : null;
         if ($type == 'Digest' && !empty($data)) {
             $params = Authentication::decodeParameters($data);
             $algo = isset($params['algorithm']) ? $params['algorithm'] : 'MD5';
             $qop = isset($params['qop']) ? $params['qop'] : 'auth';
             if (!$this->digest instanceof Digest) {
                 throw new BadRequestException('Digest not available');
             }
             if ($this->digest->getOpaque() != $params['opaque']) {
                 throw new BadRequestException('Invalid opaque');
             }
             // build ha1
             $ha1 = call_user_func_array($this->ha1Callback, array($params['username']));
             if ($algo == 'MD5-sess') {
                 $ha1 = md5($ha1 . ':' . $this->digest->getNonce() . ':' . $params['cnonce']);
             }
             // build ha2
             if ($qop == 'auth-int') {
                 $ha2 = md5($request->getMethod() . ':' . $request->getUri()->getPath() . ':' . md5($request->getBody()));
             } else {
                 $ha2 = md5($request->getMethod() . ':' . $request->getUri()->getPath());
             }
             // build response
             if ($qop == 'auth' || $qop == 'auth-int') {
                 $hash = md5($ha1 . ':' . $this->digest->getNonce() . ':' . $params['nc'] . ':' . $params['cnonce'] . ':' . $qop . ':' . $ha2);
             } else {
                 $hash = md5($ha1 . ':' . $this->digest->getNonce() . ':' . $ha2);
             }
             if (strcmp($hash, $params['response']) === 0) {
                 $this->callSuccess($response, $hash);
                 $filterChain->handle($request, $response);
             } else {
                 $this->callFailure($response);
             }
         } else {
             $this->callMissing($response);
         }
     } else {
         $this->callMissing($response);
     }
 }
示例#15
0
 /**
  * @param \PSX\Http\RequestInterface $request
  * @param \PSX\Http\ResponseInterface $response
  * @param \PSX\Dispatch\FilterChainInterface $filterChain
  */
 public function on($request, $response, $filterChain)
 {
     $filterChain->handle($request, $response);
 }
示例#16
0
文件: GroupTest.php 项目: seytar/psx
 public function handle(RequestInterface $request, ResponseInterface $response, FilterChainInterface $filterChain)
 {
     self::$calls[] = $this->id;
     $filterChain->handle($request, $response);
 }