/** * @return HttpUrl **/ protected function processPath(HttpRequest $request) { if ($request->hasServerVar('REQUEST_URI')) { $path = $this->getPath(HttpUrl::create()->parse($request->getServerVar('REQUEST_URI'))); } else { throw new RouterException('Cannot resolve path'); } return $path; }
public function chooseAction(HttpRequest $request) { $action = Primitive::choice('action')->setList($this->methodMap); if ($this->getDefaultAction()) { $action->setDefault($this->getDefaultAction()); } Form::create()->add($action)->import($request->getGet())->importMore($request->getPost())->importMore($request->getAttached()); if (!($command = $action->getValue())) { return $action->getDefault(); } return $command; }
/** * @return MappedForm **/ public function importOne($name, HttpRequest $request) { $this->checkExistence($name); $scopes = []; if (isset($this->map[$name])) { foreach ($this->map[$name] as $type) { $scopes[] = $request->getByType($type); } } elseif ($this->type) { $scopes[] = $request->getByType($this->type); } $first = true; foreach ($scopes as $scope) { if ($first) { $this->form->importOne($name, $scope); $first = false; } else { $this->form->importOneMore($name, $scope); } } return $this; }
public function store($file, $desiredName) { if (!$this->uploadUrl) { throw new UnsupportedMethodException('Don`t know how to store file!'); } $sendRequest = HttpRequest::create()->setMethod(HttpMethod::post())->setUrl(HttpUrl::create()->parse($this->uploadUrl)); $options = array_merge($this->uploadOptions, array($this->uploadFieldName => '@' . $file)); $curl = CurlHttpClient::create()->setOption(CURLOPT_POSTFIELDS, $options); $upload = function () use($curl, $sendRequest) { $resp = $curl->send($sendRequest); return $resp; }; $resp = $this->tryToDo($upload, "Tried to upload file but something happened: %s"); return $resp->getBody(); }
protected function unlink($file) { $sendRequest = HttpRequest::create()->setMethod(HttpMethod::delete())->setUrl(HttpUrl::create()->parse($this->getUploadLink($file))); /** @var CurlHttpResponse $resp */ $curl = CurlHttpClient::create()->setOption(CURLOPT_CUSTOMREQUEST, "DELETE")->setOption(CURLOPT_TIMEOUT, 25); if (is_array($this->uploadOptions) && isset($this->uploadOptions['userpwd'])) { $curl->setOption(CURLOPT_HTTPAUTH, CURLAUTH_ANY)->setOption(CURLOPT_USERPWD, $this->uploadOptions['userpwd']); } $delete = function () use($curl, $sendRequest) { $response = $curl->send($sendRequest); $status = $response->getStatus()->getId(); if ($status < 200 || $status >= 400) { throw new MissingElementException("Got HTTP response code {$status}"); } }; $this->tryToDo($delete, "File ({$file}) was not deleted, reason: %s"); return true; }
private function getPostFields(HttpRequest $request) { if ($request->hasBody()) { return $request->getBody(); } else { if ($this->oldUrlConstructor) { return UrlParamsUtils::toStringOneDeepLvl($request->getPost()); } else { $fileList = array_map([$this, 'fileFilter'], UrlParamsUtils::toParamsList($request->getFiles())); if (empty($fileList)) { return UrlParamsUtils::toString($request->getPost()); } else { $postList = UrlParamsUtils::toParamsList($request->getPost()); if (!is_null($atParam = $this->findAtParamInPost($postList))) { throw new NetworkException('Security excepion: not allowed send post param ' . $atParam . ' which begins from @ in request which contains files'); } return array_merge($postList, $fileList); } } } }
/** * check_authentication mode request **/ private function checkAuthentication(array $parameters, $manager = null) { $credentials = new OpenIdCredentials(HttpUrl::create()->parse($parameters['openid.identity']), $this->httpClient); $request = HttpRequest::create()->setMethod(HttpMethod::post())->setUrl($credentials->getServer()); if (isset($parameters['openid.invalidate_handle']) && $manager) { $request->setPostVar('openid.invalidate_handle', $parameters['openid.invalidate_handle']); } foreach (explode(',', $parameters['openid.signed']) as $key) { $key = 'openid.' . $key; $request->setPostVar($key, $parameters[$key]); } $request->setPostVar('openid.mode', 'check_authentication')->setPostVar('openid.assoc_handle', $parameters['openid.assoc_handle'])->setPostVar('openid.sig', $parameters['openid.sig'])->setPostVar('openid.signed', $parameters['openid.signed']); $response = $this->httpClient->send($request); if ($response->getStatus()->getId() != HttpStatus::CODE_200) { throw new OpenIdException('bad response code from server'); } $result = $this->parseKeyValueFormat($response->getBody()); if (!isset($result['is_valid']) || $result['is_valid'] !== 'true' && $result['is_valid'] !== 'false') { throw new OpenIdException('strange response given'); } if ($result['is_valid'] === 'true') { if (isset($result['invalidate_handle']) && $manager) { $manager->purgeByHandle($result['invalidate_handle']); } return true; } elseif ($result['is_valid'] === 'false') { return false; } }
/** * @return RouterRewrite **/ protected function setRequestParams(HttpRequest $request, array $params) { foreach ($params as $param => $value) { $request->setAttachedVar($param, $value); } return $this; }
protected function httpStat($url) { $result = array(); try { $sendRequest = HttpRequest::create()->setMethod(HttpMethod::get())->setUrl(HttpUrl::create()->parse($url)); $res = CurlHttpClient::create()->setOption(CURLOPT_RETURNTRANSFER, true)->setOption(CURLOPT_NOBODY, true)->send($sendRequest); $result['mime'] = $res->getHeader('content-type'); $result['size'] = $res->getHeader('content-length'); } catch (Exception $e) { } return $result; }
protected function isSecureRequest(HttpRequest $request) { return $request->hasServerVar('HTTPS') && $request->getServerVar('HTTPS') == 'on' || $request->hasServerVar('SERVER_PORT') && (int) $request->getServerVar('SERVER_PORT') === 443; }
protected function loadXRDS($url) { $response = $this->httpClient->send(HttpRequest::create()->setHeaderVar('Accept', self::HEADER_ACCEPT)->setMethod(HttpMethod::get())->setUrl(HttpUrl::create()->parse($url))); if ($response->getStatus()->getId() != 200) { throw new OpenIdException('can\'t fetch document'); } $this->parseXRDS($response->getBody()); return $this; }
protected function getTemplatePathList() { return [$this->templatesDirectory . $this->request->getAttachedVar('format') . '/' . $this->controllerName . '/', $this->templatesDirectory . $this->request->getAttachedVar('format') . '/']; }