Response.php 91 KB

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