Response.php 86 KB

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