Request.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: yf
  5. * Date: 2018/5/24
  6. * Time: 下午3:40
  7. */
  8. namespace EasySwoole\Http;
  9. use EasySwoole\Http\Message\ServerRequest;
  10. use EasySwoole\Http\Message\Stream;
  11. use EasySwoole\Http\Message\UploadFile;
  12. use EasySwoole\Http\Message\Uri;
  13. class Request extends ServerRequest
  14. {
  15. private $request;
  16. function __construct(\Swoole\Http\Request $request = null)
  17. {
  18. if($request){
  19. $this->request = $request;
  20. $this->initHeaders();
  21. $protocol = str_replace('HTTP/', '', $request->server['server_protocol']) ;
  22. //为单元测试准备
  23. if($request->fd){
  24. $body = new Stream($request->rawContent());
  25. }else{
  26. $body = new Stream('');
  27. }
  28. $uri = $this->initUri();
  29. $files = $this->initFiles();
  30. $method = $request->server['request_method'];
  31. parent::__construct($method, $uri, null, $body, $protocol, $request->server);
  32. $this->withCookieParams($this->initCookie())->withQueryParams($this->initGet())->withParsedBody($this->initPost())->withUploadedFiles($files);
  33. }
  34. }
  35. function getRequestParam(...$key)
  36. {
  37. $data = $this->getParsedBody() + $this->getQueryParams();
  38. if(empty($key)){
  39. return $data;
  40. }else{
  41. $res = [];
  42. foreach ($key as $item){
  43. $res[$item] = isset($data[$item])? $data[$item] : null;
  44. }
  45. if(count($key) == 1){
  46. return array_shift($res);
  47. }else{
  48. return $res;
  49. }
  50. }
  51. }
  52. function getSwooleRequest()
  53. {
  54. return $this->request;
  55. }
  56. private function initUri()
  57. {
  58. $uri = new Uri();
  59. $uri->withScheme("http");
  60. $uri->withPath($this->request->server['path_info']);
  61. $query = isset($this->request->server['query_string']) ? $this->request->server['query_string'] : '';
  62. $uri->withQuery($query);
  63. //host与port以header为准,防止经过proxy
  64. if(isset($this->request->header['host'])){
  65. $host = $this->request->header['host'];
  66. $host = explode(":",$host);
  67. $realHost = $host[0];
  68. $port = isset($host[1]) ? $host[1] : null;
  69. }else{
  70. $realHost = '127.0.0.1';
  71. $port = $this->request->server['server_port'];
  72. }
  73. $uri->withHost($realHost);
  74. $uri->withPort($port);
  75. return $uri;
  76. }
  77. private function initHeaders()
  78. {
  79. $headers = isset($this->request->header) ? $this->request->header :[];
  80. foreach ($headers as $header => $val){
  81. $this->withAddedHeader($header,$val);
  82. }
  83. }
  84. private function initFiles()
  85. {
  86. if(isset($this->request->files)){
  87. $normalized = array();
  88. foreach($this->request->files as $key => $value){
  89. //如果是二维数组文件
  90. if(is_array($value) && empty($value['tmp_name'])){
  91. $normalized[$key] = [];
  92. foreach($value as $file){
  93. if (empty($file['tmp_name'])){
  94. continue;
  95. }
  96. $file = $this->initFile($file);
  97. if($file){
  98. $normalized[$key][] = $file;
  99. }
  100. }
  101. continue;
  102. }else{
  103. $file = $this->initFile($value);
  104. if($file){
  105. $normalized[$key] = $file;
  106. }
  107. }
  108. }
  109. return $normalized;
  110. }else{
  111. return array();
  112. }
  113. }
  114. private function initFile(array $file)
  115. {
  116. if(empty($file['tmp_name'])){
  117. return null;
  118. }
  119. return new UploadFile(
  120. $file['tmp_name'],
  121. (int) $file['size'],
  122. (int) $file['error'],
  123. $file['name'],
  124. $file['type']
  125. );
  126. }
  127. private function initCookie()
  128. {
  129. return isset($this->request->cookie) ? $this->request->cookie : [];
  130. }
  131. private function initPost()
  132. {
  133. return isset($this->request->post) ? $this->request->post : [];
  134. }
  135. private function initGet()
  136. {
  137. return isset($this->request->get) ? $this->request->get : [];
  138. }
  139. final public function __toString():string
  140. {
  141. return Utility::toString($this);
  142. }
  143. public function __destruct()
  144. {
  145. $this->getBody()->close();
  146. $this->request = null;
  147. }
  148. }