<?php

class HttpContext
{  
	private $req_host;
	private $req_port;
	private $req_cookies;
	private $req_full_text;
	private $req_headers;
	
	private $resp_status_code;
	private $resp_status_message;
	private $resp_content_length;
	private $resp_body;
	private $resp_headers;
	
	public function __construct($host, $port)
	{  
		$this->req_host = $host;
		$this->req_port = $port;
		$this->req_cookies = Array();
		$this->req_headers = Array();
		$this->resp_headers = Array();
	}  
	
	public function getRequestFullText() { return $this->req_full_text; }
	public function getRequestHeaders() { return $this->req_headers; }
	public function setRequestHeaders($headers) { $this->req_headers = $headers; }
	
	public function getResponseStatusCode() { return $this->resp_status_code; }
	public function getResponseStatusMessage() { return $this->resp_status_message; }
	public function getResponseBody() { return $this->resp_body; }
	public function getResponseBodyAsSimpleXml() { return new SimpleXMLElement($this->resp_body); }
	public function getResponseHeaders() { return $this->resp_headers; }
	
	public function get($url) { return $this->request($url, 'GET', null); }
	public function post($url, $content = null) { return $this->request($url, 'POST', $content, 'application/xml; charset=utf-8'); }
	public function put($url, $content = null) { return $this->request($url, 'PUT', $content, 'application/xml; charset=utf-8'); }
	public function delete($url) { return $this->request($url, 'DELETE', null); }
	
	public function request($url, $method, $content = null, $content_type = null)
	{	
		$url_parts = parse_url($url);
		$target_host = isset($url_parts['host']) ? $url_parts['host'] : $this->req_host;
		$target_port= isset($url_parts['port']) ? $url_parts['port'] : $this->req_port;
		$relurl = $url_parts['path'] 
						. (isset($url_parts['query']) ? ('?' . $url_parts['query']) : '')
						. (isset($url_parts['fragment']) ? ('#' . $url_parts['fragment']) : '');;
		
		$fp = fsockopen($target_host, $target_port, $errno, $errstr, 30);
		if (!$fp)
		{
			echo "HttpContext error: $errstr ($errno)\n";
			return false;
		} else {
			$out = $method . ' ' . $relurl . " HTTP/1.0\r\n";
			$out .= 'Host: ' . $target_host . ($target_port === 80 ? '' : (':' . $target_port)) . "\r\n";
			
			if (count($this->req_cookies) > 0)
			{
				$i = 0;
				$out .= 'Cookie: ';
				
				foreach ($this->req_cookies as $key => $value)
				{
					if ($i > 0)
						$out .= '; ';
					
					$out .= $key . '=' . $value;
					$i++;
				}
				
				$out .= "\r\n";
			}	
			
			if ($content_type !== null)
				$out .= 'Content-Type: ' . $content_type . "\r\n";
            $out .= 'Content-Length: ' . ($content !== null ? strlen($content) : 0) . "\r\n";				
				
			foreach ($this->req_headers as $key => $value)
			{
				$out .= $key . ': ' . $value . "\r\n";
			}
			
			$out .= "Connection: Close\r\n\r\n";
			if ($content !== null)
				$out .= $content;

			$this->req_full_text = $out;
			fwrite($fp, $out);
			
			$line = fgets($fp);
			
			if (strlen($line) < 10 || substr_compare($line, 'HTTP/1.1', 0, 8) != 0)
			{
				return false;
				fclose($fp);
			}
			
			$n = strpos($line, ' ', 9);
			
			$this->resp_status_code = substr($line, 9, $n - 9);
			$this->resp_status_message = trim(substr($line, $n + 1));
			$this->resp_headers = Array();
			$this->resp_content_length = 0;
			
			while (strlen($line = trim(fgets($fp))) > 0)
			{
				$n = strpos($line, ':');
				$key = strtolower(trim(substr($line, 0, $n)));
				$value = trim(substr($line, $n + 1));
				$this->resp_headers[$key] = $value;
				
				if (strcmp($key, 'content-length') == 0)
				{
					$this->resp_content_length = $value;
				}
				else if (strcmp($key, 'set-cookie') == 0)
				{
					$cookie = explode('=', explode(';', $value)[0]);
					$this->req_cookies[$cookie[0]] = $cookie[1];
				}
			}
			
			$off = 0;
			$body = '';
			while (!feof($fp))
			{
				$line = fgets($fp);
				$off += strlen($line);
				$body .= $line;
			}
			
			$this->resp_body = $body;
				
			fclose($fp);
			return true;
		}
		
	}
}

?>