Request.php 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation;
  11. use Symfony\Component\HttpFoundation\Exception\BadRequestException;
  12. use Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException;
  13. use Symfony\Component\HttpFoundation\Exception\JsonException;
  14. use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
  15. use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
  16. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  17. // Help opcache.preload discover always-needed symbols
  18. class_exists(AcceptHeader::class);
  19. class_exists(FileBag::class);
  20. class_exists(HeaderBag::class);
  21. class_exists(HeaderUtils::class);
  22. class_exists(InputBag::class);
  23. class_exists(ParameterBag::class);
  24. class_exists(ServerBag::class);
  25. /**
  26. * Request represents an HTTP request.
  27. *
  28. * The methods dealing with URL accept / return a raw path (% encoded):
  29. * * getBasePath
  30. * * getBaseUrl
  31. * * getPathInfo
  32. * * getRequestUri
  33. * * getUri
  34. * * getUriForPath
  35. *
  36. * @author Fabien Potencier <fabien@symfony.com>
  37. */
  38. class Request
  39. {
  40. public const HEADER_FORWARDED = 0b000001; // When using RFC 7239
  41. public const HEADER_X_FORWARDED_FOR = 0b000010;
  42. public const HEADER_X_FORWARDED_HOST = 0b000100;
  43. public const HEADER_X_FORWARDED_PROTO = 0b001000;
  44. public const HEADER_X_FORWARDED_PORT = 0b010000;
  45. public const HEADER_X_FORWARDED_PREFIX = 0b100000;
  46. public const HEADER_X_FORWARDED_AWS_ELB = 0b0011010; // AWS ELB doesn't send X-Forwarded-Host
  47. public const HEADER_X_FORWARDED_TRAEFIK = 0b0111110; // All "X-Forwarded-*" headers sent by Traefik reverse proxy
  48. public const METHOD_HEAD = 'HEAD';
  49. public const METHOD_GET = 'GET';
  50. public const METHOD_POST = 'POST';
  51. public const METHOD_PUT = 'PUT';
  52. public const METHOD_PATCH = 'PATCH';
  53. public const METHOD_DELETE = 'DELETE';
  54. public const METHOD_PURGE = 'PURGE';
  55. public const METHOD_OPTIONS = 'OPTIONS';
  56. public const METHOD_TRACE = 'TRACE';
  57. public const METHOD_CONNECT = 'CONNECT';
  58. public const METHOD_QUERY = 'QUERY';
  59. /**
  60. * @var string[]
  61. */
  62. protected static array $trustedProxies = [];
  63. /**
  64. * @var string[]
  65. */
  66. protected static array $trustedHostPatterns = [];
  67. /**
  68. * @var string[]
  69. */
  70. protected static array $trustedHosts = [];
  71. protected static bool $httpMethodParameterOverride = false;
  72. /**
  73. * The HTTP methods that can be overridden.
  74. *
  75. * @var uppercase-string[]|null
  76. */
  77. protected static ?array $allowedHttpMethodOverride = null;
  78. /**
  79. * Custom parameters.
  80. */
  81. public ParameterBag $attributes;
  82. /**
  83. * Request body parameters ($_POST).
  84. *
  85. * @see getPayload() for portability between content types
  86. */
  87. public InputBag $request;
  88. /**
  89. * Query string parameters ($_GET).
  90. *
  91. * @var InputBag<string>
  92. */
  93. public InputBag $query;
  94. /**
  95. * Server and execution environment parameters ($_SERVER).
  96. */
  97. public ServerBag $server;
  98. /**
  99. * Uploaded files ($_FILES).
  100. */
  101. public FileBag $files;
  102. /**
  103. * Cookies ($_COOKIE).
  104. *
  105. * @var InputBag<string>
  106. */
  107. public InputBag $cookies;
  108. /**
  109. * Headers (taken from the $_SERVER).
  110. */
  111. public HeaderBag $headers;
  112. /**
  113. * @var string|resource|false|null
  114. */
  115. protected $content;
  116. /**
  117. * @var string[]|null
  118. */
  119. protected ?array $languages = null;
  120. /**
  121. * @var string[]|null
  122. */
  123. protected ?array $charsets = null;
  124. /**
  125. * @var string[]|null
  126. */
  127. protected ?array $encodings = null;
  128. /**
  129. * @var string[]|null
  130. */
  131. protected ?array $acceptableContentTypes = null;
  132. protected ?string $pathInfo = null;
  133. protected ?string $requestUri = null;
  134. protected ?string $baseUrl = null;
  135. protected ?string $basePath = null;
  136. protected ?string $method = null;
  137. protected ?string $format = null;
  138. protected SessionInterface|\Closure|null $session = null;
  139. protected ?string $locale = null;
  140. protected string $defaultLocale = 'en';
  141. /**
  142. * @var array<string, string[]>|null
  143. */
  144. protected static ?array $formats = null;
  145. protected static ?\Closure $requestFactory = null;
  146. private ?string $preferredFormat = null;
  147. private bool $isHostValid = true;
  148. private bool $isForwardedValid = true;
  149. private bool $isSafeContentPreferred;
  150. private array $trustedValuesCache = [];
  151. private static int $trustedHeaderSet = -1;
  152. private const FORWARDED_PARAMS = [
  153. self::HEADER_X_FORWARDED_FOR => 'for',
  154. self::HEADER_X_FORWARDED_HOST => 'host',
  155. self::HEADER_X_FORWARDED_PROTO => 'proto',
  156. self::HEADER_X_FORWARDED_PORT => 'host',
  157. ];
  158. /**
  159. * Names for headers that can be trusted when
  160. * using trusted proxies.
  161. *
  162. * The FORWARDED header is the standard as of rfc7239.
  163. *
  164. * The other headers are non-standard, but widely used
  165. * by popular reverse proxies (like Apache mod_proxy or Amazon EC2).
  166. */
  167. private const TRUSTED_HEADERS = [
  168. self::HEADER_FORWARDED => 'FORWARDED',
  169. self::HEADER_X_FORWARDED_FOR => 'X_FORWARDED_FOR',
  170. self::HEADER_X_FORWARDED_HOST => 'X_FORWARDED_HOST',
  171. self::HEADER_X_FORWARDED_PROTO => 'X_FORWARDED_PROTO',
  172. self::HEADER_X_FORWARDED_PORT => 'X_FORWARDED_PORT',
  173. self::HEADER_X_FORWARDED_PREFIX => 'X_FORWARDED_PREFIX',
  174. ];
  175. /**
  176. * This mapping is used when no exact MIME match is found in $formats.
  177. *
  178. * It enables mappings like application/soap+xml -> xml.
  179. *
  180. * @see https://datatracker.ietf.org/doc/html/rfc6839
  181. * @see https://datatracker.ietf.org/doc/html/rfc7303
  182. * @see https://www.iana.org/assignments/media-types/media-types.xhtml
  183. */
  184. private const STRUCTURED_SUFFIX_FORMATS = [
  185. 'json' => 'json',
  186. 'xml' => 'xml',
  187. 'xhtml' => 'html',
  188. 'cbor' => 'cbor',
  189. 'zip' => 'zip',
  190. 'ber' => 'asn1',
  191. 'der' => 'asn1',
  192. 'tlv' => 'tlv',
  193. 'wbxml' => 'xml',
  194. 'yaml' => 'yaml',
  195. ];
  196. private bool $isIisRewrite = false;
  197. /**
  198. * @param array $query The GET parameters
  199. * @param array $request The POST parameters
  200. * @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
  201. * @param array $cookies The COOKIE parameters
  202. * @param array $files The FILES parameters
  203. * @param array $server The SERVER parameters
  204. * @param string|resource|null $content The raw body data
  205. */
  206. public function __construct(array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = null)
  207. {
  208. $this->initialize($query, $request, $attributes, $cookies, $files, $server, $content);
  209. }
  210. /**
  211. * Sets the parameters for this request.
  212. *
  213. * This method also re-initializes all properties.
  214. *
  215. * @param array $query The GET parameters
  216. * @param array $request The POST parameters
  217. * @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
  218. * @param array $cookies The COOKIE parameters
  219. * @param array $files The FILES parameters
  220. * @param array $server The SERVER parameters
  221. * @param string|resource|null $content The raw body data
  222. */
  223. public function initialize(array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = null): void
  224. {
  225. $this->request = new InputBag($request);
  226. $this->query = new InputBag($query);
  227. $this->attributes = new ParameterBag($attributes);
  228. $this->cookies = new InputBag($cookies);
  229. $this->files = new FileBag($files);
  230. $this->server = new ServerBag($server);
  231. $this->headers = new HeaderBag($this->server->getHeaders());
  232. $this->content = $content;
  233. $this->languages = null;
  234. $this->charsets = null;
  235. $this->encodings = null;
  236. $this->acceptableContentTypes = null;
  237. $this->pathInfo = null;
  238. $this->requestUri = null;
  239. $this->baseUrl = null;
  240. $this->basePath = null;
  241. $this->method = null;
  242. $this->format = null;
  243. }
  244. /**
  245. * Creates a new request with values from PHP's super globals.
  246. */
  247. public static function createFromGlobals(): static
  248. {
  249. if (!\in_array($_SERVER['REQUEST_METHOD'] ?? null, ['PUT', 'DELETE', 'PATCH', 'QUERY'], true)) {
  250. return self::createRequestFromFactory($_GET, $_POST, [], $_COOKIE, $_FILES, $_SERVER);
  251. }
  252. try {
  253. [$post, $files] = request_parse_body();
  254. } catch (\RequestParseBodyException) {
  255. $post = $_POST;
  256. $files = $_FILES;
  257. }
  258. return self::createRequestFromFactory($_GET, $post, [], $_COOKIE, $files, $_SERVER);
  259. }
  260. /**
  261. * Creates a Request based on a given URI and configuration.
  262. *
  263. * The information contained in the URI always take precedence
  264. * over the other information (server and parameters).
  265. *
  266. * @param string $uri The URI
  267. * @param string $method The HTTP method
  268. * @param array $parameters The query (GET) or request (POST) parameters
  269. * @param array $cookies The request cookies ($_COOKIE)
  270. * @param array $files The request files ($_FILES)
  271. * @param array $server The server parameters ($_SERVER)
  272. * @param string|resource|null $content The raw body data
  273. *
  274. * @throws BadRequestException When the URI is invalid
  275. */
  276. public static function create(string $uri, string $method = 'GET', array $parameters = [], array $cookies = [], array $files = [], array $server = [], $content = null): static
  277. {
  278. $server = array_replace([
  279. 'SERVER_NAME' => 'localhost',
  280. 'SERVER_PORT' => 80,
  281. 'HTTP_HOST' => 'localhost',
  282. 'HTTP_USER_AGENT' => 'Symfony',
  283. 'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  284. 'HTTP_ACCEPT_LANGUAGE' => 'en-us,en;q=0.5',
  285. 'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
  286. 'REMOTE_ADDR' => '127.0.0.1',
  287. 'SCRIPT_NAME' => '',
  288. 'SCRIPT_FILENAME' => '',
  289. 'SERVER_PROTOCOL' => 'HTTP/1.1',
  290. 'REQUEST_TIME' => time(),
  291. 'REQUEST_TIME_FLOAT' => microtime(true),
  292. ], $server);
  293. $server['PATH_INFO'] = '';
  294. $server['REQUEST_METHOD'] = strtoupper($method);
  295. if (($i = strcspn($uri, ':/?#')) && ':' === ($uri[$i] ?? null) && (strspn($uri, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-.') !== $i || strcspn($uri, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'))) {
  296. throw new BadRequestException('Invalid URI: Scheme is malformed.');
  297. }
  298. if (false === $components = parse_url(\strlen($uri) !== strcspn($uri, '?#') ? $uri : $uri.'#')) {
  299. throw new BadRequestException('Invalid URI.');
  300. }
  301. $part = ($components['user'] ?? '').':'.($components['pass'] ?? '');
  302. if (':' !== $part && \strlen($part) !== strcspn($part, '[]')) {
  303. throw new BadRequestException('Invalid URI: Userinfo is malformed.');
  304. }
  305. if (($part = $components['host'] ?? '') && !self::isHostValid($part)) {
  306. throw new BadRequestException('Invalid URI: Host is malformed.');
  307. }
  308. if (false !== ($i = strpos($uri, '\\')) && $i < strcspn($uri, '?#')) {
  309. throw new BadRequestException('Invalid URI: A URI cannot contain a backslash.');
  310. }
  311. if (\strlen($uri) !== strcspn($uri, "\r\n\t")) {
  312. throw new BadRequestException('Invalid URI: A URI cannot contain CR/LF/TAB characters.');
  313. }
  314. if ('' !== $uri && (\ord($uri[0]) <= 32 || \ord($uri[-1]) <= 32)) {
  315. throw new BadRequestException('Invalid URI: A URI must not start nor end with ASCII control characters or spaces.');
  316. }
  317. if (isset($components['host'])) {
  318. $server['SERVER_NAME'] = $components['host'];
  319. $server['HTTP_HOST'] = $components['host'];
  320. }
  321. if (isset($components['scheme'])) {
  322. if ('https' === $components['scheme']) {
  323. $server['HTTPS'] = 'on';
  324. $server['SERVER_PORT'] = 443;
  325. } else {
  326. unset($server['HTTPS']);
  327. $server['SERVER_PORT'] = 80;
  328. }
  329. }
  330. if (isset($components['port'])) {
  331. $server['SERVER_PORT'] = $components['port'];
  332. $server['HTTP_HOST'] .= ':'.$components['port'];
  333. }
  334. if (isset($components['user'])) {
  335. $server['PHP_AUTH_USER'] = $components['user'];
  336. }
  337. if (isset($components['pass'])) {
  338. $server['PHP_AUTH_PW'] = $components['pass'];
  339. }
  340. if ('' === $path = $components['path'] ?? '') {
  341. $components['path'] = '/';
  342. } elseif (!isset($components['scheme']) && !isset($components['host']) && '/' !== $path[0]) {
  343. if (false !== $pos = strpos($path, '/')) {
  344. $path = substr($path, 0, $pos);
  345. }
  346. if (str_contains($path, ':')) {
  347. throw new BadRequestException('Invalid URI: Path is malformed.');
  348. }
  349. }
  350. switch (strtoupper($method)) {
  351. case 'POST':
  352. case 'PUT':
  353. case 'DELETE':
  354. case 'QUERY':
  355. if (!isset($server['CONTENT_TYPE'])) {
  356. $server['CONTENT_TYPE'] = 'application/x-www-form-urlencoded';
  357. }
  358. // no break
  359. case 'PATCH':
  360. $request = $parameters;
  361. $query = [];
  362. break;
  363. default:
  364. $request = [];
  365. $query = $parameters;
  366. break;
  367. }
  368. $queryString = '';
  369. if (isset($components['query'])) {
  370. parse_str(html_entity_decode($components['query']), $qs);
  371. if ($query) {
  372. $query = array_replace($qs, $query);
  373. $queryString = http_build_query($query, '', '&');
  374. } else {
  375. $query = $qs;
  376. $queryString = $components['query'];
  377. }
  378. } elseif ($query) {
  379. $queryString = http_build_query($query, '', '&');
  380. }
  381. $server['REQUEST_URI'] = $components['path'].('' !== $queryString ? '?'.$queryString : '');
  382. $server['QUERY_STRING'] = $queryString;
  383. return self::createRequestFromFactory($query, $request, [], $cookies, $files, $server, $content);
  384. }
  385. /**
  386. * Sets a callable able to create a Request instance.
  387. *
  388. * This is mainly useful when you need to override the Request class
  389. * to keep BC with an existing system. It should not be used for any
  390. * other purpose.
  391. */
  392. public static function setFactory(?callable $callable): void
  393. {
  394. self::$requestFactory = null === $callable ? null : $callable(...);
  395. }
  396. /**
  397. * Clones a request and overrides some of its parameters.
  398. *
  399. * @param array|null $query The GET parameters
  400. * @param array|null $request The POST parameters
  401. * @param array|null $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
  402. * @param array|null $cookies The COOKIE parameters
  403. * @param array|null $files The FILES parameters
  404. * @param array|null $server The SERVER parameters
  405. */
  406. public function duplicate(?array $query = null, ?array $request = null, ?array $attributes = null, ?array $cookies = null, ?array $files = null, ?array $server = null): static
  407. {
  408. $dup = clone $this;
  409. if (null !== $query) {
  410. $dup->query = new InputBag($query);
  411. }
  412. if (null !== $request) {
  413. $dup->request = new InputBag($request);
  414. }
  415. if (null !== $attributes) {
  416. $dup->attributes = new ParameterBag($attributes);
  417. }
  418. if (null !== $cookies) {
  419. $dup->cookies = new InputBag($cookies);
  420. }
  421. if (null !== $files) {
  422. $dup->files = new FileBag($files);
  423. }
  424. if (null !== $server) {
  425. $dup->server = new ServerBag($server);
  426. $dup->headers = new HeaderBag($dup->server->getHeaders());
  427. }
  428. $dup->languages = null;
  429. $dup->charsets = null;
  430. $dup->encodings = null;
  431. $dup->acceptableContentTypes = null;
  432. $dup->pathInfo = null;
  433. $dup->requestUri = null;
  434. $dup->baseUrl = null;
  435. $dup->basePath = null;
  436. $dup->method = null;
  437. $dup->format = null;
  438. if (!$dup->attributes->has('_format') && $this->attributes->has('_format')) {
  439. $dup->attributes->set('_format', $this->attributes->get('_format'));
  440. }
  441. if (!$dup->getRequestFormat(null)) {
  442. $dup->setRequestFormat($this->getRequestFormat(null));
  443. }
  444. return $dup;
  445. }
  446. /**
  447. * Clones the current request.
  448. *
  449. * Note that the session is not cloned as duplicated requests
  450. * are most of the time sub-requests of the main one.
  451. */
  452. public function __clone()
  453. {
  454. $this->query = clone $this->query;
  455. $this->request = clone $this->request;
  456. $this->attributes = clone $this->attributes;
  457. $this->cookies = clone $this->cookies;
  458. $this->files = clone $this->files;
  459. $this->server = clone $this->server;
  460. $this->headers = clone $this->headers;
  461. }
  462. public function __toString(): string
  463. {
  464. $content = $this->getContent();
  465. $cookieHeader = '';
  466. $cookies = [];
  467. foreach ($this->cookies as $k => $v) {
  468. $cookies[] = \is_array($v) ? http_build_query([$k => $v], '', '; ', \PHP_QUERY_RFC3986) : "$k=$v";
  469. }
  470. if ($cookies) {
  471. $cookieHeader = 'Cookie: '.implode('; ', $cookies)."\r\n";
  472. }
  473. return
  474. \sprintf('%s %s %s', $this->getMethod(), $this->getRequestUri(), $this->server->get('SERVER_PROTOCOL'))."\r\n".
  475. $this->headers.
  476. $cookieHeader."\r\n".
  477. $content;
  478. }
  479. /**
  480. * Overrides the PHP global variables according to this request instance.
  481. *
  482. * It overrides $_GET, $_POST, $_REQUEST, $_SERVER, $_COOKIE.
  483. * $_FILES is never overridden, see rfc1867
  484. */
  485. public function overrideGlobals(): void
  486. {
  487. $this->server->set('QUERY_STRING', static::normalizeQueryString(http_build_query($this->query->all(), '', '&')));
  488. $_GET = $this->query->all();
  489. $_POST = $this->request->all();
  490. $_SERVER = $this->server->all();
  491. $_COOKIE = $this->cookies->all();
  492. foreach ($this->headers->all() as $key => $value) {
  493. $key = strtoupper(str_replace('-', '_', $key));
  494. if (\in_array($key, ['CONTENT_TYPE', 'CONTENT_LENGTH', 'CONTENT_MD5'], true)) {
  495. $_SERVER[$key] = implode(', ', $value);
  496. } else {
  497. $_SERVER['HTTP_'.$key] = implode(', ', $value);
  498. }
  499. }
  500. $request = ['g' => $_GET, 'p' => $_POST, 'c' => $_COOKIE];
  501. $requestOrder = \ini_get('request_order') ?: \ini_get('variables_order');
  502. $requestOrder = preg_replace('#[^cgp]#', '', strtolower($requestOrder)) ?: 'gp';
  503. $_REQUEST = [[]];
  504. foreach (str_split($requestOrder) as $order) {
  505. $_REQUEST[] = $request[$order];
  506. }
  507. $_REQUEST = array_merge(...$_REQUEST);
  508. }
  509. /**
  510. * Sets a list of trusted proxies.
  511. *
  512. * You should only list the reverse proxies that you manage directly.
  513. *
  514. * @param array $proxies A list of trusted proxies, the string 'REMOTE_ADDR' will be replaced with $_SERVER['REMOTE_ADDR'] and 'PRIVATE_SUBNETS' by IpUtils::PRIVATE_SUBNETS
  515. * @param int-mask-of<Request::HEADER_*> $trustedHeaderSet A bit field to set which headers to trust from your proxies
  516. */
  517. public static function setTrustedProxies(array $proxies, int $trustedHeaderSet): void
  518. {
  519. if (false !== $i = array_search('REMOTE_ADDR', $proxies, true)) {
  520. if (isset($_SERVER['REMOTE_ADDR'])) {
  521. $proxies[$i] = $_SERVER['REMOTE_ADDR'];
  522. } else {
  523. unset($proxies[$i]);
  524. $proxies = array_values($proxies);
  525. }
  526. }
  527. if (false !== ($i = array_search('PRIVATE_SUBNETS', $proxies, true)) || false !== ($i = array_search('private_ranges', $proxies, true))) {
  528. unset($proxies[$i]);
  529. $proxies = array_merge($proxies, IpUtils::PRIVATE_SUBNETS);
  530. }
  531. self::$trustedProxies = $proxies;
  532. self::$trustedHeaderSet = $trustedHeaderSet;
  533. }
  534. /**
  535. * Gets the list of trusted proxies.
  536. *
  537. * @return string[]
  538. */
  539. public static function getTrustedProxies(): array
  540. {
  541. return self::$trustedProxies;
  542. }
  543. /**
  544. * Gets the set of trusted headers from trusted proxies.
  545. *
  546. * @return int A bit field of Request::HEADER_* that defines which headers are trusted from your proxies
  547. */
  548. public static function getTrustedHeaderSet(): int
  549. {
  550. return self::$trustedHeaderSet;
  551. }
  552. /**
  553. * Sets a list of trusted host patterns.
  554. *
  555. * You should only list the hosts you manage using regexs.
  556. *
  557. * @param array $hostPatterns A list of trusted host patterns
  558. */
  559. public static function setTrustedHosts(array $hostPatterns): void
  560. {
  561. self::$trustedHostPatterns = array_map(fn ($hostPattern) => \sprintf('{%s}i', $hostPattern), $hostPatterns);
  562. // we need to reset trusted hosts on trusted host patterns change
  563. self::$trustedHosts = [];
  564. }
  565. /**
  566. * Gets the list of trusted host patterns.
  567. *
  568. * @return string[]
  569. */
  570. public static function getTrustedHosts(): array
  571. {
  572. return self::$trustedHostPatterns;
  573. }
  574. /**
  575. * Normalizes a query string.
  576. *
  577. * It builds a normalized query string, where keys/value pairs are alphabetized,
  578. * have consistent escaping and unneeded delimiters are removed.
  579. */
  580. public static function normalizeQueryString(?string $qs): string
  581. {
  582. if ('' === ($qs ?? '')) {
  583. return '';
  584. }
  585. $qs = HeaderUtils::parseQuery($qs);
  586. ksort($qs);
  587. return http_build_query($qs, '', '&', \PHP_QUERY_RFC3986);
  588. }
  589. /**
  590. * Enables support for the _method request parameter to determine the intended HTTP method.
  591. *
  592. * Be warned that enabling this feature might lead to CSRF issues in your code.
  593. * Check that you are using CSRF tokens when required.
  594. * If the HTTP method parameter override is enabled, an html-form with method "POST" can be altered
  595. * and used to send a "PUT" or "DELETE" request via the _method request parameter.
  596. * If these methods are not protected against CSRF, this presents a possible vulnerability.
  597. *
  598. * The HTTP method can only be overridden when the real HTTP method is POST.
  599. */
  600. public static function enableHttpMethodParameterOverride(): void
  601. {
  602. self::$httpMethodParameterOverride = true;
  603. }
  604. /**
  605. * Checks whether support for the _method request parameter is enabled.
  606. */
  607. public static function getHttpMethodParameterOverride(): bool
  608. {
  609. return self::$httpMethodParameterOverride;
  610. }
  611. /**
  612. * Sets the list of HTTP methods that can be overridden.
  613. *
  614. * Set to null to allow all methods to be overridden (default). Set to an
  615. * empty array to disallow overrides entirely. Otherwise, provide the list
  616. * of uppercased method names that are allowed.
  617. *
  618. * @param uppercase-string[]|null $methods
  619. */
  620. public static function setAllowedHttpMethodOverride(?array $methods): void
  621. {
  622. if (array_intersect($methods ?? [], ['GET', 'HEAD', 'CONNECT', 'TRACE'])) {
  623. throw new \InvalidArgumentException('The HTTP methods "GET", "HEAD", "CONNECT", and "TRACE" cannot be overridden.');
  624. }
  625. self::$allowedHttpMethodOverride = $methods;
  626. }
  627. /**
  628. * Gets the list of HTTP methods that can be overridden.
  629. *
  630. * @return uppercase-string[]|null
  631. */
  632. public static function getAllowedHttpMethodOverride(): ?array
  633. {
  634. return self::$allowedHttpMethodOverride;
  635. }
  636. /**
  637. * Gets the Session.
  638. *
  639. * @throws SessionNotFoundException When session is not set properly
  640. */
  641. public function getSession(): SessionInterface
  642. {
  643. $session = $this->session;
  644. if (!$session instanceof SessionInterface && null !== $session) {
  645. $this->setSession($session = $session());
  646. }
  647. if (null === $session) {
  648. throw new SessionNotFoundException('Session has not been set.');
  649. }
  650. return $session;
  651. }
  652. /**
  653. * Whether the request contains a Session which was started in one of the
  654. * previous requests.
  655. */
  656. public function hasPreviousSession(): bool
  657. {
  658. // the check for $this->session avoids malicious users trying to fake a session cookie with proper name
  659. return $this->hasSession() && $this->cookies->has($this->getSession()->getName());
  660. }
  661. /**
  662. * Whether the request contains a Session object.
  663. *
  664. * This method does not give any information about the state of the session object,
  665. * like whether the session is started or not. It is just a way to check if this Request
  666. * is associated with a Session instance.
  667. *
  668. * @param bool $skipIfUninitialized When true, ignores factories injected by `setSessionFactory`
  669. */
  670. public function hasSession(bool $skipIfUninitialized = false): bool
  671. {
  672. return null !== $this->session && (!$skipIfUninitialized || $this->session instanceof SessionInterface);
  673. }
  674. public function setSession(SessionInterface $session): void
  675. {
  676. $this->session = $session;
  677. }
  678. /**
  679. * @internal
  680. *
  681. * @param callable(): SessionInterface $factory
  682. */
  683. public function setSessionFactory(callable $factory): void
  684. {
  685. $this->session = $factory(...);
  686. }
  687. /**
  688. * Returns the client IP addresses.
  689. *
  690. * In the returned array the most trusted IP address is first, and the
  691. * least trusted one last. The "real" client IP address is the last one,
  692. * but this is also the least trusted one. Trusted proxies are stripped.
  693. *
  694. * Use this method carefully; you should use getClientIp() instead.
  695. *
  696. * @see getClientIp()
  697. */
  698. public function getClientIps(): array
  699. {
  700. $ip = $this->server->get('REMOTE_ADDR');
  701. if (!$this->isFromTrustedProxy()) {
  702. return [$ip];
  703. }
  704. return $this->getTrustedValues(self::HEADER_X_FORWARDED_FOR, $ip) ?: [$ip];
  705. }
  706. /**
  707. * Returns the client IP address.
  708. *
  709. * This method can read the client IP address from the "X-Forwarded-For" header
  710. * when trusted proxies were set via "setTrustedProxies()". The "X-Forwarded-For"
  711. * header value is a comma+space separated list of IP addresses, the left-most
  712. * being the original client, and each successive proxy that passed the request
  713. * adding the IP address where it received the request from.
  714. *
  715. * If your reverse proxy uses a different header name than "X-Forwarded-For",
  716. * ("Client-Ip" for instance), configure it via the $trustedHeaderSet
  717. * argument of the Request::setTrustedProxies() method instead.
  718. *
  719. * @see getClientIps()
  720. * @see https://wikipedia.org/wiki/X-Forwarded-For
  721. */
  722. public function getClientIp(): ?string
  723. {
  724. return $this->getClientIps()[0];
  725. }
  726. /**
  727. * Returns current script name.
  728. */
  729. public function getScriptName(): string
  730. {
  731. return $this->server->get('SCRIPT_NAME', $this->server->get('ORIG_SCRIPT_NAME', ''));
  732. }
  733. /**
  734. * Returns the path being requested relative to the executed script.
  735. *
  736. * The path info always starts with a /.
  737. *
  738. * Suppose this request is instantiated from /mysite on localhost:
  739. *
  740. * * http://localhost/mysite returns '/'
  741. * * http://localhost/mysite/about returns '/about'
  742. * * http://localhost/mysite/enco%20ded returns '/enco%20ded'
  743. * * http://localhost/mysite/about?var=1 returns '/about'
  744. *
  745. * @return string The raw path (i.e. not urldecoded)
  746. */
  747. public function getPathInfo(): string
  748. {
  749. return $this->pathInfo ??= $this->preparePathInfo();
  750. }
  751. /**
  752. * Returns the root path from which this request is executed.
  753. *
  754. * Suppose that an index.php file instantiates this request object:
  755. *
  756. * * http://localhost/index.php returns an empty string
  757. * * http://localhost/index.php/page returns an empty string
  758. * * http://localhost/web/index.php returns '/web'
  759. * * http://localhost/we%20b/index.php returns '/we%20b'
  760. *
  761. * @return string The raw path (i.e. not urldecoded)
  762. */
  763. public function getBasePath(): string
  764. {
  765. return $this->basePath ??= $this->prepareBasePath();
  766. }
  767. /**
  768. * Returns the root URL from which this request is executed.
  769. *
  770. * The base URL never ends with a /.
  771. *
  772. * This is similar to getBasePath(), except that it also includes the
  773. * script filename (e.g. index.php) if one exists.
  774. *
  775. * @return string The raw URL (i.e. not urldecoded)
  776. */
  777. public function getBaseUrl(): string
  778. {
  779. $trustedPrefix = '';
  780. // the proxy prefix must be prepended to any prefix being needed at the webserver level
  781. if ($this->isFromTrustedProxy() && $trustedPrefixValues = $this->getTrustedValues(self::HEADER_X_FORWARDED_PREFIX)) {
  782. $trustedPrefix = rtrim($trustedPrefixValues[0], '/');
  783. }
  784. return $trustedPrefix.$this->getBaseUrlReal();
  785. }
  786. /**
  787. * Returns the real base URL received by the webserver from which this request is executed.
  788. * The URL does not include trusted reverse proxy prefix.
  789. *
  790. * @return string The raw URL (i.e. not urldecoded)
  791. */
  792. private function getBaseUrlReal(): string
  793. {
  794. return $this->baseUrl ??= $this->prepareBaseUrl();
  795. }
  796. /**
  797. * Gets the request's scheme.
  798. */
  799. public function getScheme(): string
  800. {
  801. return $this->isSecure() ? 'https' : 'http';
  802. }
  803. /**
  804. * Returns the port on which the request is made.
  805. *
  806. * This method can read the client port from the "X-Forwarded-Port" header
  807. * when trusted proxies were set via "setTrustedProxies()".
  808. *
  809. * The "X-Forwarded-Port" header must contain the client port.
  810. *
  811. * @return int|string|null Can be a string if fetched from the server bag
  812. */
  813. public function getPort(): int|string|null
  814. {
  815. if ($this->isFromTrustedProxy() && $host = $this->getTrustedValues(self::HEADER_X_FORWARDED_PORT)) {
  816. $host = $host[0];
  817. } elseif ($this->isFromTrustedProxy() && $host = $this->getTrustedValues(self::HEADER_X_FORWARDED_HOST)) {
  818. $host = $host[0];
  819. } elseif (!$host = $this->headers->get('HOST')) {
  820. return $this->server->get('SERVER_PORT');
  821. }
  822. if ('[' === $host[0]) {
  823. $pos = strpos($host, ':', strrpos($host, ']'));
  824. } else {
  825. $pos = strrpos($host, ':');
  826. }
  827. if (false !== $pos && $port = substr($host, $pos + 1)) {
  828. return (int) $port;
  829. }
  830. return 'https' === $this->getScheme() ? 443 : 80;
  831. }
  832. /**
  833. * Returns the user.
  834. */
  835. public function getUser(): ?string
  836. {
  837. return $this->headers->get('PHP_AUTH_USER');
  838. }
  839. /**
  840. * Returns the password.
  841. */
  842. public function getPassword(): ?string
  843. {
  844. return $this->headers->get('PHP_AUTH_PW');
  845. }
  846. /**
  847. * Gets the user info.
  848. *
  849. * @return string|null A user name if any and, optionally, scheme-specific information about how to gain authorization to access the server
  850. */
  851. public function getUserInfo(): ?string
  852. {
  853. $userinfo = $this->getUser();
  854. $pass = $this->getPassword();
  855. if ('' != $pass) {
  856. $userinfo .= ":$pass";
  857. }
  858. return $userinfo;
  859. }
  860. /**
  861. * Returns the HTTP host being requested.
  862. *
  863. * The port name will be appended to the host if it's non-standard.
  864. */
  865. public function getHttpHost(): string
  866. {
  867. $scheme = $this->getScheme();
  868. $port = $this->getPort();
  869. if (('http' === $scheme && 80 == $port) || ('https' === $scheme && 443 == $port)) {
  870. return $this->getHost();
  871. }
  872. return $this->getHost().':'.$port;
  873. }
  874. /**
  875. * Returns the requested URI (path and query string).
  876. *
  877. * @return string The raw URI (i.e. not URI decoded)
  878. */
  879. public function getRequestUri(): string
  880. {
  881. return $this->requestUri ??= $this->prepareRequestUri();
  882. }
  883. /**
  884. * Gets the scheme and HTTP host.
  885. *
  886. * If the URL was called with basic authentication, the user
  887. * and the password are not added to the generated string.
  888. */
  889. public function getSchemeAndHttpHost(): string
  890. {
  891. return $this->getScheme().'://'.$this->getHttpHost();
  892. }
  893. /**
  894. * Generates a normalized URI (URL) for the Request.
  895. *
  896. * @see getQueryString()
  897. */
  898. public function getUri(): string
  899. {
  900. if (null !== $qs = $this->getQueryString()) {
  901. $qs = '?'.$qs;
  902. }
  903. return $this->getSchemeAndHttpHost().$this->getBaseUrl().$this->getPathInfo().$qs;
  904. }
  905. /**
  906. * Generates a normalized URI for the given path.
  907. *
  908. * @param string $path A path to use instead of the current one
  909. */
  910. public function getUriForPath(string $path): string
  911. {
  912. return $this->getSchemeAndHttpHost().$this->getBaseUrl().$path;
  913. }
  914. /**
  915. * Returns the path as relative reference from the current Request path.
  916. *
  917. * Only the URIs path component (no schema, host etc.) is relevant and must be given.
  918. * Both paths must be absolute and not contain relative parts.
  919. * Relative URLs from one resource to another are useful when generating self-contained downloadable document archives.
  920. * Furthermore, they can be used to reduce the link size in documents.
  921. *
  922. * Example target paths, given a base path of "/a/b/c/d":
  923. * - "/a/b/c/d" -> ""
  924. * - "/a/b/c/" -> "./"
  925. * - "/a/b/" -> "../"
  926. * - "/a/b/c/other" -> "other"
  927. * - "/a/x/y" -> "../../x/y"
  928. */
  929. public function getRelativeUriForPath(string $path): string
  930. {
  931. // be sure that we are dealing with an absolute path
  932. if (!isset($path[0]) || '/' !== $path[0]) {
  933. return $path;
  934. }
  935. if ($path === $basePath = $this->getPathInfo()) {
  936. return '';
  937. }
  938. $sourceDirs = explode('/', isset($basePath[0]) && '/' === $basePath[0] ? substr($basePath, 1) : $basePath);
  939. $targetDirs = explode('/', substr($path, 1));
  940. array_pop($sourceDirs);
  941. $targetFile = array_pop($targetDirs);
  942. foreach ($sourceDirs as $i => $dir) {
  943. if (isset($targetDirs[$i]) && $dir === $targetDirs[$i]) {
  944. unset($sourceDirs[$i], $targetDirs[$i]);
  945. } else {
  946. break;
  947. }
  948. }
  949. $targetDirs[] = $targetFile;
  950. $path = str_repeat('../', \count($sourceDirs)).implode('/', $targetDirs);
  951. // A reference to the same base directory or an empty subdirectory must be prefixed with "./".
  952. // This also applies to a segment with a colon character (e.g., "file:colon") that cannot be used
  953. // as the first segment of a relative-path reference, as it would be mistaken for a scheme name
  954. // (see https://tools.ietf.org/html/rfc3986#section-4.2).
  955. return !isset($path[0]) || '/' === $path[0]
  956. || false !== ($colonPos = strpos($path, ':')) && ($colonPos < ($slashPos = strpos($path, '/')) || false === $slashPos)
  957. ? "./$path" : $path;
  958. }
  959. /**
  960. * Generates the normalized query string for the Request.
  961. *
  962. * It builds a normalized query string, where keys/value pairs are alphabetized
  963. * and have consistent escaping.
  964. */
  965. public function getQueryString(): ?string
  966. {
  967. $qs = static::normalizeQueryString($this->server->get('QUERY_STRING'));
  968. return '' === $qs ? null : $qs;
  969. }
  970. /**
  971. * Checks whether the request is secure or not.
  972. *
  973. * This method can read the client protocol from the "X-Forwarded-Proto" header
  974. * when trusted proxies were set via "setTrustedProxies()".
  975. *
  976. * The "X-Forwarded-Proto" header must contain the protocol: "https" or "http".
  977. */
  978. public function isSecure(): bool
  979. {
  980. if ($this->isFromTrustedProxy() && $proto = $this->getTrustedValues(self::HEADER_X_FORWARDED_PROTO)) {
  981. return \in_array(strtolower($proto[0]), ['https', 'on', 'ssl', '1'], true);
  982. }
  983. $https = $this->server->get('HTTPS');
  984. return $https && (!\is_string($https) || 'off' !== strtolower($https));
  985. }
  986. /**
  987. * Returns the host name.
  988. *
  989. * This method can read the client host name from the "X-Forwarded-Host" header
  990. * when trusted proxies were set via "setTrustedProxies()".
  991. *
  992. * The "X-Forwarded-Host" header must contain the client host name.
  993. *
  994. * @throws SuspiciousOperationException when the host name is invalid or not trusted
  995. */
  996. public function getHost(): string
  997. {
  998. if ($this->isFromTrustedProxy() && $host = $this->getTrustedValues(self::HEADER_X_FORWARDED_HOST)) {
  999. $host = $host[0];
  1000. } else {
  1001. $host = $this->headers->get('HOST') ?: $this->server->get('SERVER_NAME') ?: $this->server->get('SERVER_ADDR', '');
  1002. }
  1003. // trim and remove port number from host
  1004. // host is lowercase as per RFC 952/2181
  1005. $host = strtolower(preg_replace('/:\d+$/', '', trim($host)));
  1006. // the host can come from the user (HTTP_HOST and depending on the configuration, SERVER_NAME too can come from the user)
  1007. if ($host && !self::isHostValid($host)) {
  1008. if (!$this->isHostValid) {
  1009. return '';
  1010. }
  1011. $this->isHostValid = false;
  1012. throw new SuspiciousOperationException(\sprintf('Invalid Host "%s".', $host));
  1013. }
  1014. if (\count(self::$trustedHostPatterns) > 0) {
  1015. // to avoid host header injection attacks, you should provide a list of trusted host patterns
  1016. if (\in_array($host, self::$trustedHosts, true)) {
  1017. return $host;
  1018. }
  1019. foreach (self::$trustedHostPatterns as $pattern) {
  1020. if (preg_match($pattern, $host)) {
  1021. self::$trustedHosts[] = $host;
  1022. return $host;
  1023. }
  1024. }
  1025. if (!$this->isHostValid) {
  1026. return '';
  1027. }
  1028. $this->isHostValid = false;
  1029. throw new SuspiciousOperationException(\sprintf('Untrusted Host "%s".', $host));
  1030. }
  1031. return $host;
  1032. }
  1033. /**
  1034. * Sets the request method.
  1035. */
  1036. public function setMethod(string $method): void
  1037. {
  1038. $this->method = null;
  1039. $this->server->set('REQUEST_METHOD', $method);
  1040. }
  1041. /**
  1042. * Gets the request "intended" method.
  1043. *
  1044. * If the X-HTTP-Method-Override header is set, and if the method is a POST,
  1045. * then it is used to determine the "real" intended HTTP method.
  1046. *
  1047. * The _method request parameter can also be used to determine the HTTP method,
  1048. * but only if enableHttpMethodParameterOverride() has been called.
  1049. *
  1050. * The method is always an uppercased string.
  1051. *
  1052. * @see getRealMethod()
  1053. */
  1054. public function getMethod(): string
  1055. {
  1056. if (null !== $this->method) {
  1057. return $this->method;
  1058. }
  1059. $this->method = strtoupper($this->server->get('REQUEST_METHOD', 'GET'));
  1060. if ('POST' !== $this->method || !(self::$allowedHttpMethodOverride ?? true)) {
  1061. return $this->method;
  1062. }
  1063. $method = $this->headers->get('X-HTTP-METHOD-OVERRIDE');
  1064. if (!$method && self::$httpMethodParameterOverride) {
  1065. $method = $this->request->get('_method', $this->query->get('_method', 'POST'));
  1066. }
  1067. if (!\is_string($method)) {
  1068. return $this->method;
  1069. }
  1070. $method = strtoupper($method);
  1071. if (\in_array($method, ['GET', 'HEAD', 'CONNECT', 'TRACE'], true)) {
  1072. return $this->method;
  1073. }
  1074. if (self::$allowedHttpMethodOverride && !\in_array($method, self::$allowedHttpMethodOverride, true)) {
  1075. return $this->method;
  1076. }
  1077. if (\strlen($method) !== strspn($method, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')) {
  1078. throw new SuspiciousOperationException('Invalid HTTP method override.');
  1079. }
  1080. return $this->method = $method;
  1081. }
  1082. /**
  1083. * Gets the "real" request method.
  1084. *
  1085. * @see getMethod()
  1086. */
  1087. public function getRealMethod(): string
  1088. {
  1089. return strtoupper($this->server->get('REQUEST_METHOD', 'GET'));
  1090. }
  1091. /**
  1092. * Gets the mime type associated with the format.
  1093. */
  1094. public function getMimeType(string $format): ?string
  1095. {
  1096. if (null === static::$formats) {
  1097. static::initializeFormats();
  1098. }
  1099. return isset(static::$formats[$format]) ? static::$formats[$format][0] : null;
  1100. }
  1101. /**
  1102. * Gets the mime types associated with the format.
  1103. *
  1104. * @return string[]
  1105. */
  1106. public static function getMimeTypes(string $format): array
  1107. {
  1108. if (null === static::$formats) {
  1109. static::initializeFormats();
  1110. }
  1111. return static::$formats[$format] ?? [];
  1112. }
  1113. /**
  1114. * Gets the format associated with the mime type.
  1115. *
  1116. * Resolution order:
  1117. * 1) Exact match on the full MIME type (e.g. "application/json").
  1118. * 2) Match on the canonical MIME type (i.e. before the first ";" parameter).
  1119. * 3) If the type is "application/*+suffix", use the structured syntax suffix
  1120. * mapping (e.g. "application/foo+json" → "json"), when available.
  1121. * 4) If $subtypeFallback is true and no match was found:
  1122. * - return the MIME subtype (without "x-" prefix), provided it does not
  1123. * contain a "+" (e.g. "application/x-yaml" → "yaml", "text/csv" → "csv").
  1124. *
  1125. * @param string|null $mimeType The mime type to check
  1126. * @param bool $subtypeFallback Whether to fall back to the subtype if no exact match is found
  1127. */
  1128. public function getFormat(?string $mimeType, bool $subtypeFallback = false): ?string
  1129. {
  1130. $canonicalMimeType = null;
  1131. if ($mimeType && false !== $pos = strpos($mimeType, ';')) {
  1132. $canonicalMimeType = trim(substr($mimeType, 0, $pos));
  1133. }
  1134. if (null === static::$formats) {
  1135. static::initializeFormats();
  1136. }
  1137. $exactFormat = null;
  1138. $canonicalFormat = null;
  1139. foreach (static::$formats as $format => $mimeTypes) {
  1140. if (\in_array($mimeType, $mimeTypes, true)) {
  1141. $exactFormat = $format;
  1142. }
  1143. if (null !== $canonicalMimeType && \in_array($canonicalMimeType, $mimeTypes, true)) {
  1144. $canonicalFormat = $format;
  1145. }
  1146. }
  1147. if ($format = $exactFormat ?? $canonicalFormat) {
  1148. return $format;
  1149. }
  1150. if (!$canonicalMimeType ??= $mimeType) {
  1151. return null;
  1152. }
  1153. if (str_starts_with($canonicalMimeType, 'application/') && str_contains($canonicalMimeType, '+')) {
  1154. $suffix = substr(strrchr($canonicalMimeType, '+'), 1);
  1155. if (isset(self::STRUCTURED_SUFFIX_FORMATS[$suffix])) {
  1156. return self::STRUCTURED_SUFFIX_FORMATS[$suffix];
  1157. }
  1158. }
  1159. if ($subtypeFallback && str_contains($canonicalMimeType, '/')) {
  1160. [, $subtype] = explode('/', $canonicalMimeType, 2);
  1161. if (str_starts_with($subtype, 'x-')) {
  1162. $subtype = substr($subtype, 2);
  1163. }
  1164. if (!str_contains($subtype, '+')) {
  1165. return $subtype;
  1166. }
  1167. }
  1168. return null;
  1169. }
  1170. /**
  1171. * Associates a format with mime types.
  1172. *
  1173. * @param string|string[] $mimeTypes The associated mime types (the preferred one must be the first as it will be used as the content type)
  1174. */
  1175. public function setFormat(string $format, string|array $mimeTypes): void
  1176. {
  1177. if (null === static::$formats) {
  1178. static::initializeFormats();
  1179. }
  1180. static::$formats[$format] = (array) $mimeTypes;
  1181. }
  1182. /**
  1183. * Gets the request format.
  1184. *
  1185. * Here is the process to determine the format:
  1186. *
  1187. * * format defined by the user (with setRequestFormat())
  1188. * * _format request attribute
  1189. * * $default
  1190. *
  1191. * @see getPreferredFormat
  1192. */
  1193. public function getRequestFormat(?string $default = 'html'): ?string
  1194. {
  1195. $this->format ??= $this->attributes->get('_format');
  1196. return $this->format ?? $default;
  1197. }
  1198. /**
  1199. * Sets the request format.
  1200. */
  1201. public function setRequestFormat(?string $format): void
  1202. {
  1203. $this->format = $format;
  1204. }
  1205. /**
  1206. * Gets the usual name of the format associated with the request's media type (provided in the Content-Type header).
  1207. *
  1208. * @see Request::$formats
  1209. */
  1210. public function getContentTypeFormat(): ?string
  1211. {
  1212. return $this->getFormat($this->headers->get('CONTENT_TYPE', ''));
  1213. }
  1214. /**
  1215. * Sets the default locale.
  1216. */
  1217. public function setDefaultLocale(string $locale): void
  1218. {
  1219. $this->defaultLocale = $locale;
  1220. if (null === $this->locale) {
  1221. $this->setPhpDefaultLocale($locale);
  1222. }
  1223. }
  1224. /**
  1225. * Get the default locale.
  1226. */
  1227. public function getDefaultLocale(): string
  1228. {
  1229. return $this->defaultLocale;
  1230. }
  1231. /**
  1232. * Sets the locale.
  1233. */
  1234. public function setLocale(string $locale): void
  1235. {
  1236. $this->setPhpDefaultLocale($this->locale = $locale);
  1237. }
  1238. /**
  1239. * Get the locale.
  1240. */
  1241. public function getLocale(): string
  1242. {
  1243. return $this->locale ?? $this->defaultLocale;
  1244. }
  1245. /**
  1246. * Checks if the request method is of specified type.
  1247. *
  1248. * @param string $method Uppercase request method (GET, POST etc)
  1249. */
  1250. public function isMethod(string $method): bool
  1251. {
  1252. return $this->getMethod() === strtoupper($method);
  1253. }
  1254. /**
  1255. * Checks whether or not the method is safe.
  1256. *
  1257. * @see https://tools.ietf.org/html/rfc7231#section-4.2.1
  1258. */
  1259. public function isMethodSafe(): bool
  1260. {
  1261. return \in_array($this->getMethod(), ['GET', 'HEAD', 'OPTIONS', 'TRACE', 'QUERY'], true);
  1262. }
  1263. /**
  1264. * Checks whether or not the method is idempotent.
  1265. */
  1266. public function isMethodIdempotent(): bool
  1267. {
  1268. return \in_array($this->getMethod(), ['HEAD', 'GET', 'PUT', 'DELETE', 'TRACE', 'OPTIONS', 'PURGE', 'QUERY'], true);
  1269. }
  1270. /**
  1271. * Checks whether the method is cacheable or not.
  1272. *
  1273. * @see https://tools.ietf.org/html/rfc7231#section-4.2.3
  1274. */
  1275. public function isMethodCacheable(): bool
  1276. {
  1277. return \in_array($this->getMethod(), ['GET', 'HEAD', 'QUERY'], true);
  1278. }
  1279. /**
  1280. * Returns the protocol version.
  1281. *
  1282. * If the application is behind a proxy, the protocol version used in the
  1283. * requests between the client and the proxy and between the proxy and the
  1284. * server might be different. This returns the former (from the "Via" header)
  1285. * if the proxy is trusted (see "setTrustedProxies()"), otherwise it returns
  1286. * the latter (from the "SERVER_PROTOCOL" server parameter).
  1287. */
  1288. public function getProtocolVersion(): ?string
  1289. {
  1290. if ($this->isFromTrustedProxy()) {
  1291. preg_match('~^(HTTP/)?([1-9]\.[0-9])\b~', $this->headers->get('Via') ?? '', $matches);
  1292. if ($matches) {
  1293. return 'HTTP/'.$matches[2];
  1294. }
  1295. }
  1296. return $this->server->get('SERVER_PROTOCOL');
  1297. }
  1298. /**
  1299. * Returns the request body content.
  1300. *
  1301. * @param bool $asResource If true, a resource will be returned
  1302. *
  1303. * @return string|resource
  1304. *
  1305. * @psalm-return ($asResource is true ? resource : string)
  1306. */
  1307. public function getContent(bool $asResource = false)
  1308. {
  1309. if ($asResource) {
  1310. if (\is_resource($this->content)) {
  1311. rewind($this->content);
  1312. return $this->content;
  1313. }
  1314. // Content passed in parameter (test)
  1315. if (\is_string($this->content)) {
  1316. $resource = fopen('php://temp', 'r+');
  1317. fwrite($resource, $this->content);
  1318. rewind($resource);
  1319. return $resource;
  1320. }
  1321. $this->content = false;
  1322. return fopen('php://input', 'r');
  1323. }
  1324. if (\is_resource($this->content)) {
  1325. rewind($this->content);
  1326. return stream_get_contents($this->content);
  1327. }
  1328. if (null === $this->content || false === $this->content) {
  1329. $this->content = file_get_contents('php://input');
  1330. }
  1331. return $this->content;
  1332. }
  1333. /**
  1334. * Gets the decoded form or json request body.
  1335. *
  1336. * @throws JsonException When the body cannot be decoded to an array
  1337. */
  1338. public function getPayload(): InputBag
  1339. {
  1340. if ($this->request->count()) {
  1341. return clone $this->request;
  1342. }
  1343. if ('' === $content = $this->getContent()) {
  1344. return new InputBag([]);
  1345. }
  1346. try {
  1347. $content = json_decode($content, true, 512, \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR);
  1348. } catch (\JsonException $e) {
  1349. throw new JsonException('Could not decode request body.', $e->getCode(), $e);
  1350. }
  1351. if (!\is_array($content)) {
  1352. throw new JsonException(\sprintf('JSON content was expected to decode to an array, "%s" returned.', get_debug_type($content)));
  1353. }
  1354. return new InputBag($content);
  1355. }
  1356. /**
  1357. * Gets the request body decoded as array, typically from a JSON payload.
  1358. *
  1359. * @see getPayload() for portability between content types
  1360. *
  1361. * @throws JsonException When the body cannot be decoded to an array
  1362. */
  1363. public function toArray(): array
  1364. {
  1365. if ('' === $content = $this->getContent()) {
  1366. throw new JsonException('Request body is empty.');
  1367. }
  1368. try {
  1369. $content = json_decode($content, true, 512, \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR);
  1370. } catch (\JsonException $e) {
  1371. throw new JsonException('Could not decode request body.', $e->getCode(), $e);
  1372. }
  1373. if (!\is_array($content)) {
  1374. throw new JsonException(\sprintf('JSON content was expected to decode to an array, "%s" returned.', get_debug_type($content)));
  1375. }
  1376. return $content;
  1377. }
  1378. /**
  1379. * Gets the Etags.
  1380. */
  1381. public function getETags(): array
  1382. {
  1383. return preg_split('/\s*,\s*/', $this->headers->get('If-None-Match', ''), -1, \PREG_SPLIT_NO_EMPTY);
  1384. }
  1385. public function isNoCache(): bool
  1386. {
  1387. return $this->headers->hasCacheControlDirective('no-cache') || 'no-cache' == $this->headers->get('Pragma');
  1388. }
  1389. /**
  1390. * Gets the preferred format for the response by inspecting, in the following order:
  1391. * * the request format set using setRequestFormat;
  1392. * * the values of the Accept HTTP header.
  1393. *
  1394. * Note that if you use this method, you should send the "Vary: Accept" header
  1395. * in the response to prevent any issues with intermediary HTTP caches.
  1396. */
  1397. public function getPreferredFormat(?string $default = 'html'): ?string
  1398. {
  1399. if (!isset($this->preferredFormat) && null !== $preferredFormat = $this->getRequestFormat(null)) {
  1400. $this->preferredFormat = $preferredFormat;
  1401. }
  1402. if ($this->preferredFormat ?? null) {
  1403. return $this->preferredFormat;
  1404. }
  1405. foreach ($this->getAcceptableContentTypes() as $mimeType) {
  1406. if ($this->preferredFormat = $this->getFormat($mimeType)) {
  1407. return $this->preferredFormat;
  1408. }
  1409. }
  1410. return $default;
  1411. }
  1412. /**
  1413. * Returns the preferred language.
  1414. *
  1415. * @param string[] $locales An array of ordered available locales
  1416. */
  1417. public function getPreferredLanguage(?array $locales = null): ?string
  1418. {
  1419. $preferredLanguages = $this->getLanguages();
  1420. if (!$locales) {
  1421. return $preferredLanguages[0] ?? null;
  1422. }
  1423. $locales = array_map($this->formatLocale(...), $locales);
  1424. if (!$preferredLanguages) {
  1425. return $locales[0];
  1426. }
  1427. $combinations = array_merge(...array_map($this->getLanguageCombinations(...), $preferredLanguages));
  1428. foreach ($combinations as $combination) {
  1429. foreach ($locales as $locale) {
  1430. if (str_starts_with($locale, $combination)) {
  1431. return $locale;
  1432. }
  1433. }
  1434. }
  1435. return $locales[0];
  1436. }
  1437. /**
  1438. * Gets a list of languages acceptable by the client browser ordered in the user browser preferences.
  1439. *
  1440. * @return string[]
  1441. */
  1442. public function getLanguages(): array
  1443. {
  1444. if (null !== $this->languages) {
  1445. return $this->languages;
  1446. }
  1447. $languages = AcceptHeader::fromString($this->headers->get('Accept-Language'))->all();
  1448. $this->languages = [];
  1449. foreach ($languages as $acceptHeaderItem) {
  1450. $lang = $acceptHeaderItem->getValue();
  1451. $this->languages[] = self::formatLocale($lang);
  1452. }
  1453. $this->languages = array_unique($this->languages);
  1454. return $this->languages;
  1455. }
  1456. /**
  1457. * Strips the locale to only keep the canonicalized language value.
  1458. *
  1459. * Depending on the $locale value, this method can return values like :
  1460. * - language_Script_REGION: "fr_Latn_FR", "zh_Hans_TW"
  1461. * - language_Script: "fr_Latn", "zh_Hans"
  1462. * - language_REGION: "fr_FR", "zh_TW"
  1463. * - language: "fr", "zh"
  1464. *
  1465. * Invalid locale values are returned as is.
  1466. *
  1467. * @see https://wikipedia.org/wiki/IETF_language_tag
  1468. * @see https://datatracker.ietf.org/doc/html/rfc5646
  1469. */
  1470. private static function formatLocale(string $locale): string
  1471. {
  1472. [$language, $script, $region] = self::getLanguageComponents($locale);
  1473. return implode('_', array_filter([$language, $script, $region]));
  1474. }
  1475. /**
  1476. * Returns an array of all possible combinations of the language components.
  1477. *
  1478. * For instance, if the locale is "fr_Latn_FR", this method will return:
  1479. * - "fr_Latn_FR"
  1480. * - "fr_Latn"
  1481. * - "fr_FR"
  1482. * - "fr"
  1483. *
  1484. * @return string[]
  1485. */
  1486. private static function getLanguageCombinations(string $locale): array
  1487. {
  1488. [$language, $script, $region] = self::getLanguageComponents($locale);
  1489. return array_unique([
  1490. implode('_', array_filter([$language, $script, $region])),
  1491. implode('_', array_filter([$language, $script])),
  1492. implode('_', array_filter([$language, $region])),
  1493. $language,
  1494. ]);
  1495. }
  1496. /**
  1497. * Returns an array with the language components of the locale.
  1498. *
  1499. * For example:
  1500. * - If the locale is "fr_Latn_FR", this method will return "fr", "Latn", "FR"
  1501. * - If the locale is "fr_FR", this method will return "fr", null, "FR"
  1502. * - If the locale is "zh_Hans", this method will return "zh", "Hans", null
  1503. *
  1504. * @see https://wikipedia.org/wiki/IETF_language_tag
  1505. * @see https://datatracker.ietf.org/doc/html/rfc5646
  1506. *
  1507. * @return array{string, string|null, string|null}
  1508. */
  1509. private static function getLanguageComponents(string $locale): array
  1510. {
  1511. $locale = str_replace('_', '-', strtolower($locale));
  1512. $pattern = '/^([a-zA-Z]{2,3}|i-[a-zA-Z]{5,})(?:-([a-zA-Z]{4}))?(?:-([a-zA-Z]{2}))?(?:-(.+))?$/';
  1513. if (!preg_match($pattern, $locale, $matches)) {
  1514. return [$locale, null, null];
  1515. }
  1516. if (str_starts_with($matches[1], 'i-')) {
  1517. // Language not listed in ISO 639 that are not variants
  1518. // of any listed language, which can be registered with the
  1519. // i-prefix, such as i-cherokee
  1520. $matches[1] = substr($matches[1], 2);
  1521. }
  1522. return [
  1523. $matches[1],
  1524. isset($matches[2]) ? ucfirst(strtolower($matches[2])) : null,
  1525. isset($matches[3]) ? strtoupper($matches[3]) : null,
  1526. ];
  1527. }
  1528. /**
  1529. * Gets a list of charsets acceptable by the client browser in preferable order.
  1530. *
  1531. * @return string[]
  1532. */
  1533. public function getCharsets(): array
  1534. {
  1535. return $this->charsets ??= array_map('strval', array_keys(AcceptHeader::fromString($this->headers->get('Accept-Charset'))->all()));
  1536. }
  1537. /**
  1538. * Gets a list of encodings acceptable by the client browser in preferable order.
  1539. *
  1540. * @return string[]
  1541. */
  1542. public function getEncodings(): array
  1543. {
  1544. return $this->encodings ??= array_map('strval', array_keys(AcceptHeader::fromString($this->headers->get('Accept-Encoding'))->all()));
  1545. }
  1546. /**
  1547. * Gets a list of content types acceptable by the client browser in preferable order.
  1548. *
  1549. * @return string[]
  1550. */
  1551. public function getAcceptableContentTypes(): array
  1552. {
  1553. return $this->acceptableContentTypes ??= array_map('strval', array_keys(AcceptHeader::fromString($this->headers->get('Accept'))->all()));
  1554. }
  1555. /**
  1556. * Returns true if the request is an XMLHttpRequest.
  1557. *
  1558. * It works if your JavaScript library sets an X-Requested-With HTTP header.
  1559. * It is known to work with common JavaScript frameworks:
  1560. *
  1561. * @see https://wikipedia.org/wiki/List_of_Ajax_frameworks#JavaScript
  1562. */
  1563. public function isXmlHttpRequest(): bool
  1564. {
  1565. return 'XMLHttpRequest' == $this->headers->get('X-Requested-With');
  1566. }
  1567. /**
  1568. * Checks whether the client browser prefers safe content or not according to RFC8674.
  1569. *
  1570. * @see https://tools.ietf.org/html/rfc8674
  1571. */
  1572. public function preferSafeContent(): bool
  1573. {
  1574. if (isset($this->isSafeContentPreferred)) {
  1575. return $this->isSafeContentPreferred;
  1576. }
  1577. if (!$this->isSecure()) {
  1578. // see https://tools.ietf.org/html/rfc8674#section-3
  1579. return $this->isSafeContentPreferred = false;
  1580. }
  1581. return $this->isSafeContentPreferred = AcceptHeader::fromString($this->headers->get('Prefer'))->has('safe');
  1582. }
  1583. /*
  1584. * The following methods are derived from code of the Zend Framework (1.10dev - 2010-01-24)
  1585. *
  1586. * Code subject to the new BSD license (https://framework.zend.com/license).
  1587. *
  1588. * Copyright (c) 2005-2010 Zend Technologies USA Inc. (https://www.zend.com/)
  1589. */
  1590. protected function prepareRequestUri(): string
  1591. {
  1592. $requestUri = '';
  1593. if ($this->isIisRewrite() && '' != $this->server->get('UNENCODED_URL')) {
  1594. // IIS7 with URL Rewrite: make sure we get the unencoded URL (double slash problem)
  1595. $requestUri = $this->server->get('UNENCODED_URL');
  1596. $this->server->remove('UNENCODED_URL');
  1597. } elseif ($this->server->has('REQUEST_URI')) {
  1598. $requestUri = $this->server->get('REQUEST_URI');
  1599. if ('' !== $requestUri && '/' === $requestUri[0]) {
  1600. // To only use path and query remove the fragment.
  1601. if (false !== $pos = strpos($requestUri, '#')) {
  1602. $requestUri = substr($requestUri, 0, $pos);
  1603. }
  1604. } else {
  1605. // HTTP proxy reqs setup request URI with scheme and host [and port] + the URL path,
  1606. // only use URL path.
  1607. $uriComponents = parse_url($requestUri);
  1608. if (isset($uriComponents['path'])) {
  1609. $requestUri = $uriComponents['path'];
  1610. }
  1611. if (isset($uriComponents['query'])) {
  1612. $requestUri .= '?'.$uriComponents['query'];
  1613. }
  1614. }
  1615. } elseif ($this->server->has('ORIG_PATH_INFO')) {
  1616. // IIS 5.0, PHP as CGI
  1617. $requestUri = $this->server->get('ORIG_PATH_INFO');
  1618. if ('' != $this->server->get('QUERY_STRING')) {
  1619. $requestUri .= '?'.$this->server->get('QUERY_STRING');
  1620. }
  1621. $this->server->remove('ORIG_PATH_INFO');
  1622. }
  1623. // normalize the request URI to ease creating sub-requests from this request
  1624. $this->server->set('REQUEST_URI', $requestUri);
  1625. return $requestUri;
  1626. }
  1627. /**
  1628. * Prepares the base URL.
  1629. */
  1630. protected function prepareBaseUrl(): string
  1631. {
  1632. $filename = basename($this->server->get('SCRIPT_FILENAME', ''));
  1633. if (basename($this->server->get('SCRIPT_NAME', '')) === $filename) {
  1634. $baseUrl = $this->server->get('SCRIPT_NAME');
  1635. } elseif (basename($this->server->get('PHP_SELF', '')) === $filename) {
  1636. $baseUrl = $this->server->get('PHP_SELF');
  1637. } elseif (basename($this->server->get('ORIG_SCRIPT_NAME', '')) === $filename) {
  1638. $baseUrl = $this->server->get('ORIG_SCRIPT_NAME'); // 1and1 shared hosting compatibility
  1639. } else {
  1640. // Backtrack up the script_filename to find the portion matching
  1641. // php_self
  1642. $path = $this->server->get('PHP_SELF', '');
  1643. $file = $this->server->get('SCRIPT_FILENAME', '');
  1644. $segs = explode('/', trim($file, '/'));
  1645. $segs = array_reverse($segs);
  1646. $index = 0;
  1647. $last = \count($segs);
  1648. $baseUrl = '';
  1649. do {
  1650. $seg = $segs[$index];
  1651. $baseUrl = '/'.$seg.$baseUrl;
  1652. ++$index;
  1653. } while ($last > $index && (false !== $pos = strpos($path, $baseUrl)) && 0 != $pos);
  1654. }
  1655. // Does the baseUrl have anything in common with the request_uri?
  1656. $requestUri = $this->getRequestUri();
  1657. if ('' !== $requestUri && '/' !== $requestUri[0]) {
  1658. $requestUri = '/'.$requestUri;
  1659. }
  1660. if ($baseUrl && null !== $prefix = $this->getUrlencodedPrefix($requestUri, $baseUrl)) {
  1661. // full $baseUrl matches
  1662. return $prefix;
  1663. }
  1664. if ($baseUrl && null !== $prefix = $this->getUrlencodedPrefix($requestUri, rtrim(\dirname($baseUrl), '/'.\DIRECTORY_SEPARATOR).'/')) {
  1665. // directory portion of $baseUrl matches
  1666. return rtrim($prefix, '/'.\DIRECTORY_SEPARATOR);
  1667. }
  1668. $truncatedRequestUri = $requestUri;
  1669. if (false !== $pos = strpos($requestUri, '?')) {
  1670. $truncatedRequestUri = substr($requestUri, 0, $pos);
  1671. }
  1672. $basename = basename($baseUrl ?? '');
  1673. if (!$basename || !strpos(rawurldecode($truncatedRequestUri), $basename)) {
  1674. // no match whatsoever; set it blank
  1675. return '';
  1676. }
  1677. // If using mod_rewrite or ISAPI_Rewrite strip the script filename
  1678. // out of baseUrl. $pos !== 0 makes sure it is not matching a value
  1679. // from PATH_INFO or QUERY_STRING
  1680. if (\strlen($requestUri) >= \strlen($baseUrl) && (false !== $pos = strpos($requestUri, $baseUrl)) && 0 !== $pos) {
  1681. $baseUrl = substr($requestUri, 0, $pos + \strlen($baseUrl));
  1682. }
  1683. return rtrim($baseUrl, '/'.\DIRECTORY_SEPARATOR);
  1684. }
  1685. /**
  1686. * Prepares the base path.
  1687. */
  1688. protected function prepareBasePath(): string
  1689. {
  1690. $baseUrl = $this->getBaseUrl();
  1691. if (!$baseUrl) {
  1692. return '';
  1693. }
  1694. $filename = basename($this->server->get('SCRIPT_FILENAME'));
  1695. if (basename($baseUrl) === $filename) {
  1696. $basePath = \dirname($baseUrl);
  1697. } else {
  1698. $basePath = $baseUrl;
  1699. }
  1700. if ('\\' === \DIRECTORY_SEPARATOR) {
  1701. $basePath = str_replace('\\', '/', $basePath);
  1702. }
  1703. return rtrim($basePath, '/');
  1704. }
  1705. /**
  1706. * Prepares the path info.
  1707. */
  1708. protected function preparePathInfo(): string
  1709. {
  1710. if (null === ($requestUri = $this->getRequestUri())) {
  1711. return '/';
  1712. }
  1713. // Remove the query string from REQUEST_URI
  1714. if (false !== $pos = strpos($requestUri, '?')) {
  1715. $requestUri = substr($requestUri, 0, $pos);
  1716. }
  1717. if ('' !== $requestUri && '/' !== $requestUri[0]) {
  1718. $requestUri = '/'.$requestUri;
  1719. }
  1720. if (null === ($baseUrl = $this->getBaseUrlReal())) {
  1721. return $requestUri;
  1722. }
  1723. $pathInfo = substr($requestUri, \strlen($baseUrl));
  1724. if ('' === $pathInfo || '/' !== $pathInfo[0]) {
  1725. return '/'.$pathInfo;
  1726. }
  1727. return $pathInfo;
  1728. }
  1729. /**
  1730. * Initializes HTTP request formats.
  1731. */
  1732. protected static function initializeFormats(): void
  1733. {
  1734. static::$formats = [
  1735. 'html' => ['text/html', 'application/xhtml+xml'],
  1736. 'txt' => ['text/plain'],
  1737. 'js' => ['application/javascript', 'application/x-javascript', 'text/javascript'],
  1738. 'css' => ['text/css'],
  1739. 'json' => ['application/json', 'application/x-json'],
  1740. 'jsonld' => ['application/ld+json'],
  1741. 'xml' => ['text/xml', 'application/xml', 'application/x-xml'],
  1742. 'rdf' => ['application/rdf+xml'],
  1743. 'atom' => ['application/atom+xml'],
  1744. 'rss' => ['application/rss+xml'],
  1745. 'form' => ['application/x-www-form-urlencoded', 'multipart/form-data'],
  1746. 'soap' => ['application/soap+xml'],
  1747. 'problem' => ['application/problem+json'],
  1748. 'hal' => ['application/hal+json', 'application/hal+xml'],
  1749. 'jsonapi' => ['application/vnd.api+json'],
  1750. 'yaml' => ['text/yaml', 'application/x-yaml'],
  1751. 'wbxml' => ['application/vnd.wap.wbxml'],
  1752. 'pdf' => ['application/pdf'],
  1753. 'csv' => ['text/csv'],
  1754. ];
  1755. }
  1756. private function setPhpDefaultLocale(string $locale): void
  1757. {
  1758. // if either the class Locale doesn't exist, or an exception is thrown when
  1759. // setting the default locale, the intl module is not installed, and
  1760. // the call can be ignored:
  1761. try {
  1762. if (class_exists(\Locale::class, false)) {
  1763. \Locale::setDefault($locale);
  1764. }
  1765. } catch (\Exception) {
  1766. }
  1767. }
  1768. /**
  1769. * Returns the prefix as encoded in the string when the string starts with
  1770. * the given prefix, null otherwise.
  1771. */
  1772. private function getUrlencodedPrefix(string $string, string $prefix): ?string
  1773. {
  1774. if ($this->isIisRewrite()) {
  1775. // ISS with UrlRewriteModule might report SCRIPT_NAME/PHP_SELF with wrong case
  1776. // see https://github.com/php/php-src/issues/11981
  1777. if (0 !== stripos(rawurldecode($string), $prefix)) {
  1778. return null;
  1779. }
  1780. } elseif (!str_starts_with(rawurldecode($string), $prefix)) {
  1781. return null;
  1782. }
  1783. $len = \strlen($prefix);
  1784. if (preg_match(\sprintf('#^(%%[[:xdigit:]]{2}|.){%d}#', $len), $string, $match)) {
  1785. return $match[0];
  1786. }
  1787. return null;
  1788. }
  1789. private static function createRequestFromFactory(array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = null): static
  1790. {
  1791. if (self::$requestFactory) {
  1792. $request = (self::$requestFactory)($query, $request, $attributes, $cookies, $files, $server, $content);
  1793. if (!$request instanceof self) {
  1794. throw new \LogicException('The Request factory must return an instance of Symfony\Component\HttpFoundation\Request.');
  1795. }
  1796. return $request;
  1797. }
  1798. return new static($query, $request, $attributes, $cookies, $files, $server, $content);
  1799. }
  1800. /**
  1801. * Indicates whether this request originated from a trusted proxy.
  1802. *
  1803. * This can be useful to determine whether or not to trust the
  1804. * contents of a proxy-specific header.
  1805. */
  1806. public function isFromTrustedProxy(): bool
  1807. {
  1808. return self::$trustedProxies && IpUtils::checkIp($this->server->get('REMOTE_ADDR', ''), self::$trustedProxies);
  1809. }
  1810. /**
  1811. * This method is rather heavy because it splits and merges headers, and it's called by many other methods such as
  1812. * getPort(), isSecure(), getHost(), getClientIps(), getBaseUrl() etc. Thus, we try to cache the results for
  1813. * best performance.
  1814. */
  1815. private function getTrustedValues(int $type, ?string $ip = null): array
  1816. {
  1817. $cacheKey = $type."\0".((self::$trustedHeaderSet & $type) ? $this->headers->get(self::TRUSTED_HEADERS[$type]) : '');
  1818. $cacheKey .= "\0".$ip."\0".$this->headers->get(self::TRUSTED_HEADERS[self::HEADER_FORWARDED]);
  1819. if (isset($this->trustedValuesCache[$cacheKey])) {
  1820. return $this->trustedValuesCache[$cacheKey];
  1821. }
  1822. $clientValues = [];
  1823. $forwardedValues = [];
  1824. if ((self::$trustedHeaderSet & $type) && $this->headers->has(self::TRUSTED_HEADERS[$type])) {
  1825. foreach (explode(',', $this->headers->get(self::TRUSTED_HEADERS[$type])) as $v) {
  1826. $clientValues[] = (self::HEADER_X_FORWARDED_PORT === $type ? '0.0.0.0:' : '').trim($v);
  1827. }
  1828. }
  1829. if ((self::$trustedHeaderSet & self::HEADER_FORWARDED) && (isset(self::FORWARDED_PARAMS[$type])) && $this->headers->has(self::TRUSTED_HEADERS[self::HEADER_FORWARDED])) {
  1830. $forwarded = $this->headers->get(self::TRUSTED_HEADERS[self::HEADER_FORWARDED]);
  1831. $parts = HeaderUtils::split($forwarded, ',;=');
  1832. $param = self::FORWARDED_PARAMS[$type];
  1833. foreach ($parts as $subParts) {
  1834. if (null === $v = HeaderUtils::combine($subParts)[$param] ?? null) {
  1835. continue;
  1836. }
  1837. if (self::HEADER_X_FORWARDED_PORT === $type) {
  1838. if (str_ends_with($v, ']') || false === $v = strrchr($v, ':')) {
  1839. $v = $this->isSecure() ? ':443' : ':80';
  1840. }
  1841. $v = '0.0.0.0'.$v;
  1842. }
  1843. $forwardedValues[] = $v;
  1844. }
  1845. }
  1846. if (null !== $ip) {
  1847. $clientValues = $this->normalizeAndFilterClientIps($clientValues, $ip);
  1848. $forwardedValues = $this->normalizeAndFilterClientIps($forwardedValues, $ip);
  1849. }
  1850. if ($forwardedValues === $clientValues || !$clientValues) {
  1851. return $this->trustedValuesCache[$cacheKey] = $forwardedValues;
  1852. }
  1853. if (!$forwardedValues) {
  1854. return $this->trustedValuesCache[$cacheKey] = $clientValues;
  1855. }
  1856. if (!$this->isForwardedValid) {
  1857. return $this->trustedValuesCache[$cacheKey] = null !== $ip ? ['0.0.0.0', $ip] : [];
  1858. }
  1859. $this->isForwardedValid = false;
  1860. throw new ConflictingHeadersException(\sprintf('The request has both a trusted "%s" header and a trusted "%s" header, conflicting with each other. You should either configure your proxy to remove one of them, or configure your project to distrust the offending one.', self::TRUSTED_HEADERS[self::HEADER_FORWARDED], self::TRUSTED_HEADERS[$type]));
  1861. }
  1862. private function normalizeAndFilterClientIps(array $clientIps, string $ip): array
  1863. {
  1864. if (!$clientIps) {
  1865. return [];
  1866. }
  1867. $clientIps[] = $ip; // Complete the IP chain with the IP the request actually came from
  1868. $firstTrustedIp = null;
  1869. foreach ($clientIps as $key => $clientIp) {
  1870. if (strpos($clientIp, '.')) {
  1871. // Strip :port from IPv4 addresses. This is allowed in Forwarded
  1872. // and may occur in X-Forwarded-For.
  1873. $i = strpos($clientIp, ':');
  1874. if ($i) {
  1875. $clientIps[$key] = $clientIp = substr($clientIp, 0, $i);
  1876. }
  1877. } elseif (str_starts_with($clientIp, '[')) {
  1878. // Strip brackets and :port from IPv6 addresses.
  1879. $i = strpos($clientIp, ']', 1);
  1880. $clientIps[$key] = $clientIp = substr($clientIp, 1, $i - 1);
  1881. }
  1882. if (!filter_var($clientIp, \FILTER_VALIDATE_IP)) {
  1883. unset($clientIps[$key]);
  1884. continue;
  1885. }
  1886. if (IpUtils::checkIp($clientIp, self::$trustedProxies)) {
  1887. unset($clientIps[$key]);
  1888. // Fallback to this when the client IP falls into the range of trusted proxies
  1889. $firstTrustedIp ??= $clientIp;
  1890. }
  1891. }
  1892. // Now the IP chain contains only untrusted proxies and the client IP
  1893. return $clientIps ? array_reverse($clientIps) : [$firstTrustedIp];
  1894. }
  1895. /**
  1896. * Is this IIS with UrlRewriteModule?
  1897. *
  1898. * This method consumes, caches and removed the IIS_WasUrlRewritten env var,
  1899. * so we don't inherit it to sub-requests.
  1900. */
  1901. private function isIisRewrite(): bool
  1902. {
  1903. if (1 === $this->server->getInt('IIS_WasUrlRewritten')) {
  1904. $this->isIisRewrite = true;
  1905. $this->server->remove('IIS_WasUrlRewritten');
  1906. }
  1907. return $this->isIisRewrite;
  1908. }
  1909. /**
  1910. * See https://url.spec.whatwg.org/.
  1911. */
  1912. private static function isHostValid(string $host): bool
  1913. {
  1914. if ('[' === $host[0]) {
  1915. return ']' === $host[-1] && filter_var(substr($host, 1, -1), \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6);
  1916. }
  1917. if (preg_match('/\.[0-9]++\.?$/D', $host)) {
  1918. return null !== filter_var($host, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4 | \FILTER_NULL_ON_FAILURE);
  1919. }
  1920. // use preg_replace() instead of preg_match() to prevent DoS attacks with long host names
  1921. return '' === preg_replace('/[-a-zA-Z0-9_]++\.?/', '', $host);
  1922. }
  1923. }