Processor.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php
  2. /**
  3. * A parallel HTTP client written in pure PHP
  4. *
  5. * @author hightman <hightman@twomice.net>
  6. * @link http://hightman.cn
  7. * @copyright Copyright (c) 2015 Twomice Studio.
  8. */
  9. namespace hightman\http;
  10. /**
  11. * Http processor
  12. * Handle requests
  13. * @author hightman
  14. * @since 1.0
  15. */
  16. class Processor
  17. {
  18. /**
  19. * @var string request key
  20. */
  21. public $key;
  22. /**
  23. * @var Client client object
  24. */
  25. public $cli;
  26. /**
  27. * @var Request request object
  28. */
  29. public $req;
  30. /**
  31. * @var Response response object
  32. */
  33. public $res;
  34. /**
  35. * @var Connection
  36. */
  37. public $conn = null;
  38. /**
  39. * @var boolean whether the process is completed
  40. */
  41. public $finished;
  42. protected $headerOk, $timeBegin, $chunkLeft;
  43. /**
  44. * Constructor
  45. * @param Client $cli
  46. * @param Request $req
  47. * @param string|integer $key
  48. */
  49. public function __construct($cli, $req, $key = null)
  50. {
  51. $this->cli = $cli;
  52. $this->req = $req;
  53. $this->key = $key;
  54. $this->res = new Response($req->getRawUrl());
  55. $this->finished = $this->headerOk = false;
  56. $this->timeBegin = microtime(true);
  57. }
  58. /**
  59. * Destructor
  60. */
  61. public function __destruct()
  62. {
  63. if ($this->conn) {
  64. $this->conn->close();
  65. }
  66. $this->req = $this->cli = $this->res = $this->conn = null;
  67. }
  68. /**
  69. * Get connection
  70. * @return Connection the connection object, returns null if the connection fails or need to queue.
  71. */
  72. public function getConn()
  73. {
  74. if ($this->conn === null) {
  75. $this->conn = Connection::connect($this->req->getUrlParam('conn'), $this);
  76. if ($this->conn === false) {
  77. $this->res->error = Connection::getLastError();
  78. $this->finish();
  79. } else {
  80. if ($this->conn !== null) {
  81. $this->conn->addWriteData($this->getRequestBuf());
  82. }
  83. }
  84. }
  85. return $this->conn;
  86. }
  87. public function send()
  88. {
  89. if ($this->conn->write() === false) {
  90. $this->finish('BROKEN');
  91. }
  92. }
  93. public function recv()
  94. {
  95. return $this->headerOk ? $this->readBody() : $this->readHeader();
  96. }
  97. /**
  98. * Finish the processor
  99. * @param string $type finish type, supports: NORMAL, BROKEN, TIMEOUT
  100. */
  101. public function finish($type = 'NORMAL')
  102. {
  103. $this->finished = true;
  104. if ($type === 'BROKEN') {
  105. $this->res->error = Connection::getLastError();
  106. } else {
  107. if ($type !== 'NORMAL') {
  108. $this->res->error = ucfirst(strtolower($type));
  109. }
  110. }
  111. // gzip decode
  112. $encoding = $this->res->getHeader('content-encoding');
  113. if ($encoding !== null && strstr($encoding, 'gzip')) {
  114. $this->res->body = Client::gzdecode($this->res->body);
  115. }
  116. // parser
  117. $this->res->timeCost = microtime(true) - $this->timeBegin;
  118. $this->cli->runParser($this->res, $this->req, $this->key);
  119. // conn
  120. if ($this->conn) {
  121. // close conn
  122. $close = $this->res->getHeader('connection');
  123. $this->conn->close($type !== 'NORMAL' || !strcasecmp($close, 'close'));
  124. $this->conn = null;
  125. // redirect
  126. if (($this->res->status === 301 || $this->res->status === 302)
  127. && $this->res->numRedirected < $this->req->getMaxRedirect()
  128. && ($location = $this->res->getHeader('location')) !== null
  129. ) {
  130. Client::debug('redirect to \'', $location, '\'');
  131. $req = $this->req;
  132. if (!preg_match('/^https?:\/\//i', $location)) {
  133. $pa = $req->getUrlParams();
  134. $url = $pa['scheme'] . '://' . $pa['host'];
  135. if (isset($pa['port'])) {
  136. $url .= ':' . $pa['port'];
  137. }
  138. if (substr($location, 0, 1) == '/') {
  139. $url .= $location;
  140. } else {
  141. $url .= substr($pa['path'], 0, strrpos($pa['path'], '/') + 1) . $location;
  142. }
  143. $location = $url; /// FIXME: strip relative '../../'
  144. }
  145. // change new url
  146. $prevUrl = $req->getUrl();
  147. $req->setUrl($location);
  148. if (!$req->getHeader('referer')) {
  149. $req->setHeader('referer', $prevUrl);
  150. }
  151. if ($req->getMethod() !== 'HEAD') {
  152. $req->setMethod('GET');
  153. }
  154. $req->clearCookie();
  155. $req->setHeader('host', null);
  156. $req->setHeader('x-server-ip', null);
  157. $req->setHeader('content-type', null);
  158. $req->setBody(null);
  159. // reset response
  160. $this->res->numRedirected++;
  161. $this->finished = $this->headerOk = false;
  162. return $this->res->reset();
  163. }
  164. }
  165. Client::debug('finished', $this->res->hasError() ? ' (' . $this->res->error . ')' : '');
  166. $this->req = $this->cli = null;
  167. }
  168. private function readHeader()
  169. {
  170. // read header
  171. while (($line = $this->conn->getLine()) !== null) {
  172. if ($line === false) {
  173. return $this->finish('BROKEN');
  174. }
  175. if ($line === '') {
  176. $this->headerOk = true;
  177. $this->chunkLeft = 0;
  178. return $this->readBody();
  179. }
  180. Client::debug('read header line: ', $line);
  181. if (!strncmp('HTTP/', $line, 5)) {
  182. $line = trim(substr($line, strpos($line, ' ')));
  183. list($this->res->status, $this->res->statusText) = explode(' ', $line, 2);
  184. $this->res->status = intval($this->res->status);
  185. } else {
  186. if (!strncasecmp('Set-Cookie: ', $line, 12)) {
  187. $cookie = $this->parseCookieLine($line);
  188. if ($cookie !== false) {
  189. $this->res->setRawCookie($cookie['name'], $cookie['value']);
  190. $this->cli->setRawCookie($cookie['name'], $cookie['value'], $cookie['expires'], $cookie['domain'], $cookie['path']);
  191. }
  192. } else {
  193. list($k, $v) = explode(':', $line, 2);
  194. $this->res->addHeader($k, trim($v));
  195. }
  196. }
  197. }
  198. }
  199. private function readBody()
  200. {
  201. // head only
  202. if ($this->req->getMethod() === 'HEAD') {
  203. return $this->finish();
  204. }
  205. // chunked
  206. $res = $this->res;
  207. $conn = $this->conn;
  208. $length = $res->getHeader('content-length');
  209. $encoding = $res->getHeader('transfer-encoding');
  210. if ($encoding !== null && !strcasecmp($encoding, 'chunked')) {
  211. // unfinished chunk
  212. if ($this->chunkLeft > 0) {
  213. $buf = $conn->read($this->chunkLeft);
  214. if ($buf === false) {
  215. return $this->finish('BROKEN');
  216. }
  217. if (is_string($buf)) {
  218. Client::debug('read chunkLeft(', $this->chunkLeft, ')=', strlen($buf));
  219. $res->body .= $buf;
  220. $this->chunkLeft -= strlen($buf);
  221. if ($this->chunkLeft === 0) {
  222. // strip CRLF
  223. $res->body = substr($res->body, 0, -2);
  224. }
  225. }
  226. if ($this->chunkLeft > 0) {
  227. return;
  228. }
  229. }
  230. // next chunk
  231. while (($line = $conn->getLine()) !== null) {
  232. if ($line === false) {
  233. return $this->finish('BROKEN');
  234. }
  235. Client::debug('read chunk line: ', $line);
  236. if (($pos = strpos($line, ';')) !== false) {
  237. $line = substr($line, 0, $pos);
  238. }
  239. $size = intval(hexdec(trim($line)));
  240. if ($size <= 0) {
  241. while ($line = $conn->getLine()) // tail header
  242. {
  243. if ($line === '') {
  244. break;
  245. }
  246. Client::debug('read tailer line: ', $line);
  247. if (($pos = strpos($line, ':')) !== false) {
  248. $res->addHeader(substr($line, 0, $pos), trim(substr($line, $pos + 1)));
  249. }
  250. }
  251. return $this->finish();
  252. }
  253. // add CRLF, save to chunkLeft for next loop
  254. $this->chunkLeft = $size + 2; // add CRLF
  255. return;
  256. }
  257. } else {
  258. if ($length !== null) {
  259. $size = intval($length) - strlen($res->body);
  260. if ($size > 0) {
  261. $buf = $conn->read($size);
  262. if ($buf === false) {
  263. return $this->finish('BROKEN');
  264. }
  265. if (is_string($buf)) {
  266. Client::debug('read fixedBody(', $size, ')=', strlen($buf));
  267. $res->body .= $buf;
  268. $size -= strlen($buf);
  269. }
  270. }
  271. if ($size === 0) {
  272. return $this->finish();
  273. }
  274. } else {
  275. if ($res->body === '') {
  276. $res->setHeader('connection', 'close');
  277. }
  278. if (($buf = $conn->read()) === false) {
  279. return $this->finish();
  280. }
  281. if (is_string($buf)) {
  282. Client::debug('read streamBody()=', strlen($buf));
  283. $res->body .= $buf;
  284. }
  285. }
  286. }
  287. }
  288. private function parseCookieLine($line)
  289. {
  290. $now = time();
  291. $cookie = ['name' => '', 'value' => '', 'expires' => null, 'path' => '/'];
  292. $cookie['domain'] = $this->req->getHeader('host');
  293. $parts = explode(';', substr($line, 12));
  294. foreach ($parts as $part) {
  295. if (($pos = strpos($part, '=')) === false) {
  296. continue;
  297. }
  298. $k = trim(substr($part, 0, $pos));
  299. $v = trim(substr($part, $pos + 1));
  300. if ($cookie['name'] === '') {
  301. $cookie['name'] = $k;
  302. $cookie['value'] = $v;
  303. } else {
  304. $k = strtolower($k);
  305. if ($k === 'expires') {
  306. $cookie[$k] = strtotime($v);
  307. if ($cookie[$k] < $now) {
  308. $cookie['value'] = '';
  309. }
  310. } else {
  311. if ($k === 'domain') {
  312. $pos = strpos($cookie['domain'], $v);
  313. if ($pos === 0 || substr($cookie['domain'], $pos, 1) === '.' || substr($cookie['domain'], $pos + 1, 1) === '.') {
  314. $cookie[$k] = $v;
  315. }
  316. } else {
  317. if (isset($cookie[$k])) {
  318. $cookie[$k] = $v;
  319. }
  320. }
  321. }
  322. }
  323. }
  324. if ($cookie['name'] !== '') {
  325. return $cookie;
  326. }
  327. return false;
  328. }
  329. private function getRequestBuf()
  330. {
  331. // request line
  332. $cli = $this->cli;
  333. $req = $this->req;
  334. $pa = $req->getUrlParams();
  335. $header = $req->getMethod() . ' ' . $pa['path'];
  336. if (isset($pa['query'])) {
  337. $header .= '?' . $pa['query'];
  338. }
  339. $header .= ' HTTP/1.1' . Client::CRLF;
  340. // body (must call prior than headers)
  341. $body = $req->getBody();
  342. Client::debug('request body(', strlen($body) . ')');
  343. // header
  344. $cli->applyCookie($req);
  345. foreach (array_merge($cli->getHeader(null), $req->getHeader(null)) as $key => $value) {
  346. $header .= $this->formatHeaderLine($key, $value);
  347. }
  348. Client::debug('request header: ', Client::CRLF, $header);
  349. return $header . Client::CRLF . $body;
  350. }
  351. private function formatHeaderLine($key, $value)
  352. {
  353. if (is_array($value)) {
  354. $line = '';
  355. foreach ($value as $val) {
  356. $line .= $this->formatHeaderLine($key, $val);
  357. }
  358. return $line;
  359. }
  360. if (strpos($key, '-') === false) {
  361. $line = ucfirst($key);
  362. } else {
  363. $parts = explode('-', $key);
  364. $line = ucfirst($parts[0]);
  365. for ($i = 1; $i < count($parts); $i++) {
  366. $line .= '-' . ucfirst($parts[$i]);
  367. }
  368. }
  369. $line .= ': ' . $value . Client::CRLF;
  370. return $line;
  371. }
  372. }