Request.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. <?php
  2. /**
  3. * This file is part of workerman.
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the MIT-LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @author walkor<walkor@workerman.net>
  10. * @copyright walkor<walkor@workerman.net>
  11. * @link http://www.workerman.net/
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. declare(strict_types=1);
  15. namespace Workerman\Protocols\Http;
  16. use Exception;
  17. use RuntimeException;
  18. use Workerman\Connection\TcpConnection;
  19. use Workerman\Protocols\Http;
  20. use function array_walk_recursive;
  21. use function bin2hex;
  22. use function clearstatcache;
  23. use function count;
  24. use function explode;
  25. use function file_put_contents;
  26. use function is_file;
  27. use function json_decode;
  28. use function ltrim;
  29. use function microtime;
  30. use function pack;
  31. use function parse_str;
  32. use function parse_url;
  33. use function preg_match;
  34. use function preg_replace;
  35. use function strlen;
  36. use function strpos;
  37. use function strstr;
  38. use function strtolower;
  39. use function substr;
  40. use function tempnam;
  41. use function trim;
  42. use function unlink;
  43. use function urlencode;
  44. /**
  45. * Class Request
  46. * @package Workerman\Protocols\Http
  47. */
  48. class Request
  49. {
  50. /**
  51. * Connection.
  52. *
  53. * @var ?TcpConnection
  54. */
  55. public ?TcpConnection $connection = null;
  56. /**
  57. * Session instance.
  58. *
  59. * @var ?Session
  60. */
  61. public ?Session $session = null;
  62. /**
  63. * @var int
  64. */
  65. public static int $maxFileUploads = 1024;
  66. /**
  67. * Properties.
  68. *
  69. * @var array
  70. */
  71. public array $properties = [];
  72. /**
  73. * Http buffer.
  74. *
  75. * @var string
  76. */
  77. protected string $buffer;
  78. /**
  79. * Request data.
  80. *
  81. * @var array
  82. */
  83. protected array $data = [];
  84. /**
  85. * Enable cache.
  86. *
  87. * @var bool
  88. */
  89. protected static bool $enableCache = true;
  90. /**
  91. * Request constructor.
  92. *
  93. * @param string $buffer
  94. */
  95. public function __construct(string $buffer)
  96. {
  97. $this->buffer = $buffer;
  98. }
  99. /**
  100. * Get query.
  101. *
  102. * @param string|null $name
  103. * @param mixed|null $default
  104. * @return mixed
  105. */
  106. public function get(string $name = null, mixed $default = null): mixed
  107. {
  108. if (!isset($this->data['get'])) {
  109. $this->parseGet();
  110. }
  111. if (null === $name) {
  112. return $this->data['get'];
  113. }
  114. return $this->data['get'][$name] ?? $default;
  115. }
  116. /**
  117. * Get post.
  118. *
  119. * @param string|null $name
  120. * @param mixed|null $default
  121. * @return mixed
  122. */
  123. public function post(string $name = null, mixed $default = null): mixed
  124. {
  125. if (!isset($this->data['post'])) {
  126. $this->parsePost();
  127. }
  128. if (null === $name) {
  129. return $this->data['post'];
  130. }
  131. return $this->data['post'][$name] ?? $default;
  132. }
  133. /**
  134. * Get header item by name.
  135. *
  136. * @param string|null $name
  137. * @param mixed|null $default
  138. * @return mixed
  139. */
  140. public function header(string $name = null, mixed $default = null): mixed
  141. {
  142. if (!isset($this->data['headers'])) {
  143. $this->parseHeaders();
  144. }
  145. if (null === $name) {
  146. return $this->data['headers'];
  147. }
  148. $name = strtolower($name);
  149. return $this->data['headers'][$name] ?? $default;
  150. }
  151. /**
  152. * Get cookie item by name.
  153. *
  154. * @param string|null $name
  155. * @param mixed|null $default
  156. * @return mixed
  157. */
  158. public function cookie(string $name = null, mixed $default = null): mixed
  159. {
  160. if (!isset($this->data['cookie'])) {
  161. $this->data['cookie'] = [];
  162. parse_str(preg_replace('/; ?/', '&', $this->header('cookie', '')), $this->data['cookie']);
  163. foreach ($this->data['cookie'] as $key => $value) {
  164. if ($key[0] ?? '' === '$') {
  165. unset($this->data['cookie'][$key]);
  166. continue;
  167. }
  168. $this->data['cookie'][$key] = trim($value, '"');
  169. }
  170. }
  171. if ($name === null) {
  172. return $this->data['cookie'];
  173. }
  174. return $this->data['cookie'][$name] ?? $default;
  175. }
  176. /**
  177. * Get upload files.
  178. *
  179. * @param string|null $name
  180. * @return array|null
  181. */
  182. public function file(string $name = null)
  183. {
  184. if (!isset($this->data['files'])) {
  185. $this->parsePost();
  186. }
  187. if (null === $name) {
  188. return $this->data['files'];
  189. }
  190. return $this->data['files'][$name] ?? null;
  191. }
  192. /**
  193. * Get method.
  194. *
  195. * @return string
  196. */
  197. public function method(): string
  198. {
  199. if (!isset($this->data['method'])) {
  200. $this->parseHeadFirstLine();
  201. }
  202. return $this->data['method'];
  203. }
  204. /**
  205. * Get http protocol version.
  206. *
  207. * @return string
  208. */
  209. public function protocolVersion(): string
  210. {
  211. if (!isset($this->data['protocolVersion'])) {
  212. $this->parseProtocolVersion();
  213. }
  214. return $this->data['protocolVersion'];
  215. }
  216. /**
  217. * Get host.
  218. *
  219. * @param bool $withoutPort
  220. * @return string|null
  221. */
  222. public function host(bool $withoutPort = false): ?string
  223. {
  224. $host = $this->header('host');
  225. if ($host && $withoutPort && $pos = strpos($host, ':')) {
  226. return substr($host, 0, $pos);
  227. }
  228. return $host;
  229. }
  230. /**
  231. * Get uri.
  232. *
  233. * @return string
  234. */
  235. public function uri(): string
  236. {
  237. if (!isset($this->data['uri'])) {
  238. $this->parseHeadFirstLine();
  239. }
  240. return $this->data['uri'];
  241. }
  242. /**
  243. * Get path.
  244. *
  245. * @return string
  246. */
  247. public function path(): string
  248. {
  249. if (!isset($this->data['path'])) {
  250. $this->data['path'] = (string)parse_url($this->uri(), PHP_URL_PATH);
  251. }
  252. return $this->data['path'];
  253. }
  254. /**
  255. * Get query string.
  256. *
  257. * @return string
  258. */
  259. public function queryString(): string
  260. {
  261. if (!isset($this->data['query_string'])) {
  262. $this->data['query_string'] = (string)parse_url($this->uri(), PHP_URL_QUERY);
  263. }
  264. return $this->data['query_string'];
  265. }
  266. /**
  267. * Get session.
  268. *
  269. * @return Session
  270. * @throws Exception
  271. */
  272. public function session(): Session
  273. {
  274. if ($this->session === null) {
  275. $this->session = new Session($this->sessionId());
  276. }
  277. return $this->session;
  278. }
  279. /**
  280. * Get/Set session id.
  281. *
  282. * @param null $sessionId
  283. * @return string
  284. * @throws Exception
  285. */
  286. public function sessionId($sessionId = null): string
  287. {
  288. if ($sessionId) {
  289. unset($this->sid);
  290. }
  291. if (!isset($this->sid)) {
  292. $sessionName = Session::$name;
  293. $sid = $sessionId ? '' : $this->cookie($sessionName);
  294. if ($sid === '' || $sid === null) {
  295. if (!$this->connection) {
  296. throw new RuntimeException('Request->session() fail, header already send');
  297. }
  298. $sid = $sessionId ?: static::createSessionId();
  299. $cookieParams = Session::getCookieParams();
  300. $this->setSidCookie($sessionName, $sid, $cookieParams);
  301. }
  302. $this->sid = $sid;
  303. }
  304. return $this->sid;
  305. }
  306. /**
  307. * Session regenerate id.
  308. *
  309. * @param bool $deleteOldSession
  310. * @return string
  311. * @throws Exception
  312. */
  313. public function sessionRegenerateId(bool $deleteOldSession = false): string
  314. {
  315. $session = $this->session();
  316. $sessionData = $session->all();
  317. if ($deleteOldSession) {
  318. $session->flush();
  319. }
  320. $newSid = static::createSessionId();
  321. $session = new Session($newSid);
  322. $session->put($sessionData);
  323. $cookieParams = Session::getCookieParams();
  324. $sessionName = Session::$name;
  325. $this->setSidCookie($sessionName, $newSid, $cookieParams);
  326. return $newSid;
  327. }
  328. /**
  329. * Get http raw head.
  330. *
  331. * @return string
  332. */
  333. public function rawHead(): string
  334. {
  335. if (!isset($this->data['head'])) {
  336. $this->data['head'] = strstr($this->buffer, "\r\n\r\n", true);
  337. }
  338. return $this->data['head'];
  339. }
  340. /**
  341. * Get http raw body.
  342. *
  343. * @return string
  344. */
  345. public function rawBody(): string
  346. {
  347. return substr($this->buffer, strpos($this->buffer, "\r\n\r\n") + 4);
  348. }
  349. /**
  350. * Get raw buffer.
  351. *
  352. * @return string
  353. */
  354. public function rawBuffer(): string
  355. {
  356. return $this->buffer;
  357. }
  358. /**
  359. * Enable or disable cache.
  360. *
  361. * @param bool $value
  362. */
  363. public static function enableCache(bool $value): void
  364. {
  365. static::$enableCache = $value;
  366. }
  367. /**
  368. * Parse first line of http header buffer.
  369. *
  370. * @return void
  371. */
  372. protected function parseHeadFirstLine(): void
  373. {
  374. $firstLine = strstr($this->buffer, "\r\n", true);
  375. $tmp = explode(' ', $firstLine, 3);
  376. $this->data['method'] = $tmp[0];
  377. $this->data['uri'] = $tmp[1] ?? '/';
  378. }
  379. /**
  380. * Parse protocol version.
  381. *
  382. * @return void
  383. */
  384. protected function parseProtocolVersion(): void
  385. {
  386. $firstLine = strstr($this->buffer, "\r\n", true);
  387. $protocolVersion = substr(strstr($firstLine, 'HTTP/'), 5);
  388. $this->data['protocolVersion'] = $protocolVersion ?: '1.0';
  389. }
  390. /**
  391. * Parse headers.
  392. *
  393. * @return void
  394. */
  395. protected function parseHeaders(): void
  396. {
  397. static $cache = [];
  398. $this->data['headers'] = [];
  399. $rawHead = $this->rawHead();
  400. $endLinePosition = strpos($rawHead, "\r\n");
  401. if ($endLinePosition === false) {
  402. return;
  403. }
  404. $headBuffer = substr($rawHead, $endLinePosition + 2);
  405. $cacheable = static::$enableCache && !isset($headBuffer[4096]);
  406. if ($cacheable && isset($cache[$headBuffer])) {
  407. $this->data['headers'] = $cache[$headBuffer];
  408. return;
  409. }
  410. $headData = explode("\r\n", $headBuffer);
  411. foreach ($headData as $content) {
  412. if (str_contains($content, ':')) {
  413. list($key, $value) = explode(':', $content, 2);
  414. $key = strtolower($key);
  415. $value = ltrim($value);
  416. } else {
  417. $key = strtolower($content);
  418. $value = '';
  419. }
  420. if (isset($this->data['headers'][$key])) {
  421. $this->data['headers'][$key] = "{$this->data['headers'][$key]},$value";
  422. } else {
  423. $this->data['headers'][$key] = $value;
  424. }
  425. }
  426. if ($cacheable) {
  427. $cache[$headBuffer] = $this->data['headers'];
  428. if (count($cache) > 128) {
  429. unset($cache[key($cache)]);
  430. }
  431. }
  432. }
  433. /**
  434. * Parse head.
  435. *
  436. * @return void
  437. */
  438. protected function parseGet(): void
  439. {
  440. static $cache = [];
  441. $queryString = $this->queryString();
  442. $this->data['get'] = [];
  443. if ($queryString === '') {
  444. return;
  445. }
  446. $cacheable = static::$enableCache && !isset($queryString[1024]);
  447. if ($cacheable && isset($cache[$queryString])) {
  448. $this->data['get'] = $cache[$queryString];
  449. return;
  450. }
  451. parse_str($queryString, $this->data['get']);
  452. if ($cacheable) {
  453. $cache[$queryString] = $this->data['get'];
  454. if (count($cache) > 256) {
  455. unset($cache[key($cache)]);
  456. }
  457. }
  458. }
  459. /**
  460. * Parse post.
  461. *
  462. * @return void
  463. */
  464. protected function parsePost(): void
  465. {
  466. static $cache = [];
  467. $this->data['post'] = $this->data['files'] = [];
  468. $contentType = $this->header('content-type', '');
  469. if (preg_match('/boundary="?(\S+)"?/', $contentType, $match)) {
  470. $httpPostBoundary = '--' . $match[1];
  471. $this->parseUploadFiles($httpPostBoundary);
  472. return;
  473. }
  474. $bodyBuffer = $this->rawBody();
  475. if ($bodyBuffer === '') {
  476. return;
  477. }
  478. $cacheable = static::$enableCache && !isset($bodyBuffer[1024]);
  479. if ($cacheable && isset($cache[$bodyBuffer])) {
  480. $this->data['post'] = $cache[$bodyBuffer];
  481. return;
  482. }
  483. if (preg_match('/\bjson\b/i', $contentType)) {
  484. $this->data['post'] = (array)json_decode($bodyBuffer, true);
  485. } else {
  486. parse_str($bodyBuffer, $this->data['post']);
  487. }
  488. if ($cacheable) {
  489. $cache[$bodyBuffer] = $this->data['post'];
  490. if (count($cache) > 256) {
  491. unset($cache[key($cache)]);
  492. }
  493. }
  494. }
  495. /**
  496. * Parse upload files.
  497. *
  498. * @param string $httpPostBoundary
  499. * @return void
  500. */
  501. protected function parseUploadFiles(string $httpPostBoundary): void
  502. {
  503. $httpPostBoundary = trim($httpPostBoundary, '"');
  504. $buffer = $this->buffer;
  505. $postEncodeString = '';
  506. $filesEncodeString = '';
  507. $files = [];
  508. $bodyPosition = strpos($buffer, "\r\n\r\n") + 4;
  509. $offset = $bodyPosition + strlen($httpPostBoundary) + 2;
  510. $maxCount = static::$maxFileUploads;
  511. while ($maxCount-- > 0 && $offset) {
  512. $offset = $this->parseUploadFile($httpPostBoundary, $offset, $postEncodeString, $filesEncodeString, $files);
  513. }
  514. if ($postEncodeString) {
  515. parse_str($postEncodeString, $this->data['post']);
  516. }
  517. if ($filesEncodeString) {
  518. parse_str($filesEncodeString, $this->data['files']);
  519. array_walk_recursive($this->data['files'], function (&$value) use ($files) {
  520. $value = $files[$value];
  521. });
  522. }
  523. }
  524. /**
  525. * Parse upload file.
  526. *
  527. * @param $boundary
  528. * @param $sectionStartOffset
  529. * @param $postEncodeString
  530. * @param $filesEncodeStr
  531. * @param $files
  532. * @return int
  533. */
  534. protected function parseUploadFile($boundary, $sectionStartOffset, &$postEncodeString, &$filesEncodeStr, &$files): int
  535. {
  536. $file = [];
  537. $boundary = "\r\n$boundary";
  538. if (strlen($this->buffer) < $sectionStartOffset) {
  539. return 0;
  540. }
  541. $sectionEndOffset = strpos($this->buffer, $boundary, $sectionStartOffset);
  542. if (!$sectionEndOffset) {
  543. return 0;
  544. }
  545. $contentLinesEndOffset = strpos($this->buffer, "\r\n\r\n", $sectionStartOffset);
  546. if (!$contentLinesEndOffset || $contentLinesEndOffset + 4 > $sectionEndOffset) {
  547. return 0;
  548. }
  549. $contentLinesStr = substr($this->buffer, $sectionStartOffset, $contentLinesEndOffset - $sectionStartOffset);
  550. $contentLines = explode("\r\n", trim($contentLinesStr . "\r\n"));
  551. $boundaryValue = substr($this->buffer, $contentLinesEndOffset + 4, $sectionEndOffset - $contentLinesEndOffset - 4);
  552. $uploadKey = false;
  553. foreach ($contentLines as $contentLine) {
  554. if (!strpos($contentLine, ': ')) {
  555. return 0;
  556. }
  557. list($key, $value) = explode(': ', $contentLine);
  558. switch (strtolower($key)) {
  559. case "content-disposition":
  560. // Is file data.
  561. if (preg_match('/name="(.*?)"; filename="(.*?)"/i', $value, $match)) {
  562. $error = 0;
  563. $tmpFile = '';
  564. $fileName = $match[1];
  565. $size = strlen($boundaryValue);
  566. $tmpUploadDir = HTTP::uploadTmpDir();
  567. if (!$tmpUploadDir) {
  568. $error = UPLOAD_ERR_NO_TMP_DIR;
  569. } else if ($boundaryValue === '' && $fileName === '') {
  570. $error = UPLOAD_ERR_NO_FILE;
  571. } else {
  572. $tmpFile = tempnam($tmpUploadDir, 'workerman.upload.');
  573. if ($tmpFile === false || false === file_put_contents($tmpFile, $boundaryValue)) {
  574. $error = UPLOAD_ERR_CANT_WRITE;
  575. }
  576. }
  577. $uploadKey = $fileName;
  578. // Parse upload files.
  579. $file = array_merge($file, [
  580. 'name' => $match[2],
  581. 'tmp_name' => $tmpFile,
  582. 'size' => $size,
  583. 'error' => $error
  584. ]);
  585. if (!isset($file['type'])) $file['type'] = '';
  586. break;
  587. } // Is post field.
  588. else {
  589. // Parse $POST.
  590. if (preg_match('/name="(.*?)"$/', $value, $match)) {
  591. $k = $match[1];
  592. $postEncodeString .= urlencode($k) . "=" . urlencode($boundaryValue) . '&';
  593. }
  594. return $sectionEndOffset + strlen($boundary) + 2;
  595. }
  596. case "content-type":
  597. $file['type'] = trim($value);
  598. break;
  599. }
  600. }
  601. if ($uploadKey === false) {
  602. return 0;
  603. }
  604. $filesEncodeStr .= urlencode($uploadKey) . '=' . count($files) . '&';
  605. $files[] = $file;
  606. return $sectionEndOffset + strlen($boundary) + 2;
  607. }
  608. /**
  609. * Create session id.
  610. *
  611. * @return string
  612. * @throws Exception
  613. */
  614. public static function createSessionId(): string
  615. {
  616. return bin2hex(pack('d', microtime(true)) . random_bytes(8));
  617. }
  618. /**
  619. * @param string $sessionName
  620. * @param string $sid
  621. * @param array $cookieParams
  622. * @return void
  623. */
  624. protected function setSidCookie(string $sessionName, string $sid, array $cookieParams): void
  625. {
  626. if (!$this->connection) {
  627. throw new RuntimeException('Request->setSidCookie() fail, header already send');
  628. }
  629. $this->connection->headers['Set-Cookie'] = [$sessionName . '=' . $sid
  630. . (empty($cookieParams['domain']) ? '' : '; Domain=' . $cookieParams['domain'])
  631. . (empty($cookieParams['lifetime']) ? '' : '; Max-Age=' . $cookieParams['lifetime'])
  632. . (empty($cookieParams['path']) ? '' : '; Path=' . $cookieParams['path'])
  633. . (empty($cookieParams['samesite']) ? '' : '; SameSite=' . $cookieParams['samesite'])
  634. . (!$cookieParams['secure'] ? '' : '; Secure')
  635. . (!$cookieParams['httponly'] ? '' : '; HttpOnly')];
  636. }
  637. /**
  638. * __toString.
  639. */
  640. public function __toString()
  641. {
  642. return $this->buffer;
  643. }
  644. /**
  645. * Setter.
  646. *
  647. * @param string $name
  648. * @param mixed $value
  649. * @return void
  650. */
  651. public function __set(string $name, mixed $value)
  652. {
  653. $this->properties[$name] = $value;
  654. }
  655. /**
  656. * Getter.
  657. *
  658. * @param string $name
  659. * @return mixed|null
  660. */
  661. public function __get(string $name)
  662. {
  663. return $this->properties[$name] ?? null;
  664. }
  665. /**
  666. * Isset.
  667. *
  668. * @param string $name
  669. * @return bool
  670. */
  671. public function __isset(string $name)
  672. {
  673. return isset($this->properties[$name]);
  674. }
  675. /**
  676. * Unset.
  677. *
  678. * @param string $name
  679. * @return void
  680. */
  681. public function __unset(string $name)
  682. {
  683. unset($this->properties[$name]);
  684. }
  685. /**
  686. * __destruct.
  687. *
  688. * @return void
  689. */
  690. public function __destruct()
  691. {
  692. if (isset($this->data['files'])) {
  693. clearstatcache();
  694. array_walk_recursive($this->data['files'], function ($value, $key) {
  695. if ($key === 'tmp_name') {
  696. if (is_file($value)) {
  697. unlink($value);
  698. }
  699. }
  700. });
  701. }
  702. }
  703. }