public function resolve(RequestInterface $request, Context $context) { $sql = 'SELECT id, methods, path, controller, config FROM fusio_routes WHERE status = 1 AND methods LIKE :method'; $method = $request->getMethod(); $pathMatcher = new PathMatcher($request->getUri()->getPath()); $result = $this->connection->fetchAll($sql, array('method' => '%' . $method . '%')); foreach ($result as $row) { $parameters = array(); if (in_array($method, explode('|', $row['methods'])) && $pathMatcher->match($row['path'], $parameters)) { $config = $row['config']; $config = !empty($config) ? unserialize($config) : null; $context->set(Context::KEY_FRAGMENT, $parameters); $context->set(Context::KEY_PATH, $row['path']); $context->set(Context::KEY_SOURCE, $row['controller']); $context->set('fusio.config', $config); $context->set('fusio.routeId', $row['id']); return $request; } } return null; }
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); }
public function handle(RequestInterface $request, ResponseInterface $response, FilterChainInterface $filterChain) { if (in_array($request->getMethod(), $this->requestMethods)) { $this->filter->handle($request, $response, $filterChain); } else { $filterChain->handle($request, $response); } }
public static function createRequest(RequestInterface $request) { $psrRequest = ServerRequestFactory::fromGlobals()->withUri($request->getUri())->withMethod($request->getMethod())->withBody($request->getBody()); foreach ($request->getHeaders() as $name => $values) { $psrRequest = $psrRequest->withHeader($name, $values); } return $psrRequest; }
protected function getCacheKey(RequestInterface $request) { if ($request->getMethod() == 'GET') { if ($this->keyGenerator === null) { return $this->getKeyDefaultImpl($request); } else { return call_user_func_array($this->keyGenerator, array($request)); } } return null; }
public function request(RequestInterface $request, Options $options) { $url = $request->getUri(); foreach ($this->resources as $resource) { $resourceUrl = new Url($resource['url']); if ($resource['method'] == $request->getMethod() && $resourceUrl->getHost() == $url->getHost() && $resourceUrl->getPath() == $url->getPath() && $resourceUrl->getQuery() == $url->getQuery()) { $response = $resource['handler']($request); return ResponseParser::convert($response); } } throw new Exception('Resource not available ' . $request->getMethod() . ' ' . $url); }
public function resolve(RequestInterface $request, Context $context) { $sql = 'SELECT id, methods, path, controller FROM fusio_routes WHERE status = :status '; $paths = ['backend', 'consumer', 'authorization', 'export', 'doc']; $found = false; $path = $request->getUri()->getPath(); $params = ['status' => TableRoutes::STATUS_ACTIVE]; // check whether we have a known system path foreach ($paths as $systemPath) { if (strpos($path, '/' . $systemPath) === 0) { $found = true; $sql .= 'AND path LIKE :path'; $params['path'] = '/' . $systemPath . '%'; break; } } // if not we only want to search the user routes and exclude all system // paths if (!$found) { foreach ($paths as $index => $systemPath) { $key = 'path_' . $index; $sql .= 'AND path NOT LIKE :' . $key . ' '; $params[$key] = '/' . $systemPath . '%'; } } $method = $request->getMethod(); $pathMatcher = new PathMatcher($path); $result = $this->connection->fetchAll($sql, $params); foreach ($result as $row) { $parameters = array(); if (in_array($method, explode('|', $row['methods'])) && $pathMatcher->match($row['path'], $parameters)) { $context->set(Context::KEY_FRAGMENT, $parameters); $context->set(Context::KEY_PATH, $row['path']); $context->set(Context::KEY_SOURCE, $row['controller']); $context->set('fusio.routeId', $row['id']); return $request; } } return null; }
/** * Configures the writer * * @param \PSX\Data\WriterInterface $writer */ protected function configureWriter(WriterInterface $writer) { if ($writer instanceof Writer\TemplateAbstract) { if (!$writer->getBaseDir()) { $writer->setBaseDir(PSX_PATH_LIBRARY); } if (!$writer->getControllerClass()) { $writer->setControllerClass(get_class($this)); } } elseif ($writer instanceof Writer\Soap) { if (!$writer->getRequestMethod()) { $writer->setRequestMethod($this->request->getMethod()); } } elseif ($writer instanceof Writer\Jsonp) { if (!$writer->getCallbackName()) { $writer->setCallbackName($this->getParameter('callback')); } } }
public function resolve(RequestInterface $request, Context $context) { $routingCollection = $this->routingParser->getCollection(); $method = $request->getMethod(); $pathMatcher = new PathMatcher($request->getUri()->getPath()); foreach ($routingCollection as $routing) { $parameters = array(); if (in_array($method, $routing[RoutingCollection::ROUTING_METHODS]) && $pathMatcher->match($routing[RoutingCollection::ROUTING_PATH], $parameters)) { $source = $routing[RoutingCollection::ROUTING_SOURCE]; if ($source[0] == '~') { $request->setUri(new Uri(substr($source, 1))); return $this->resolve($request, $context); } $context->set(Context::KEY_PATH, $routing[RoutingCollection::ROUTING_PATH]); $context->set(Context::KEY_FRAGMENT, $parameters); $context->set(Context::KEY_SOURCE, $source); return $request; } } return null; }
public function log($appId, $routeId, $ip, RequestInterface $request) { $now = new \DateTime(); $this->connection->insert('fusio_log', array('appId' => $appId, 'routeId' => $routeId, 'ip' => $ip, 'userAgent' => $request->getHeader('User-Agent'), 'method' => $request->getMethod(), 'path' => $request->getRequestTarget(), 'header' => $this->getHeadersAsString($request), 'body' => $this->getBodyAsString($request), 'date' => $now->format('Y-m-d H:i:s'))); return $this->connection->lastInsertId(); }
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); } }
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); } }
/** * @return string */ public function getMethod() { return $this->request->getMethod(); }
public static function assignHttpContext($context, RequestInterface $request, Options $options = null) { stream_context_set_option($context, 'http', 'method', $request->getMethod()); stream_context_set_option($context, 'http', 'protocol_version', $request->getProtocolVersion() ?: 1.1); // until chunked transfer encoding if fully implemented we remove the // header if ($request->hasHeader('Transfer-Encoding')) { $request->removeHeader('Transfer-Encoding'); } // set header $headers = implode(Http::$newLine, ResponseParser::buildHeaderFromMessage($request)); stream_context_set_option($context, 'http', 'header', $headers); // set body $body = $request->getBody(); if ($body !== null && !in_array($request->getMethod(), array('HEAD', 'GET'))) { stream_context_set_option($context, 'http', 'content', (string) $body); } if ($options !== null) { // set proxy $proxy = $options->getProxy(); if (!empty($proxy)) { stream_context_set_option($context, 'http', 'proxy', $proxy); } // set follow location stream_context_set_option($context, 'http', 'follow_location', (int) $options->getFollowLocation()); stream_context_set_option($context, 'http', 'max_redirects', $options->getMaxRedirects()); // set timeout $timeout = $options->getTimeout(); if (!empty($timeout)) { stream_context_set_option($context, 'http', 'timeout', $timeout); } } }
/** * @param \PSX\Http\RequestInterface $request * @return string */ public static function buildStatusLine(RequestInterface $request) { $method = $request->getMethod(); $target = $request->getRequestTarget(); $protocol = $request->getProtocolVersion(); if (empty($target)) { throw new Exception('Target not set'); } $method = !empty($method) ? $method : 'GET'; $protocol = !empty($protocol) ? $protocol : 'HTTP/1.1'; return $method . ' ' . $target . ' ' . $protocol; }
public function request(RequestInterface $request, Options $options) { $this->header = array(); $this->body = fopen('php://temp', 'r+'); $handle = curl_init($request->getUri()->toString()); curl_setopt($handle, CURLOPT_HEADER, false); curl_setopt($handle, CURLOPT_RETURNTRANSFER, false); curl_setopt($handle, CURLOPT_HEADERFUNCTION, array($this, 'header')); curl_setopt($handle, CURLOPT_WRITEFUNCTION, array($this, 'write')); curl_setopt($handle, CURLOPT_CUSTOMREQUEST, $request->getMethod()); // set header $headers = ResponseParser::buildHeaderFromMessage($request); if (!empty($headers)) { if (!$request->hasHeader('Expect')) { $headers[] = 'Expect:'; } curl_setopt($handle, CURLOPT_HTTPHEADER, $headers); } // set body $body = $request->getBody(); if ($body !== null && !in_array($request->getMethod(), array('HEAD', 'GET'))) { if ($request->getHeader('Transfer-Encoding') == 'chunked') { curl_setopt($handle, CURLOPT_UPLOAD, true); curl_setopt($handle, CURLOPT_READFUNCTION, function ($handle, $fd, $length) use($body) { return $body->read($length); }); } else { curl_setopt($handle, CURLOPT_POSTFIELDS, (string) $body); } } // set proxy $proxy = $options->getProxy(); if (!empty($proxy)) { curl_setopt($handle, CURLOPT_PROXY, $proxy); } // set follow location curl_setopt($handle, CURLOPT_FOLLOWLOCATION, $options->getFollowLocation() && $this->hasFollowLocation); curl_setopt($handle, CURLOPT_MAXREDIRS, $options->getMaxRedirects()); // set ssl if ($options->getSsl() !== false && ($options->getSsl() === true || strcasecmp($request->getUri()->getScheme(), 'https') === 0)) { $caPath = $options->getCaPath(); if (!empty($caPath)) { curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, 2); if (is_file($caPath)) { curl_setopt($handle, CURLOPT_CAINFO, $caPath); } elseif (is_dir($caPath)) { curl_setopt($handle, CURLOPT_CAPATH, $caPath); } } else { curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, 0); } } // set timeout $timeout = $options->getTimeout(); if (!empty($timeout)) { curl_setopt($handle, CURLOPT_TIMEOUT, $timeout); } // callback $callback = $options->getCallback(); if (!empty($callback)) { call_user_func_array($callback, array($handle, $request)); } curl_exec($handle); // if follow location is active modify the header since all headers from // each redirection are included if ($options->getFollowLocation() && $this->hasFollowLocation) { $positions = array(); foreach ($this->header as $key => $header) { if (substr($header, 0, 5) == 'HTTP/') { $positions[] = $key; } } if (count($positions) > 1) { $this->header = array_slice($this->header, end($positions) - 1); } } if (curl_errno($handle)) { throw new HandlerException('Curl error: ' . curl_error($handle)); } curl_close($handle); // build response rewind($this->body); $response = ResponseParser::buildResponseFromHeader($this->header); if ($request->getMethod() != 'HEAD') { $response->setBody(new TempStream($this->body)); } else { $response->setBody(new StringStream()); } return $response; }