Response.php 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 2.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Http;
  16. use Cake\Core\Configure;
  17. use Cake\Filesystem\File;
  18. use Cake\Filesystem\Folder;
  19. use Cake\Http\Cookie\Cookie;
  20. use Cake\Http\Cookie\CookieCollection;
  21. use Cake\Http\Cookie\CookieInterface;
  22. use Cake\Http\CorsBuilder;
  23. use Cake\Http\Exception\NotFoundException;
  24. use Cake\Log\Log;
  25. use DateTime;
  26. use DateTimeInterface;
  27. use DateTimeZone;
  28. use InvalidArgumentException;
  29. use Psr\Http\Message\ResponseInterface;
  30. use Psr\Http\Message\StreamInterface;
  31. use Zend\Diactoros\MessageTrait;
  32. use Zend\Diactoros\Stream;
  33. /**
  34. * Responses contain the response text, status and headers of a HTTP response.
  35. */
  36. class Response implements ResponseInterface
  37. {
  38. use MessageTrait;
  39. /**
  40. * Holds HTTP response statuses
  41. *
  42. * @var array
  43. */
  44. protected $_statusCodes = [
  45. 100 => 'Continue',
  46. 101 => 'Switching Protocols',
  47. 102 => 'Processing',
  48. 200 => 'OK',
  49. 201 => 'Created',
  50. 202 => 'Accepted',
  51. 203 => 'Non-Authoritative Information',
  52. 204 => 'No Content',
  53. 205 => 'Reset Content',
  54. 206 => 'Partial Content',
  55. 207 => 'Multi-status',
  56. 208 => 'Already Reported',
  57. 226 => 'IM used',
  58. 300 => 'Multiple Choices',
  59. 301 => 'Moved Permanently',
  60. 302 => 'Found',
  61. 303 => 'See Other',
  62. 304 => 'Not Modified',
  63. 305 => 'Use Proxy',
  64. 306 => '(Unused)',
  65. 307 => 'Temporary Redirect',
  66. 308 => 'Permanent Redirect',
  67. 400 => 'Bad Request',
  68. 401 => 'Unauthorized',
  69. 402 => 'Payment Required',
  70. 403 => 'Forbidden',
  71. 404 => 'Not Found',
  72. 405 => 'Method Not Allowed',
  73. 406 => 'Not Acceptable',
  74. 407 => 'Proxy Authentication Required',
  75. 408 => 'Request Timeout',
  76. 409 => 'Conflict',
  77. 410 => 'Gone',
  78. 411 => 'Length Required',
  79. 412 => 'Precondition Failed',
  80. 413 => 'Request Entity Too Large',
  81. 414 => 'Request-URI Too Large',
  82. 415 => 'Unsupported Media Type',
  83. 416 => 'Requested range not satisfiable',
  84. 417 => 'Expectation Failed',
  85. 418 => 'I\'m a teapot',
  86. 421 => 'Misdirected Request',
  87. 422 => 'Unprocessable Entity',
  88. 423 => 'Locked',
  89. 424 => 'Failed Dependency',
  90. 425 => 'Unordered Collection',
  91. 426 => 'Upgrade Required',
  92. 428 => 'Precondition Required',
  93. 429 => 'Too Many Requests',
  94. 431 => 'Request Header Fields Too Large',
  95. 444 => 'Connection Closed Without Response',
  96. 451 => 'Unavailable For Legal Reasons',
  97. 499 => 'Client Closed Request',
  98. 500 => 'Internal Server Error',
  99. 501 => 'Not Implemented',
  100. 502 => 'Bad Gateway',
  101. 503 => 'Service Unavailable',
  102. 504 => 'Gateway Timeout',
  103. 505 => 'Unsupported Version',
  104. 506 => 'Variant Also Negotiates',
  105. 507 => 'Insufficient Storage',
  106. 508 => 'Loop Detected',
  107. 510 => 'Not Extended',
  108. 511 => 'Network Authentication Required',
  109. 599 => 'Network Connect Timeout Error',
  110. ];
  111. /**
  112. * Holds type key to mime type mappings for known mime types.
  113. *
  114. * @var array
  115. */
  116. protected $_mimeTypes = [
  117. 'html' => ['text/html', '*/*'],
  118. 'json' => 'application/json',
  119. 'xml' => ['application/xml', 'text/xml'],
  120. 'xhtml' => ['application/xhtml+xml', 'application/xhtml', 'text/xhtml'],
  121. 'webp' => 'image/webp',
  122. 'rss' => 'application/rss+xml',
  123. 'ai' => 'application/postscript',
  124. 'bcpio' => 'application/x-bcpio',
  125. 'bin' => 'application/octet-stream',
  126. 'ccad' => 'application/clariscad',
  127. 'cdf' => 'application/x-netcdf',
  128. 'class' => 'application/octet-stream',
  129. 'cpio' => 'application/x-cpio',
  130. 'cpt' => 'application/mac-compactpro',
  131. 'csh' => 'application/x-csh',
  132. 'csv' => ['text/csv', 'application/vnd.ms-excel'],
  133. 'dcr' => 'application/x-director',
  134. 'dir' => 'application/x-director',
  135. 'dms' => 'application/octet-stream',
  136. 'doc' => 'application/msword',
  137. 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  138. 'drw' => 'application/drafting',
  139. 'dvi' => 'application/x-dvi',
  140. 'dwg' => 'application/acad',
  141. 'dxf' => 'application/dxf',
  142. 'dxr' => 'application/x-director',
  143. 'eot' => 'application/vnd.ms-fontobject',
  144. 'eps' => 'application/postscript',
  145. 'exe' => 'application/octet-stream',
  146. 'ez' => 'application/andrew-inset',
  147. 'flv' => 'video/x-flv',
  148. 'gtar' => 'application/x-gtar',
  149. 'gz' => 'application/x-gzip',
  150. 'bz2' => 'application/x-bzip',
  151. '7z' => 'application/x-7z-compressed',
  152. 'hdf' => 'application/x-hdf',
  153. 'hqx' => 'application/mac-binhex40',
  154. 'ico' => 'image/x-icon',
  155. 'ips' => 'application/x-ipscript',
  156. 'ipx' => 'application/x-ipix',
  157. 'js' => 'application/javascript',
  158. 'jsonapi' => 'application/vnd.api+json',
  159. 'latex' => 'application/x-latex',
  160. 'lha' => 'application/octet-stream',
  161. 'lsp' => 'application/x-lisp',
  162. 'lzh' => 'application/octet-stream',
  163. 'man' => 'application/x-troff-man',
  164. 'me' => 'application/x-troff-me',
  165. 'mif' => 'application/vnd.mif',
  166. 'ms' => 'application/x-troff-ms',
  167. 'nc' => 'application/x-netcdf',
  168. 'oda' => 'application/oda',
  169. 'otf' => 'font/otf',
  170. 'pdf' => 'application/pdf',
  171. 'pgn' => 'application/x-chess-pgn',
  172. 'pot' => 'application/vnd.ms-powerpoint',
  173. 'pps' => 'application/vnd.ms-powerpoint',
  174. 'ppt' => 'application/vnd.ms-powerpoint',
  175. 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  176. 'ppz' => 'application/vnd.ms-powerpoint',
  177. 'pre' => 'application/x-freelance',
  178. 'prt' => 'application/pro_eng',
  179. 'ps' => 'application/postscript',
  180. 'roff' => 'application/x-troff',
  181. 'scm' => 'application/x-lotusscreencam',
  182. 'set' => 'application/set',
  183. 'sh' => 'application/x-sh',
  184. 'shar' => 'application/x-shar',
  185. 'sit' => 'application/x-stuffit',
  186. 'skd' => 'application/x-koan',
  187. 'skm' => 'application/x-koan',
  188. 'skp' => 'application/x-koan',
  189. 'skt' => 'application/x-koan',
  190. 'smi' => 'application/smil',
  191. 'smil' => 'application/smil',
  192. 'sol' => 'application/solids',
  193. 'spl' => 'application/x-futuresplash',
  194. 'src' => 'application/x-wais-source',
  195. 'step' => 'application/STEP',
  196. 'stl' => 'application/SLA',
  197. 'stp' => 'application/STEP',
  198. 'sv4cpio' => 'application/x-sv4cpio',
  199. 'sv4crc' => 'application/x-sv4crc',
  200. 'svg' => 'image/svg+xml',
  201. 'svgz' => 'image/svg+xml',
  202. 'swf' => 'application/x-shockwave-flash',
  203. 't' => 'application/x-troff',
  204. 'tar' => 'application/x-tar',
  205. 'tcl' => 'application/x-tcl',
  206. 'tex' => 'application/x-tex',
  207. 'texi' => 'application/x-texinfo',
  208. 'texinfo' => 'application/x-texinfo',
  209. 'tr' => 'application/x-troff',
  210. 'tsp' => 'application/dsptype',
  211. 'ttc' => 'font/ttf',
  212. 'ttf' => 'font/ttf',
  213. 'unv' => 'application/i-deas',
  214. 'ustar' => 'application/x-ustar',
  215. 'vcd' => 'application/x-cdlink',
  216. 'vda' => 'application/vda',
  217. 'xlc' => 'application/vnd.ms-excel',
  218. 'xll' => 'application/vnd.ms-excel',
  219. 'xlm' => 'application/vnd.ms-excel',
  220. 'xls' => 'application/vnd.ms-excel',
  221. 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  222. 'xlw' => 'application/vnd.ms-excel',
  223. 'zip' => 'application/zip',
  224. 'aif' => 'audio/x-aiff',
  225. 'aifc' => 'audio/x-aiff',
  226. 'aiff' => 'audio/x-aiff',
  227. 'au' => 'audio/basic',
  228. 'kar' => 'audio/midi',
  229. 'mid' => 'audio/midi',
  230. 'midi' => 'audio/midi',
  231. 'mp2' => 'audio/mpeg',
  232. 'mp3' => 'audio/mpeg',
  233. 'mpga' => 'audio/mpeg',
  234. 'ogg' => 'audio/ogg',
  235. 'oga' => 'audio/ogg',
  236. 'spx' => 'audio/ogg',
  237. 'ra' => 'audio/x-realaudio',
  238. 'ram' => 'audio/x-pn-realaudio',
  239. 'rm' => 'audio/x-pn-realaudio',
  240. 'rpm' => 'audio/x-pn-realaudio-plugin',
  241. 'snd' => 'audio/basic',
  242. 'tsi' => 'audio/TSP-audio',
  243. 'wav' => 'audio/x-wav',
  244. 'aac' => 'audio/aac',
  245. 'asc' => 'text/plain',
  246. 'c' => 'text/plain',
  247. 'cc' => 'text/plain',
  248. 'css' => 'text/css',
  249. 'etx' => 'text/x-setext',
  250. 'f' => 'text/plain',
  251. 'f90' => 'text/plain',
  252. 'h' => 'text/plain',
  253. 'hh' => 'text/plain',
  254. 'htm' => ['text/html', '*/*'],
  255. 'ics' => 'text/calendar',
  256. 'm' => 'text/plain',
  257. 'rtf' => 'text/rtf',
  258. 'rtx' => 'text/richtext',
  259. 'sgm' => 'text/sgml',
  260. 'sgml' => 'text/sgml',
  261. 'tsv' => 'text/tab-separated-values',
  262. 'tpl' => 'text/template',
  263. 'txt' => 'text/plain',
  264. 'text' => 'text/plain',
  265. 'avi' => 'video/x-msvideo',
  266. 'fli' => 'video/x-fli',
  267. 'mov' => 'video/quicktime',
  268. 'movie' => 'video/x-sgi-movie',
  269. 'mpe' => 'video/mpeg',
  270. 'mpeg' => 'video/mpeg',
  271. 'mpg' => 'video/mpeg',
  272. 'qt' => 'video/quicktime',
  273. 'viv' => 'video/vnd.vivo',
  274. 'vivo' => 'video/vnd.vivo',
  275. 'ogv' => 'video/ogg',
  276. 'webm' => 'video/webm',
  277. 'mp4' => 'video/mp4',
  278. 'm4v' => 'video/mp4',
  279. 'f4v' => 'video/mp4',
  280. 'f4p' => 'video/mp4',
  281. 'm4a' => 'audio/mp4',
  282. 'f4a' => 'audio/mp4',
  283. 'f4b' => 'audio/mp4',
  284. 'gif' => 'image/gif',
  285. 'ief' => 'image/ief',
  286. 'jpg' => 'image/jpeg',
  287. 'jpeg' => 'image/jpeg',
  288. 'jpe' => 'image/jpeg',
  289. 'pbm' => 'image/x-portable-bitmap',
  290. 'pgm' => 'image/x-portable-graymap',
  291. 'png' => 'image/png',
  292. 'pnm' => 'image/x-portable-anymap',
  293. 'ppm' => 'image/x-portable-pixmap',
  294. 'ras' => 'image/cmu-raster',
  295. 'rgb' => 'image/x-rgb',
  296. 'tif' => 'image/tiff',
  297. 'tiff' => 'image/tiff',
  298. 'xbm' => 'image/x-xbitmap',
  299. 'xpm' => 'image/x-xpixmap',
  300. 'xwd' => 'image/x-xwindowdump',
  301. 'psd' => ['application/photoshop', 'application/psd', 'image/psd', 'image/x-photoshop', 'image/photoshop', 'zz-application/zz-winassoc-psd'],
  302. 'ice' => 'x-conference/x-cooltalk',
  303. 'iges' => 'model/iges',
  304. 'igs' => 'model/iges',
  305. 'mesh' => 'model/mesh',
  306. 'msh' => 'model/mesh',
  307. 'silo' => 'model/mesh',
  308. 'vrml' => 'model/vrml',
  309. 'wrl' => 'model/vrml',
  310. 'mime' => 'www/mime',
  311. 'pdb' => 'chemical/x-pdb',
  312. 'xyz' => 'chemical/x-pdb',
  313. 'javascript' => 'application/javascript',
  314. 'form' => 'application/x-www-form-urlencoded',
  315. 'file' => 'multipart/form-data',
  316. 'xhtml-mobile' => 'application/vnd.wap.xhtml+xml',
  317. 'atom' => 'application/atom+xml',
  318. 'amf' => 'application/x-amf',
  319. 'wap' => ['text/vnd.wap.wml', 'text/vnd.wap.wmlscript', 'image/vnd.wap.wbmp'],
  320. 'wml' => 'text/vnd.wap.wml',
  321. 'wmlscript' => 'text/vnd.wap.wmlscript',
  322. 'wbmp' => 'image/vnd.wap.wbmp',
  323. 'woff' => 'application/x-font-woff',
  324. 'appcache' => 'text/cache-manifest',
  325. 'manifest' => 'text/cache-manifest',
  326. 'htc' => 'text/x-component',
  327. 'rdf' => 'application/xml',
  328. 'crx' => 'application/x-chrome-extension',
  329. 'oex' => 'application/x-opera-extension',
  330. 'xpi' => 'application/x-xpinstall',
  331. 'safariextz' => 'application/octet-stream',
  332. 'webapp' => 'application/x-web-app-manifest+json',
  333. 'vcf' => 'text/x-vcard',
  334. 'vtt' => 'text/vtt',
  335. 'mkv' => 'video/x-matroska',
  336. 'pkpass' => 'application/vnd.apple.pkpass',
  337. 'ajax' => 'text/html',
  338. 'bmp' => 'image/bmp'
  339. ];
  340. /**
  341. * Protocol header to send to the client
  342. *
  343. * @var string
  344. */
  345. protected $_protocol = 'HTTP/1.1';
  346. /**
  347. * Status code to send to the client
  348. *
  349. * @var int
  350. */
  351. protected $_status = 200;
  352. /**
  353. * Content type to send. This can be an 'extension' that will be transformed using the $_mimetypes array
  354. * or a complete mime-type
  355. *
  356. * @var string
  357. */
  358. protected $_contentType = 'text/html';
  359. /**
  360. * File object for file to be read out as response
  361. *
  362. * @var \Cake\Filesystem\File|null
  363. */
  364. protected $_file;
  365. /**
  366. * File range. Used for requesting ranges of files.
  367. *
  368. * @var array
  369. */
  370. protected $_fileRange = [];
  371. /**
  372. * The charset the response body is encoded with
  373. *
  374. * @var string
  375. */
  376. protected $_charset = 'UTF-8';
  377. /**
  378. * Holds all the cache directives that will be converted
  379. * into headers when sending the request
  380. *
  381. * @var array
  382. */
  383. protected $_cacheDirectives = [];
  384. /**
  385. * Collection of cookies to send to the client
  386. *
  387. * @var \Cake\Http\Cookie\CookieCollection
  388. */
  389. protected $_cookies = null;
  390. /**
  391. * Reason Phrase
  392. *
  393. * @var string
  394. */
  395. protected $_reasonPhrase = 'OK';
  396. /**
  397. * Stream mode options.
  398. *
  399. * @var string
  400. */
  401. protected $_streamMode = 'wb+';
  402. /**
  403. * Stream target or resource object.
  404. *
  405. * @var string|resource
  406. */
  407. protected $_streamTarget = 'php://memory';
  408. /**
  409. * Constructor
  410. *
  411. * @param array $options list of parameters to setup the response. Possible values are:
  412. * - body: the response text that should be sent to the client
  413. * - statusCodes: additional allowable response codes
  414. * - status: the HTTP status code to respond with
  415. * - type: a complete mime-type string or an extension mapped in this class
  416. * - charset: the charset for the response body
  417. */
  418. public function __construct(array $options = [])
  419. {
  420. if (isset($options['streamTarget'])) {
  421. $this->_streamTarget = $options['streamTarget'];
  422. }
  423. if (isset($options['streamMode'])) {
  424. $this->_streamMode = $options['streamMode'];
  425. }
  426. if (isset($options['stream'])) {
  427. if (!$options['stream'] instanceof StreamInterface) {
  428. throw new InvalidArgumentException('Stream option must be an object that implements StreamInterface');
  429. }
  430. $this->stream = $options['stream'];
  431. } else {
  432. $this->_createStream();
  433. }
  434. if (isset($options['body'])) {
  435. $this->stream->write($options['body']);
  436. }
  437. if (isset($options['statusCodes'])) {
  438. $this->httpCodes($options['statusCodes']);
  439. }
  440. if (isset($options['status'])) {
  441. $this->_setStatus($options['status']);
  442. }
  443. if (!isset($options['charset'])) {
  444. $options['charset'] = Configure::read('App.encoding');
  445. }
  446. $this->_charset = $options['charset'];
  447. if (isset($options['type'])) {
  448. $this->_contentType = $this->resolveType($options['type']);
  449. }
  450. $this->_setContentType();
  451. $this->_cookies = new CookieCollection();
  452. }
  453. /**
  454. * Creates the stream object.
  455. *
  456. * @return void
  457. */
  458. protected function _createStream()
  459. {
  460. $this->stream = new Stream($this->_streamTarget, $this->_streamMode);
  461. }
  462. /**
  463. * Sends the complete response to the client including headers and message body.
  464. * Will echo out the content in the response body.
  465. *
  466. * @return void
  467. * @deprecated 3.4.0 Will be removed in 4.0.0
  468. */
  469. public function send()
  470. {
  471. deprecationWarning('Response::send() will be removed in 4.0.0');
  472. if ($this->hasHeader('Location') && $this->_status === 200) {
  473. $this->statusCode(302);
  474. }
  475. $this->_setContent();
  476. $this->sendHeaders();
  477. if ($this->_file) {
  478. $this->_sendFile($this->_file, $this->_fileRange);
  479. $this->_file = null;
  480. $this->_fileRange = [];
  481. } else {
  482. $this->_sendContent($this->body());
  483. }
  484. if (function_exists('fastcgi_finish_request')) {
  485. fastcgi_finish_request();
  486. }
  487. }
  488. /**
  489. * Sends the HTTP headers and cookies.
  490. *
  491. * @return void
  492. * @deprecated 3.4.0 Will be removed in 4.0.0
  493. */
  494. public function sendHeaders()
  495. {
  496. deprecationWarning(
  497. 'Will be removed in 4.0.0'
  498. );
  499. $file = $line = null;
  500. if (headers_sent($file, $line)) {
  501. Log::warning("Headers already sent in {$file}:{$line}");
  502. return;
  503. }
  504. $codeMessage = $this->_statusCodes[$this->_status];
  505. $this->_setCookies();
  506. $this->_sendHeader("{$this->_protocol} {$this->_status} {$codeMessage}");
  507. $this->_setContentType();
  508. foreach ($this->headers as $header => $values) {
  509. foreach ((array)$values as $value) {
  510. $this->_sendHeader($header, $value);
  511. }
  512. }
  513. }
  514. /**
  515. * Sets the cookies that have been added via Cake\Http\Response::cookie() before any
  516. * other output is sent to the client. Will set the cookies in the order they
  517. * have been set.
  518. *
  519. * @return void
  520. * @deprecated 3.4.0 Will be removed in 4.0.0
  521. */
  522. protected function _setCookies()
  523. {
  524. deprecationWarning(
  525. 'Will be removed in 4.0.0'
  526. );
  527. foreach ($this->_cookies as $cookie) {
  528. setcookie(
  529. $cookie->getName(),
  530. $cookie->getValue(),
  531. $cookie->getExpiresTimestamp(),
  532. $cookie->getPath(),
  533. $cookie->getDomain(),
  534. $cookie->isSecure(),
  535. $cookie->isHttpOnly()
  536. );
  537. }
  538. }
  539. /**
  540. * Formats the Content-Type header based on the configured contentType and charset
  541. * the charset will only be set in the header if the response is of type text/*
  542. *
  543. * @return void
  544. */
  545. protected function _setContentType()
  546. {
  547. if (in_array($this->_status, [304, 204])) {
  548. $this->_clearHeader('Content-Type');
  549. return;
  550. }
  551. $whitelist = [
  552. 'application/javascript', 'application/xml', 'application/rss+xml'
  553. ];
  554. $charset = false;
  555. if ($this->_charset &&
  556. (strpos($this->_contentType, 'text/') === 0 || in_array($this->_contentType, $whitelist))
  557. ) {
  558. $charset = true;
  559. }
  560. if ($charset) {
  561. $this->_setHeader('Content-Type', "{$this->_contentType}; charset={$this->_charset}");
  562. } else {
  563. $this->_setHeader('Content-Type', (string)$this->_contentType);
  564. }
  565. }
  566. /**
  567. * Sets the response body to an empty text if the status code is 204 or 304
  568. *
  569. * @return void
  570. * @deprecated 3.4.0 Will be removed in 4.0.0
  571. */
  572. protected function _setContent()
  573. {
  574. deprecationWarning(
  575. 'Will be removed in 4.0.0'
  576. );
  577. if (in_array($this->_status, [304, 204])) {
  578. $this->body('');
  579. }
  580. }
  581. /**
  582. * Sends a header to the client.
  583. *
  584. * @param string $name the header name
  585. * @param string|null $value the header value
  586. * @return void
  587. * @deprecated 3.4.0 Will be removed in 4.0.0
  588. */
  589. protected function _sendHeader($name, $value = null)
  590. {
  591. deprecationWarning(
  592. 'Will be removed in 4.0.0'
  593. );
  594. if ($value === null) {
  595. header($name);
  596. } else {
  597. header("{$name}: {$value}");
  598. }
  599. }
  600. /**
  601. * Sends a content string to the client.
  602. *
  603. * If the content is a callable, it is invoked. The callable should either
  604. * return a string or output content directly and have no return value.
  605. *
  606. * @param string|callable $content String to send as response body or callable
  607. * which returns/outputs content.
  608. * @return void
  609. * @deprecated 3.4.0 Will be removed in 4.0.0
  610. */
  611. protected function _sendContent($content)
  612. {
  613. deprecationWarning(
  614. 'Will be removed in 4.0.0'
  615. );
  616. if (!is_string($content) && is_callable($content)) {
  617. $content = $content();
  618. }
  619. echo $content;
  620. }
  621. /**
  622. * Buffers a header string to be sent
  623. * Returns the complete list of buffered headers
  624. *
  625. * ### Single header
  626. * ```
  627. * header('Location', 'http://example.com');
  628. * ```
  629. *
  630. * ### Multiple headers
  631. * ```
  632. * header(['Location' => 'http://example.com', 'X-Extra' => 'My header']);
  633. * ```
  634. *
  635. * ### String header
  636. * ```
  637. * header('WWW-Authenticate: Negotiate');
  638. * ```
  639. *
  640. * ### Array of string headers
  641. * ```
  642. * header(['WWW-Authenticate: Negotiate', 'Content-type: application/pdf']);
  643. * ```
  644. *
  645. * Multiple calls for setting the same header name will have the same effect as setting the header once
  646. * with the last value sent for it
  647. * ```
  648. * header('WWW-Authenticate: Negotiate');
  649. * header('WWW-Authenticate: Not-Negotiate');
  650. * ```
  651. * will have the same effect as only doing
  652. * ```
  653. * header('WWW-Authenticate: Not-Negotiate');
  654. * ```
  655. *
  656. * @param string|array|null $header An array of header strings or a single header string
  657. * - an associative array of "header name" => "header value" is also accepted
  658. * - an array of string headers is also accepted
  659. * @param string|array|null $value The header value(s)
  660. * @return array List of headers to be sent
  661. * @deprecated 3.4.0 Use `withHeader()`, `getHeaderLine()` and `getHeaders()` instead.
  662. */
  663. public function header($header = null, $value = null)
  664. {
  665. deprecationWarning(
  666. 'Response::header() is deprecated. ' .
  667. 'Use `withHeader()`, `getHeaderLine()` and `getHeaders()` instead.'
  668. );
  669. if ($header === null) {
  670. return $this->getSimpleHeaders();
  671. }
  672. $headers = is_array($header) ? $header : [$header => $value];
  673. foreach ($headers as $header => $value) {
  674. if (is_numeric($header)) {
  675. list($header, $value) = [$value, null];
  676. }
  677. if ($value === null) {
  678. list($header, $value) = explode(':', $header, 2);
  679. }
  680. $lower = strtolower($header);
  681. if (array_key_exists($lower, $this->headerNames)) {
  682. $header = $this->headerNames[$lower];
  683. } else {
  684. $this->headerNames[$lower] = $header;
  685. }
  686. $this->headers[$header] = is_array($value) ? array_map('trim', $value) : [trim($value)];
  687. }
  688. return $this->getSimpleHeaders();
  689. }
  690. /**
  691. * Backwards compatibility helper for getting flattened headers.
  692. *
  693. * Previously CakePHP would store headers as a simple dictionary, now that
  694. * we're supporting PSR7, the internal storage has each header as an array.
  695. *
  696. * @return array
  697. */
  698. protected function getSimpleHeaders()
  699. {
  700. $out = [];
  701. foreach ($this->headers as $key => $values) {
  702. $header = $this->headerNames[strtolower($key)];
  703. if (count($values) === 1) {
  704. $values = $values[0];
  705. }
  706. $out[$header] = $values;
  707. }
  708. return $out;
  709. }
  710. /**
  711. * Accessor for the location header.
  712. *
  713. * Get/Set the Location header value.
  714. *
  715. * @param null|string $url Either null to get the current location, or a string to set one.
  716. * @return string|null When setting the location null will be returned. When reading the location
  717. * a string of the current location header value (if any) will be returned.
  718. * @deprecated 3.4.0 Mutable responses are deprecated. Use `withLocation()` and `getHeaderLine()`
  719. * instead.
  720. */
  721. public function location($url = null)
  722. {
  723. deprecationWarning(
  724. 'Response::location() is deprecated. ' .
  725. 'Mutable responses are deprecated. Use `withLocation()` and `getHeaderLine()` instead.'
  726. );
  727. if ($url === null) {
  728. $result = $this->getHeaderLine('Location');
  729. if (!$result) {
  730. return null;
  731. }
  732. return $result;
  733. }
  734. if ($this->_status === 200) {
  735. $this->_status = 302;
  736. }
  737. $this->_setHeader('Location', $url);
  738. return null;
  739. }
  740. /**
  741. * Return an instance with an updated location header.
  742. *
  743. * If the current status code is 200, it will be replaced
  744. * with 302.
  745. *
  746. * @param string $url The location to redirect to.
  747. * @return static A new response with the Location header set.
  748. */
  749. public function withLocation($url)
  750. {
  751. $new = $this->withHeader('Location', $url);
  752. if ($new->_status === 200) {
  753. $new->_status = 302;
  754. }
  755. return $new;
  756. }
  757. /**
  758. * Sets a header.
  759. *
  760. * @param string $header Header key.
  761. * @param string $value Header value.
  762. * @return void
  763. */
  764. protected function _setHeader($header, $value)
  765. {
  766. $normalized = strtolower($header);
  767. $this->headerNames[$normalized] = $header;
  768. $this->headers[$header] = [$value];
  769. }
  770. /**
  771. * Clear header
  772. *
  773. * @param string $header Header key.
  774. * @return void
  775. */
  776. protected function _clearHeader($header)
  777. {
  778. $normalized = strtolower($header);
  779. if (!isset($this->headerNames[$normalized])) {
  780. return;
  781. }
  782. $original = $this->headerNames[$normalized];
  783. unset($this->headerNames[$normalized], $this->headers[$original]);
  784. }
  785. /**
  786. * Buffers the response message to be sent
  787. * if $content is null the current buffer is returned
  788. *
  789. * @param string|callable|null $content the string or callable message to be sent
  790. * @return string|null Current message buffer if $content param is passed as null
  791. * @deprecated 3.4.0 Mutable response methods are deprecated. Use `withBody()`/`withStringBody()` and `getBody()` instead.
  792. */
  793. public function body($content = null)
  794. {
  795. deprecationWarning(
  796. 'Response::body() is deprecated. ' .
  797. 'Mutable response methods are deprecated. Use `withBody()` and `getBody()` instead.'
  798. );
  799. if ($content === null) {
  800. if ($this->stream->isSeekable()) {
  801. $this->stream->rewind();
  802. }
  803. $result = $this->stream->getContents();
  804. if (strlen($result) === 0) {
  805. return null;
  806. }
  807. return $result;
  808. }
  809. // Compatibility with closure/streaming responses
  810. if (!is_string($content) && is_callable($content)) {
  811. $this->stream = new CallbackStream($content);
  812. } else {
  813. $this->_createStream();
  814. $this->stream->write($content);
  815. }
  816. return $content;
  817. }
  818. /**
  819. * Handles the callable body for backward compatibility reasons.
  820. *
  821. * @param callable $content Callable content.
  822. * @return string
  823. */
  824. protected function _handleCallableBody(callable $content)
  825. {
  826. ob_start();
  827. $result1 = $content();
  828. $result2 = ob_get_contents();
  829. ob_get_clean();
  830. if ($result1) {
  831. return $result1;
  832. }
  833. return $result2;
  834. }
  835. /**
  836. * Sets the HTTP status code to be sent
  837. * if $code is null the current code is returned
  838. *
  839. * If the status code is 304 or 204, the existing Content-Type header
  840. * will be cleared, as these response codes have no body.
  841. *
  842. * @param int|null $code the HTTP status code
  843. * @return int Current status code
  844. * @throws \InvalidArgumentException When an unknown status code is reached.
  845. * @deprecated 3.4.0 Use `getStatusCode()` and `withStatus()` instead.
  846. */
  847. public function statusCode($code = null)
  848. {
  849. deprecationWarning(
  850. 'Response::statusCode() is deprecated. ' .
  851. 'Use `getStatusCode()` and `withStatus()` instead.'
  852. );
  853. if ($code === null) {
  854. return $this->_status;
  855. }
  856. if (!isset($this->_statusCodes[$code])) {
  857. throw new InvalidArgumentException('Unknown status code');
  858. }
  859. $this->_setStatus($code);
  860. return $code;
  861. }
  862. /**
  863. * Gets the response status code.
  864. *
  865. * The status code is a 3-digit integer result code of the server's attempt
  866. * to understand and satisfy the request.
  867. *
  868. * @return int Status code.
  869. */
  870. public function getStatusCode()
  871. {
  872. return $this->_status;
  873. }
  874. /**
  875. * Return an instance with the specified status code and, optionally, reason phrase.
  876. *
  877. * If no reason phrase is specified, implementations MAY choose to default
  878. * to the RFC 7231 or IANA recommended reason phrase for the response's
  879. * status code.
  880. *
  881. * This method MUST be implemented in such a way as to retain the
  882. * immutability of the message, and MUST return an instance that has the
  883. * updated status and reason phrase.
  884. *
  885. * If the status code is 304 or 204, the existing Content-Type header
  886. * will be cleared, as these response codes have no body.
  887. *
  888. * @link https://tools.ietf.org/html/rfc7231#section-6
  889. * @link https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
  890. * @param int $code The 3-digit integer result code to set.
  891. * @param string $reasonPhrase The reason phrase to use with the
  892. * provided status code; if none is provided, implementations MAY
  893. * use the defaults as suggested in the HTTP specification.
  894. * @return static
  895. * @throws \InvalidArgumentException For invalid status code arguments.
  896. */
  897. public function withStatus($code, $reasonPhrase = '')
  898. {
  899. $new = clone $this;
  900. $new->_setStatus($code, $reasonPhrase);
  901. return $new;
  902. }
  903. /**
  904. * Modifier for response status
  905. *
  906. * @param int $code The code to set.
  907. * @param string $reasonPhrase The response reason phrase.
  908. * @return void
  909. * @throws \InvalidArgumentException For invalid status code arguments.
  910. */
  911. protected function _setStatus($code, $reasonPhrase = '')
  912. {
  913. if (!isset($this->_statusCodes[$code])) {
  914. throw new InvalidArgumentException(sprintf(
  915. 'Invalid status code: %s. Use a valid HTTP status code in range 1xx - 5xx.',
  916. $code
  917. ));
  918. }
  919. $this->_status = $code;
  920. if (empty($reasonPhrase)) {
  921. $reasonPhrase = $this->_statusCodes[$code];
  922. }
  923. $this->_reasonPhrase = $reasonPhrase;
  924. $this->_setContentType();
  925. }
  926. /**
  927. * Gets the response reason phrase associated with the status code.
  928. *
  929. * Because a reason phrase is not a required element in a response
  930. * status line, the reason phrase value MAY be null. Implementations MAY
  931. * choose to return the default RFC 7231 recommended reason phrase (or those
  932. * listed in the IANA HTTP Status Code Registry) for the response's
  933. * status code.
  934. *
  935. * @link https://tools.ietf.org/html/rfc7231#section-6
  936. * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
  937. * @return string Reason phrase; must return an empty string if none present.
  938. */
  939. public function getReasonPhrase()
  940. {
  941. return $this->_reasonPhrase;
  942. }
  943. /**
  944. * Queries & sets valid HTTP response codes & messages.
  945. *
  946. * @param int|array|null $code If $code is an integer, then the corresponding code/message is
  947. * returned if it exists, null if it does not exist. If $code is an array, then the
  948. * keys are used as codes and the values as messages to add to the default HTTP
  949. * codes. The codes must be integers greater than 99 and less than 1000. Keep in
  950. * mind that the HTTP specification outlines that status codes begin with a digit
  951. * between 1 and 5, which defines the class of response the client is to expect.
  952. * Example:
  953. *
  954. * httpCodes(404); // returns [404 => 'Not Found']
  955. *
  956. * httpCodes([
  957. * 381 => 'Unicorn Moved',
  958. * 555 => 'Unexpected Minotaur'
  959. * ]); // sets these new values, and returns true
  960. *
  961. * httpCodes([
  962. * 0 => 'Nothing Here',
  963. * -1 => 'Reverse Infinity',
  964. * 12345 => 'Universal Password',
  965. * 'Hello' => 'World'
  966. * ]); // throws an exception due to invalid codes
  967. *
  968. * For more on HTTP status codes see: http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1
  969. *
  970. * @return mixed Associative array of the HTTP codes as keys, and the message
  971. * strings as values, or null of the given $code does not exist.
  972. * @throws \InvalidArgumentException If an attempt is made to add an invalid status code
  973. * @deprecated 3.4.0 Will be removed in 4.0.0
  974. */
  975. public function httpCodes($code = null)
  976. {
  977. deprecationWarning('Response::httpCodes(). Will be removed in 4.0.0');
  978. if (empty($code)) {
  979. return $this->_statusCodes;
  980. }
  981. if (is_array($code)) {
  982. $codes = array_keys($code);
  983. $min = min($codes);
  984. if (!is_int($min) || $min < 100 || max($codes) > 999) {
  985. throw new InvalidArgumentException('Invalid status code');
  986. }
  987. $this->_statusCodes = $code + $this->_statusCodes;
  988. return true;
  989. }
  990. if (!isset($this->_statusCodes[$code])) {
  991. return null;
  992. }
  993. return [$code => $this->_statusCodes[$code]];
  994. }
  995. /**
  996. * Sets the response content type. It can be either a file extension
  997. * which will be mapped internally to a mime-type or a string representing a mime-type
  998. * if $contentType is null the current content type is returned
  999. * if $contentType is an associative array, content type definitions will be stored/replaced
  1000. *
  1001. * ### Setting the content type
  1002. *
  1003. * ```
  1004. * type('jpg');
  1005. * ```
  1006. *
  1007. * If you attempt to set the type on a 304 or 204 status code response, the
  1008. * content type will not take effect as these status codes do not have content-types.
  1009. *
  1010. * ### Returning the current content type
  1011. *
  1012. * ```
  1013. * type();
  1014. * ```
  1015. *
  1016. * ### Storing content type definitions
  1017. *
  1018. * ```
  1019. * type(['keynote' => 'application/keynote', 'bat' => 'application/bat']);
  1020. * ```
  1021. *
  1022. * ### Replacing a content type definition
  1023. *
  1024. * ```
  1025. * type(['jpg' => 'text/plain']);
  1026. * ```
  1027. *
  1028. * @param string|null $contentType Content type key.
  1029. * @return mixed Current content type or false if supplied an invalid content type.
  1030. * @deprecated 3.5.5 Use getType() or withType() instead.
  1031. */
  1032. public function type($contentType = null)
  1033. {
  1034. deprecationWarning(
  1035. 'Response::type() is deprecated. ' .
  1036. 'Use setTypeMap(), getType() or withType() instead.'
  1037. );
  1038. if ($contentType === null) {
  1039. return $this->getType();
  1040. }
  1041. if (is_array($contentType)) {
  1042. foreach ($contentType as $type => $definition) {
  1043. $this->_mimeTypes[$type] = $definition;
  1044. }
  1045. return $this->getType();
  1046. }
  1047. if (isset($this->_mimeTypes[$contentType])) {
  1048. $contentType = $this->_mimeTypes[$contentType];
  1049. $contentType = is_array($contentType) ? current($contentType) : $contentType;
  1050. }
  1051. if (strpos($contentType, '/') === false) {
  1052. return false;
  1053. }
  1054. $this->_contentType = $contentType;
  1055. $this->_setContentType();
  1056. return $contentType;
  1057. }
  1058. /**
  1059. * Sets a content type definition into the map.
  1060. *
  1061. * E.g.: setTypeMap('xhtml', ['application/xhtml+xml', 'application/xhtml'])
  1062. *
  1063. * This is needed for RequestHandlerComponent and recognition of types.
  1064. *
  1065. * @param string $type Content type.
  1066. * @param string|array $mimeType Definition of the mime type.
  1067. * @return void
  1068. */
  1069. public function setTypeMap($type, $mimeType)
  1070. {
  1071. $this->_mimeTypes[$type] = $mimeType;
  1072. }
  1073. /**
  1074. * Returns the current content type.
  1075. *
  1076. * @return string
  1077. */
  1078. public function getType()
  1079. {
  1080. return $this->_contentType;
  1081. }
  1082. /**
  1083. * Get an updated response with the content type set.
  1084. *
  1085. * If you attempt to set the type on a 304 or 204 status code response, the
  1086. * content type will not take effect as these status codes do not have content-types.
  1087. *
  1088. * @param string $contentType Either a file extension which will be mapped to a mime-type or a concrete mime-type.
  1089. * @return static
  1090. */
  1091. public function withType($contentType)
  1092. {
  1093. $mappedType = $this->resolveType($contentType);
  1094. $new = clone $this;
  1095. $new->_contentType = $mappedType;
  1096. $new->_setContentType();
  1097. return $new;
  1098. }
  1099. /**
  1100. * Translate and validate content-types.
  1101. *
  1102. * @param string $contentType The content-type or type alias.
  1103. * @return string The resolved content-type
  1104. * @throws \InvalidArgumentException When an invalid content-type or alias is used.
  1105. */
  1106. protected function resolveType($contentType)
  1107. {
  1108. $mapped = $this->getMimeType($contentType);
  1109. if ($mapped) {
  1110. return is_array($mapped) ? current($mapped) : $mapped;
  1111. }
  1112. if (strpos($contentType, '/') === false) {
  1113. throw new InvalidArgumentException(sprintf('"%s" is an invalid content type.', $contentType));
  1114. }
  1115. return $contentType;
  1116. }
  1117. /**
  1118. * Returns the mime type definition for an alias
  1119. *
  1120. * e.g `getMimeType('pdf'); // returns 'application/pdf'`
  1121. *
  1122. * @param string $alias the content type alias to map
  1123. * @return mixed String mapped mime type or false if $alias is not mapped
  1124. */
  1125. public function getMimeType($alias)
  1126. {
  1127. if (isset($this->_mimeTypes[$alias])) {
  1128. return $this->_mimeTypes[$alias];
  1129. }
  1130. return false;
  1131. }
  1132. /**
  1133. * Maps a content-type back to an alias
  1134. *
  1135. * e.g `mapType('application/pdf'); // returns 'pdf'`
  1136. *
  1137. * @param string|array $ctype Either a string content type to map, or an array of types.
  1138. * @return string|array|null Aliases for the types provided.
  1139. */
  1140. public function mapType($ctype)
  1141. {
  1142. if (is_array($ctype)) {
  1143. return array_map([$this, 'mapType'], $ctype);
  1144. }
  1145. foreach ($this->_mimeTypes as $alias => $types) {
  1146. if (in_array($ctype, (array)$types)) {
  1147. return $alias;
  1148. }
  1149. }
  1150. return null;
  1151. }
  1152. /**
  1153. * Sets the response charset
  1154. * if $charset is null the current charset is returned
  1155. *
  1156. * @param string|null $charset Character set string.
  1157. * @return string Current charset
  1158. * @deprecated 3.5.0 Use getCharset()/withCharset() instead.
  1159. */
  1160. public function charset($charset = null)
  1161. {
  1162. deprecationWarning(
  1163. 'Response::charset() is deprecated. ' .
  1164. 'Use getCharset()/withCharset() instead.'
  1165. );
  1166. if ($charset === null) {
  1167. return $this->_charset;
  1168. }
  1169. $this->_charset = $charset;
  1170. $this->_setContentType();
  1171. return $this->_charset;
  1172. }
  1173. /**
  1174. * Returns the current charset.
  1175. *
  1176. * @return string
  1177. */
  1178. public function getCharset()
  1179. {
  1180. return $this->_charset;
  1181. }
  1182. /**
  1183. * Get a new instance with an updated charset.
  1184. *
  1185. * @param string $charset Character set string.
  1186. * @return static
  1187. */
  1188. public function withCharset($charset)
  1189. {
  1190. $new = clone $this;
  1191. $new->_charset = $charset;
  1192. $new->_setContentType();
  1193. return $new;
  1194. }
  1195. /**
  1196. * Sets the correct headers to instruct the client to not cache the response
  1197. *
  1198. * @return void
  1199. * @deprecated 3.4.0 Use withDisabledCache() instead.
  1200. */
  1201. public function disableCache()
  1202. {
  1203. deprecationWarning(
  1204. 'Response::disableCache() is deprecated. ' .
  1205. 'Use withDisabledCache() instead.'
  1206. );
  1207. $this->_setHeader('Expires', 'Mon, 26 Jul 1997 05:00:00 GMT');
  1208. $this->_setHeader('Last-Modified', gmdate('D, d M Y H:i:s') . ' GMT');
  1209. $this->_setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
  1210. }
  1211. /**
  1212. * Create a new instance with headers to instruct the client to not cache the response
  1213. *
  1214. * @return static
  1215. */
  1216. public function withDisabledCache()
  1217. {
  1218. return $this->withHeader('Expires', 'Mon, 26 Jul 1997 05:00:00 GMT')
  1219. ->withHeader('Last-Modified', gmdate('D, d M Y H:i:s') . ' GMT')
  1220. ->withHeader('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
  1221. }
  1222. /**
  1223. * Sets the correct headers to instruct the client to cache the response.
  1224. *
  1225. * @param string $since a valid time since the response text has not been modified
  1226. * @param string $time a valid time for cache expiry
  1227. * @return void
  1228. * @deprecated 3.4.0 Use withCache() instead.
  1229. */
  1230. public function cache($since, $time = '+1 day')
  1231. {
  1232. deprecationWarning(
  1233. 'Response::cache() is deprecated. ' .
  1234. 'Use withCache() instead.'
  1235. );
  1236. if (!is_int($time)) {
  1237. $time = strtotime($time);
  1238. }
  1239. $this->_setHeader('Date', gmdate('D, j M Y G:i:s ', time()) . 'GMT');
  1240. $this->modified($since);
  1241. $this->expires($time);
  1242. $this->sharable(true);
  1243. $this->maxAge($time - time());
  1244. }
  1245. /**
  1246. * Create a new instance with the headers to enable client caching.
  1247. *
  1248. * @param string $since a valid time since the response text has not been modified
  1249. * @param string $time a valid time for cache expiry
  1250. * @return static
  1251. */
  1252. public function withCache($since, $time = '+1 day')
  1253. {
  1254. if (!is_int($time)) {
  1255. $time = strtotime($time);
  1256. }
  1257. return $this->withHeader('Date', gmdate('D, j M Y G:i:s ', time()) . 'GMT')
  1258. ->withModified($since)
  1259. ->withExpires($time)
  1260. ->withSharable(true)
  1261. ->withMaxAge($time - time());
  1262. }
  1263. /**
  1264. * Sets whether a response is eligible to be cached by intermediate proxies
  1265. * This method controls the `public` or `private` directive in the Cache-Control
  1266. * header
  1267. *
  1268. * @param bool|null $public If set to true, the Cache-Control header will be set as public
  1269. * if set to false, the response will be set to private
  1270. * if no value is provided, it will return whether the response is sharable or not
  1271. * @param int|null $time time in seconds after which the response should no longer be considered fresh
  1272. * @return bool|null
  1273. */
  1274. public function sharable($public = null, $time = null)
  1275. {
  1276. deprecationWarning(
  1277. 'Response::sharable() is deprecated. ' .
  1278. 'Use withSharable() instead.'
  1279. );
  1280. if ($public === null) {
  1281. $public = array_key_exists('public', $this->_cacheDirectives);
  1282. $private = array_key_exists('private', $this->_cacheDirectives);
  1283. $noCache = array_key_exists('no-cache', $this->_cacheDirectives);
  1284. if (!$public && !$private && !$noCache) {
  1285. return null;
  1286. }
  1287. return $public || !($private || $noCache);
  1288. }
  1289. if ($public) {
  1290. $this->_cacheDirectives['public'] = true;
  1291. unset($this->_cacheDirectives['private']);
  1292. } else {
  1293. $this->_cacheDirectives['private'] = true;
  1294. unset($this->_cacheDirectives['public']);
  1295. }
  1296. $this->maxAge($time);
  1297. if (!$time) {
  1298. $this->_setCacheControl();
  1299. }
  1300. return (bool)$public;
  1301. }
  1302. /**
  1303. * Create a new instace with the public/private Cache-Control directive set.
  1304. *
  1305. * @param bool $public If set to true, the Cache-Control header will be set as public
  1306. * if set to false, the response will be set to private.
  1307. * @param int|null $time time in seconds after which the response should no longer be considered fresh.
  1308. * @return static
  1309. */
  1310. public function withSharable($public, $time = null)
  1311. {
  1312. $new = clone $this;
  1313. unset($new->_cacheDirectives['private'], $new->_cacheDirectives['public']);
  1314. $key = $public ? 'public' : 'private';
  1315. $new->_cacheDirectives[$key] = true;
  1316. if ($time !== null) {
  1317. $new->_cacheDirectives['max-age'] = $time;
  1318. }
  1319. $new->_setCacheControl();
  1320. return $new;
  1321. }
  1322. /**
  1323. * Sets the Cache-Control s-maxage directive.
  1324. *
  1325. * The max-age is the number of seconds after which the response should no longer be considered
  1326. * a good candidate to be fetched from a shared cache (like in a proxy server).
  1327. * If called with no parameters, this function will return the current max-age value if any
  1328. *
  1329. * @deprecated 3.6.5 Use withSharedMaxAge() instead.
  1330. * @param int|null $seconds if null, the method will return the current s-maxage value
  1331. * @return int|null
  1332. */
  1333. public function sharedMaxAge($seconds = null)
  1334. {
  1335. deprecationWarning(
  1336. 'Response::sharedMaxAge() is deprecated. ' .
  1337. 'Use withSharedMaxAge() instead.'
  1338. );
  1339. if ($seconds !== null) {
  1340. $this->_cacheDirectives['s-maxage'] = $seconds;
  1341. $this->_setCacheControl();
  1342. }
  1343. if (isset($this->_cacheDirectives['s-maxage'])) {
  1344. return $this->_cacheDirectives['s-maxage'];
  1345. }
  1346. return null;
  1347. }
  1348. /**
  1349. * Create a new instance with the Cache-Control s-maxage directive.
  1350. *
  1351. * The max-age is the number of seconds after which the response should no longer be considered
  1352. * a good candidate to be fetched from a shared cache (like in a proxy server).
  1353. *
  1354. * @param int $seconds The number of seconds for shared max-age
  1355. * @return static
  1356. */
  1357. public function withSharedMaxAge($seconds)
  1358. {
  1359. $new = clone $this;
  1360. $new->_cacheDirectives['s-maxage'] = $seconds;
  1361. $new->_setCacheControl();
  1362. return $new;
  1363. }
  1364. /**
  1365. * Sets the Cache-Control max-age directive.
  1366. * The max-age is the number of seconds after which the response should no longer be considered
  1367. * a good candidate to be fetched from the local (client) cache.
  1368. * If called with no parameters, this function will return the current max-age value if any
  1369. *
  1370. * @deprecated 3.6.5 Use withMaxAge() instead.
  1371. * @param int|null $seconds if null, the method will return the current max-age value
  1372. * @return int|null
  1373. */
  1374. public function maxAge($seconds = null)
  1375. {
  1376. deprecationWarning(
  1377. 'Response::maxAge() is deprecated. ' .
  1378. 'Use withMaxAge() instead.'
  1379. );
  1380. if ($seconds !== null) {
  1381. $this->_cacheDirectives['max-age'] = $seconds;
  1382. $this->_setCacheControl();
  1383. }
  1384. if (isset($this->_cacheDirectives['max-age'])) {
  1385. return $this->_cacheDirectives['max-age'];
  1386. }
  1387. return null;
  1388. }
  1389. /**
  1390. * Create an instance with Cache-Control max-age directive set.
  1391. *
  1392. * The max-age is the number of seconds after which the response should no longer be considered
  1393. * a good candidate to be fetched from the local (client) cache.
  1394. *
  1395. * @param int $seconds The seconds a cached response can be considered valid
  1396. * @return static
  1397. */
  1398. public function withMaxAge($seconds)
  1399. {
  1400. $new = clone $this;
  1401. $new->_cacheDirectives['max-age'] = $seconds;
  1402. $new->_setCacheControl();
  1403. return $new;
  1404. }
  1405. /**
  1406. * Sets the Cache-Control must-revalidate directive.
  1407. * must-revalidate indicates that the response should not be served
  1408. * stale by a cache under any circumstance without first revalidating
  1409. * with the origin.
  1410. * If called with no parameters, this function will return whether must-revalidate is present.
  1411. *
  1412. * @param bool|null $enable if null, the method will return the current
  1413. * must-revalidate value. If boolean sets or unsets the directive.
  1414. * @return bool
  1415. * @deprecated 3.4.0 Use withMustRevalidate() instead.
  1416. */
  1417. public function mustRevalidate($enable = null)
  1418. {
  1419. deprecationWarning(
  1420. 'Response::mustRevalidate() is deprecated. ' .
  1421. 'Use withMustRevalidate() instead.'
  1422. );
  1423. if ($enable !== null) {
  1424. if ($enable) {
  1425. $this->_cacheDirectives['must-revalidate'] = true;
  1426. } else {
  1427. unset($this->_cacheDirectives['must-revalidate']);
  1428. }
  1429. $this->_setCacheControl();
  1430. }
  1431. return array_key_exists('must-revalidate', $this->_cacheDirectives);
  1432. }
  1433. /**
  1434. * Create an instance with Cache-Control must-revalidate directive set.
  1435. *
  1436. * Sets the Cache-Control must-revalidate directive.
  1437. * must-revalidate indicates that the response should not be served
  1438. * stale by a cache under any circumstance without first revalidating
  1439. * with the origin.
  1440. *
  1441. * @param bool $enable If boolean sets or unsets the directive.
  1442. * @return static
  1443. */
  1444. public function withMustRevalidate($enable)
  1445. {
  1446. $new = clone $this;
  1447. if ($enable) {
  1448. $new->_cacheDirectives['must-revalidate'] = true;
  1449. } else {
  1450. unset($new->_cacheDirectives['must-revalidate']);
  1451. }
  1452. $new->_setCacheControl();
  1453. return $new;
  1454. }
  1455. /**
  1456. * Helper method to generate a valid Cache-Control header from the options set
  1457. * in other methods
  1458. *
  1459. * @return void
  1460. */
  1461. protected function _setCacheControl()
  1462. {
  1463. $control = '';
  1464. foreach ($this->_cacheDirectives as $key => $val) {
  1465. $control .= $val === true ? $key : sprintf('%s=%s', $key, $val);
  1466. $control .= ', ';
  1467. }
  1468. $control = rtrim($control, ', ');
  1469. $this->_setHeader('Cache-Control', $control);
  1470. }
  1471. /**
  1472. * Sets the Expires header for the response by taking an expiration time
  1473. * If called with no parameters it will return the current Expires value
  1474. *
  1475. * ### Examples:
  1476. *
  1477. * `$response->expires('now')` Will Expire the response cache now
  1478. * `$response->expires(new DateTime('+1 day'))` Will set the expiration in next 24 hours
  1479. * `$response->expires()` Will return the current expiration header value
  1480. *
  1481. * @param string|\DateTimeInterface|null $time Valid time string or \DateTime instance.
  1482. * @return string|null
  1483. * @deprecated 3.4.0 Use withExpires() instead.
  1484. */
  1485. public function expires($time = null)
  1486. {
  1487. deprecationWarning(
  1488. 'Response::expires() is deprecated. ' .
  1489. 'Use withExpires() instead.'
  1490. );
  1491. if ($time !== null) {
  1492. $date = $this->_getUTCDate($time);
  1493. $this->_setHeader('Expires', $date->format('D, j M Y H:i:s') . ' GMT');
  1494. }
  1495. if ($this->hasHeader('Expires')) {
  1496. return $this->getHeaderLine('Expires');
  1497. }
  1498. return null;
  1499. }
  1500. /**
  1501. * Create a new instance with the Expires header set.
  1502. *
  1503. * ### Examples:
  1504. *
  1505. * ```
  1506. * // Will Expire the response cache now
  1507. * $response->withExpires('now')
  1508. *
  1509. * // Will set the expiration in next 24 hours
  1510. * $response->withExpires(new DateTime('+1 day'))
  1511. * ```
  1512. *
  1513. * @param string|\DateTimeInterface $time Valid time string or \DateTime instance.
  1514. * @return static
  1515. */
  1516. public function withExpires($time)
  1517. {
  1518. $date = $this->_getUTCDate($time);
  1519. return $this->withHeader('Expires', $date->format('D, j M Y H:i:s') . ' GMT');
  1520. }
  1521. /**
  1522. * Sets the Last-Modified header for the response by taking a modification time
  1523. * If called with no parameters it will return the current Last-Modified value
  1524. *
  1525. * ### Examples:
  1526. *
  1527. * `$response->modified('now')` Will set the Last-Modified to the current time
  1528. * `$response->modified(new DateTime('+1 day'))` Will set the modification date in the past 24 hours
  1529. * `$response->modified()` Will return the current Last-Modified header value
  1530. *
  1531. * @param string|\DateTimeInterface|null $time Valid time string or \DateTime instance.
  1532. * @return string|null
  1533. * @deprecated 3.4.0 Use withModified() instead.
  1534. */
  1535. public function modified($time = null)
  1536. {
  1537. deprecationWarning(
  1538. 'Response::modified() is deprecated. ' .
  1539. 'Use withModified() or getHeaderLine("Last-Modified") instead.'
  1540. );
  1541. if ($time !== null) {
  1542. $date = $this->_getUTCDate($time);
  1543. $this->_setHeader('Last-Modified', $date->format('D, j M Y H:i:s') . ' GMT');
  1544. }
  1545. if ($this->hasHeader('Last-Modified')) {
  1546. return $this->getHeaderLine('Last-Modified');
  1547. }
  1548. return null;
  1549. }
  1550. /**
  1551. * Create a new instance with the Last-Modified header set.
  1552. *
  1553. * ### Examples:
  1554. *
  1555. * ```
  1556. * // Will Expire the response cache now
  1557. * $response->withModified('now')
  1558. *
  1559. * // Will set the expiration in next 24 hours
  1560. * $response->withModified(new DateTime('+1 day'))
  1561. * ```
  1562. *
  1563. * @param string|\DateTimeInterface $time Valid time string or \DateTimeInterface instance.
  1564. * @return static
  1565. */
  1566. public function withModified($time)
  1567. {
  1568. $date = $this->_getUTCDate($time);
  1569. return $this->withHeader('Last-Modified', $date->format('D, j M Y H:i:s') . ' GMT');
  1570. }
  1571. /**
  1572. * Sets the response as Not Modified by removing any body contents
  1573. * setting the status code to "304 Not Modified" and removing all
  1574. * conflicting headers
  1575. *
  1576. * *Warning* This method mutates the response in-place and should be avoided.
  1577. *
  1578. * @return void
  1579. */
  1580. public function notModified()
  1581. {
  1582. $this->_createStream();
  1583. $this->_setStatus(304);
  1584. $remove = [
  1585. 'Allow',
  1586. 'Content-Encoding',
  1587. 'Content-Language',
  1588. 'Content-Length',
  1589. 'Content-MD5',
  1590. 'Content-Type',
  1591. 'Last-Modified'
  1592. ];
  1593. foreach ($remove as $header) {
  1594. $this->_clearHeader($header);
  1595. }
  1596. }
  1597. /**
  1598. * Create a new instance as 'not modified'
  1599. *
  1600. * This will remove any body contents set the status code
  1601. * to "304" and removing headers that describe
  1602. * a response body.
  1603. *
  1604. * @return static
  1605. */
  1606. public function withNotModified()
  1607. {
  1608. $new = $this->withStatus(304);
  1609. $new->_createStream();
  1610. $remove = [
  1611. 'Allow',
  1612. 'Content-Encoding',
  1613. 'Content-Language',
  1614. 'Content-Length',
  1615. 'Content-MD5',
  1616. 'Content-Type',
  1617. 'Last-Modified'
  1618. ];
  1619. foreach ($remove as $header) {
  1620. $new = $new->withoutHeader($header);
  1621. }
  1622. return $new;
  1623. }
  1624. /**
  1625. * Sets the Vary header for the response, if an array is passed,
  1626. * values will be imploded into a comma separated string. If no
  1627. * parameters are passed, then an array with the current Vary header
  1628. * value is returned
  1629. *
  1630. * @param string|array|null $cacheVariances A single Vary string or an array
  1631. * containing the list for variances.
  1632. * @return array|null
  1633. * @deprecated 3.4.0 Use withVary() instead.
  1634. */
  1635. public function vary($cacheVariances = null)
  1636. {
  1637. deprecationWarning(
  1638. 'Response::vary() is deprecated. ' .
  1639. 'Use withVary() instead.'
  1640. );
  1641. if ($cacheVariances !== null) {
  1642. $cacheVariances = (array)$cacheVariances;
  1643. $this->_setHeader('Vary', implode(', ', $cacheVariances));
  1644. }
  1645. if ($this->hasHeader('Vary')) {
  1646. return explode(', ', $this->getHeaderLine('Vary'));
  1647. }
  1648. return null;
  1649. }
  1650. /**
  1651. * Create a new instance with the Vary header set.
  1652. *
  1653. * If an array is passed values will be imploded into a comma
  1654. * separated string. If no parameters are passed, then an
  1655. * array with the current Vary header value is returned
  1656. *
  1657. * @param string|array $cacheVariances A single Vary string or an array
  1658. * containing the list for variances.
  1659. * @return static
  1660. */
  1661. public function withVary($cacheVariances)
  1662. {
  1663. return $this->withHeader('Vary', (array)$cacheVariances);
  1664. }
  1665. /**
  1666. * Sets the response Etag, Etags are a strong indicative that a response
  1667. * can be cached by a HTTP client. A bad way of generating Etags is
  1668. * creating a hash of the response output, instead generate a unique
  1669. * hash of the unique components that identifies a request, such as a
  1670. * modification time, a resource Id, and anything else you consider it
  1671. * makes it unique.
  1672. *
  1673. * Second parameter is used to instruct clients that the content has
  1674. * changed, but semantically, it can be used as the same thing. Think
  1675. * for instance of a page with a hit counter, two different page views
  1676. * are equivalent, but they differ by a few bytes. This leaves off to
  1677. * the Client the decision of using or not the cached page.
  1678. *
  1679. * If no parameters are passed, current Etag header is returned.
  1680. *
  1681. * @param string|null $hash The unique hash that identifies this response
  1682. * @param bool $weak Whether the response is semantically the same as
  1683. * other with the same hash or not
  1684. * @return string|null
  1685. * @deprecated 3.4.0 Use withEtag() instead.
  1686. */
  1687. public function etag($hash = null, $weak = false)
  1688. {
  1689. deprecationWarning(
  1690. 'Response::etag() is deprecated. ' .
  1691. 'Use withEtag() or getHeaderLine("Etag") instead.'
  1692. );
  1693. if ($hash !== null) {
  1694. $this->_setHeader('Etag', sprintf('%s"%s"', $weak ? 'W/' : null, $hash));
  1695. }
  1696. if ($this->hasHeader('Etag')) {
  1697. return $this->getHeaderLine('Etag');
  1698. }
  1699. return null;
  1700. }
  1701. /**
  1702. * Create a new instance with the Etag header set.
  1703. *
  1704. * Etags are a strong indicative that a response can be cached by a
  1705. * HTTP client. A bad way of generating Etags is creating a hash of
  1706. * the response output, instead generate a unique hash of the
  1707. * unique components that identifies a request, such as a
  1708. * modification time, a resource Id, and anything else you consider it
  1709. * that makes the response unique.
  1710. *
  1711. * The second parameter is used to inform clients that the content has
  1712. * changed, but semantically it is equivalent to existing cached values. Consider
  1713. * a page with a hit counter, two different page views are equivalent, but
  1714. * they differ by a few bytes. This permits the Client to decide whether they should
  1715. * use the cached data.
  1716. *
  1717. * @param string $hash The unique hash that identifies this response
  1718. * @param bool $weak Whether the response is semantically the same as
  1719. * other with the same hash or not. Defaults to false
  1720. * @return static
  1721. */
  1722. public function withEtag($hash, $weak = false)
  1723. {
  1724. $hash = sprintf('%s"%s"', $weak ? 'W/' : null, $hash);
  1725. return $this->withHeader('Etag', $hash);
  1726. }
  1727. /**
  1728. * Returns a DateTime object initialized at the $time param and using UTC
  1729. * as timezone
  1730. *
  1731. * @param string|int|\DateTimeInterface|null $time Valid time string or \DateTimeInterface instance.
  1732. * @return \DateTimeInterface
  1733. */
  1734. protected function _getUTCDate($time = null)
  1735. {
  1736. if ($time instanceof DateTimeInterface) {
  1737. $result = clone $time;
  1738. } elseif (is_int($time)) {
  1739. $result = new DateTime(date('Y-m-d H:i:s', $time));
  1740. } else {
  1741. $result = new DateTime($time);
  1742. }
  1743. return $result->setTimezone(new DateTimeZone('UTC'));
  1744. }
  1745. /**
  1746. * Sets the correct output buffering handler to send a compressed response. Responses will
  1747. * be compressed with zlib, if the extension is available.
  1748. *
  1749. * @return bool false if client does not accept compressed responses or no handler is available, true otherwise
  1750. */
  1751. public function compress()
  1752. {
  1753. $compressionEnabled = ini_get('zlib.output_compression') !== '1' &&
  1754. extension_loaded('zlib') &&
  1755. (strpos(env('HTTP_ACCEPT_ENCODING'), 'gzip') !== false);
  1756. return $compressionEnabled && ob_start('ob_gzhandler');
  1757. }
  1758. /**
  1759. * Returns whether the resulting output will be compressed by PHP
  1760. *
  1761. * @return bool
  1762. */
  1763. public function outputCompressed()
  1764. {
  1765. return strpos(env('HTTP_ACCEPT_ENCODING'), 'gzip') !== false
  1766. && (ini_get('zlib.output_compression') === '1' || in_array('ob_gzhandler', ob_list_handlers()));
  1767. }
  1768. /**
  1769. * Sets the correct headers to instruct the browser to download the response as a file.
  1770. *
  1771. * @param string $filename The name of the file as the browser will download the response
  1772. * @return void
  1773. * @deprecated 3.4.0 Use withDownload() instead.
  1774. */
  1775. public function download($filename)
  1776. {
  1777. deprecationWarning(
  1778. 'Response::download() is deprecated. ' .
  1779. 'Use withDownload() instead.'
  1780. );
  1781. $this->header('Content-Disposition', 'attachment; filename="' . $filename . '"');
  1782. }
  1783. /**
  1784. * Create a new instance with the Content-Disposition header set.
  1785. *
  1786. * @param string $filename The name of the file as the browser will download the response
  1787. * @return static
  1788. */
  1789. public function withDownload($filename)
  1790. {
  1791. return $this->withHeader('Content-Disposition', 'attachment; filename="' . $filename . '"');
  1792. }
  1793. /**
  1794. * Sets the protocol to be used when sending the response. Defaults to HTTP/1.1
  1795. * If called with no arguments, it will return the current configured protocol
  1796. *
  1797. * @param string|null $protocol Protocol to be used for sending response.
  1798. * @return string Protocol currently set
  1799. * @deprecated 3.4.0 Use getProtocolVersion() instead.
  1800. */
  1801. public function protocol($protocol = null)
  1802. {
  1803. deprecationWarning(
  1804. 'Response::protocol() is deprecated. ' .
  1805. 'Use getProtocolVersion() instead.'
  1806. );
  1807. if ($protocol !== null) {
  1808. $this->_protocol = $protocol;
  1809. }
  1810. return $this->_protocol;
  1811. }
  1812. /**
  1813. * Sets the Content-Length header for the response
  1814. * If called with no arguments returns the last Content-Length set
  1815. *
  1816. * @param int|null $bytes Number of bytes
  1817. * @return string|null
  1818. * @deprecated 3.4.0 Use withLength() to set length instead.
  1819. */
  1820. public function length($bytes = null)
  1821. {
  1822. deprecationWarning(
  1823. 'Response::length() is deprecated. ' .
  1824. 'Use withLength() instead.'
  1825. );
  1826. if ($bytes !== null) {
  1827. $this->_setHeader('Content-Length', $bytes);
  1828. }
  1829. if ($this->hasHeader('Content-Length')) {
  1830. return $this->getHeaderLine('Content-Length');
  1831. }
  1832. return null;
  1833. }
  1834. /**
  1835. * Create a new response with the Content-Length header set.
  1836. *
  1837. * @param int|string $bytes Number of bytes
  1838. * @return static
  1839. */
  1840. public function withLength($bytes)
  1841. {
  1842. return $this->withHeader('Content-Length', (string)$bytes);
  1843. }
  1844. /**
  1845. * Create a new response with the Link header set.
  1846. *
  1847. * ### Examples
  1848. *
  1849. * ```
  1850. * $response = $response->withAddedLink('http://example.com?page=1', ['rel' => 'prev'])
  1851. * ->withAddedLink('http://example.com?page=3', ['rel' => 'next']);
  1852. * ```
  1853. *
  1854. * Will generate:
  1855. *
  1856. * ```
  1857. * Link: <http://example.com?page=1>; rel="prev"
  1858. * Link: <http://example.com?page=3>; rel="next"
  1859. * ```
  1860. *
  1861. * @param string $url The LinkHeader url.
  1862. * @param array $options The LinkHeader params.
  1863. * @return static
  1864. * @since 3.6.0
  1865. */
  1866. public function withAddedLink($url, $options = [])
  1867. {
  1868. $params = [];
  1869. foreach ($options as $key => $option) {
  1870. $params[] = $key . '="' . $option . '"';
  1871. }
  1872. $param = '';
  1873. if ($params) {
  1874. $param = '; ' . implode('; ', $params);
  1875. }
  1876. return $this->withAddedHeader('Link', '<' . $url . '>' . $param);
  1877. }
  1878. /**
  1879. * Checks whether a response has not been modified according to the 'If-None-Match'
  1880. * (Etags) and 'If-Modified-Since' (last modification date) request
  1881. * headers. If the response is detected to be not modified, it
  1882. * is marked as so accordingly so the client can be informed of that.
  1883. *
  1884. * In order to mark a response as not modified, you need to set at least
  1885. * the Last-Modified etag response header before calling this method. Otherwise
  1886. * a comparison will not be possible.
  1887. *
  1888. * *Warning* This method mutates the response in-place and should be avoided.
  1889. *
  1890. * @param \Cake\Http\ServerRequest $request Request object
  1891. * @return bool Whether the response was marked as not modified or not.
  1892. */
  1893. public function checkNotModified(ServerRequest $request)
  1894. {
  1895. $etags = preg_split('/\s*,\s*/', (string)$request->getHeaderLine('If-None-Match'), 0, PREG_SPLIT_NO_EMPTY);
  1896. $responseTag = $this->getHeaderLine('Etag');
  1897. $etagMatches = null;
  1898. if ($responseTag) {
  1899. $etagMatches = in_array('*', $etags) || in_array($responseTag, $etags);
  1900. }
  1901. $modifiedSince = $request->getHeaderLine('If-Modified-Since');
  1902. $timeMatches = null;
  1903. if ($modifiedSince && $this->hasHeader('Last-Modified')) {
  1904. $timeMatches = strtotime($this->getHeaderLine('Last-Modified')) === strtotime($modifiedSince);
  1905. }
  1906. if ($etagMatches === null && $timeMatches === null) {
  1907. return false;
  1908. }
  1909. $notModified = $etagMatches !== false && $timeMatches !== false;
  1910. if ($notModified) {
  1911. $this->notModified();
  1912. }
  1913. return $notModified;
  1914. }
  1915. /**
  1916. * String conversion. Fetches the response body as a string.
  1917. * Does *not* send headers.
  1918. * If body is a callable, a blank string is returned.
  1919. *
  1920. * @return string
  1921. */
  1922. public function __toString()
  1923. {
  1924. $this->stream->rewind();
  1925. return (string)$this->stream->getContents();
  1926. }
  1927. /**
  1928. * Getter/Setter for cookie configs
  1929. *
  1930. * This method acts as a setter/getter depending on the type of the argument.
  1931. * If the method is called with no arguments, it returns all configurations.
  1932. *
  1933. * If the method is called with a string as argument, it returns either the
  1934. * given configuration if it is set, or null, if it's not set.
  1935. *
  1936. * If the method is called with an array as argument, it will set the cookie
  1937. * configuration to the cookie container.
  1938. *
  1939. * ### Options (when setting a configuration)
  1940. * - name: The Cookie name
  1941. * - value: Value of the cookie
  1942. * - expire: Time the cookie expires in
  1943. * - path: Path the cookie applies to
  1944. * - domain: Domain the cookie is for.
  1945. * - secure: Is the cookie https?
  1946. * - httpOnly: Is the cookie available in the client?
  1947. *
  1948. * ### Examples
  1949. *
  1950. * ### Getting all cookies
  1951. *
  1952. * `$this->cookie()`
  1953. *
  1954. * ### Getting a certain cookie configuration
  1955. *
  1956. * `$this->cookie('MyCookie')`
  1957. *
  1958. * ### Setting a cookie configuration
  1959. *
  1960. * `$this->cookie((array) $options)`
  1961. *
  1962. * @param array|null $options Either null to get all cookies, string for a specific cookie
  1963. * or array to set cookie.
  1964. * @return mixed
  1965. * @deprecated 3.4.0 Use getCookie(), getCookies() and withCookie() instead.
  1966. */
  1967. public function cookie($options = null)
  1968. {
  1969. deprecationWarning(
  1970. 'Response::cookie() is deprecated. ' .
  1971. 'Use getCookie(), getCookies() and withCookie() instead.'
  1972. );
  1973. if ($options === null) {
  1974. return $this->getCookies();
  1975. }
  1976. if (is_string($options)) {
  1977. if (!$this->_cookies->has($options)) {
  1978. return null;
  1979. }
  1980. $cookie = $this->_cookies->get($options);
  1981. return $this->convertCookieToArray($cookie);
  1982. }
  1983. $options += [
  1984. 'name' => 'CakeCookie[default]',
  1985. 'value' => '',
  1986. 'expire' => 0,
  1987. 'path' => '/',
  1988. 'domain' => '',
  1989. 'secure' => false,
  1990. 'httpOnly' => false
  1991. ];
  1992. $expires = $options['expire'] ? new DateTime('@' . $options['expire']) : null;
  1993. $cookie = new Cookie(
  1994. $options['name'],
  1995. $options['value'],
  1996. $expires,
  1997. $options['path'],
  1998. $options['domain'],
  1999. $options['secure'],
  2000. $options['httpOnly']
  2001. );
  2002. $this->_cookies = $this->_cookies->add($cookie);
  2003. }
  2004. /**
  2005. * Create a new response with a cookie set.
  2006. *
  2007. * ### Data
  2008. *
  2009. * - `value`: Value of the cookie
  2010. * - `expire`: Time the cookie expires in
  2011. * - `path`: Path the cookie applies to
  2012. * - `domain`: Domain the cookie is for.
  2013. * - `secure`: Is the cookie https?
  2014. * - `httpOnly`: Is the cookie available in the client?
  2015. *
  2016. * ### Examples
  2017. *
  2018. * ```
  2019. * // set scalar value with defaults
  2020. * $response = $response->withCookie('remember_me', 1);
  2021. *
  2022. * // customize cookie attributes
  2023. * $response = $response->withCookie('remember_me', ['path' => '/login']);
  2024. *
  2025. * // add a cookie object
  2026. * $response = $response->withCookie(new Cookie('remember_me', 1));
  2027. * ```
  2028. *
  2029. * @param string|\Cake\Http\Cookie\Cookie $name The name of the cookie to set, or a cookie object
  2030. * @param array|string $data Either a string value, or an array of cookie options.
  2031. * @return static
  2032. */
  2033. public function withCookie($name, $data = '')
  2034. {
  2035. if ($name instanceof Cookie) {
  2036. $cookie = $name;
  2037. } else {
  2038. deprecationWarning(
  2039. get_called_class() . '::withCookie(string $name, array $data) is deprecated. ' .
  2040. 'Pass an instance of \Cake\Http\Cookie\Cookie instead.'
  2041. );
  2042. if (!is_array($data)) {
  2043. $data = ['value' => $data];
  2044. }
  2045. $data += [
  2046. 'value' => '',
  2047. 'expire' => 0,
  2048. 'path' => '/',
  2049. 'domain' => '',
  2050. 'secure' => false,
  2051. 'httpOnly' => false
  2052. ];
  2053. $expires = $data['expire'] ? new DateTime('@' . $data['expire']) : null;
  2054. $cookie = new Cookie(
  2055. $name,
  2056. $data['value'],
  2057. $expires,
  2058. $data['path'],
  2059. $data['domain'],
  2060. $data['secure'],
  2061. $data['httpOnly']
  2062. );
  2063. }
  2064. $new = clone $this;
  2065. $new->_cookies = $new->_cookies->add($cookie);
  2066. return $new;
  2067. }
  2068. /**
  2069. * Create a new response with an expired cookie set.
  2070. *
  2071. * ### Options
  2072. *
  2073. * - `path`: Path the cookie applies to
  2074. * - `domain`: Domain the cookie is for.
  2075. * - `secure`: Is the cookie https?
  2076. * - `httpOnly`: Is the cookie available in the client?
  2077. *
  2078. * ### Examples
  2079. *
  2080. * ```
  2081. * // set scalar value with defaults
  2082. * $response = $response->withExpiredCookie('remember_me');
  2083. *
  2084. * // customize cookie attributes
  2085. * $response = $response->withExpiredCookie('remember_me', ['path' => '/login']);
  2086. *
  2087. * // add a cookie object
  2088. * $response = $response->withExpiredCookie(new Cookie('remember_me'));
  2089. * ```
  2090. *
  2091. * @param string|\Cake\Http\Cookie\CookieInterface $name The name of the cookie to expire, or a cookie object
  2092. * @param array $options An array of cookie options.
  2093. * @return static
  2094. */
  2095. public function withExpiredCookie($name, $options = [])
  2096. {
  2097. if ($name instanceof CookieInterface) {
  2098. $cookie = $name->withExpired();
  2099. } else {
  2100. deprecationWarning(
  2101. get_called_class() . '::withExpiredCookie(string $name, array $data) is deprecated. ' .
  2102. 'Pass an instance of \Cake\Http\Cookie\Cookie instead.'
  2103. );
  2104. $options += [
  2105. 'path' => '/',
  2106. 'domain' => '',
  2107. 'secure' => false,
  2108. 'httpOnly' => false
  2109. ];
  2110. $cookie = new Cookie(
  2111. $name,
  2112. '',
  2113. DateTime::createFromFormat('U', 1),
  2114. $options['path'],
  2115. $options['domain'],
  2116. $options['secure'],
  2117. $options['httpOnly']
  2118. );
  2119. }
  2120. $new = clone $this;
  2121. $new->_cookies = $new->_cookies->add($cookie);
  2122. return $new;
  2123. }
  2124. /**
  2125. * Read a single cookie from the response.
  2126. *
  2127. * This method provides read access to pending cookies. It will
  2128. * not read the `Set-Cookie` header if set.
  2129. *
  2130. * @param string $name The cookie name you want to read.
  2131. * @return array|null Either the cookie data or null
  2132. */
  2133. public function getCookie($name)
  2134. {
  2135. if (!$this->_cookies->has($name)) {
  2136. return null;
  2137. }
  2138. $cookie = $this->_cookies->get($name);
  2139. return $this->convertCookieToArray($cookie);
  2140. }
  2141. /**
  2142. * Get all cookies in the response.
  2143. *
  2144. * Returns an associative array of cookie name => cookie data.
  2145. *
  2146. * @return array
  2147. */
  2148. public function getCookies()
  2149. {
  2150. $out = [];
  2151. foreach ($this->_cookies as $cookie) {
  2152. $out[$cookie->getName()] = $this->convertCookieToArray($cookie);
  2153. }
  2154. return $out;
  2155. }
  2156. /**
  2157. * Convert the cookie into an array of its properties.
  2158. *
  2159. * This method is compatible with the historical behavior of Cake\Http\Response,
  2160. * where `httponly` is `httpOnly` and `expires` is `expire`
  2161. *
  2162. * @param \Cake\Http\Cookie\CookieInterface $cookie Cookie object.
  2163. * @return array
  2164. */
  2165. protected function convertCookieToArray(CookieInterface $cookie)
  2166. {
  2167. return [
  2168. 'name' => $cookie->getName(),
  2169. 'value' => $cookie->getStringValue(),
  2170. 'path' => $cookie->getPath(),
  2171. 'domain' => $cookie->getDomain(),
  2172. 'secure' => $cookie->isSecure(),
  2173. 'httpOnly' => $cookie->isHttpOnly(),
  2174. 'expire' => $cookie->getExpiresTimestamp()
  2175. ];
  2176. }
  2177. /**
  2178. * Get the CookieCollection from the response
  2179. *
  2180. * @return \Cake\Http\Cookie\CookieCollection
  2181. */
  2182. public function getCookieCollection()
  2183. {
  2184. return $this->_cookies;
  2185. }
  2186. /**
  2187. * Get a new instance with provided cookie collection.
  2188. *
  2189. * @param \Cake\Http\Cookie\CookieCollection $cookieCollection Cookie collection to set.
  2190. * @return static
  2191. */
  2192. public function withCookieCollection(CookieCollection $cookieCollection)
  2193. {
  2194. $new = clone $this;
  2195. $new->_cookies = $cookieCollection;
  2196. return $new;
  2197. }
  2198. /**
  2199. * Setup access for origin and methods on cross origin requests
  2200. *
  2201. * This method allow multiple ways to setup the domains, see the examples
  2202. *
  2203. * ### Full URI
  2204. * ```
  2205. * cors($request, 'https://www.cakephp.org');
  2206. * ```
  2207. *
  2208. * ### URI with wildcard
  2209. * ```
  2210. * cors($request, 'https://*.cakephp.org');
  2211. * ```
  2212. *
  2213. * ### Ignoring the requested protocol
  2214. * ```
  2215. * cors($request, 'www.cakephp.org');
  2216. * ```
  2217. *
  2218. * ### Any URI
  2219. * ```
  2220. * cors($request, '*');
  2221. * ```
  2222. *
  2223. * ### Whitelist of URIs
  2224. * ```
  2225. * cors($request, ['http://www.cakephp.org', '*.google.com', 'https://myproject.github.io']);
  2226. * ```
  2227. *
  2228. * *Note* The `$allowedDomains`, `$allowedMethods`, `$allowedHeaders` parameters are deprecated.
  2229. * Instead the builder object should be used.
  2230. *
  2231. * @param \Cake\Http\ServerRequest $request Request object
  2232. * @param string|array $allowedDomains List of allowed domains, see method description for more details
  2233. * @param string|array $allowedMethods List of HTTP verbs allowed
  2234. * @param string|array $allowedHeaders List of HTTP headers allowed
  2235. * @return \Cake\Http\CorsBuilder A builder object the provides a fluent interface for defining
  2236. * additional CORS headers.
  2237. */
  2238. public function cors(ServerRequest $request, $allowedDomains = [], $allowedMethods = [], $allowedHeaders = [])
  2239. {
  2240. $origin = $request->getHeaderLine('Origin');
  2241. $ssl = $request->is('ssl');
  2242. $builder = new CorsBuilder($this, $origin, $ssl);
  2243. if (!$origin) {
  2244. return $builder;
  2245. }
  2246. if (empty($allowedDomains) && empty($allowedMethods) && empty($allowedHeaders)) {
  2247. return $builder;
  2248. }
  2249. deprecationWarning(
  2250. 'The $allowedDomains, $allowedMethods, and $allowedHeaders parameters of Response::cors() ' .
  2251. 'are deprecated. Instead you should use the builder methods on the return of cors().'
  2252. );
  2253. $updated = $builder->allowOrigin($allowedDomains)
  2254. ->allowMethods((array)$allowedMethods)
  2255. ->allowHeaders((array)$allowedHeaders)
  2256. ->build();
  2257. // If $updated is a new instance, mutate this object in-place
  2258. // to retain existing behavior.
  2259. if ($updated !== $this) {
  2260. foreach ($updated->getHeaders() as $name => $values) {
  2261. if (!$this->hasHeader($name)) {
  2262. $this->_setHeader($name, $values[0]);
  2263. }
  2264. }
  2265. }
  2266. return $builder;
  2267. }
  2268. /**
  2269. * Setup for display or download the given file.
  2270. *
  2271. * If $_SERVER['HTTP_RANGE'] is set a slice of the file will be
  2272. * returned instead of the entire file.
  2273. *
  2274. * ### Options keys
  2275. *
  2276. * - name: Alternate download name
  2277. * - download: If `true` sets download header and forces file to be downloaded rather than displayed in browser
  2278. *
  2279. * @param string $path Path to file. If the path is not an absolute path that resolves
  2280. * to a file, `APP` will be prepended to the path (this behavior is deprecated).
  2281. * @param array $options Options See above.
  2282. * @return void
  2283. * @throws \Cake\Http\Exception\NotFoundException
  2284. * @deprecated 3.4.0 Use withFile() instead.
  2285. */
  2286. public function file($path, array $options = [])
  2287. {
  2288. deprecationWarning(
  2289. 'Response::file() is deprecated. ' .
  2290. 'Use withFile() instead.'
  2291. );
  2292. $file = $this->validateFile($path);
  2293. $options += [
  2294. 'name' => null,
  2295. 'download' => null
  2296. ];
  2297. $extension = strtolower($file->ext());
  2298. $download = $options['download'];
  2299. if ((!$extension || $this->type($extension) === false) && $download === null) {
  2300. $download = true;
  2301. }
  2302. $fileSize = $file->size();
  2303. if ($download) {
  2304. $agent = env('HTTP_USER_AGENT');
  2305. if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent)) {
  2306. $contentType = 'application/octet-stream';
  2307. } elseif (preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) {
  2308. $contentType = 'application/force-download';
  2309. }
  2310. if (!empty($contentType)) {
  2311. $this->type($contentType);
  2312. }
  2313. if ($options['name'] === null) {
  2314. $name = $file->name;
  2315. } else {
  2316. $name = $options['name'];
  2317. }
  2318. $this->download($name);
  2319. $this->header('Content-Transfer-Encoding', 'binary');
  2320. }
  2321. $this->header('Accept-Ranges', 'bytes');
  2322. $httpRange = env('HTTP_RANGE');
  2323. if (isset($httpRange)) {
  2324. $this->_fileRange($file, $httpRange);
  2325. } else {
  2326. $this->header('Content-Length', $fileSize);
  2327. }
  2328. $this->_file = $file;
  2329. $this->stream = new Stream($file->path, 'rb');
  2330. }
  2331. /**
  2332. * Create a new instance that is based on a file.
  2333. *
  2334. * This method will augment both the body and a number of related headers.
  2335. *
  2336. * If `$_SERVER['HTTP_RANGE']` is set, a slice of the file will be
  2337. * returned instead of the entire file.
  2338. *
  2339. * ### Options keys
  2340. *
  2341. * - name: Alternate download name
  2342. * - download: If `true` sets download header and forces file to
  2343. * be downloaded rather than displayed inline.
  2344. *
  2345. * @param string $path Path to file. If the path is not an absolute path that resolves
  2346. * to a file, `APP` will be prepended to the path (this behavior is deprecated).
  2347. * @param array $options Options See above.
  2348. * @return static
  2349. * @throws \Cake\Http\Exception\NotFoundException
  2350. */
  2351. public function withFile($path, array $options = [])
  2352. {
  2353. $file = $this->validateFile($path);
  2354. $options += [
  2355. 'name' => null,
  2356. 'download' => null
  2357. ];
  2358. $extension = strtolower($file->ext());
  2359. $mapped = $this->getMimeType($extension);
  2360. if ((!$extension || !$mapped) && $options['download'] === null) {
  2361. $options['download'] = true;
  2362. }
  2363. $new = clone $this;
  2364. if ($mapped) {
  2365. $new = $new->withType($extension);
  2366. }
  2367. $fileSize = $file->size();
  2368. if ($options['download']) {
  2369. $agent = env('HTTP_USER_AGENT');
  2370. if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent)) {
  2371. $contentType = 'application/octet-stream';
  2372. } elseif (preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) {
  2373. $contentType = 'application/force-download';
  2374. }
  2375. if (isset($contentType)) {
  2376. $new = $new->withType($contentType);
  2377. }
  2378. $name = $options['name'] ?: $file->name;
  2379. $new = $new->withDownload($name)
  2380. ->withHeader('Content-Transfer-Encoding', 'binary');
  2381. }
  2382. $new = $new->withHeader('Accept-Ranges', 'bytes');
  2383. $httpRange = env('HTTP_RANGE');
  2384. if (isset($httpRange)) {
  2385. $new->_fileRange($file, $httpRange);
  2386. } else {
  2387. $new = $new->withHeader('Content-Length', (string)$fileSize);
  2388. }
  2389. $new->_file = $file;
  2390. $new->stream = new Stream($file->path, 'rb');
  2391. return $new;
  2392. }
  2393. /**
  2394. * Convenience method to set a string into the response body
  2395. *
  2396. * @param string $string The string to be sent
  2397. * @return static
  2398. */
  2399. public function withStringBody($string)
  2400. {
  2401. $new = clone $this;
  2402. $new->_createStream();
  2403. $new->stream->write((string)$string);
  2404. return $new;
  2405. }
  2406. /**
  2407. * Validate a file path is a valid response body.
  2408. *
  2409. * @param string $path The path to the file.
  2410. * @throws \Cake\Http\Exception\NotFoundException
  2411. * @return \Cake\Filesystem\File
  2412. */
  2413. protected function validateFile($path)
  2414. {
  2415. if (strpos($path, '../') !== false || strpos($path, '..\\') !== false) {
  2416. throw new NotFoundException(__d('cake', 'The requested file contains `..` and will not be read.'));
  2417. }
  2418. if (!is_file($path)) {
  2419. deprecationWarning(
  2420. 'Automatic prefixing of paths with `APP` by `Response::file()` and `withFile()` is deprecated. ' .
  2421. 'Use absolute paths instead.'
  2422. );
  2423. $path = APP . $path;
  2424. }
  2425. if (!Folder::isAbsolute($path)) {
  2426. deprecationWarning(
  2427. 'Serving files via `file()` or `withFile()` using relative paths is deprecated.' .
  2428. 'Use an absolute path instead.'
  2429. );
  2430. }
  2431. $file = new File($path);
  2432. if (!$file->exists() || !$file->readable()) {
  2433. if (Configure::read('debug')) {
  2434. throw new NotFoundException(sprintf('The requested file %s was not found or not readable', $path));
  2435. }
  2436. throw new NotFoundException(__d('cake', 'The requested file was not found'));
  2437. }
  2438. return $file;
  2439. }
  2440. /**
  2441. * Get the current file if one exists.
  2442. *
  2443. * @return \Cake\Filesystem\File|null The file to use in the response or null
  2444. */
  2445. public function getFile()
  2446. {
  2447. return $this->_file;
  2448. }
  2449. /**
  2450. * Apply a file range to a file and set the end offset.
  2451. *
  2452. * If an invalid range is requested a 416 Status code will be used
  2453. * in the response.
  2454. *
  2455. * @param \Cake\Filesystem\File $file The file to set a range on.
  2456. * @param string $httpRange The range to use.
  2457. * @return void
  2458. * @deprecated 3.4.0 Long term this needs to be refactored to follow immutable paradigms.
  2459. * However for now, it is simpler to leave this alone.
  2460. */
  2461. protected function _fileRange($file, $httpRange)
  2462. {
  2463. $fileSize = $file->size();
  2464. $lastByte = $fileSize - 1;
  2465. $start = 0;
  2466. $end = $lastByte;
  2467. preg_match('/^bytes\s*=\s*(\d+)?\s*-\s*(\d+)?$/', $httpRange, $matches);
  2468. if ($matches) {
  2469. $start = $matches[1];
  2470. $end = isset($matches[2]) ? $matches[2] : '';
  2471. }
  2472. if ($start === '') {
  2473. $start = $fileSize - $end;
  2474. $end = $lastByte;
  2475. }
  2476. if ($end === '') {
  2477. $end = $lastByte;
  2478. }
  2479. if ($start > $end || $end > $lastByte || $start > $lastByte) {
  2480. $this->_setStatus(416);
  2481. $this->_setHeader('Content-Range', 'bytes 0-' . $lastByte . '/' . $fileSize);
  2482. return;
  2483. }
  2484. $this->_setHeader('Content-Length', $end - $start + 1);
  2485. $this->_setHeader('Content-Range', 'bytes ' . $start . '-' . $end . '/' . $fileSize);
  2486. $this->_setStatus(206);
  2487. $this->_fileRange = [$start, $end];
  2488. }
  2489. /**
  2490. * Reads out a file, and echos the content to the client.
  2491. *
  2492. * @param \Cake\Filesystem\File $file File object
  2493. * @param array $range The range to read out of the file.
  2494. * @return bool True is whole file is echoed successfully or false if client connection is lost in between
  2495. * @deprecated 3.4.0 Will be removed in 4.0.0
  2496. */
  2497. protected function _sendFile($file, $range)
  2498. {
  2499. deprecationWarning('Will be removed in 4.0.0');
  2500. ob_implicit_flush(true);
  2501. $file->open('rb');
  2502. $end = $start = false;
  2503. if ($range) {
  2504. list($start, $end) = $range;
  2505. }
  2506. if ($start !== false) {
  2507. $file->offset($start);
  2508. }
  2509. $bufferSize = 8192;
  2510. if (strpos(ini_get('disable_functions'), 'set_time_limit') === false) {
  2511. set_time_limit(0);
  2512. }
  2513. session_write_close();
  2514. while (!feof($file->handle)) {
  2515. if (!$this->_isActive()) {
  2516. $file->close();
  2517. return false;
  2518. }
  2519. $offset = $file->offset();
  2520. if ($end && $offset >= $end) {
  2521. break;
  2522. }
  2523. if ($end && $offset + $bufferSize >= $end) {
  2524. $bufferSize = $end - $offset + 1;
  2525. }
  2526. echo fread($file->handle, $bufferSize);
  2527. }
  2528. $file->close();
  2529. return true;
  2530. }
  2531. /**
  2532. * Returns true if connection is still active
  2533. *
  2534. * @return bool
  2535. * @deprecated 3.4.0 Will be removed in 4.0.0
  2536. */
  2537. protected function _isActive()
  2538. {
  2539. deprecationWarning('Will be removed in 4.0.0');
  2540. return connection_status() === CONNECTION_NORMAL && !connection_aborted();
  2541. }
  2542. /**
  2543. * Clears the contents of the topmost output buffer and discards them
  2544. *
  2545. * @return bool
  2546. * @deprecated 3.2.4 This function is not needed anymore
  2547. */
  2548. protected function _clearBuffer()
  2549. {
  2550. deprecationWarning(
  2551. 'This function is not needed anymore and will be removed.'
  2552. );
  2553. //@codingStandardsIgnoreStart
  2554. return @ob_end_clean();
  2555. //@codingStandardsIgnoreEnd
  2556. }
  2557. /**
  2558. * Flushes the contents of the output buffer
  2559. *
  2560. * @return void
  2561. * @deprecated 3.2.4 This function is not needed anymore
  2562. */
  2563. protected function _flushBuffer()
  2564. {
  2565. deprecationWarning(
  2566. 'This function is not needed anymore and will be removed.'
  2567. );
  2568. //@codingStandardsIgnoreStart
  2569. @flush();
  2570. if (ob_get_level()) {
  2571. @ob_flush();
  2572. }
  2573. //@codingStandardsIgnoreEnd
  2574. }
  2575. /**
  2576. * Stop execution of the current script. Wraps exit() making
  2577. * testing easier.
  2578. *
  2579. * @param int|string $status See https://secure.php.net/exit for values
  2580. * @return void
  2581. * @deprecated 3.4.0 Will be removed in 4.0.0
  2582. */
  2583. public function stop($status = 0)
  2584. {
  2585. deprecationWarning('Will be removed in 4.0.0');
  2586. exit($status);
  2587. }
  2588. /**
  2589. * Returns an array that can be used to describe the internal state of this
  2590. * object.
  2591. *
  2592. * @return array
  2593. */
  2594. public function __debugInfo()
  2595. {
  2596. return [
  2597. 'status' => $this->_status,
  2598. 'contentType' => $this->_contentType,
  2599. 'headers' => $this->headers,
  2600. 'file' => $this->_file,
  2601. 'fileRange' => $this->_fileRange,
  2602. 'cookies' => $this->_cookies,
  2603. 'cacheDirectives' => $this->_cacheDirectives,
  2604. 'body' => (string)$this->getBody(),
  2605. ];
  2606. }
  2607. }
  2608. // @deprecated 3.4.0 Add backwards compat alias.
  2609. class_alias('Cake\Http\Response', 'Cake\Network\Response');