Response.php 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 2.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Http;
  17. use Cake\Core\Configure;
  18. use Cake\Http\Cookie\CookieCollection;
  19. use Cake\Http\Cookie\CookieInterface;
  20. use Cake\Http\Exception\NotFoundException;
  21. use DateTime;
  22. use DateTimeInterface;
  23. use DateTimeZone;
  24. use InvalidArgumentException;
  25. use Laminas\Diactoros\MessageTrait;
  26. use Laminas\Diactoros\Stream;
  27. use Psr\Http\Message\ResponseInterface;
  28. use Psr\Http\Message\StreamInterface;
  29. use SplFileInfo;
  30. use Stringable;
  31. /**
  32. * Responses contain the response text, status and headers of a HTTP response.
  33. *
  34. * There are external packages such as `fig/http-message-util` that provide HTTP
  35. * status code constants. These can be used with any method that accepts or
  36. * returns a status code integer. Keep in mind that these constants might
  37. * include status codes that are now allowed which will throw an
  38. * `\InvalidArgumentException`.
  39. */
  40. class Response implements ResponseInterface, Stringable
  41. {
  42. use MessageTrait;
  43. /**
  44. * @var int
  45. */
  46. public const STATUS_CODE_MIN = 100;
  47. /**
  48. * @var int
  49. */
  50. public const STATUS_CODE_MAX = 599;
  51. /**
  52. * Allowed HTTP status codes and their default description.
  53. *
  54. * @var array<int, string>
  55. */
  56. protected array $_statusCodes = [
  57. 100 => 'Continue',
  58. 101 => 'Switching Protocols',
  59. 102 => 'Processing',
  60. 200 => 'OK',
  61. 201 => 'Created',
  62. 202 => 'Accepted',
  63. 203 => 'Non-Authoritative Information',
  64. 204 => 'No Content',
  65. 205 => 'Reset Content',
  66. 206 => 'Partial Content',
  67. 207 => 'Multi-status',
  68. 208 => 'Already Reported',
  69. 226 => 'IM used',
  70. 300 => 'Multiple Choices',
  71. 301 => 'Moved Permanently',
  72. 302 => 'Found',
  73. 303 => 'See Other',
  74. 304 => 'Not Modified',
  75. 305 => 'Use Proxy',
  76. 306 => '(Unused)',
  77. 307 => 'Temporary Redirect',
  78. 308 => 'Permanent Redirect',
  79. 400 => 'Bad Request',
  80. 401 => 'Unauthorized',
  81. 402 => 'Payment Required',
  82. 403 => 'Forbidden',
  83. 404 => 'Not Found',
  84. 405 => 'Method Not Allowed',
  85. 406 => 'Not Acceptable',
  86. 407 => 'Proxy Authentication Required',
  87. 408 => 'Request Timeout',
  88. 409 => 'Conflict',
  89. 410 => 'Gone',
  90. 411 => 'Length Required',
  91. 412 => 'Precondition Failed',
  92. 413 => 'Request Entity Too Large',
  93. 414 => 'Request-URI Too Large',
  94. 415 => 'Unsupported Media Type',
  95. 416 => 'Requested range not satisfiable',
  96. 417 => 'Expectation Failed',
  97. 418 => 'I\'m a teapot',
  98. 421 => 'Misdirected Request',
  99. 422 => 'Unprocessable Entity',
  100. 423 => 'Locked',
  101. 424 => 'Failed Dependency',
  102. 425 => 'Unordered Collection',
  103. 426 => 'Upgrade Required',
  104. 428 => 'Precondition Required',
  105. 429 => 'Too Many Requests',
  106. 431 => 'Request Header Fields Too Large',
  107. 444 => 'Connection Closed Without Response',
  108. 451 => 'Unavailable For Legal Reasons',
  109. 499 => 'Client Closed Request',
  110. 500 => 'Internal Server Error',
  111. 501 => 'Not Implemented',
  112. 502 => 'Bad Gateway',
  113. 503 => 'Service Unavailable',
  114. 504 => 'Gateway Timeout',
  115. 505 => 'Unsupported Version',
  116. 506 => 'Variant Also Negotiates',
  117. 507 => 'Insufficient Storage',
  118. 508 => 'Loop Detected',
  119. 510 => 'Not Extended',
  120. 511 => 'Network Authentication Required',
  121. 599 => 'Network Connect Timeout Error',
  122. ];
  123. /**
  124. * Holds type key to mime type mappings for known mime types.
  125. *
  126. * @var array<string, mixed>
  127. */
  128. protected array $_mimeTypes = [
  129. 'html' => ['text/html', '*/*'],
  130. 'json' => 'application/json',
  131. 'xml' => ['application/xml', 'text/xml'],
  132. 'xhtml' => ['application/xhtml+xml', 'application/xhtml', 'text/xhtml'],
  133. 'webp' => 'image/webp',
  134. 'rss' => 'application/rss+xml',
  135. 'ai' => 'application/postscript',
  136. 'bcpio' => 'application/x-bcpio',
  137. 'bin' => 'application/octet-stream',
  138. 'ccad' => 'application/clariscad',
  139. 'cdf' => 'application/x-netcdf',
  140. 'class' => 'application/octet-stream',
  141. 'cpio' => 'application/x-cpio',
  142. 'cpt' => 'application/mac-compactpro',
  143. 'csh' => 'application/x-csh',
  144. 'csv' => ['text/csv', 'application/vnd.ms-excel'],
  145. 'dcr' => 'application/x-director',
  146. 'dir' => 'application/x-director',
  147. 'dms' => 'application/octet-stream',
  148. 'doc' => 'application/msword',
  149. 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  150. 'drw' => 'application/drafting',
  151. 'dvi' => 'application/x-dvi',
  152. 'dwg' => 'application/acad',
  153. 'dxf' => 'application/dxf',
  154. 'dxr' => 'application/x-director',
  155. 'eot' => 'application/vnd.ms-fontobject',
  156. 'eps' => 'application/postscript',
  157. 'exe' => 'application/octet-stream',
  158. 'ez' => 'application/andrew-inset',
  159. 'flv' => 'video/x-flv',
  160. 'gtar' => 'application/x-gtar',
  161. 'gz' => 'application/x-gzip',
  162. 'bz2' => 'application/x-bzip',
  163. '7z' => 'application/x-7z-compressed',
  164. 'hal' => ['application/hal+xml', 'application/vnd.hal+xml'],
  165. 'haljson' => ['application/hal+json', 'application/vnd.hal+json'],
  166. 'halxml' => ['application/hal+xml', 'application/vnd.hal+xml'],
  167. 'hdf' => 'application/x-hdf',
  168. 'hqx' => 'application/mac-binhex40',
  169. 'ico' => 'image/x-icon',
  170. 'ips' => 'application/x-ipscript',
  171. 'ipx' => 'application/x-ipix',
  172. 'js' => 'application/javascript',
  173. 'jsonapi' => 'application/vnd.api+json',
  174. 'latex' => 'application/x-latex',
  175. 'jsonld' => 'application/ld+json',
  176. 'kml' => 'application/vnd.google-earth.kml+xml',
  177. 'kmz' => 'application/vnd.google-earth.kmz',
  178. 'lha' => 'application/octet-stream',
  179. 'lsp' => 'application/x-lisp',
  180. 'lzh' => 'application/octet-stream',
  181. 'man' => 'application/x-troff-man',
  182. 'me' => 'application/x-troff-me',
  183. 'mif' => 'application/vnd.mif',
  184. 'ms' => 'application/x-troff-ms',
  185. 'nc' => 'application/x-netcdf',
  186. 'oda' => 'application/oda',
  187. 'otf' => 'font/otf',
  188. 'pdf' => 'application/pdf',
  189. 'pgn' => 'application/x-chess-pgn',
  190. 'pot' => 'application/vnd.ms-powerpoint',
  191. 'pps' => 'application/vnd.ms-powerpoint',
  192. 'ppt' => 'application/vnd.ms-powerpoint',
  193. 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  194. 'ppz' => 'application/vnd.ms-powerpoint',
  195. 'pre' => 'application/x-freelance',
  196. 'prt' => 'application/pro_eng',
  197. 'ps' => 'application/postscript',
  198. 'roff' => 'application/x-troff',
  199. 'scm' => 'application/x-lotusscreencam',
  200. 'set' => 'application/set',
  201. 'sh' => 'application/x-sh',
  202. 'shar' => 'application/x-shar',
  203. 'sit' => 'application/x-stuffit',
  204. 'skd' => 'application/x-koan',
  205. 'skm' => 'application/x-koan',
  206. 'skp' => 'application/x-koan',
  207. 'skt' => 'application/x-koan',
  208. 'smi' => 'application/smil',
  209. 'smil' => 'application/smil',
  210. 'sol' => 'application/solids',
  211. 'spl' => 'application/x-futuresplash',
  212. 'src' => 'application/x-wais-source',
  213. 'step' => 'application/STEP',
  214. 'stl' => 'application/SLA',
  215. 'stp' => 'application/STEP',
  216. 'sv4cpio' => 'application/x-sv4cpio',
  217. 'sv4crc' => 'application/x-sv4crc',
  218. 'svg' => 'image/svg+xml',
  219. 'svgz' => 'image/svg+xml',
  220. 'swf' => 'application/x-shockwave-flash',
  221. 't' => 'application/x-troff',
  222. 'tar' => 'application/x-tar',
  223. 'tcl' => 'application/x-tcl',
  224. 'tex' => 'application/x-tex',
  225. 'texi' => 'application/x-texinfo',
  226. 'texinfo' => 'application/x-texinfo',
  227. 'tr' => 'application/x-troff',
  228. 'tsp' => 'application/dsptype',
  229. 'ttc' => 'font/ttf',
  230. 'ttf' => 'font/ttf',
  231. 'unv' => 'application/i-deas',
  232. 'ustar' => 'application/x-ustar',
  233. 'vcd' => 'application/x-cdlink',
  234. 'vda' => 'application/vda',
  235. 'xlc' => 'application/vnd.ms-excel',
  236. 'xll' => 'application/vnd.ms-excel',
  237. 'xlm' => 'application/vnd.ms-excel',
  238. 'xls' => 'application/vnd.ms-excel',
  239. 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  240. 'xlw' => 'application/vnd.ms-excel',
  241. 'zip' => 'application/zip',
  242. 'aif' => 'audio/x-aiff',
  243. 'aifc' => 'audio/x-aiff',
  244. 'aiff' => 'audio/x-aiff',
  245. 'au' => 'audio/basic',
  246. 'kar' => 'audio/midi',
  247. 'mid' => 'audio/midi',
  248. 'midi' => 'audio/midi',
  249. 'mp2' => 'audio/mpeg',
  250. 'mp3' => 'audio/mpeg',
  251. 'mpga' => 'audio/mpeg',
  252. 'ogg' => 'audio/ogg',
  253. 'oga' => 'audio/ogg',
  254. 'spx' => 'audio/ogg',
  255. 'ra' => 'audio/x-realaudio',
  256. 'ram' => 'audio/x-pn-realaudio',
  257. 'rm' => 'audio/x-pn-realaudio',
  258. 'rpm' => 'audio/x-pn-realaudio-plugin',
  259. 'snd' => 'audio/basic',
  260. 'tsi' => 'audio/TSP-audio',
  261. 'wav' => 'audio/x-wav',
  262. 'aac' => 'audio/aac',
  263. 'asc' => 'text/plain',
  264. 'c' => 'text/plain',
  265. 'cc' => 'text/plain',
  266. 'css' => 'text/css',
  267. 'etx' => 'text/x-setext',
  268. 'f' => 'text/plain',
  269. 'f90' => 'text/plain',
  270. 'h' => 'text/plain',
  271. 'hh' => 'text/plain',
  272. 'htm' => ['text/html', '*/*'],
  273. 'ics' => 'text/calendar',
  274. 'm' => 'text/plain',
  275. 'rtf' => 'text/rtf',
  276. 'rtx' => 'text/richtext',
  277. 'sgm' => 'text/sgml',
  278. 'sgml' => 'text/sgml',
  279. 'tsv' => 'text/tab-separated-values',
  280. 'tpl' => 'text/template',
  281. 'txt' => 'text/plain',
  282. 'text' => 'text/plain',
  283. 'avi' => 'video/x-msvideo',
  284. 'fli' => 'video/x-fli',
  285. 'mov' => 'video/quicktime',
  286. 'movie' => 'video/x-sgi-movie',
  287. 'mpe' => 'video/mpeg',
  288. 'mpeg' => 'video/mpeg',
  289. 'mpg' => 'video/mpeg',
  290. 'qt' => 'video/quicktime',
  291. 'viv' => 'video/vnd.vivo',
  292. 'vivo' => 'video/vnd.vivo',
  293. 'ogv' => 'video/ogg',
  294. 'webm' => 'video/webm',
  295. 'mp4' => 'video/mp4',
  296. 'm4v' => 'video/mp4',
  297. 'f4v' => 'video/mp4',
  298. 'f4p' => 'video/mp4',
  299. 'm4a' => 'audio/mp4',
  300. 'f4a' => 'audio/mp4',
  301. 'f4b' => 'audio/mp4',
  302. 'gif' => 'image/gif',
  303. 'ief' => 'image/ief',
  304. 'jpg' => 'image/jpeg',
  305. 'jpeg' => 'image/jpeg',
  306. 'jpe' => 'image/jpeg',
  307. 'pbm' => 'image/x-portable-bitmap',
  308. 'pgm' => 'image/x-portable-graymap',
  309. 'png' => 'image/png',
  310. 'pnm' => 'image/x-portable-anymap',
  311. 'ppm' => 'image/x-portable-pixmap',
  312. 'ras' => 'image/cmu-raster',
  313. 'rgb' => 'image/x-rgb',
  314. 'tif' => 'image/tiff',
  315. 'tiff' => 'image/tiff',
  316. 'xbm' => 'image/x-xbitmap',
  317. 'xpm' => 'image/x-xpixmap',
  318. 'xwd' => 'image/x-xwindowdump',
  319. 'psd' => [
  320. 'application/photoshop',
  321. 'application/psd',
  322. 'image/psd',
  323. 'image/x-photoshop',
  324. 'image/photoshop',
  325. 'zz-application/zz-winassoc-psd',
  326. ],
  327. 'ice' => 'x-conference/x-cooltalk',
  328. 'iges' => 'model/iges',
  329. 'igs' => 'model/iges',
  330. 'mesh' => 'model/mesh',
  331. 'msh' => 'model/mesh',
  332. 'silo' => 'model/mesh',
  333. 'vrml' => 'model/vrml',
  334. 'wrl' => 'model/vrml',
  335. 'mime' => 'www/mime',
  336. 'pdb' => 'chemical/x-pdb',
  337. 'xyz' => 'chemical/x-pdb',
  338. 'javascript' => 'application/javascript',
  339. 'form' => 'application/x-www-form-urlencoded',
  340. 'file' => 'multipart/form-data',
  341. 'xhtml-mobile' => 'application/vnd.wap.xhtml+xml',
  342. 'atom' => 'application/atom+xml',
  343. 'amf' => 'application/x-amf',
  344. 'wap' => ['text/vnd.wap.wml', 'text/vnd.wap.wmlscript', 'image/vnd.wap.wbmp'],
  345. 'wml' => 'text/vnd.wap.wml',
  346. 'wmlscript' => 'text/vnd.wap.wmlscript',
  347. 'wbmp' => 'image/vnd.wap.wbmp',
  348. 'woff' => 'application/x-font-woff',
  349. 'appcache' => 'text/cache-manifest',
  350. 'manifest' => 'text/cache-manifest',
  351. 'htc' => 'text/x-component',
  352. 'rdf' => 'application/xml',
  353. 'crx' => 'application/x-chrome-extension',
  354. 'oex' => 'application/x-opera-extension',
  355. 'xpi' => 'application/x-xpinstall',
  356. 'safariextz' => 'application/octet-stream',
  357. 'webapp' => 'application/x-web-app-manifest+json',
  358. 'vcf' => 'text/x-vcard',
  359. 'vtt' => 'text/vtt',
  360. 'mkv' => 'video/x-matroska',
  361. 'pkpass' => 'application/vnd.apple.pkpass',
  362. 'ajax' => 'text/html',
  363. 'bmp' => 'image/bmp',
  364. ];
  365. /**
  366. * Status code to send to the client
  367. *
  368. * @var int
  369. */
  370. protected int $_status = 200;
  371. /**
  372. * File object for file to be read out as response
  373. *
  374. * @var \SplFileInfo|null
  375. */
  376. protected ?SplFileInfo $_file = null;
  377. /**
  378. * File range. Used for requesting ranges of files.
  379. *
  380. * @var array<int>
  381. */
  382. protected array $_fileRange = [];
  383. /**
  384. * The charset the response body is encoded with
  385. *
  386. * @var string
  387. */
  388. protected string $_charset = 'UTF-8';
  389. /**
  390. * Holds all the cache directives that will be converted
  391. * into headers when sending the request
  392. *
  393. * @var array
  394. */
  395. protected array $_cacheDirectives = [];
  396. /**
  397. * Collection of cookies to send to the client
  398. *
  399. * @var \Cake\Http\Cookie\CookieCollection
  400. */
  401. protected CookieCollection $_cookies;
  402. /**
  403. * Reason Phrase
  404. *
  405. * @var string
  406. */
  407. protected string $_reasonPhrase = 'OK';
  408. /**
  409. * Stream mode options.
  410. *
  411. * @var string
  412. */
  413. protected string $_streamMode = 'wb+';
  414. /**
  415. * Stream target or resource object.
  416. *
  417. * @var resource|string
  418. */
  419. protected $_streamTarget = 'php://memory';
  420. /**
  421. * Constructor
  422. *
  423. * @param array<string, mixed> $options list of parameters to setup the response. Possible values are:
  424. *
  425. * - body: the response text that should be sent to the client
  426. * - status: the HTTP status code to respond with
  427. * - type: a complete mime-type string or an extension mapped in this class
  428. * - charset: the charset for the response body
  429. * @throws \InvalidArgumentException
  430. */
  431. public function __construct(array $options = [])
  432. {
  433. $this->_streamTarget = $options['streamTarget'] ?? $this->_streamTarget;
  434. $this->_streamMode = $options['streamMode'] ?? $this->_streamMode;
  435. if (isset($options['stream'])) {
  436. if (!$options['stream'] instanceof StreamInterface) {
  437. throw new InvalidArgumentException('Stream option must be an object that implements StreamInterface');
  438. }
  439. $this->stream = $options['stream'];
  440. } else {
  441. $this->_createStream();
  442. }
  443. if (isset($options['body'])) {
  444. $this->stream->write($options['body']);
  445. }
  446. if (isset($options['status'])) {
  447. $this->_setStatus($options['status']);
  448. }
  449. if (!isset($options['charset'])) {
  450. $options['charset'] = Configure::read('App.encoding');
  451. }
  452. $this->_charset = $options['charset'];
  453. $type = 'text/html';
  454. if (isset($options['type'])) {
  455. $type = $this->resolveType($options['type']);
  456. }
  457. $this->_setContentType($type);
  458. $this->_cookies = new CookieCollection();
  459. }
  460. /**
  461. * Creates the stream object.
  462. *
  463. * @return void
  464. */
  465. protected function _createStream(): void
  466. {
  467. $this->stream = new Stream($this->_streamTarget, $this->_streamMode);
  468. }
  469. /**
  470. * Formats the Content-Type header based on the configured contentType and charset
  471. * the charset will only be set in the header if the response is of type text/*
  472. *
  473. * @param string $type The type to set.
  474. * @return void
  475. */
  476. protected function _setContentType(string $type): void
  477. {
  478. if (in_array($this->_status, [304, 204], true)) {
  479. $this->_clearHeader('Content-Type');
  480. return;
  481. }
  482. $allowed = [
  483. 'application/javascript', 'application/xml', 'application/rss+xml',
  484. ];
  485. $charset = false;
  486. if (
  487. $this->_charset &&
  488. (
  489. str_starts_with($type, 'text/') ||
  490. in_array($type, $allowed, true)
  491. )
  492. ) {
  493. $charset = true;
  494. }
  495. if ($charset && !str_contains($type, ';')) {
  496. $this->_setHeader('Content-Type', "{$type}; charset={$this->_charset}");
  497. } else {
  498. $this->_setHeader('Content-Type', $type);
  499. }
  500. }
  501. /**
  502. * Return an instance with an updated location header.
  503. *
  504. * If the current status code is 200, it will be replaced
  505. * with 302.
  506. *
  507. * @param string $url The location to redirect to.
  508. * @return static A new response with the Location header set.
  509. */
  510. public function withLocation(string $url): static
  511. {
  512. $new = $this->withHeader('Location', $url);
  513. if ($new->_status === 200) {
  514. $new->_status = 302;
  515. }
  516. return $new;
  517. }
  518. /**
  519. * Sets a header.
  520. *
  521. * @phpstan-param non-empty-string $header
  522. * @param string $header Header key.
  523. * @param string $value Header value.
  524. * @return void
  525. */
  526. protected function _setHeader(string $header, string $value): void
  527. {
  528. $normalized = strtolower($header);
  529. $this->headerNames[$normalized] = $header;
  530. $this->headers[$header] = [$value];
  531. }
  532. /**
  533. * Clear header
  534. *
  535. * @phpstan-param non-empty-string $header
  536. * @param string $header Header key.
  537. * @return void
  538. */
  539. protected function _clearHeader(string $header): void
  540. {
  541. $normalized = strtolower($header);
  542. if (!isset($this->headerNames[$normalized])) {
  543. return;
  544. }
  545. $original = $this->headerNames[$normalized];
  546. unset($this->headerNames[$normalized], $this->headers[$original]);
  547. }
  548. /**
  549. * Gets the response status code.
  550. *
  551. * The status code is a 3-digit integer result code of the server's attempt
  552. * to understand and satisfy the request.
  553. *
  554. * @return int Status code.
  555. */
  556. public function getStatusCode(): int
  557. {
  558. return $this->_status;
  559. }
  560. /**
  561. * Return an instance with the specified status code and, optionally, reason phrase.
  562. *
  563. * If no reason phrase is specified, implementations MAY choose to default
  564. * to the RFC 7231 or IANA recommended reason phrase for the response's
  565. * status code.
  566. *
  567. * This method MUST be implemented in such a way as to retain the
  568. * immutability of the message, and MUST return an instance that has the
  569. * updated status and reason phrase.
  570. *
  571. * If the status code is 304 or 204, the existing Content-Type header
  572. * will be cleared, as these response codes have no body.
  573. *
  574. * There are external packages such as `fig/http-message-util` that provide HTTP
  575. * status code constants. These can be used with any method that accepts or
  576. * returns a status code integer. However, keep in mind that these constants
  577. * might include status codes that are now allowed which will throw an
  578. * `\InvalidArgumentException`.
  579. *
  580. * @link https://tools.ietf.org/html/rfc7231#section-6
  581. * @link https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
  582. * @param int $code The 3-digit integer status code to set.
  583. * @param string $reasonPhrase The reason phrase to use with the
  584. * provided status code; if none is provided, implementations MAY
  585. * use the defaults as suggested in the HTTP specification.
  586. * @return static
  587. * @throws \InvalidArgumentException For invalid status code arguments.
  588. * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
  589. */
  590. public function withStatus($code, $reasonPhrase = ''): static
  591. {
  592. $new = clone $this;
  593. $new->_setStatus($code, $reasonPhrase);
  594. return $new;
  595. }
  596. /**
  597. * Modifier for response status
  598. *
  599. * @param int $code The status code to set.
  600. * @param string $reasonPhrase The response reason phrase.
  601. * @return void
  602. * @throws \InvalidArgumentException For invalid status code arguments.
  603. */
  604. protected function _setStatus(int $code, string $reasonPhrase = ''): void
  605. {
  606. if ($code < static::STATUS_CODE_MIN || $code > static::STATUS_CODE_MAX) {
  607. throw new InvalidArgumentException(sprintf(
  608. 'Invalid status code: %s. Use a valid HTTP status code in range 1xx - 5xx.',
  609. $code
  610. ));
  611. }
  612. $this->_status = $code;
  613. if ($reasonPhrase === '' && isset($this->_statusCodes[$code])) {
  614. $reasonPhrase = $this->_statusCodes[$code];
  615. }
  616. $this->_reasonPhrase = $reasonPhrase;
  617. // These status codes don't have bodies and can't have content-types.
  618. if (in_array($code, [304, 204], true)) {
  619. $this->_clearHeader('Content-Type');
  620. }
  621. }
  622. /**
  623. * Gets the response reason phrase associated with the status code.
  624. *
  625. * Because a reason phrase is not a required element in a response
  626. * status line, the reason phrase value MAY be null. Implementations MAY
  627. * choose to return the default RFC 7231 recommended reason phrase (or those
  628. * listed in the IANA HTTP Status Code Registry) for the response's
  629. * status code.
  630. *
  631. * @link https://tools.ietf.org/html/rfc7231#section-6
  632. * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
  633. * @return string Reason phrase; must return an empty string if none present.
  634. */
  635. public function getReasonPhrase(): string
  636. {
  637. return $this->_reasonPhrase;
  638. }
  639. /**
  640. * Sets a content type definition into the map.
  641. *
  642. * E.g.: setTypeMap('xhtml', ['application/xhtml+xml', 'application/xhtml'])
  643. *
  644. * This is needed for RequestHandlerComponent and recognition of types.
  645. *
  646. * @param string $type Content type.
  647. * @param array<string>|string $mimeType Definition of the mime type.
  648. * @return void
  649. */
  650. public function setTypeMap(string $type, array|string $mimeType): void
  651. {
  652. $this->_mimeTypes[$type] = $mimeType;
  653. }
  654. /**
  655. * Returns the current content type.
  656. *
  657. * @return string
  658. */
  659. public function getType(): string
  660. {
  661. $header = $this->getHeaderLine('Content-Type');
  662. if (str_contains($header, ';')) {
  663. return explode(';', $header)[0];
  664. }
  665. return $header;
  666. }
  667. /**
  668. * Get an updated response with the content type set.
  669. *
  670. * If you attempt to set the type on a 304 or 204 status code response, the
  671. * content type will not take effect as these status codes do not have content-types.
  672. *
  673. * @param string $contentType Either a file extension which will be mapped to a mime-type or a concrete mime-type.
  674. * @return static
  675. */
  676. public function withType(string $contentType): static
  677. {
  678. $mappedType = $this->resolveType($contentType);
  679. $new = clone $this;
  680. $new->_setContentType($mappedType);
  681. return $new;
  682. }
  683. /**
  684. * Translate and validate content-types.
  685. *
  686. * @param string $contentType The content-type or type alias.
  687. * @return string The resolved content-type
  688. * @throws \InvalidArgumentException When an invalid content-type or alias is used.
  689. */
  690. protected function resolveType(string $contentType): string
  691. {
  692. $mapped = $this->getMimeType($contentType);
  693. if ($mapped) {
  694. return is_array($mapped) ? current($mapped) : $mapped;
  695. }
  696. if (!str_contains($contentType, '/')) {
  697. throw new InvalidArgumentException(sprintf('"%s" is an invalid content type.', $contentType));
  698. }
  699. return $contentType;
  700. }
  701. /**
  702. * Returns the mime type definition for an alias
  703. *
  704. * e.g `getMimeType('pdf'); // returns 'application/pdf'`
  705. *
  706. * @param string $alias the content type alias to map
  707. * @return array|string|false String mapped mime type or false if $alias is not mapped
  708. */
  709. public function getMimeType(string $alias): array|string|false
  710. {
  711. return $this->_mimeTypes[$alias] ?? false;
  712. }
  713. /**
  714. * Maps a content-type back to an alias
  715. *
  716. * e.g `mapType('application/pdf'); // returns 'pdf'`
  717. *
  718. * @param array|string $ctype Either a string content type to map, or an array of types.
  719. * @return array|string|null Aliases for the types provided.
  720. */
  721. public function mapType(array|string $ctype): array|string|null
  722. {
  723. if (is_array($ctype)) {
  724. return array_map([$this, 'mapType'], $ctype);
  725. }
  726. foreach ($this->_mimeTypes as $alias => $types) {
  727. if (in_array($ctype, (array)$types, true)) {
  728. return $alias;
  729. }
  730. }
  731. return null;
  732. }
  733. /**
  734. * Returns the current charset.
  735. *
  736. * @return string
  737. */
  738. public function getCharset(): string
  739. {
  740. return $this->_charset;
  741. }
  742. /**
  743. * Get a new instance with an updated charset.
  744. *
  745. * @param string $charset Character set string.
  746. * @return static
  747. */
  748. public function withCharset(string $charset): static
  749. {
  750. $new = clone $this;
  751. $new->_charset = $charset;
  752. $new->_setContentType($this->getType());
  753. return $new;
  754. }
  755. /**
  756. * Create a new instance with headers to instruct the client to not cache the response
  757. *
  758. * @return static
  759. */
  760. public function withDisabledCache(): static
  761. {
  762. return $this->withHeader('Expires', 'Mon, 26 Jul 1997 05:00:00 GMT')
  763. ->withHeader('Last-Modified', gmdate(DATE_RFC7231))
  764. ->withHeader('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
  765. }
  766. /**
  767. * Create a new instance with the headers to enable client caching.
  768. *
  769. * @param string|int $since a valid time since the response text has not been modified
  770. * @param string|int $time a valid time for cache expiry
  771. * @return static
  772. */
  773. public function withCache(string|int $since, string|int $time = '+1 day'): static
  774. {
  775. if (!is_int($time)) {
  776. $time = strtotime($time);
  777. if ($time === false) {
  778. throw new InvalidArgumentException(
  779. 'Invalid time parameter. Ensure your time value can be parsed by strtotime'
  780. );
  781. }
  782. }
  783. return $this->withHeader('Date', gmdate(DATE_RFC7231, time()))
  784. ->withModified($since)
  785. ->withExpires($time)
  786. ->withSharable(true)
  787. ->withMaxAge($time - time());
  788. }
  789. /**
  790. * Create a new instace with the public/private Cache-Control directive set.
  791. *
  792. * @param bool $public If set to true, the Cache-Control header will be set as public
  793. * if set to false, the response will be set to private.
  794. * @param int|null $time time in seconds after which the response should no longer be considered fresh.
  795. * @return static
  796. */
  797. public function withSharable(bool $public, ?int $time = null): static
  798. {
  799. $new = clone $this;
  800. unset($new->_cacheDirectives['private'], $new->_cacheDirectives['public']);
  801. $key = $public ? 'public' : 'private';
  802. $new->_cacheDirectives[$key] = true;
  803. if ($time !== null) {
  804. $new->_cacheDirectives['max-age'] = $time;
  805. }
  806. $new->_setCacheControl();
  807. return $new;
  808. }
  809. /**
  810. * Create a new instance with the Cache-Control s-maxage directive.
  811. *
  812. * The max-age is the number of seconds after which the response should no longer be considered
  813. * a good candidate to be fetched from a shared cache (like in a proxy server).
  814. *
  815. * @param int $seconds The number of seconds for shared max-age
  816. * @return static
  817. */
  818. public function withSharedMaxAge(int $seconds): static
  819. {
  820. $new = clone $this;
  821. $new->_cacheDirectives['s-maxage'] = $seconds;
  822. $new->_setCacheControl();
  823. return $new;
  824. }
  825. /**
  826. * Create an instance with Cache-Control max-age directive set.
  827. *
  828. * The max-age is the number of seconds after which the response should no longer be considered
  829. * a good candidate to be fetched from the local (client) cache.
  830. *
  831. * @param int $seconds The seconds a cached response can be considered valid
  832. * @return static
  833. */
  834. public function withMaxAge(int $seconds): static
  835. {
  836. $new = clone $this;
  837. $new->_cacheDirectives['max-age'] = $seconds;
  838. $new->_setCacheControl();
  839. return $new;
  840. }
  841. /**
  842. * Create an instance with Cache-Control must-revalidate directive set.
  843. *
  844. * Sets the Cache-Control must-revalidate directive.
  845. * must-revalidate indicates that the response should not be served
  846. * stale by a cache under any circumstance without first revalidating
  847. * with the origin.
  848. *
  849. * @param bool $enable If boolean sets or unsets the directive.
  850. * @return static
  851. */
  852. public function withMustRevalidate(bool $enable): static
  853. {
  854. $new = clone $this;
  855. if ($enable) {
  856. $new->_cacheDirectives['must-revalidate'] = true;
  857. } else {
  858. unset($new->_cacheDirectives['must-revalidate']);
  859. }
  860. $new->_setCacheControl();
  861. return $new;
  862. }
  863. /**
  864. * Helper method to generate a valid Cache-Control header from the options set
  865. * in other methods
  866. *
  867. * @return void
  868. */
  869. protected function _setCacheControl(): void
  870. {
  871. $control = '';
  872. foreach ($this->_cacheDirectives as $key => $val) {
  873. $control .= $val === true ? $key : sprintf('%s=%s', $key, $val);
  874. $control .= ', ';
  875. }
  876. $control = rtrim($control, ', ');
  877. $this->_setHeader('Cache-Control', $control);
  878. }
  879. /**
  880. * Create a new instance with the Expires header set.
  881. *
  882. * ### Examples:
  883. *
  884. * ```
  885. * // Will Expire the response cache now
  886. * $response->withExpires('now')
  887. *
  888. * // Will set the expiration in next 24 hours
  889. * $response->withExpires(new DateTime('+1 day'))
  890. * ```
  891. *
  892. * @param \DateTimeInterface|string|int|null $time Valid time string or \DateTime instance.
  893. * @return static
  894. */
  895. public function withExpires(DateTimeInterface|string|int|null $time): static
  896. {
  897. $date = $this->_getUTCDate($time);
  898. return $this->withHeader('Expires', $date->format(DATE_RFC7231));
  899. }
  900. /**
  901. * Create a new instance with the Last-Modified header set.
  902. *
  903. * ### Examples:
  904. *
  905. * ```
  906. * // Will Expire the response cache now
  907. * $response->withModified('now')
  908. *
  909. * // Will set the expiration in next 24 hours
  910. * $response->withModified(new DateTime('+1 day'))
  911. * ```
  912. *
  913. * @param \DateTimeInterface|string|int $time Valid time string or \DateTime instance.
  914. * @return static
  915. */
  916. public function withModified(DateTimeInterface|string|int $time): static
  917. {
  918. $date = $this->_getUTCDate($time);
  919. return $this->withHeader('Last-Modified', $date->format(DATE_RFC7231));
  920. }
  921. /**
  922. * Sets the response as Not Modified by removing any body contents
  923. * setting the status code to "304 Not Modified" and removing all
  924. * conflicting headers
  925. *
  926. * *Warning* This method mutates the response in-place and should be avoided.
  927. *
  928. * @deprecated 4.4.0 Use `withNotModified()` instead.
  929. * @return void
  930. */
  931. public function notModified(): void
  932. {
  933. deprecationWarning(
  934. 'The `notModified()` method is deprecated. ' .
  935. 'Use `withNotModified() instead, and remember immutability of with* methods.'
  936. );
  937. $this->_createStream();
  938. $this->_setStatus(304);
  939. $remove = [
  940. 'Allow',
  941. 'Content-Encoding',
  942. 'Content-Language',
  943. 'Content-Length',
  944. 'Content-MD5',
  945. 'Content-Type',
  946. 'Last-Modified',
  947. ];
  948. foreach ($remove as $header) {
  949. $this->_clearHeader($header);
  950. }
  951. }
  952. /**
  953. * Create a new instance as 'not modified'
  954. *
  955. * This will remove any body contents set the status code
  956. * to "304" and removing headers that describe
  957. * a response body.
  958. *
  959. * @return static
  960. */
  961. public function withNotModified(): static
  962. {
  963. $new = $this->withStatus(304);
  964. $new->_createStream();
  965. $remove = [
  966. 'Allow',
  967. 'Content-Encoding',
  968. 'Content-Language',
  969. 'Content-Length',
  970. 'Content-MD5',
  971. 'Content-Type',
  972. 'Last-Modified',
  973. ];
  974. foreach ($remove as $header) {
  975. $new = $new->withoutHeader($header);
  976. }
  977. return $new;
  978. }
  979. /**
  980. * Create a new instance with the Vary header set.
  981. *
  982. * If an array is passed values will be imploded into a comma
  983. * separated string. If no parameters are passed, then an
  984. * array with the current Vary header value is returned
  985. *
  986. * @param array<string>|string $cacheVariances A single Vary string or an array
  987. * containing the list for variances.
  988. * @return static
  989. */
  990. public function withVary(array|string $cacheVariances): static
  991. {
  992. return $this->withHeader('Vary', (array)$cacheVariances);
  993. }
  994. /**
  995. * Create a new instance with the Etag header set.
  996. *
  997. * Etags are a strong indicative that a response can be cached by a
  998. * HTTP client. A bad way of generating Etags is creating a hash of
  999. * the response output, instead generate a unique hash of the
  1000. * unique components that identifies a request, such as a
  1001. * modification time, a resource Id, and anything else you consider it
  1002. * that makes the response unique.
  1003. *
  1004. * The second parameter is used to inform clients that the content has
  1005. * changed, but semantically it is equivalent to existing cached values. Consider
  1006. * a page with a hit counter, two different page views are equivalent, but
  1007. * they differ by a few bytes. This permits the Client to decide whether they should
  1008. * use the cached data.
  1009. *
  1010. * @param string $hash The unique hash that identifies this response
  1011. * @param bool $weak Whether the response is semantically the same as
  1012. * other with the same hash or not. Defaults to false
  1013. * @return static
  1014. */
  1015. public function withEtag(string $hash, bool $weak = false): static
  1016. {
  1017. $hash = sprintf('%s"%s"', $weak ? 'W/' : '', $hash);
  1018. return $this->withHeader('Etag', $hash);
  1019. }
  1020. /**
  1021. * Returns a DateTime object initialized at the $time param and using UTC
  1022. * as timezone
  1023. *
  1024. * @param \DateTimeInterface|string|int|null $time Valid time string or \DateTimeInterface instance.
  1025. * @return \DateTimeInterface
  1026. */
  1027. protected function _getUTCDate(DateTimeInterface|string|int|null $time = null): DateTimeInterface
  1028. {
  1029. if ($time instanceof DateTimeInterface) {
  1030. $result = clone $time;
  1031. } elseif (is_int($time)) {
  1032. $result = new DateTime(date('Y-m-d H:i:s', $time));
  1033. } else {
  1034. $result = new DateTime($time ?? 'now');
  1035. }
  1036. /** @psalm-suppress UndefinedInterfaceMethod */
  1037. return $result->setTimezone(new DateTimeZone('UTC'));
  1038. }
  1039. /**
  1040. * Sets the correct output buffering handler to send a compressed response. Responses will
  1041. * be compressed with zlib, if the extension is available.
  1042. *
  1043. * @return bool false if client does not accept compressed responses or no handler is available, true otherwise
  1044. */
  1045. public function compress(): bool
  1046. {
  1047. return ini_get('zlib.output_compression') !== '1' &&
  1048. extension_loaded('zlib') &&
  1049. str_contains((string)env('HTTP_ACCEPT_ENCODING'), 'gzip') &&
  1050. ob_start('ob_gzhandler');
  1051. }
  1052. /**
  1053. * Returns whether the resulting output will be compressed by PHP
  1054. *
  1055. * @return bool
  1056. */
  1057. public function outputCompressed(): bool
  1058. {
  1059. return str_contains((string)env('HTTP_ACCEPT_ENCODING'), 'gzip')
  1060. && (ini_get('zlib.output_compression') === '1' || in_array('ob_gzhandler', ob_list_handlers(), true));
  1061. }
  1062. /**
  1063. * Create a new instance with the Content-Disposition header set.
  1064. *
  1065. * @param string $filename The name of the file as the browser will download the response
  1066. * @return static
  1067. */
  1068. public function withDownload(string $filename): static
  1069. {
  1070. return $this->withHeader('Content-Disposition', 'attachment; filename="' . $filename . '"');
  1071. }
  1072. /**
  1073. * Create a new response with the Content-Length header set.
  1074. *
  1075. * @param string|int $bytes Number of bytes
  1076. * @return static
  1077. */
  1078. public function withLength(string|int $bytes): static
  1079. {
  1080. return $this->withHeader('Content-Length', (string)$bytes);
  1081. }
  1082. /**
  1083. * Create a new response with the Link header set.
  1084. *
  1085. * ### Examples
  1086. *
  1087. * ```
  1088. * $response = $response->withAddedLink('http://example.com?page=1', ['rel' => 'prev'])
  1089. * ->withAddedLink('http://example.com?page=3', ['rel' => 'next']);
  1090. * ```
  1091. *
  1092. * Will generate:
  1093. *
  1094. * ```
  1095. * Link: <http://example.com?page=1>; rel="prev"
  1096. * Link: <http://example.com?page=3>; rel="next"
  1097. * ```
  1098. *
  1099. * @param string $url The LinkHeader url.
  1100. * @param array<string, mixed> $options The LinkHeader params.
  1101. * @return static
  1102. * @since 3.6.0
  1103. */
  1104. public function withAddedLink(string $url, array $options = []): static
  1105. {
  1106. $params = [];
  1107. foreach ($options as $key => $option) {
  1108. $params[] = $key . '="' . $option . '"';
  1109. }
  1110. $param = '';
  1111. if ($params) {
  1112. $param = '; ' . implode('; ', $params);
  1113. }
  1114. return $this->withAddedHeader('Link', '<' . $url . '>' . $param);
  1115. }
  1116. /**
  1117. * Checks whether a response has not been modified according to the 'If-None-Match'
  1118. * (Etags) and 'If-Modified-Since' (last modification date) request
  1119. * headers.
  1120. *
  1121. * In order to interact with this method you must mark responses as not modified.
  1122. * You need to set at least one of the `Last-Modified` or `Etag` response headers
  1123. * before calling this method. Otherwise a comparison will not be possible.
  1124. *
  1125. * @param \Cake\Http\ServerRequest $request Request object
  1126. * @return bool Whether the response is 'modified' based on cache headers.
  1127. */
  1128. public function isNotModified(ServerRequest $request): bool
  1129. {
  1130. $etags = preg_split('/\s*,\s*/', $request->getHeaderLine('If-None-Match'), 0, PREG_SPLIT_NO_EMPTY);
  1131. $responseTag = $this->getHeaderLine('Etag');
  1132. $etagMatches = null;
  1133. if ($responseTag) {
  1134. $etagMatches = in_array('*', $etags, true) || in_array($responseTag, $etags, true);
  1135. }
  1136. $modifiedSince = $request->getHeaderLine('If-Modified-Since');
  1137. $timeMatches = null;
  1138. if ($modifiedSince && $this->hasHeader('Last-Modified')) {
  1139. $timeMatches = strtotime($this->getHeaderLine('Last-Modified')) === strtotime($modifiedSince);
  1140. }
  1141. if ($etagMatches === null && $timeMatches === null) {
  1142. return false;
  1143. }
  1144. return $etagMatches !== false && $timeMatches !== false;
  1145. }
  1146. /**
  1147. * Checks whether a response has not been modified according to the 'If-None-Match'
  1148. * (Etags) and 'If-Modified-Since' (last modification date) request
  1149. * headers. If the response is detected to be not modified, it
  1150. * is marked as so accordingly so the client can be informed of that.
  1151. *
  1152. * In order to mark a response as not modified, you need to set at least
  1153. * the Last-Modified etag response header before calling this method. Otherwise
  1154. * a comparison will not be possible.
  1155. *
  1156. * *Warning* This method mutates the response in-place and should be avoided.
  1157. *
  1158. * @param \Cake\Http\ServerRequest $request Request object
  1159. * @return bool Whether the response was marked as not modified or not.
  1160. * @deprecated 4.4.0 Use `isNotModified()` and `withNotModified()` instead.
  1161. */
  1162. public function checkNotModified(ServerRequest $request): bool
  1163. {
  1164. deprecationWarning(
  1165. 'The `checkNotModified()` method is deprecated. ' .
  1166. 'Use `isNotModified() instead and `withNoModified()` instead.'
  1167. );
  1168. if ($this->isNotModified($request)) {
  1169. $this->notModified();
  1170. return true;
  1171. }
  1172. return false;
  1173. }
  1174. /**
  1175. * String conversion. Fetches the response body as a string.
  1176. * Does *not* send headers.
  1177. * If body is a callable, a blank string is returned.
  1178. *
  1179. * @return string
  1180. */
  1181. public function __toString(): string
  1182. {
  1183. $this->stream->rewind();
  1184. return $this->stream->getContents();
  1185. }
  1186. /**
  1187. * Create a new response with a cookie set.
  1188. *
  1189. * ### Example
  1190. *
  1191. * ```
  1192. * // add a cookie object
  1193. * $response = $response->withCookie(new Cookie('remember_me', 1));
  1194. * ```
  1195. *
  1196. * @param \Cake\Http\Cookie\CookieInterface $cookie cookie object
  1197. * @return static
  1198. */
  1199. public function withCookie(CookieInterface $cookie): static
  1200. {
  1201. $new = clone $this;
  1202. $new->_cookies = $new->_cookies->add($cookie);
  1203. return $new;
  1204. }
  1205. /**
  1206. * Create a new response with an expired cookie set.
  1207. *
  1208. * ### Example
  1209. *
  1210. * ```
  1211. * // add a cookie object
  1212. * $response = $response->withExpiredCookie(new Cookie('remember_me'));
  1213. * ```
  1214. *
  1215. * @param \Cake\Http\Cookie\CookieInterface $cookie cookie object
  1216. * @return static
  1217. */
  1218. public function withExpiredCookie(CookieInterface $cookie): static
  1219. {
  1220. $cookie = $cookie->withExpired();
  1221. $new = clone $this;
  1222. $new->_cookies = $new->_cookies->add($cookie);
  1223. return $new;
  1224. }
  1225. /**
  1226. * Read a single cookie from the response.
  1227. *
  1228. * This method provides read access to pending cookies. It will
  1229. * not read the `Set-Cookie` header if set.
  1230. *
  1231. * @param string $name The cookie name you want to read.
  1232. * @return array|null Either the cookie data or null
  1233. */
  1234. public function getCookie(string $name): ?array
  1235. {
  1236. if (!$this->_cookies->has($name)) {
  1237. return null;
  1238. }
  1239. return $this->_cookies->get($name)->toArray();
  1240. }
  1241. /**
  1242. * Get all cookies in the response.
  1243. *
  1244. * Returns an associative array of cookie name => cookie data.
  1245. *
  1246. * @return array
  1247. */
  1248. public function getCookies(): array
  1249. {
  1250. $out = [];
  1251. /** @var array<\Cake\Http\Cookie\Cookie> $cookies */
  1252. $cookies = $this->_cookies;
  1253. foreach ($cookies as $cookie) {
  1254. $out[$cookie->getName()] = $cookie->toArray();
  1255. }
  1256. return $out;
  1257. }
  1258. /**
  1259. * Get the CookieCollection from the response
  1260. *
  1261. * @return \Cake\Http\Cookie\CookieCollection
  1262. */
  1263. public function getCookieCollection(): CookieCollection
  1264. {
  1265. return $this->_cookies;
  1266. }
  1267. /**
  1268. * Get a new instance with provided cookie collection.
  1269. *
  1270. * @param \Cake\Http\Cookie\CookieCollection $cookieCollection Cookie collection to set.
  1271. * @return static
  1272. */
  1273. public function withCookieCollection(CookieCollection $cookieCollection): static
  1274. {
  1275. $new = clone $this;
  1276. $new->_cookies = $cookieCollection;
  1277. return $new;
  1278. }
  1279. /**
  1280. * Get a CorsBuilder instance for defining CORS headers.
  1281. *
  1282. * This method allow multiple ways to setup the domains, see the examples
  1283. *
  1284. * ### Full URI
  1285. * ```
  1286. * cors($request, 'https://www.cakephp.org');
  1287. * ```
  1288. *
  1289. * ### URI with wildcard
  1290. * ```
  1291. * cors($request, 'https://*.cakephp.org');
  1292. * ```
  1293. *
  1294. * ### Ignoring the requested protocol
  1295. * ```
  1296. * cors($request, 'www.cakephp.org');
  1297. * ```
  1298. *
  1299. * ### Any URI
  1300. * ```
  1301. * cors($request, '*');
  1302. * ```
  1303. *
  1304. * ### Allowed list of URIs
  1305. * ```
  1306. * cors($request, ['http://www.cakephp.org', '*.google.com', 'https://myproject.github.io']);
  1307. * ```
  1308. *
  1309. * *Note* The `$allowedDomains`, `$allowedMethods`, `$allowedHeaders` parameters are deprecated.
  1310. * Instead the builder object should be used.
  1311. *
  1312. * @param \Cake\Http\ServerRequest $request Request object
  1313. * @return \Cake\Http\CorsBuilder A builder object the provides a fluent interface for defining
  1314. * additional CORS headers.
  1315. */
  1316. public function cors(ServerRequest $request): CorsBuilder
  1317. {
  1318. $origin = $request->getHeaderLine('Origin');
  1319. $ssl = $request->is('ssl');
  1320. return new CorsBuilder($this, $origin, $ssl);
  1321. }
  1322. /**
  1323. * Create a new instance that is based on a file.
  1324. *
  1325. * This method will augment both the body and a number of related headers.
  1326. *
  1327. * If `$_SERVER['HTTP_RANGE']` is set, a slice of the file will be
  1328. * returned instead of the entire file.
  1329. *
  1330. * ### Options keys
  1331. *
  1332. * - name: Alternate download name
  1333. * - download: If `true` sets download header and forces file to
  1334. * be downloaded rather than displayed inline.
  1335. *
  1336. * @param string $path Absolute path to file.
  1337. * @param array<string, mixed> $options Options See above.
  1338. * @return static
  1339. * @throws \Cake\Http\Exception\NotFoundException
  1340. */
  1341. public function withFile(string $path, array $options = []): static
  1342. {
  1343. $file = $this->validateFile($path);
  1344. $options += [
  1345. 'name' => null,
  1346. 'download' => null,
  1347. ];
  1348. $extension = strtolower($file->getExtension());
  1349. $mapped = $this->getMimeType($extension);
  1350. if ((!$extension || !$mapped) && $options['download'] === null) {
  1351. $options['download'] = true;
  1352. }
  1353. $new = clone $this;
  1354. if ($mapped) {
  1355. $new = $new->withType($extension);
  1356. }
  1357. $fileSize = $file->getSize();
  1358. if ($options['download']) {
  1359. $agent = (string)env('HTTP_USER_AGENT');
  1360. if ($agent && preg_match('%Opera([/ ])([0-9].[0-9]{1,2})%', $agent)) {
  1361. $contentType = 'application/octet-stream';
  1362. } elseif ($agent && preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) {
  1363. $contentType = 'application/force-download';
  1364. }
  1365. if (isset($contentType)) {
  1366. $new = $new->withType($contentType);
  1367. }
  1368. $name = $options['name'] ?: $file->getFileName();
  1369. $new = $new->withDownload($name)
  1370. ->withHeader('Content-Transfer-Encoding', 'binary');
  1371. }
  1372. $new = $new->withHeader('Accept-Ranges', 'bytes');
  1373. $httpRange = (string)env('HTTP_RANGE');
  1374. if ($httpRange) {
  1375. $new->_fileRange($file, $httpRange);
  1376. } else {
  1377. $new = $new->withHeader('Content-Length', (string)$fileSize);
  1378. }
  1379. $new->_file = $file;
  1380. $new->stream = new Stream($file->getPathname(), 'rb');
  1381. return $new;
  1382. }
  1383. /**
  1384. * Convenience method to set a string into the response body
  1385. *
  1386. * @param string|null $string The string to be sent
  1387. * @return static
  1388. */
  1389. public function withStringBody(?string $string): static
  1390. {
  1391. $new = clone $this;
  1392. $new->_createStream();
  1393. $new->stream->write((string)$string);
  1394. return $new;
  1395. }
  1396. /**
  1397. * Validate a file path is a valid response body.
  1398. *
  1399. * @param string $path The path to the file.
  1400. * @throws \Cake\Http\Exception\NotFoundException
  1401. * @return \SplFileInfo
  1402. */
  1403. protected function validateFile(string $path): SplFileInfo
  1404. {
  1405. if (str_contains($path, '../') || str_contains($path, '..\\')) {
  1406. throw new NotFoundException(__d('cake', 'The requested file contains `..` and will not be read.'));
  1407. }
  1408. $file = new SplFileInfo($path);
  1409. if (!$file->isFile() || !$file->isReadable()) {
  1410. if (Configure::read('debug')) {
  1411. throw new NotFoundException(sprintf('The requested file %s was not found or not readable', $path));
  1412. }
  1413. throw new NotFoundException(__d('cake', 'The requested file was not found'));
  1414. }
  1415. return $file;
  1416. }
  1417. /**
  1418. * Get the current file if one exists.
  1419. *
  1420. * @return \SplFileInfo|null The file to use in the response or null
  1421. */
  1422. public function getFile(): ?SplFileInfo
  1423. {
  1424. return $this->_file;
  1425. }
  1426. /**
  1427. * Apply a file range to a file and set the end offset.
  1428. *
  1429. * If an invalid range is requested a 416 Status code will be used
  1430. * in the response.
  1431. *
  1432. * @param \SplFileInfo $file The file to set a range on.
  1433. * @param string $httpRange The range to use.
  1434. * @return void
  1435. */
  1436. protected function _fileRange(SplFileInfo $file, string $httpRange): void
  1437. {
  1438. $fileSize = $file->getSize();
  1439. $lastByte = $fileSize - 1;
  1440. $start = 0;
  1441. $end = $lastByte;
  1442. preg_match('/^bytes\s*=\s*(\d+)?\s*-\s*(\d+)?$/', $httpRange, $matches);
  1443. if ($matches) {
  1444. $start = $matches[1];
  1445. $end = $matches[2] ?? '';
  1446. }
  1447. if ($start === '') {
  1448. $start = $fileSize - (int)$end;
  1449. $end = $lastByte;
  1450. }
  1451. if ($end === '') {
  1452. $end = $lastByte;
  1453. }
  1454. if ($start > $end || $end > $lastByte || $start > $lastByte) {
  1455. $this->_setStatus(416);
  1456. $this->_setHeader('Content-Range', 'bytes 0-' . $lastByte . '/' . $fileSize);
  1457. return;
  1458. }
  1459. /** @psalm-suppress PossiblyInvalidOperand */
  1460. $this->_setHeader('Content-Length', (string)($end - $start + 1));
  1461. $this->_setHeader('Content-Range', 'bytes ' . $start . '-' . $end . '/' . $fileSize);
  1462. $this->_setStatus(206);
  1463. /**
  1464. * @var int $start
  1465. * @var int $end
  1466. */
  1467. $this->_fileRange = [$start, $end];
  1468. }
  1469. /**
  1470. * Returns an array that can be used to describe the internal state of this
  1471. * object.
  1472. *
  1473. * @return array<string, mixed>
  1474. */
  1475. public function __debugInfo(): array
  1476. {
  1477. return [
  1478. 'status' => $this->_status,
  1479. 'contentType' => $this->getType(),
  1480. 'headers' => $this->headers,
  1481. 'file' => $this->_file,
  1482. 'fileRange' => $this->_fileRange,
  1483. 'cookies' => $this->_cookies,
  1484. 'cacheDirectives' => $this->_cacheDirectives,
  1485. 'body' => (string)$this->getBody(),
  1486. ];
  1487. }
  1488. }