async file reader restructure part 4

This commit is contained in:
kremsy 2015-09-30 22:51:12 +02:00
parent fed08286ee
commit 67239cd1a9
2 changed files with 40 additions and 31 deletions

View File

@ -5,6 +5,7 @@
namespace ManiaControl\Files;
use cURL\Event;
use cURL\Request;
use ManiaControl\ManiaControl;
class AsyncHttpRequest {
@ -23,7 +24,28 @@ class AsyncHttpRequest {
$this->url = $url;
}
/**
* Create a new cURL Request for the given URL
*
* @param string $url
* @return Request
*/
private function newRequest($url) {
$request = new Request($url);
$request->getOptions()->set(CURLOPT_TIMEOUT, 60)->set(CURLOPT_HEADER, false)// don't display response header
->set(CURLOPT_CRLF, true)// linux line feed
->set(CURLOPT_ENCODING, '')// accept encoding
->set(CURLOPT_USERAGENT, 'ManiaControl v' . ManiaControl::VERSION)// user-agent
->set(CURLOPT_RETURNTRANSFER, true); // return instead of output content
return $request;
}
//TODO merge loadFile / postData
/**
* Carry out a GetData Request
*
* @param int $keepAlive
*/
public function getData($keepAlive = 0) {
array_push($this->headers, 'Content-Type: ' . $this->contentType);
if ($keepAlive) {
@ -31,8 +53,7 @@ class AsyncHttpRequest {
array_push($headers, 'Connection: Keep-Alive');
}
$fileReader = new AsynchronousFileReader($this->maniaControl);
$request = $fileReader->newRequest($this->url);
$request = $this->newRequest($this->url);
$request->getOptions()->set(CURLOPT_AUTOREFERER, true)// accept link reference
->set(CURLOPT_HTTPHEADER, $this->headers); // headers
@ -47,10 +68,14 @@ class AsyncHttpRequest {
call_user_func($function, $content, $error);
});
$fileReader = new AsynchronousFileReader($this->maniaControl);
$fileReader->addRequest($request);
}
/**
* Carry out a PostData Request
*/
public function postData() {
array_push($this->headers, 'Content-Type: ' . $this->contentType);
array_push($this->headers, 'Keep-Alive: timeout=600, max=2000');
@ -63,9 +88,7 @@ class AsyncHttpRequest {
}
$fileReader = new AsynchronousFileReader($this->maniaControl);
$request = $fileReader->newRequest($this->url);
$request = $this->newRequest($this->url);
$request->getOptions()->set(CURLOPT_POST, true)// post method
->set(CURLOPT_POSTFIELDS, $content)// post content field
->set(CURLOPT_HTTPHEADER, $this->headers) // headers
@ -81,6 +104,7 @@ class AsyncHttpRequest {
call_user_func($function, $content, $error);
});
$fileReader = new AsynchronousFileReader($this->maniaControl);
$fileReader->addRequest($request);
}

View File

@ -66,6 +66,7 @@ class AsynchronousFileReader {
* @param string $contentType
* @param int $keepAlive
* @param array $headers Additional Headers
* @deprecated @see ManiaControl\Files\AsyncHttpRequest
*/
public function loadFile($url, callable $function, $contentType = 'UTF-8', $keepAlive = 0, $headers = array()) {
$httpRequest = new AsyncHttpRequest($this->maniaControl, $url);
@ -73,32 +74,6 @@ class AsynchronousFileReader {
$httpRequest->getData($keepAlive);
}
/**
* Create a new cURL Request for the given URL, DO NOT CALL MANUALLY!
*
* @param string $url
* @return Request
*/
public function newRequest($url) {
$request = new Request($url);
$request->getOptions()->set(CURLOPT_TIMEOUT, 60)->set(CURLOPT_HEADER, false)// don't display response header
->set(CURLOPT_CRLF, true)// linux line feed
->set(CURLOPT_ENCODING, '')// accept encoding
->set(CURLOPT_USERAGENT, 'ManiaControl v' . ManiaControl::VERSION)// user-agent
->set(CURLOPT_RETURNTRANSFER, true); // return instead of output content
return $request;
}
/**
* Add a Request to the queue, DO NOT CALL MANUALLY!
*
* @param Request $request
*/
public function addRequest(Request $request) {
array_push($this->requests, $request);
}
/**
* Send Data via POST Method
*
@ -108,10 +83,20 @@ class AsynchronousFileReader {
* @param bool $compression
* @param string $contentType
* @param array $headers Additional Headers
* @deprecated @see ManiaControl\Files\AsyncHttpRequest
*/
public function postData($url, callable $function, $content, $compression = false, $contentType = 'text/xml; charset=UTF-8;', $headers = array()) {
$httpRequest = new AsyncHttpRequest($this->maniaControl, $url);
$httpRequest->setCallable($function)->setContent($content)->setCompression($compression)->setContentType($contentType)->setHeaders($headers);
$httpRequest->postData();
}
/**
* Add a Request to the queue, DO NOT CALL MANUALLY!
*
* @param Request $request
*/
public function addRequest(Request $request) {
array_push($this->requests, $request);
}
}