/** * Schedule requests to run. * * @codeCoverageIgnore * * @return integer Number of finished requests */ public function schedule() { static $RawResponse = <<<EOT HTTP/1.1 200 OK Server: nginx Date: Thu, 10 Apr 2014 00:00:00 GMT Content-Type: text/html; charset=utf-8 Transfer-Encoding: chunked Connection: keep-alive Vary: Accept-Encoding, Cookie X-Frame-Options: SAMEORIGIN Set-Cookie: test=foo; expires=Wed, 01-Jan-2020 00:00:00 GMT Content-Encoding: gzip <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Untitled</title> </head> <body> <h1>Heading</h1> <p>Paragraph</p> </body> </html> EOT; static $RawInvalidResponse = <<<EOT HTTP/1.1 0 INVALID Server: nginx Date: Thu, 10 Apr 2014 00:00:00 GMT Content-Type: text/html; charset=utf-8 Transfer-Encoding: chunked Connection: keep-alive Vary: Accept-Encoding, Cookie X-Frame-Options: SAMEORIGIN Content-Encoding: gzip EOT; static $RawRedirectResponse = <<<EOT HTTP/1.1 307 Temporary Redirect Server: nginx Date: Thu, 10 Apr 2014 00:00:00 GMT Content-Type: text/html; charset=utf-8 Transfer-Encoding: chunked Connection: keep-alive Vary: Accept-Encoding, Cookie X-Frame-Options: SAMEORIGIN Content-Encoding: gzip Location: http://example.com EOT; // Number of requests finished $return = 0; // Loop through each request foreach ($this as $Request) { if (($Response = $this[$Request]) instanceof IResponse) { // Response not finished if (!isset($Response['Finished']) ?: !$Response['Finished']) { // Redirect? if ($this->Redirect) { $this->Redirect = false; $Response = $RawRedirectResponse; } elseif ($this->Invalid) { $this->Invalid = false; $Response = $RawInvalidResponse; } else { $Response = $RawResponse; } // Finish request $Response = Response::createFromString($Response); $Response->URI = $Request->URI; $Response->RequestURI = $Request->URI; $Response['Running'] = $this->Timeout; $Response['Finished'] = !$this->Timeout; $this[$Request] = $Response; // Upadte count $return++; } } } // Done return $return; }
/** * Does grunt work of finishing a request. * * @ignore * @param \BLW\Type\HTTP\IRequest $Request */ private function _finish(IRequest $Request) { // Validate $Request if ($this->contains($Request)) { if (($Response = $this[$Request]) instanceof IResponse) { $Handle = $Response['handle']; // Create Response // Is code known? if ($Response->isValidCode(intval(curl_getinfo($Handle, CURLINFO_HTTP_CODE)))) { // parse response $Response = Response::createFromString(curl_multi_getcontent($Handle)); // Update handle $Response['handle'] = $Handle; // RequestURI $Response->setRequestURI($Request->getURI()); // Update $this[$Request] = $Response; } // Update finished flag $Response['Running'] = false; $Response['Finished'] = true; // Update info $this->_update($Request); // Free Handle $this->_Helper->freeHandle($Handle); // Does mediator exist? if ($this->_Mediator instanceof IMediator) { // Request.Finished event $this->_do('Request.Finished', new Event($this, array('Request' => &$Request, 'Response' => &$Response))); } } } }
/** * Ques a request for handling in the client. * * @see \BLW\Type\HTTP\IClient::run() IClient::run() * @see \BLW\Type\HTTP\IClient::sendAll() IClient::sendAll() * @see \BLW\Type\HTTP\IClient::download() IClient::downoad() * * @throws \BLW\Model\InvalidArgumentException If <code>$Request</code> is not valid. * * @param \BLW\Type\HTTP\IRequest $Request * Request to que. * @return boolean <code>TRUE</code> if successful. <code>FALSE</code> if unsuccessful or if the request is already qued. */ public function send(IRequest $Request) { // Does Client contain request? if ($this->contains($Request)) { return false; } // Is request $Valid? Add to que. if (!$this->validateRequest($Request)) { throw new InvalidArgumentException(0); } // Create Response $Response = new Response('HTTP', '1.0', 0); $Response['Running'] = false; $Response['Finished'] = false; $Response->setRequestURI($Request->getURI()); // Add to que $this->attach($Request, $Response); // Done return true; }
/** * Parse HAR results for information about last request. * * @param \stdClass $Result * Result from either send() or wait() method. * @return \BLW\Type\HTTP\IResponse Parsed response. <code>FALSE</code> on error. */ public function parseResults($Result) { // Invalid result if (!$Result instanceof \stdClass) { return false; // Result not ok? return false } elseif (@strtolower($Result->status) != 'ok') { return false; } // Current URI of page $URI = $Result->log->pages[0]->url; $ResuestURI = new GenericURI($Result->log->pages[0]->id); // Search for response in entries while (list($k, $HAR) = each($Result->log->entries)) { if ($HAR->request->url == $URI) { // Redirect? if ($HAR->response->redirectURL) { // Update $URL -($URI = $HAR->response->redirectURL); // Restart reset($Result->log->entries); continue; } // Return found result $Parts = explode('/', $HAR->response->httpVersion, 2); // Create response $Response = new Response(trim($Parts[0]), trim($Parts[1]), $HAR->response->status); // Loop through headers foreach ($HAR->response->headers as $Header) { // 1st time seing header? if (!isset($Response->Header[$Header->name])) { // Add header with key $Response->Header[$Header->name] = $Response->createHeader($Header->name, $Header->value); // Not first time? } else { // Add header without key $Response->Header[] = $Response->createHeader($Header->name, $Header->value); } } // Content-Type $Response->Header['Content-Type'] = isset($Response->Header['Content-Type']) ? $Response->Header['Content-Type'] : new ContentType($HAR->response->content->mimeType); // URI $Response->URI = new GenericURI($Result->log->pages[0]->url, $ResuestURI); $Response->RequestURI = $ResuestURI; // Content $Response->Body['Content'] = $Result->log->pages[0]->html; // Done return $Response; } } // Error return false; }