/web_agent/src/request/lmbSocketWebAgentRequest.class.php

https://github.com/kugu/limb · PHP · 240 lines · 189 code · 28 blank · 23 comment · 15 complexity · 31d01b2012c09bd6a263c3bf7ea486cf MD5 · raw file

  1. <?php
  2. /*
  3. * Limb PHP Framework
  4. *
  5. * @link http://limb-project.com
  6. * @copyright Copyright &copy; 2004-2009 BIT(http://bit-creative.com)
  7. * @license LGPL http://www.gnu.org/copyleft/lesser.html
  8. */
  9. lmb_require(dirname(__FILE__).'/lmbAbstractWebAgentRequest.class.php');
  10. lmb_require(dirname(__FILE__).'/../lmbWebServerResponse.class.php');
  11. lmb_require(dirname(__FILE__).'/../lmbWebAgentCookie.class.php');
  12. lmb_require(dirname(__FILE__).'/../lmbWebAgentHeaders.class.php');
  13. lmb_require(dirname(__FILE__).'/../lmbWebServerCookiesCollection.class.php');
  14. /**
  15. * Web request with sockets
  16. *
  17. * @package web_agent
  18. * @version $Id: lmbSocketWebAgentRequest.class.php 7686 2009-03-04 19:57:12Z korchasa $
  19. */
  20. class lmbSocketWebAgentRequest extends lmbAbstractWebAgentRequest {
  21. protected $request_data = '';
  22. protected $request_headers = null;
  23. protected $request_method = '';
  24. protected $parsed_url = null;
  25. protected $response_data = array();
  26. protected $response_headers = null;
  27. protected function prepareRequestData($url, $method)
  28. {
  29. $this->setRequestMethod($method);
  30. $this->setRequestUrl($url);
  31. $this->initRequestData();
  32. $this->prepareCookies();
  33. $this->prepareUserAgent();
  34. $this->prepareAcceptCharset();
  35. $this->prepareHeaders();
  36. $this->prepareContent();
  37. $this->assembleRequestData();
  38. }
  39. protected function setRequestMethod($method)
  40. {
  41. $this->request_method = strtoupper($method);
  42. }
  43. protected function setRequestUrl($url)
  44. {
  45. $this->parsed_url = parse_url($url);
  46. }
  47. protected function getRequestHostWithPort()
  48. {
  49. $host = $this->getRequestHost();
  50. $port = $this->getRequestPort();
  51. if($port != 80) $host .= ':'.$port;
  52. return $host;
  53. }
  54. protected function getRequestPort()
  55. {
  56. return isset($this->parsed_url['port']) ? $this->parsed_url['port'] : 80;
  57. }
  58. protected function getRequestHost()
  59. {
  60. return $this->parsed_url['host'];
  61. }
  62. protected function getRequestPath()
  63. {
  64. $path = '/';
  65. if(isset($this->parsed_url['path']))
  66. $path = $this->parsed_url['path'];
  67. if(isset($this->parsed_url['query']))
  68. $path .= '?'.$this->parsed_url['query'];
  69. return $path;
  70. }
  71. protected function initRequestData()
  72. {
  73. $this->request_headers = new lmbWebAgentHeaders();
  74. $this->request_headers->setRaw(
  75. $this->request_method.' '.$this->getRequestPath().' HTTP/1.1'
  76. );
  77. $this->addHeader('host', $this->getRequestHostWithPort());
  78. $this->addHeader('connection', 'close');
  79. }
  80. protected function addHeader($name, $value)
  81. {
  82. $this->request_headers->set($name, $value);
  83. }
  84. protected function prepareCookies()
  85. {
  86. if($this->cookies->hasCookies())
  87. $this->addHeader('Cookie', $this->cookies->export());
  88. }
  89. protected function prepareUserAgent()
  90. {
  91. if($this->user_agent)
  92. $this->addHeader('User-Agent', $this->user_agent);
  93. }
  94. protected function prepareAcceptCharset()
  95. {
  96. if($this->accept_charset)
  97. $this->addHeader('Accept-Charset', $this->accept_charset);
  98. }
  99. protected function prepareHeaders()
  100. {
  101. $this->headers->copyTo($this->request_headers);
  102. }
  103. protected function prepareContent()
  104. {
  105. if($this->content)
  106. $this->addHeader('Content-length', strlen($this->content));
  107. }
  108. protected function assembleRequestData()
  109. {
  110. $this->request_data = $this->request_headers->exportHeaders()."\r\n".$this->content;
  111. }
  112. function doRequest($url, $method = 'GET')
  113. {
  114. $this->prepareRequestData($url, $method);
  115. //echo '<pre>', $this->request_data, '</pre>';
  116. if($this->readData())
  117. {
  118. $headers = $this->readHeaders();
  119. $status = $this->readStatus();
  120. $mediatype = $this->readMediaType();
  121. $charset = $this->readCharset();
  122. $cookies = $this->readCookies();
  123. $content = $this->readContent();
  124. /*ini_set('xdebug.var_display_max_depth', 4 );
  125. var_dump(
  126. $status,
  127. $mediatype,
  128. $charset,
  129. $cookies,
  130. $headers
  131. );
  132. echo $content;*/
  133. return new lmbWebServerResponse(
  134. $content,
  135. $status,
  136. $mediatype,
  137. $charset,
  138. $cookies,
  139. $headers
  140. );
  141. }
  142. else
  143. {
  144. return new lmbWebServerResponse('', 400, '', '', new lmbWebServerCookiesCollection(), new lmbWebAgentHeaders());
  145. }
  146. }
  147. protected function readData()
  148. {
  149. if($fp = fsockopen($this->getRequestHost(), $this->getRequestPort()))
  150. {
  151. fwrite($fp, $this->request_data);
  152. $this->response_data = array();
  153. while(!feof($fp))
  154. $this->response_data[] = fgets($fp);
  155. fclose($fp);
  156. return true;
  157. }
  158. else
  159. return false;
  160. }
  161. protected function readHeaders()
  162. {
  163. $this->response_headers = new lmbWebAgentHeaders();
  164. while($header = trim(array_shift($this->response_data))) {
  165. $this->response_headers->parse($header);
  166. }
  167. return $this->response_headers;
  168. }
  169. protected function readStatus()
  170. {
  171. $first = $this->response_headers->getFirst();
  172. if(!$first) return 400;
  173. if(!preg_match('#^HTTP/\S+[ ]+([0-9]+)#', $first, $a))
  174. return false;
  175. return $a[1];
  176. }
  177. protected function getContentTypeHeader()
  178. {
  179. return strtolower($this->response_headers->get('content-type'));
  180. }
  181. protected function readMediaType()
  182. {
  183. $content_type = $this->getContentTypeHeader();
  184. if(!$content_type)
  185. return 'text/html';
  186. return substr($content_type, 0, strpos($content_type, ';'));
  187. }
  188. protected function readCharset()
  189. {
  190. $content_type = $this->getContentTypeHeader();
  191. if(!$content_type)
  192. return $this->getDefaultCharset();
  193. $charset_pos = strpos($content_type, 'charset=');
  194. if($charset_pos === false)
  195. return $this->getDefaultCharset();
  196. return substr($content_type, $charset_pos + 8);
  197. }
  198. protected function readCookies()
  199. {
  200. $cookies = new lmbWebServerCookiesCollection();
  201. $n = 0;
  202. while(($value = $this->response_headers->get('set-cookie', $n ++)) !== null)
  203. {
  204. $cookies->add(new lmbWebServerCookie($value));
  205. }
  206. return $cookies;
  207. }
  208. protected function readContent()
  209. {
  210. return implode('', $this->response_data);
  211. }
  212. }