/** * Manipulate Http Request * * @param iHttpRequest $request */ function withHttpRequest(iHttpRequest $request) { $params = \Poirot\Std\iterator_to_array($this); $body = json_encode($params); $request->setBody($body); $request->getHeaders()->set(HeaderFactory::factory('Content-Type', 'application/json')); }
/** * Manipulate Http Request * * @param iHttpRequest $request */ function withHttpRequest(iHttpRequest $request) { $params = \Poirot\Std\iterator_to_array($this); $body = http_build_query($params, null, '&'); $request->setBody($body); $request->getHeaders()->set(HeaderFactory::factory('Content-Type', 'application/x-www-form-urlencoded')); }
/** * Match with Request * * - merge with current params * * - manipulate params on match * exp. when match host it contain host param * with matched value * * @param iHttpRequest $request * * @return iHRouter|false */ function match(iHttpRequest $request) { $uri = $request->getUri(); $scheme = $uri->getScheme(); if ($scheme !== $this->inOptions()->getScheme()) { return false; } $routeMatch = clone $this; return $routeMatch; }
/** * @param HttpSocketTransporter $transporter * @param HttpResponse $response * @param Streamable $stream * @param iHttpRequest $request * * @return mixed */ function __invoke($transporter = null, $response = null, $stream = null, $request = null) { $statusCode = $response->getStatCode(); # Handle 100 and 101 responses if ($statusCode == 100 || $statusCode == 101) { ## receive data will continue after events $transporter->reset(); } # HEAD requests and 204 or 304 stat codes are not expected to have a body if ($statusCode == 304 || $statusCode == 204 || $request->getMethod() == HttpRequest::METHOD_HEAD) { ## do not continue with body return ['continue' => false]; } /*$statusPlugin = new Status(['message_object' => $response]); if (!$statusPlugin->isSuccess()) ## always connection will closed, no need to continue return ['continue' => false];*/ }
/** * @param HttpSocketTransporter $transporter * @param iHttpRequest $request * * @return mixed */ function __invoke($transporter = null, $request = null) { # Header Content-Length: /** * Http Messages With Body Should be with Content-Length * !! without this post requests always not working * @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.4 * @see https://issues.apache.org/jira/browse/TS-2902 */ $body = $request->getBody(); $length = false; if ($body) { if ($body instanceof iStreamable) { $length = $body->getSize(); } else { $length = strlen($body); } } if ($length !== false) { if (!$request->getHeaders()->has('Content-Length')) { $request->getHeaders()->set(HeaderFactory::factory('Content-Length', (string) $length)); } } }
protected function __match(iHttpRequest $request, $criteria, array $regexDef) { $path = $request->getUri()->getPath(); /*if ($this->_translationKeys) { if (!isset($options['translator']) || !$options['translator'] instanceof Translator) { throw new \RuntimeException('No translator provided'); } $translator = $options['translator']; $textDomain = (isset($options['text_domain']) ? $options['text_domain'] : 'default'); $locale = (isset($options['locale']) ? $options['locale'] : null); foreach ($this->_translationKeys as $key) { $regex = str_replace('#' . $key . '#', $translator->translate($key, $textDomain, $locale), $regex); } }*/ # match criteria: $parts = $this->__parseRouteDefinition($criteria); $regex = $this->__buildRegex($parts, $regexDef); ## hash meta for router segment, unique for each file call /*$backTrace = debug_backtrace(null, 1); $hashMeta = end($backTrace)['file'];*/ $hashMeta = 'ds'; $pathOffset = $this->inOptions()->getPathOffset(); $routerSegment = $request->meta()->__router_segment__; if ($routerSegment) { $routerSegment = isset($routerSegment[$hashMeta]) ? $routerSegment = $routerSegment[$hashMeta] : null; } if (!$pathOffset && $routerSegment) { $pathOffset = $routerSegment; $pathOffset = [end($pathOffset), null]; ### offset from last match to end(null), used on split } if ($pathOffset !== null) { ## extract path offset to match $path = call_user_func_array([$path, 'split'], $pathOffset); } $regex = $this->inOptions()->getExactMatch() ? "(^{$regex}\$)" : "(^{$regex})"; ## only start with criteria "/pages[/other/paths]" $result = preg_match($regex, $path->toString(), $matches); if ($result) { ## calculate matched path offset $curMatchDepth = (new SeqPathJoinUri($matches[0]))->getDepth(); if (!$pathOffset) { $start = null; $end = $curMatchDepth; } else { $start = current($pathOffset) + $curMatchDepth; $end = $start + $curMatchDepth; } $pathOffset = [$start, $end]; } if (!$result) { return false; } ### inject offset as metadata to get back on linked routers if ($pathOffset) { // $this->options()->setPathOffset($pathOffset); ### using on assemble things and ... $rSegement =& $request->meta()->__router_segment__; if (!is_array($rSegement)) { $rSegement = []; } $rSegement[$hashMeta] = $pathOffset; } $params = []; foreach ($this->_paramMap as $index => $name) { if (isset($matches[$index]) && $matches[$index] !== '') { $params[$name] = $this->_decode($matches[$index]); } } $routerMatch = clone $this; $routerMatch->params()->from(new Entity($params)); return $routerMatch; }