code improvements for common curl options
This commit is contained in:
		| @@ -62,14 +62,8 @@ class AsynchronousFileReader { | |||||||
| 	 * @param callable $function | 	 * @param callable $function | ||||||
| 	 * @param string   $contentType | 	 * @param string   $contentType | ||||||
| 	 * @param int      $keepAlive | 	 * @param int      $keepAlive | ||||||
| 	 * @return bool |  | ||||||
| 	 */ | 	 */ | ||||||
| 	public function loadFile($url, callable $function, $contentType = 'UTF-8', $keepAlive = 0) { | 	public function loadFile($url, callable $function, $contentType = 'UTF-8', $keepAlive = 0) { | ||||||
| 		if (!$url) { |  | ||||||
| 			$this->maniaControl->log('Missing URL!'); |  | ||||||
| 			return false; |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		$headers = array(); | 		$headers = array(); | ||||||
| 		array_push($headers, 'Content-Type: ' . $contentType); | 		array_push($headers, 'Content-Type: ' . $contentType); | ||||||
| 		if ($keepAlive) { | 		if ($keepAlive) { | ||||||
| @@ -77,8 +71,8 @@ class AsynchronousFileReader { | |||||||
| 			array_push($headers, 'Connection: Keep-Alive'); | 			array_push($headers, 'Connection: Keep-Alive'); | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		$request = new Request($url); | 		$request = $this->newRequest($url); | ||||||
| 		$this->prepareOptions($request->getOptions()) | 		$request->getOptions() | ||||||
| 		        ->set(CURLOPT_AUTOREFERER, true) // accept link reference | 		        ->set(CURLOPT_AUTOREFERER, true) // accept link reference | ||||||
| 		        ->set(CURLOPT_HTTPHEADER, $headers); // headers | 		        ->set(CURLOPT_HTTPHEADER, $headers); // headers | ||||||
|  |  | ||||||
| @@ -95,23 +89,24 @@ class AsynchronousFileReader { | |||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
| 		$this->addRequest($request); | 		$this->addRequest($request); | ||||||
| 		return true; |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	/** | 	/** | ||||||
| 	 * Prepare the cURL Options | 	 * Create a new cURL Request for the given URL | ||||||
| 	 * | 	 * | ||||||
| 	 * @param Options $options | 	 * @param string $url | ||||||
| 	 * @return Options | 	 * @return Request | ||||||
| 	 */ | 	 */ | ||||||
| 	private function prepareOptions(Options $options) { | 	protected function newRequest($url) { | ||||||
| 		$options->set(CURLOPT_TIMEOUT, 10) | 		$request = new Request($url); | ||||||
|  | 		$request->getOptions() | ||||||
|  | 		        ->set(CURLOPT_TIMEOUT, 10) | ||||||
| 		        ->set(CURLOPT_HEADER, false) // don't display response header | 		        ->set(CURLOPT_HEADER, false) // don't display response header | ||||||
| 		        ->set(CURLOPT_CRLF, true) // linux line feed | 		        ->set(CURLOPT_CRLF, true) // linux line feed | ||||||
| 		        ->set(CURLOPT_ENCODING, '') // accept encoding | 		        ->set(CURLOPT_ENCODING, '') // accept encoding | ||||||
| 		        ->set(CURLOPT_USERAGENT, 'ManiaControl v' . ManiaControl::VERSION) | 		        ->set(CURLOPT_USERAGENT, 'ManiaControl v' . ManiaControl::VERSION) // user-agent | ||||||
| 		        ->set(CURLOPT_RETURNTRANSFER, true); | 		        ->set(CURLOPT_RETURNTRANSFER, true); // return instead of output content | ||||||
| 		return $options; | 		return $request; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	/** | 	/** | ||||||
| @@ -119,7 +114,7 @@ class AsynchronousFileReader { | |||||||
| 	 * | 	 * | ||||||
| 	 * @param Request $request | 	 * @param Request $request | ||||||
| 	 */ | 	 */ | ||||||
| 	public function addRequest(Request $request) { | 	protected function addRequest(Request $request) { | ||||||
| 		array_push($this->requests, $request); | 		array_push($this->requests, $request); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| @@ -131,29 +126,22 @@ class AsynchronousFileReader { | |||||||
| 	 * @param string   $content | 	 * @param string   $content | ||||||
| 	 * @param bool     $compression | 	 * @param bool     $compression | ||||||
| 	 * @param string   $contentType | 	 * @param string   $contentType | ||||||
| 	 * @return bool |  | ||||||
| 	 */ | 	 */ | ||||||
| 	public function postData($url, callable $function, $content, $compression = false, | 	public function postData($url, callable $function, $content, $compression = false, | ||||||
| 	                         $contentType = 'text/xml; charset=UTF-8;') { | 	                         $contentType = 'text/xml; charset=UTF-8;') { | ||||||
| 		if (!$url) { |  | ||||||
| 			$this->maniaControl->log("Url is empty"); |  | ||||||
| 			return false; |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		$content = str_replace(array("\r", "\n"), '', $content); |  | ||||||
|  |  | ||||||
| 		$headers = array(); | 		$headers = array(); | ||||||
| 		array_push($headers, 'Content-Type: ' . $contentType); | 		array_push($headers, 'Content-Type: ' . $contentType); | ||||||
| 		array_push($headers, 'Keep-Alive: 300'); | 		array_push($headers, 'Keep-Alive: 300'); | ||||||
| 		array_push($headers, 'Connection: Keep-Alive'); | 		array_push($headers, 'Connection: Keep-Alive'); | ||||||
|  |  | ||||||
|  | 		$content = str_replace(array("\r", "\n"), '', $content); | ||||||
| 		if ($compression) { | 		if ($compression) { | ||||||
| 			$content = zlib_encode($content, 31); | 			$content = zlib_encode($content, 31); | ||||||
| 			array_push($headers, 'Content-Encoding: gzip'); | 			array_push($headers, 'Content-Encoding: gzip'); | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		$request = new Request($url); | 		$request = $this->newRequest($url); | ||||||
| 		$this->prepareOptions($request->getOptions()) | 		$request->getOptions() | ||||||
| 		        ->set(CURLOPT_POST, true) // post method | 		        ->set(CURLOPT_POST, true) // post method | ||||||
| 		        ->set(CURLOPT_POSTFIELDS, $content) // post content field | 		        ->set(CURLOPT_POSTFIELDS, $content) // post content field | ||||||
| 		        ->set(CURLOPT_HTTPHEADER, $headers); // headers | 		        ->set(CURLOPT_HTTPHEADER, $headers); // headers | ||||||
| @@ -171,6 +159,5 @@ class AsynchronousFileReader { | |||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
| 		$this->addRequest($request); | 		$this->addRequest($request); | ||||||
| 		return true; |  | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user