<commit_before><?php

namespace SimpleHttpClient;

use \Event;
use \EventBase;
use \EventUtil;
use \EventBuffer;
use \EventDnsBase;
use \EventBufferEvent;
use \EventHttpConnection;
use \EventHttpRequest;
use \SplQueue;

class SimpleHttpClient
{
	protected $scheme;
	protected $host;
	protected $port;
	protected $client;
	protected $user;
	protected $pass;
	protected $contentType;
	protected $debug;
	protected $multi;
	protected $queue;
	private $count = 0;
	private $buffers = array();
	private $errors = array();
	private $filter;

	const MIN_WATERMARK = 512;

	private $validKeys = [
		'scheme', 'host', 'port', 'user', 'pass', 'contentType', 'debug', 'multi'
	];

	public function getBuffers($filter = null){
		$return = array();
		foreach($this->buffers as $key=>$value){
			if(is_string($filter)){
				$return[$key] = $this->commonFilters($filter, $value);
			}
			elseif(is_callable($filter)){
				$return[$key] = $filter($value);
			}
			else{
				return $this->buffers;
			}
		}
		return $return;
	}

	public function getCount(){
		return $this->count;
	}

	public function flush(){
		$this->count = 0;
		$this->buffers = array();
	}

	private function commonFilters($filterName, $value){
		switch($filterName){
			case 'headers':
				list($header,$body) = explode("\r\n\r\n",$value,2);
				return $header;
				break;
			case 'body':
				list($header,$body) = explode("\r\n\r\n",$value,2);
				return $body;
				break;
		}
	}

	public function __construct(Array $options = []){
		$this->host = $options['host'];
		$this->port = $options['port'];
		$this->user = $options['user'];
		$this->pass = $options['pass'];
		$this->contentType = $options['contentType'] ?: 'application/json';
		$this->debug = empty($options['debug']) ? false : true;
		$this->multi = empty($options['multi']) ? null : $options['multi'];
		$this->queue = new SplQueue();
		$this->base = new EventBase();
	}

	public function setUser($user){
		$this->user = $user;
	}

	public function setPassword($pass){
		$this->pass = $pass;
	}

	public function setHost($host){
		$this->host = $host;
	}

	public function getOptions(){
		$options = [];
		foreach($this->validKeys as $key){
			$options[$key] = $this->$key;
		}
		return $options;
	}

	public function setPort($port){
		$this->port = $port;
	}

	public function setDebug($debug){
		$this->debug = $debug;
	}

	public function setMulti($multi){
		$this->multi = $multi;
	}

	public function setContentType($contentType){
		$this->contentType = $contentType;
	}

	public function setOption($key, $value){
		if(in_array($k, $this->validKeys)){
			$this->$k = $v;
		}
		else{
			throw new \Exception('Invalid option: '.$k);
		}
	}

	public function setOptions(Array $options){
		foreach($options as $k => $v){
			$this->setOption($k, $v);
		}
	}

	public function get($path){
		return $this->sendRequest('GET',$path);
	}

	public function put($path,$body){
		return $this->sendRequest('PUT',$path,$body);
	}

	public function post($path,$body){
		return $this->sendRequest('POST',$path,$body);
	}

	public function custom($method,$path,$body){
		return $this->sendRequest($method,$path,$body);
	}

	public function delete($path, $body){
		return $this->sendRequest('DELETE', $path, $body);
	}

	public function fetch(){
		if(!empty($this->queue)){
			foreach($this->queue as $fn){
				$fn();
				$this->queue->dequeue();
			}
			$this->base->dispatch();
		}
	}

	public function sendRequest($method, $url, $body = '') {
		$host = $this->host;
		$port = $this->port;
		$base = $this->base;
		$count = ++$this->count;
		$fn = function() use($base, $host, $port, $method, $url, $body, $count) {

			$dns_base = new EventDnsBase($base, TRUE);

			$readcb = function($bev, $count){
				$bev->readBuffer($bev->input);
				$this->buffers[$count] = empty($this->buffers[$count]) ? '' : $this->buffers[$count];
				while($line = $bev->input->read(SimpleHttpClient::MIN_WATERMARK * 2)){
					$this->buffers[$count] .= $line;
				}
			};

			$eventcb = function($bev, $events, $count){
				if($events & (EventBufferEvent::ERROR | EventBufferEvent::EOF)){
					if($events & EventBufferEvent::ERROR){
						$this->errors[$count] = 'DNS error: '.$bev->getDnsErrorString().PHP_EOL;
					}
					if($events & EventBufferEvent::EOF){
						$this->buffers[$count] .= $bev->input->read(SimpleHttpClient::MIN_WATERMARK * 10);
					}<commit_msg>empty check to get rid of notices<commit_after><?php

namespace SimpleHttpClient;

use \Event;
use \EventBase;
use \EventUtil;
use \EventBuffer;
use \EventDnsBase;
use \EventBufferEvent;
use \EventHttpConnection;
use \EventHttpRequest;
use \SplQueue;

class SimpleHttpClient
{
	protected $scheme;
	protected $host;
	protected $port;
	protected $client;
	protected $user;
	protected $pass;
	protected $contentType;
	protected $debug;
	protected $multi;
	protected $queue;
	private $count = 0;
	private $buffers = array();
	private $errors = array();
	private $filter;

	const MIN_WATERMARK = 512;

	private $validKeys = [
		'scheme', 'host', 'port', 'user', 'pass', 'contentType', 'debug', 'multi'
	];

	public function getBuffers($filter = null){
		$return = array();
		foreach($this->buffers as $key=>$value){
			if(is_string($filter)){
				$return[$key] = $this->commonFilters($filter, $value);
			}
			elseif(is_callable($filter)){
				$return[$key] = $filter($value);
			}
			else{
				return $this->buffers;
			}
		}
		return $return;
	}

	public function getCount(){
		return $this->count;
	}

	public function flush(){
		$this->count = 0;
		$this->buffers = array();
	}

	private function commonFilters($filterName, $value){
		switch($filterName){
			case 'headers':
				list($header,$body) = explode("\r\n\r\n",$value,2);
				return $header;
				break;
			case 'body':
				list($header,$body) = explode("\r\n\r\n",$value,2);
				return $body;
				break;
		}
	}

	public function __construct(Array $options = []){
		$this->host = $options['host'];
		$this->port = $options['port'];
		$this->user = $options['user'];
		$this->pass = $options['pass'];
		$this->contentType = $options['contentType'] ?: 'application/json';
		$this->debug = empty($options['debug']) ? false : true;
		$this->multi = empty($options['multi']) ? null : $options['multi'];
		$this->queue = new SplQueue();
		$this->base = new EventBase();
	}

	public function setUser($user){
		$this->user = $user;
	}

	public function setPassword($pass){
		$this->pass = $pass;
	}

	public function setHost($host){
		$this->host = $host;
	}

	public function getOptions(){
		$options = [];
		foreach($this->validKeys as $key){
			$options[$key] = $this->$key;
		}
		return $options;
	}

	public function setPort($port){
		$this->port = $port;
	}

	public function setDebug($debug){
		$this->debug = $debug;
	}

	public function setMulti($multi){
		$this->multi = $multi;
	}

	public function setContentType($contentType){
		$this->contentType = $contentType;
	}

	public function setOption($key, $value){
		if(in_array($k, $this->validKeys)){
			$this->$k = $v;
		}
		else{
			throw new \Exception('Invalid option: '.$k);
		}
	}

	public function setOptions(Array $options){
		foreach($options as $k => $v){
			$this->setOption($k, $v);
		}
	}

	public function get($path){
		return $this->sendRequest('GET',$path);
	}

	public function put($path,$body){
		return $this->sendRequest('PUT',$path,$body);
	}

	public function post($path,$body){
		return $this->sendRequest('POST',$path,$body);
	}

	public function custom($method,$path,$body){
		return $this->sendRequest($method,$path,$body);
	}

	public function delete($path, $body){
		return $this->sendRequest('DELETE', $path, $body);
	}

	public function fetch(){
		if(!empty($this->queue)){
			foreach($this->queue as $fn){
				$fn();
				$this->queue->dequeue();
			}
			$this->base->dispatch();
		}
	}

	public function sendRequest($method, $url, $body = '') {
		$host = $this->host;
		$port = $this->port;
		$base = $this->base;
		$count = ++$this->count;
		$fn = function() use($base, $host, $port, $method, $url, $body, $count) {

			$dns_base = new EventDnsBase($base, TRUE);

			$readcb = function($bev, $count){
				$bev->readBuffer($bev->input);
				$this->buffers[$count] = empty($this->buffers[$count]) ? '' : $this->buffers[$count];
				while($line = $bev->input->read(SimpleHttpClient::MIN_WATERMARK * 2)){
					$this->buffers[$count] .= $line;
				}
			};

			$eventcb = function($bev, $events, $count){
				if($events & (EventBufferEvent::ERROR | EventBufferEvent::EOF)){
					if($events & EventBufferEvent::ERROR){
						$this->errors[$count] = 'DNS error: '.$bev->getDnsErrorString().PHP_EOL;
					}
					if($events & EventBufferEvent::EOF){
						$this->buffers[$count] = empty($this->buffers[$count]) ? '' : $this->buffers[$count];
						$this->buffers[$count] .= $bev->input->read(SimpleHttpClient::MIN_WATERMARK * 10);
					}
{'fraction_non_alphanumeric': 0.19809610154125112, 'fraction_numerical': 0.0022665457842248413, 'mean_word_length': 4.182031708749266, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 120, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '964674', 'n_tokens_mistral': 3460, 'n_tokens_neox': 3052, 'n_words': 633}