/** * 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')); }
/** * @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)); } } }
/** * Build Platform Specific Expression To Send * Trough Connection * * @param iApiMethod|ReqMethod $ReqMethod Method Interface * * @return HttpRequest */ function makeExpression(iApiMethod $ReqMethod) { ## make a copy of browser when making changes on it by ReqMethod ### with bind browser options $CUR_BROWSER = $this->browser; $this->browser = clone $CUR_BROWSER; if (!$ReqMethod instanceof ReqMethod) { $ReqMethod = new ReqMethod($ReqMethod->toArray()); } # Request Options: ## (1) /* * $browser->POST('/api/v1/auth/login', [ * 'form_data' => [ * // ... */ if ($ReqMethod->getBrowser()) { ## Browser specific options $prepConn = false; foreach ($ReqMethod->getBrowser()->props()->readable as $prop) { if ($ReqMethod->getBrowser()->__isset($prop)) { $this->browser->optsData()->__set($prop, $ReqMethod->getBrowser()->__get($prop)); $prepConn = true; } } ## prepare connection again with new configs !$prepConn ?: $this->prepareTransporter($this->_connection, true); } // ... if ($ReqMethod->getUri() instanceof iHttpUri) { ## Reset Server Base Url When Absolute Http URI Requested /* * $browser->get( * 'http://www.pasargad-co.ir/forms/contact' * , [ 'connection' => ['time_out' => 30], * // ... */ $t_uri = $ReqMethod->getUri(); if ($t_uri->getHost()) { $this->browser->optsData()->setBaseUrl($t_uri); $this->prepareTransporter($this->_connection); } ### continue with sequence http uri $t_uri = $ReqMethod->getUri()->getPath() ? $ReqMethod->getUri()->getPath() : new SeqPathJoinUri('/'); $ReqMethod->setUri($t_uri); } # Build Request Http Message: ## (2) $REQUEST = $this->__getRequestObject(); $REQUEST->setMethod($ReqMethod->getMethod()); $serverUrl = $this->_connection->inOptions()->getServerUrl(); $serverUrl = new HttpUri($serverUrl); $REQUEST->setHost($serverUrl->getHost()); ## req Headers ------------------------------------------------------------------\ ### default headers $reqHeaders = $REQUEST->getHeaders(); if ($this->browser->optsData()->__isset('user_agent')) { $reqHeaders->set(HeaderFactory::factory('User-Agent', $this->browser->optsData()->getUserAgent())); } $reqHeaders->set(HeaderFactory::factory('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8')); $reqHeaders->set(HeaderFactory::factory('Cache-Control', 'no-cache')); /*if ($this->browser->inOptions()->getConnection()) (!$this->browser->inOptions()->getConnection()->isAllowDecoding()) ?: $reqHeaders->set(HeaderFactory::factory('Accept-Encoding' , 'gzip, deflate, sdch' ));*/ ### headers as default browser defined header foreach ($this->browser->optsData()->getRequest()->getHeaders() as $h) { $reqHeaders->set($h); } ### headers as request method options if ($ReqMethod->getHeaders()) { /** @var iHeader $h */ foreach ($ReqMethod->getHeaders() as $h) { if ($reqHeaders->has($h->getLabel())) { $reqHeaders = $reqHeaders->del($h->getLabel()); } $reqHeaders->set($h); } } ## req Uri ----------------------------------------------------------------------\ $basePath = null; if ($this->browser->optsData()->__isset('base_url')) { $basePath = $this->browser->optsData()->getBaseUrl()->getPath(); } if ($basePath === null) { $basePath = new SeqPathJoinUri('/'); } $targetUri = $basePath->merge($ReqMethod->getUri()); ### merge with request base url path $REQUEST->getUri()->setPath($targetUri); ### remove unnecessary iHttpUri parts such as port, host, ... ### its presented as host and etc. on Request Message Object $uri = new HttpUri(['path' => $REQUEST->getUri()->getPath(), 'query' => $REQUEST->getUri()->getQuery(), 'fragment' => $REQUEST->getUri()->getFragment()]); $REQUEST->setUri($uri); ## req body ---------------------------------------------------------------------\ $REQUEST->setBody($ReqMethod->getBody()); # Implement Browser Plugins: ## (3) foreach ($ReqMethod->getBrowser()->props()->readable as $prop) { if (!$this->getPluginManager()->has($prop)) { /* * $browser->POST('/api/v1/auth/login', [ * 'form_data' => [ // <=== plugin form_data will trigger with this params * // ... */ continue; } ## no plugin bind on this option /** @var Browser\Plugin\AbstractBrowserPlugin $plugin */ $plugin = $this->getPluginManager()->fresh($prop); $plugin->from($ReqMethod->getBrowser()->__get($prop)); ### options for service if ($plugin instanceof iBrowserExpressionPlugin) { $plugin->withHttpRequest($REQUEST); } } $this->browser = $CUR_BROWSER; return $REQUEST; }