web.py 144 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824
  1. #
  2. # Copyright 2009 Facebook
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. """``tornado.web`` provides a simple web framework with asynchronous
  16. features that allow it to scale to large numbers of open connections,
  17. making it ideal for `long polling
  18. <http://en.wikipedia.org/wiki/Push_technology#Long_polling>`_.
  19. Here is a simple "Hello, world" example app:
  20. .. testcode::
  21. import asyncio
  22. import tornado
  23. class MainHandler(tornado.web.RequestHandler):
  24. def get(self):
  25. self.write("Hello, world")
  26. async def main():
  27. application = tornado.web.Application([
  28. (r"/", MainHandler),
  29. ])
  30. application.listen(8888)
  31. await asyncio.Event().wait()
  32. if __name__ == "__main__":
  33. asyncio.run(main())
  34. See the :doc:`guide` for additional information.
  35. Thread-safety notes
  36. -------------------
  37. In general, methods on `RequestHandler` and elsewhere in Tornado are
  38. not thread-safe. In particular, methods such as
  39. `~RequestHandler.write()`, `~RequestHandler.finish()`, and
  40. `~RequestHandler.flush()` must only be called from the main thread. If
  41. you use multiple threads it is important to use `.IOLoop.add_callback`
  42. to transfer control back to the main thread before finishing the
  43. request, or to limit your use of other threads to
  44. `.IOLoop.run_in_executor` and ensure that your callbacks running in
  45. the executor do not refer to Tornado objects.
  46. """
  47. import base64
  48. import binascii
  49. import datetime
  50. import email.utils
  51. import functools
  52. import gzip
  53. import hashlib
  54. import hmac
  55. import http.cookies
  56. from inspect import isclass
  57. from io import BytesIO
  58. import mimetypes
  59. import numbers
  60. import os.path
  61. import re
  62. import socket
  63. import sys
  64. import threading
  65. import time
  66. import warnings
  67. import tornado
  68. import traceback
  69. import types
  70. import urllib.parse
  71. from urllib.parse import urlencode
  72. from tornado.concurrent import Future, future_set_result_unless_cancelled
  73. from tornado import escape
  74. from tornado import gen
  75. from tornado.httpserver import HTTPServer
  76. from tornado import httputil
  77. from tornado import iostream
  78. from tornado import locale
  79. from tornado.log import access_log, app_log, gen_log
  80. from tornado import template
  81. from tornado.escape import utf8, _unicode
  82. from tornado.routing import (
  83. AnyMatches,
  84. DefaultHostMatches,
  85. HostMatches,
  86. ReversibleRouter,
  87. Rule,
  88. ReversibleRuleRouter,
  89. URLSpec,
  90. _RuleList,
  91. )
  92. from tornado.util import ObjectDict, unicode_type, _websocket_mask
  93. url = URLSpec
  94. from typing import (
  95. Dict,
  96. Any,
  97. Union,
  98. Optional,
  99. Awaitable,
  100. Tuple,
  101. List,
  102. Callable,
  103. Iterable,
  104. Generator,
  105. Type,
  106. TypeVar,
  107. cast,
  108. overload,
  109. )
  110. from types import TracebackType
  111. import typing
  112. if typing.TYPE_CHECKING:
  113. from typing import Set # noqa: F401
  114. # The following types are accepted by RequestHandler.set_header
  115. # and related methods.
  116. _HeaderTypes = Union[bytes, unicode_type, int, numbers.Integral, datetime.datetime]
  117. _CookieSecretTypes = Union[str, bytes, Dict[int, str], Dict[int, bytes]]
  118. MIN_SUPPORTED_SIGNED_VALUE_VERSION = 1
  119. """The oldest signed value version supported by this version of Tornado.
  120. Signed values older than this version cannot be decoded.
  121. .. versionadded:: 3.2.1
  122. """
  123. MAX_SUPPORTED_SIGNED_VALUE_VERSION = 2
  124. """The newest signed value version supported by this version of Tornado.
  125. Signed values newer than this version cannot be decoded.
  126. .. versionadded:: 3.2.1
  127. """
  128. DEFAULT_SIGNED_VALUE_VERSION = 2
  129. """The signed value version produced by `.RequestHandler.create_signed_value`.
  130. May be overridden by passing a ``version`` keyword argument.
  131. .. versionadded:: 3.2.1
  132. """
  133. DEFAULT_SIGNED_VALUE_MIN_VERSION = 1
  134. """The oldest signed value accepted by `.RequestHandler.get_signed_cookie`.
  135. May be overridden by passing a ``min_version`` keyword argument.
  136. .. versionadded:: 3.2.1
  137. """
  138. class _ArgDefaultMarker:
  139. pass
  140. _ARG_DEFAULT = _ArgDefaultMarker()
  141. class RequestHandler:
  142. """Base class for HTTP request handlers.
  143. Subclasses must define at least one of the methods defined in the
  144. "Entry points" section below.
  145. Applications should not construct `RequestHandler` objects
  146. directly and subclasses should not override ``__init__`` (override
  147. `~RequestHandler.initialize` instead).
  148. """
  149. SUPPORTED_METHODS: Tuple[str, ...] = (
  150. "GET",
  151. "HEAD",
  152. "POST",
  153. "DELETE",
  154. "PATCH",
  155. "PUT",
  156. "OPTIONS",
  157. )
  158. _template_loaders = {} # type: Dict[str, template.BaseLoader]
  159. _template_loader_lock = threading.Lock()
  160. _remove_control_chars_regex = re.compile(r"[\x00-\x08\x0e-\x1f]")
  161. _stream_request_body = False
  162. # Will be set in _execute.
  163. _transforms = None # type: List[OutputTransform]
  164. path_args = None # type: List[str]
  165. path_kwargs = None # type: Dict[str, str]
  166. def __init__(
  167. self,
  168. application: "Application",
  169. request: httputil.HTTPServerRequest,
  170. **kwargs: Any,
  171. ) -> None:
  172. super().__init__()
  173. self.application = application
  174. self.request = request
  175. self._headers_written = False
  176. self._finished = False
  177. self._auto_finish = True
  178. self._prepared_future = None
  179. self.ui = ObjectDict(
  180. (n, self._ui_method(m)) for n, m in application.ui_methods.items()
  181. )
  182. # UIModules are available as both `modules` and `_tt_modules` in the
  183. # template namespace. Historically only `modules` was available
  184. # but could be clobbered by user additions to the namespace.
  185. # The template {% module %} directive looks in `_tt_modules` to avoid
  186. # possible conflicts.
  187. self.ui["_tt_modules"] = _UIModuleNamespace(self, application.ui_modules)
  188. self.ui["modules"] = self.ui["_tt_modules"]
  189. self.clear()
  190. assert self.request.connection is not None
  191. # TODO: need to add set_close_callback to HTTPConnection interface
  192. self.request.connection.set_close_callback( # type: ignore
  193. self.on_connection_close
  194. )
  195. self.initialize(**kwargs) # type: ignore
  196. def _initialize(self) -> None:
  197. pass
  198. initialize = _initialize # type: Callable[..., None]
  199. """Hook for subclass initialization. Called for each request.
  200. A dictionary passed as the third argument of a ``URLSpec`` will be
  201. supplied as keyword arguments to ``initialize()``.
  202. Example::
  203. class ProfileHandler(RequestHandler):
  204. def initialize(self, database):
  205. self.database = database
  206. def get(self, username):
  207. ...
  208. app = Application([
  209. (r'/user/(.*)', ProfileHandler, dict(database=database)),
  210. ])
  211. """
  212. @property
  213. def settings(self) -> Dict[str, Any]:
  214. """An alias for `self.application.settings <Application.settings>`."""
  215. return self.application.settings
  216. def _unimplemented_method(self, *args: str, **kwargs: str) -> None:
  217. raise HTTPError(405)
  218. head = _unimplemented_method # type: Callable[..., Optional[Awaitable[None]]]
  219. get = _unimplemented_method # type: Callable[..., Optional[Awaitable[None]]]
  220. post = _unimplemented_method # type: Callable[..., Optional[Awaitable[None]]]
  221. delete = _unimplemented_method # type: Callable[..., Optional[Awaitable[None]]]
  222. patch = _unimplemented_method # type: Callable[..., Optional[Awaitable[None]]]
  223. put = _unimplemented_method # type: Callable[..., Optional[Awaitable[None]]]
  224. options = _unimplemented_method # type: Callable[..., Optional[Awaitable[None]]]
  225. def prepare(self) -> Optional[Awaitable[None]]:
  226. """Called at the beginning of a request before `get`/`post`/etc.
  227. Override this method to perform common initialization regardless
  228. of the request method. There is no guarantee that ``prepare`` will
  229. be called if an error occurs that is handled by the framework.
  230. Asynchronous support: Use ``async def`` or decorate this method with
  231. `.gen.coroutine` to make it asynchronous.
  232. If this method returns an ``Awaitable`` execution will not proceed
  233. until the ``Awaitable`` is done.
  234. .. versionadded:: 3.1
  235. Asynchronous support.
  236. """
  237. pass
  238. def on_finish(self) -> None:
  239. """Called after the end of a request.
  240. Override this method to perform cleanup, logging, etc. This method is primarily intended as
  241. a counterpart to `prepare`. However, there are a few error cases where ``on_finish`` may be
  242. called when ``prepare`` has not. (These are considered bugs and may be fixed in the future,
  243. but for now you may need to check to see if the initialization work done in ``prepare`` has
  244. occurred)
  245. ``on_finish`` may not produce any output, as it is called after the response has been sent
  246. to the client.
  247. """
  248. pass
  249. def on_connection_close(self) -> None:
  250. """Called in async handlers if the client closed the connection.
  251. Override this to clean up resources associated with
  252. long-lived connections. Note that this method is called only if
  253. the connection was closed during asynchronous processing; if you
  254. need to do cleanup after every request override `on_finish`
  255. instead.
  256. Proxies may keep a connection open for a time (perhaps
  257. indefinitely) after the client has gone away, so this method
  258. may not be called promptly after the end user closes their
  259. connection.
  260. """
  261. if _has_stream_request_body(self.__class__):
  262. if not self.request._body_future.done():
  263. self.request._body_future.set_exception(iostream.StreamClosedError())
  264. self.request._body_future.exception()
  265. def clear(self) -> None:
  266. """Resets all headers and content for this response."""
  267. self._headers = httputil.HTTPHeaders(
  268. {
  269. "Server": "TornadoServer/%s" % tornado.version,
  270. "Content-Type": "text/html; charset=UTF-8",
  271. "Date": httputil.format_timestamp(time.time()),
  272. }
  273. )
  274. self.set_default_headers()
  275. self._write_buffer = [] # type: List[bytes]
  276. self._status_code = 200
  277. self._reason = httputil.responses[200]
  278. def set_default_headers(self) -> None:
  279. """Override this to set HTTP headers at the beginning of the request.
  280. For example, this is the place to set a custom ``Server`` header.
  281. Note that setting such headers in the normal flow of request
  282. processing may not do what you want, since headers may be reset
  283. during error handling.
  284. """
  285. pass
  286. def set_status(self, status_code: int, reason: Optional[str] = None) -> None:
  287. """Sets the status code for our response.
  288. :arg int status_code: Response status code.
  289. :arg str reason: Human-readable reason phrase describing the status
  290. code (for example, the "Not Found" in ``HTTP/1.1 404 Not Found``).
  291. Normally determined automatically from `http.client.responses`; this
  292. argument should only be used if you need to use a non-standard
  293. status code.
  294. .. versionchanged:: 5.0
  295. No longer validates that the response code is in
  296. `http.client.responses`.
  297. """
  298. self._status_code = status_code
  299. if reason is not None:
  300. if "<" in reason or not httputil._ABNF.reason_phrase.fullmatch(reason):
  301. # Logically this would be better as an exception, but this method
  302. # is called on error-handling paths that would need some refactoring
  303. # to tolerate internal errors cleanly.
  304. #
  305. # The check for "<" is a defense-in-depth against XSS attacks (we also
  306. # escape the reason when rendering error pages).
  307. reason = "Unknown"
  308. self._reason = escape.native_str(reason)
  309. else:
  310. self._reason = httputil.responses.get(status_code, "Unknown")
  311. def get_status(self) -> int:
  312. """Returns the status code for our response."""
  313. return self._status_code
  314. def set_header(self, name: str, value: _HeaderTypes) -> None:
  315. """Sets the given response header name and value.
  316. All header values are converted to strings (`datetime` objects
  317. are formatted according to the HTTP specification for the
  318. ``Date`` header).
  319. """
  320. self._headers[name] = self._convert_header_value(value)
  321. def add_header(self, name: str, value: _HeaderTypes) -> None:
  322. """Adds the given response header and value.
  323. Unlike `set_header`, `add_header` may be called multiple times
  324. to return multiple values for the same header.
  325. """
  326. self._headers.add(name, self._convert_header_value(value))
  327. def clear_header(self, name: str) -> None:
  328. """Clears an outgoing header, undoing a previous `set_header` call.
  329. Note that this method does not apply to multi-valued headers
  330. set by `add_header`.
  331. """
  332. if name in self._headers:
  333. del self._headers[name]
  334. # https://www.rfc-editor.org/rfc/rfc9110#name-field-values
  335. _VALID_HEADER_CHARS = re.compile(r"[\x09\x20-\x7e\x80-\xff]*")
  336. def _convert_header_value(self, value: _HeaderTypes) -> str:
  337. # Convert the input value to a str. This type check is a bit
  338. # subtle: The bytes case only executes on python 3, and the
  339. # unicode case only executes on python 2, because the other
  340. # cases are covered by the first match for str.
  341. if isinstance(value, str):
  342. retval = value
  343. elif isinstance(value, bytes):
  344. # Non-ascii characters in headers are not well supported,
  345. # but if you pass bytes, use latin1 so they pass through as-is.
  346. retval = value.decode("latin1")
  347. elif isinstance(value, numbers.Integral):
  348. # return immediately since we know the converted value will be safe
  349. return str(value)
  350. elif isinstance(value, datetime.datetime):
  351. return httputil.format_timestamp(value)
  352. else:
  353. raise TypeError("Unsupported header value %r" % value)
  354. # If \n is allowed into the header, it is possible to inject
  355. # additional headers or split the request.
  356. if RequestHandler._VALID_HEADER_CHARS.fullmatch(retval) is None:
  357. raise ValueError("Unsafe header value %r", retval)
  358. return retval
  359. @overload
  360. def get_argument(self, name: str, default: str, strip: bool = True) -> str:
  361. pass
  362. @overload
  363. def get_argument( # noqa: F811
  364. self, name: str, default: _ArgDefaultMarker = _ARG_DEFAULT, strip: bool = True
  365. ) -> str:
  366. pass
  367. @overload
  368. def get_argument( # noqa: F811
  369. self, name: str, default: None, strip: bool = True
  370. ) -> Optional[str]:
  371. pass
  372. def get_argument( # noqa: F811
  373. self,
  374. name: str,
  375. default: Union[None, str, _ArgDefaultMarker] = _ARG_DEFAULT,
  376. strip: bool = True,
  377. ) -> Optional[str]:
  378. """Returns the value of the argument with the given name.
  379. If default is not provided, the argument is considered to be
  380. required, and we raise a `MissingArgumentError` if it is missing.
  381. If the argument appears in the request more than once, we return the
  382. last value.
  383. This method searches both the query and body arguments.
  384. """
  385. return self._get_argument(name, default, self.request.arguments, strip)
  386. def get_arguments(self, name: str, strip: bool = True) -> List[str]:
  387. """Returns a list of the arguments with the given name.
  388. If the argument is not present, returns an empty list.
  389. This method searches both the query and body arguments.
  390. """
  391. # Make sure `get_arguments` isn't accidentally being called with a
  392. # positional argument that's assumed to be a default (like in
  393. # `get_argument`.)
  394. assert isinstance(strip, bool)
  395. return self._get_arguments(name, self.request.arguments, strip)
  396. @overload
  397. def get_body_argument(self, name: str, default: str, strip: bool = True) -> str:
  398. pass
  399. @overload
  400. def get_body_argument( # noqa: F811
  401. self, name: str, default: _ArgDefaultMarker = _ARG_DEFAULT, strip: bool = True
  402. ) -> str:
  403. pass
  404. @overload
  405. def get_body_argument( # noqa: F811
  406. self, name: str, default: None, strip: bool = True
  407. ) -> Optional[str]:
  408. pass
  409. def get_body_argument( # noqa: F811
  410. self,
  411. name: str,
  412. default: Union[None, str, _ArgDefaultMarker] = _ARG_DEFAULT,
  413. strip: bool = True,
  414. ) -> Optional[str]:
  415. """Returns the value of the argument with the given name
  416. from the request body.
  417. If default is not provided, the argument is considered to be
  418. required, and we raise a `MissingArgumentError` if it is missing.
  419. If the argument appears in the url more than once, we return the
  420. last value.
  421. .. versionadded:: 3.2
  422. """
  423. return self._get_argument(name, default, self.request.body_arguments, strip)
  424. def get_body_arguments(self, name: str, strip: bool = True) -> List[str]:
  425. """Returns a list of the body arguments with the given name.
  426. If the argument is not present, returns an empty list.
  427. .. versionadded:: 3.2
  428. """
  429. return self._get_arguments(name, self.request.body_arguments, strip)
  430. @overload
  431. def get_query_argument(self, name: str, default: str, strip: bool = True) -> str:
  432. pass
  433. @overload
  434. def get_query_argument( # noqa: F811
  435. self, name: str, default: _ArgDefaultMarker = _ARG_DEFAULT, strip: bool = True
  436. ) -> str:
  437. pass
  438. @overload
  439. def get_query_argument( # noqa: F811
  440. self, name: str, default: None, strip: bool = True
  441. ) -> Optional[str]:
  442. pass
  443. def get_query_argument( # noqa: F811
  444. self,
  445. name: str,
  446. default: Union[None, str, _ArgDefaultMarker] = _ARG_DEFAULT,
  447. strip: bool = True,
  448. ) -> Optional[str]:
  449. """Returns the value of the argument with the given name
  450. from the request query string.
  451. If default is not provided, the argument is considered to be
  452. required, and we raise a `MissingArgumentError` if it is missing.
  453. If the argument appears in the url more than once, we return the
  454. last value.
  455. .. versionadded:: 3.2
  456. """
  457. return self._get_argument(name, default, self.request.query_arguments, strip)
  458. def get_query_arguments(self, name: str, strip: bool = True) -> List[str]:
  459. """Returns a list of the query arguments with the given name.
  460. If the argument is not present, returns an empty list.
  461. .. versionadded:: 3.2
  462. """
  463. return self._get_arguments(name, self.request.query_arguments, strip)
  464. def _get_argument(
  465. self,
  466. name: str,
  467. default: Union[None, str, _ArgDefaultMarker],
  468. source: Dict[str, List[bytes]],
  469. strip: bool = True,
  470. ) -> Optional[str]:
  471. args = self._get_arguments(name, source, strip=strip)
  472. if not args:
  473. if isinstance(default, _ArgDefaultMarker):
  474. raise MissingArgumentError(name)
  475. return default
  476. return args[-1]
  477. def _get_arguments(
  478. self, name: str, source: Dict[str, List[bytes]], strip: bool = True
  479. ) -> List[str]:
  480. values = []
  481. for v in source.get(name, []):
  482. s = self.decode_argument(v, name=name)
  483. if isinstance(s, unicode_type):
  484. # Get rid of any weird control chars (unless decoding gave
  485. # us bytes, in which case leave it alone)
  486. s = RequestHandler._remove_control_chars_regex.sub(" ", s)
  487. if strip:
  488. s = s.strip()
  489. values.append(s)
  490. return values
  491. def decode_argument(self, value: bytes, name: Optional[str] = None) -> str:
  492. """Decodes an argument from the request.
  493. The argument has been percent-decoded and is now a byte string.
  494. By default, this method decodes the argument as utf-8 and returns
  495. a unicode string, but this may be overridden in subclasses.
  496. This method is used as a filter for both `get_argument()` and for
  497. values extracted from the url and passed to `get()`/`post()`/etc.
  498. The name of the argument is provided if known, but may be None
  499. (e.g. for unnamed groups in the url regex).
  500. """
  501. try:
  502. return _unicode(value)
  503. except UnicodeDecodeError:
  504. raise HTTPError(
  505. 400, "Invalid unicode in {}: {!r}".format(name or "url", value[:40])
  506. )
  507. @property
  508. def cookies(self) -> Dict[str, http.cookies.Morsel]:
  509. """An alias for
  510. `self.request.cookies <.httputil.HTTPServerRequest.cookies>`."""
  511. return self.request.cookies
  512. @overload
  513. def get_cookie(self, name: str, default: str) -> str:
  514. pass
  515. @overload
  516. def get_cookie(self, name: str, default: None = None) -> Optional[str]:
  517. pass
  518. def get_cookie(self, name: str, default: Optional[str] = None) -> Optional[str]:
  519. """Returns the value of the request cookie with the given name.
  520. If the named cookie is not present, returns ``default``.
  521. This method only returns cookies that were present in the request.
  522. It does not see the outgoing cookies set by `set_cookie` in this
  523. handler.
  524. """
  525. if self.request.cookies is not None and name in self.request.cookies:
  526. return self.request.cookies[name].value
  527. return default
  528. def set_cookie(
  529. self,
  530. name: str,
  531. value: Union[str, bytes],
  532. domain: Optional[str] = None,
  533. expires: Optional[Union[float, Tuple, datetime.datetime]] = None,
  534. path: str = "/",
  535. expires_days: Optional[float] = None,
  536. # Keyword-only args start here for historical reasons.
  537. *,
  538. max_age: Optional[int] = None,
  539. httponly: bool = False,
  540. secure: bool = False,
  541. samesite: Optional[str] = None,
  542. **kwargs: Any,
  543. ) -> None:
  544. """Sets an outgoing cookie name/value with the given options.
  545. Newly-set cookies are not immediately visible via `get_cookie`;
  546. they are not present until the next request.
  547. Most arguments are passed directly to `http.cookies.Morsel` directly.
  548. See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie
  549. for more information.
  550. ``expires`` may be a numeric timestamp as returned by `time.time`,
  551. a time tuple as returned by `time.gmtime`, or a
  552. `datetime.datetime` object. ``expires_days`` is provided as a convenience
  553. to set an expiration time in days from today (if both are set, ``expires``
  554. is used).
  555. .. deprecated:: 6.3
  556. Keyword arguments are currently accepted case-insensitively.
  557. In Tornado 7.0 this will be changed to only accept lowercase
  558. arguments.
  559. """
  560. # The cookie library only accepts type str, in both python 2 and 3
  561. name = escape.native_str(name)
  562. value = escape.native_str(value)
  563. if re.search(r"[\x00-\x20]", value):
  564. # Legacy check for control characters in cookie values. This check is no longer needed
  565. # since the cookie library escapes these characters correctly now. It will be removed
  566. # in the next feature release.
  567. raise ValueError(f"Invalid cookie {name!r}: {value!r}")
  568. for attr_name, attr_value in [
  569. ("name", name),
  570. ("domain", domain),
  571. ("path", path),
  572. ("samesite", samesite),
  573. ]:
  574. # Cookie attributes may not contain control characters or semicolons (except when
  575. # escaped in the value). A check for control characters was added to the http.cookies
  576. # library in a Feb 2026 security release; as of March it still does not check for
  577. # semicolons.
  578. #
  579. # When a semicolon check is added to the standard library (and the release has had time
  580. # for adoption), this check may be removed, but be mindful of the fact that this may
  581. # change the timing of the exception (to the generation of the Set-Cookie header in
  582. # flush()). We m
  583. if attr_value is not None and re.search(r"[\x00-\x20\x3b\x7f]", attr_value):
  584. raise http.cookies.CookieError(
  585. f"Invalid cookie attribute {attr_name}={attr_value!r} for cookie {name!r}"
  586. )
  587. if not hasattr(self, "_new_cookie"):
  588. self._new_cookie = (
  589. http.cookies.SimpleCookie()
  590. ) # type: http.cookies.SimpleCookie
  591. if name in self._new_cookie:
  592. del self._new_cookie[name]
  593. self._new_cookie[name] = value
  594. morsel = self._new_cookie[name]
  595. if domain:
  596. morsel["domain"] = domain
  597. if expires_days is not None and not expires:
  598. expires = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(
  599. days=expires_days
  600. )
  601. if expires:
  602. morsel["expires"] = httputil.format_timestamp(expires)
  603. if path:
  604. morsel["path"] = path
  605. if max_age:
  606. # Note change from _ to -.
  607. morsel["max-age"] = str(max_age)
  608. if httponly:
  609. # Note that SimpleCookie ignores the value here. The presense of an
  610. # httponly (or secure) key is treated as true.
  611. morsel["httponly"] = True
  612. if secure:
  613. morsel["secure"] = True
  614. if samesite:
  615. morsel["samesite"] = samesite
  616. if kwargs:
  617. # The setitem interface is case-insensitive, so continue to support
  618. # kwargs for backwards compatibility until we can remove deprecated
  619. # features.
  620. for k, v in kwargs.items():
  621. morsel[k] = v
  622. warnings.warn(
  623. f"Deprecated arguments to set_cookie: {set(kwargs.keys())} "
  624. "(should be lowercase)",
  625. DeprecationWarning,
  626. )
  627. def clear_cookie(self, name: str, **kwargs: Any) -> None:
  628. """Deletes the cookie with the given name.
  629. This method accepts the same arguments as `set_cookie`, except for
  630. ``expires`` and ``max_age``. Clearing a cookie requires the same
  631. ``domain`` and ``path`` arguments as when it was set. In some cases the
  632. ``samesite`` and ``secure`` arguments are also required to match. Other
  633. arguments are ignored.
  634. Similar to `set_cookie`, the effect of this method will not be
  635. seen until the following request.
  636. .. versionchanged:: 6.3
  637. Now accepts all keyword arguments that ``set_cookie`` does.
  638. The ``samesite`` and ``secure`` flags have recently become
  639. required for clearing ``samesite="none"`` cookies.
  640. """
  641. for excluded_arg in ["expires", "max_age"]:
  642. if excluded_arg in kwargs:
  643. raise TypeError(
  644. f"clear_cookie() got an unexpected keyword argument '{excluded_arg}'"
  645. )
  646. expires = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(
  647. days=365
  648. )
  649. self.set_cookie(name, value="", expires=expires, **kwargs)
  650. def clear_all_cookies(self, **kwargs: Any) -> None:
  651. """Attempt to delete all the cookies the user sent with this request.
  652. See `clear_cookie` for more information on keyword arguments. Due to
  653. limitations of the cookie protocol, it is impossible to determine on the
  654. server side which values are necessary for the ``domain``, ``path``,
  655. ``samesite``, or ``secure`` arguments, this method can only be
  656. successful if you consistently use the same values for these arguments
  657. when setting cookies.
  658. Similar to `set_cookie`, the effect of this method will not be seen
  659. until the following request.
  660. .. versionchanged:: 3.2
  661. Added the ``path`` and ``domain`` parameters.
  662. .. versionchanged:: 6.3
  663. Now accepts all keyword arguments that ``set_cookie`` does.
  664. .. deprecated:: 6.3
  665. The increasingly complex rules governing cookies have made it
  666. impossible for a ``clear_all_cookies`` method to work reliably
  667. since all we know about cookies are their names. Applications
  668. should generally use ``clear_cookie`` one at a time instead.
  669. """
  670. for name in self.request.cookies:
  671. self.clear_cookie(name, **kwargs)
  672. def set_signed_cookie(
  673. self,
  674. name: str,
  675. value: Union[str, bytes],
  676. expires_days: Optional[float] = 30,
  677. version: Optional[int] = None,
  678. **kwargs: Any,
  679. ) -> None:
  680. """Signs and timestamps a cookie so it cannot be forged.
  681. You must specify the ``cookie_secret`` setting in your Application
  682. to use this method. It should be a long, random sequence of bytes
  683. to be used as the HMAC secret for the signature.
  684. To read a cookie set with this method, use `get_signed_cookie()`.
  685. Note that the ``expires_days`` parameter sets the lifetime of the
  686. cookie in the browser, but is independent of the ``max_age_days``
  687. parameter to `get_signed_cookie`.
  688. A value of None limits the lifetime to the current browser session.
  689. Secure cookies may contain arbitrary byte values, not just unicode
  690. strings (unlike regular cookies)
  691. Similar to `set_cookie`, the effect of this method will not be
  692. seen until the following request.
  693. .. versionchanged:: 3.2.1
  694. Added the ``version`` argument. Introduced cookie version 2
  695. and made it the default.
  696. .. versionchanged:: 6.3
  697. Renamed from ``set_secure_cookie`` to ``set_signed_cookie`` to
  698. avoid confusion with other uses of "secure" in cookie attributes
  699. and prefixes. The old name remains as an alias.
  700. """
  701. self.set_cookie(
  702. name,
  703. self.create_signed_value(name, value, version=version),
  704. expires_days=expires_days,
  705. **kwargs,
  706. )
  707. set_secure_cookie = set_signed_cookie
  708. def create_signed_value(
  709. self, name: str, value: Union[str, bytes], version: Optional[int] = None
  710. ) -> bytes:
  711. """Signs and timestamps a string so it cannot be forged.
  712. Normally used via set_signed_cookie, but provided as a separate
  713. method for non-cookie uses. To decode a value not stored
  714. as a cookie use the optional value argument to get_signed_cookie.
  715. .. versionchanged:: 3.2.1
  716. Added the ``version`` argument. Introduced cookie version 2
  717. and made it the default.
  718. """
  719. self.require_setting("cookie_secret", "secure cookies")
  720. secret = self.application.settings["cookie_secret"]
  721. key_version = None
  722. if isinstance(secret, dict):
  723. if self.application.settings.get("key_version") is None:
  724. raise Exception("key_version setting must be used for secret_key dicts")
  725. key_version = self.application.settings["key_version"]
  726. return create_signed_value(
  727. secret, name, value, version=version, key_version=key_version
  728. )
  729. def get_signed_cookie(
  730. self,
  731. name: str,
  732. value: Optional[str] = None,
  733. max_age_days: float = 31,
  734. min_version: Optional[int] = None,
  735. ) -> Optional[bytes]:
  736. """Returns the given signed cookie if it validates, or None.
  737. The decoded cookie value is returned as a byte string (unlike
  738. `get_cookie`).
  739. Similar to `get_cookie`, this method only returns cookies that
  740. were present in the request. It does not see outgoing cookies set by
  741. `set_signed_cookie` in this handler.
  742. .. versionchanged:: 3.2.1
  743. Added the ``min_version`` argument. Introduced cookie version 2;
  744. both versions 1 and 2 are accepted by default.
  745. .. versionchanged:: 6.3
  746. Renamed from ``get_secure_cookie`` to ``get_signed_cookie`` to
  747. avoid confusion with other uses of "secure" in cookie attributes
  748. and prefixes. The old name remains as an alias.
  749. """
  750. self.require_setting("cookie_secret", "secure cookies")
  751. if value is None:
  752. value = self.get_cookie(name)
  753. return decode_signed_value(
  754. self.application.settings["cookie_secret"],
  755. name,
  756. value,
  757. max_age_days=max_age_days,
  758. min_version=min_version,
  759. )
  760. get_secure_cookie = get_signed_cookie
  761. def get_signed_cookie_key_version(
  762. self, name: str, value: Optional[str] = None
  763. ) -> Optional[int]:
  764. """Returns the signing key version of the secure cookie.
  765. The version is returned as int.
  766. .. versionchanged:: 6.3
  767. Renamed from ``get_secure_cookie_key_version`` to
  768. ``set_signed_cookie_key_version`` to avoid confusion with other
  769. uses of "secure" in cookie attributes and prefixes. The old name
  770. remains as an alias.
  771. """
  772. self.require_setting("cookie_secret", "secure cookies")
  773. if value is None:
  774. value = self.get_cookie(name)
  775. if value is None:
  776. return None
  777. return get_signature_key_version(value)
  778. get_secure_cookie_key_version = get_signed_cookie_key_version
  779. def redirect(
  780. self, url: str, permanent: bool = False, status: Optional[int] = None
  781. ) -> None:
  782. """Sends a redirect to the given (optionally relative) URL.
  783. If the ``status`` argument is specified, that value is used as the
  784. HTTP status code; otherwise either 301 (permanent) or 302
  785. (temporary) is chosen based on the ``permanent`` argument.
  786. The default is 302 (temporary).
  787. """
  788. if self._headers_written:
  789. raise Exception("Cannot redirect after headers have been written")
  790. if status is None:
  791. status = 301 if permanent else 302
  792. else:
  793. assert isinstance(status, int) and 300 <= status <= 399
  794. self.set_status(status)
  795. self.set_header("Location", utf8(url))
  796. self.finish()
  797. def write(self, chunk: Union[str, bytes, dict]) -> None:
  798. """Writes the given chunk to the output buffer.
  799. To write the output to the network, use the `flush()` method below.
  800. If the given chunk is a dictionary, we write it as JSON and set
  801. the Content-Type of the response to be ``application/json``.
  802. (if you want to send JSON as a different ``Content-Type``, call
  803. ``set_header`` *after* calling ``write()``).
  804. Note that lists are not converted to JSON because of a potential
  805. cross-site security vulnerability. All JSON output should be
  806. wrapped in a dictionary. More details at
  807. http://haacked.com/archive/2009/06/25/json-hijacking.aspx/ and
  808. https://github.com/facebook/tornado/issues/1009
  809. """
  810. if self._finished:
  811. raise RuntimeError("Cannot write() after finish()")
  812. if not isinstance(chunk, (bytes, unicode_type, dict)):
  813. message = "write() only accepts bytes, unicode, and dict objects"
  814. if isinstance(chunk, list):
  815. message += (
  816. ". Lists not accepted for security reasons; see "
  817. + "http://www.tornadoweb.org/en/stable/web.html#tornado.web.RequestHandler.write" # noqa: E501
  818. )
  819. raise TypeError(message)
  820. if isinstance(chunk, dict):
  821. chunk = escape.json_encode(chunk)
  822. self.set_header("Content-Type", "application/json; charset=UTF-8")
  823. chunk = utf8(chunk)
  824. self._write_buffer.append(chunk)
  825. def render(self, template_name: str, **kwargs: Any) -> "Future[None]":
  826. """Renders the template with the given arguments as the response.
  827. ``render()`` calls ``finish()``, so no other output methods can be called
  828. after it.
  829. Returns a `.Future` with the same semantics as the one returned by `finish`.
  830. Awaiting this `.Future` is optional.
  831. .. versionchanged:: 5.1
  832. Now returns a `.Future` instead of ``None``.
  833. """
  834. if self._finished:
  835. raise RuntimeError("Cannot render() after finish()")
  836. html = self.render_string(template_name, **kwargs)
  837. # Insert the additional JS and CSS added by the modules on the page
  838. js_embed = []
  839. js_files = []
  840. css_embed = []
  841. css_files = []
  842. html_heads = []
  843. html_bodies = []
  844. for module in getattr(self, "_active_modules", {}).values():
  845. embed_part = module.embedded_javascript()
  846. if embed_part:
  847. js_embed.append(utf8(embed_part))
  848. file_part = module.javascript_files()
  849. if file_part:
  850. if isinstance(file_part, (unicode_type, bytes)):
  851. js_files.append(_unicode(file_part))
  852. else:
  853. js_files.extend(file_part)
  854. embed_part = module.embedded_css()
  855. if embed_part:
  856. css_embed.append(utf8(embed_part))
  857. file_part = module.css_files()
  858. if file_part:
  859. if isinstance(file_part, (unicode_type, bytes)):
  860. css_files.append(_unicode(file_part))
  861. else:
  862. css_files.extend(file_part)
  863. head_part = module.html_head()
  864. if head_part:
  865. html_heads.append(utf8(head_part))
  866. body_part = module.html_body()
  867. if body_part:
  868. html_bodies.append(utf8(body_part))
  869. if js_files:
  870. # Maintain order of JavaScript files given by modules
  871. js = self.render_linked_js(js_files)
  872. sloc = html.rindex(b"</body>")
  873. html = html[:sloc] + utf8(js) + b"\n" + html[sloc:]
  874. if js_embed:
  875. js_bytes = self.render_embed_js(js_embed)
  876. sloc = html.rindex(b"</body>")
  877. html = html[:sloc] + js_bytes + b"\n" + html[sloc:]
  878. if css_files:
  879. css = self.render_linked_css(css_files)
  880. hloc = html.index(b"</head>")
  881. html = html[:hloc] + utf8(css) + b"\n" + html[hloc:]
  882. if css_embed:
  883. css_bytes = self.render_embed_css(css_embed)
  884. hloc = html.index(b"</head>")
  885. html = html[:hloc] + css_bytes + b"\n" + html[hloc:]
  886. if html_heads:
  887. hloc = html.index(b"</head>")
  888. html = html[:hloc] + b"".join(html_heads) + b"\n" + html[hloc:]
  889. if html_bodies:
  890. hloc = html.index(b"</body>")
  891. html = html[:hloc] + b"".join(html_bodies) + b"\n" + html[hloc:]
  892. return self.finish(html)
  893. def render_linked_js(self, js_files: Iterable[str]) -> str:
  894. """Default method used to render the final js links for the
  895. rendered webpage.
  896. Override this method in a sub-classed controller to change the output.
  897. """
  898. paths = []
  899. unique_paths = set() # type: Set[str]
  900. for path in js_files:
  901. if not is_absolute(path):
  902. path = self.static_url(path)
  903. if path not in unique_paths:
  904. paths.append(path)
  905. unique_paths.add(path)
  906. return "".join(
  907. '<script src="'
  908. + escape.xhtml_escape(p)
  909. + '" type="text/javascript"></script>'
  910. for p in paths
  911. )
  912. def render_embed_js(self, js_embed: Iterable[bytes]) -> bytes:
  913. """Default method used to render the final embedded js for the
  914. rendered webpage.
  915. Override this method in a sub-classed controller to change the output.
  916. """
  917. return (
  918. b'<script type="text/javascript">\n//<![CDATA[\n'
  919. + b"\n".join(js_embed)
  920. + b"\n//]]>\n</script>"
  921. )
  922. def render_linked_css(self, css_files: Iterable[str]) -> str:
  923. """Default method used to render the final css links for the
  924. rendered webpage.
  925. Override this method in a sub-classed controller to change the output.
  926. """
  927. paths = []
  928. unique_paths = set() # type: Set[str]
  929. for path in css_files:
  930. if not is_absolute(path):
  931. path = self.static_url(path)
  932. if path not in unique_paths:
  933. paths.append(path)
  934. unique_paths.add(path)
  935. return "".join(
  936. '<link href="' + escape.xhtml_escape(p) + '" '
  937. 'type="text/css" rel="stylesheet"/>'
  938. for p in paths
  939. )
  940. def render_embed_css(self, css_embed: Iterable[bytes]) -> bytes:
  941. """Default method used to render the final embedded css for the
  942. rendered webpage.
  943. Override this method in a sub-classed controller to change the output.
  944. """
  945. return b'<style type="text/css">\n' + b"\n".join(css_embed) + b"\n</style>"
  946. def render_string(self, template_name: str, **kwargs: Any) -> bytes:
  947. """Generate the given template with the given arguments.
  948. We return the generated byte string (in utf8). To generate and
  949. write a template as a response, use render() above.
  950. """
  951. # If no template_path is specified, use the path of the calling file
  952. template_path = self.get_template_path()
  953. if not template_path:
  954. frame = sys._getframe(0)
  955. web_file = frame.f_code.co_filename
  956. while frame.f_code.co_filename == web_file and frame.f_back is not None:
  957. frame = frame.f_back
  958. assert frame.f_code.co_filename is not None
  959. template_path = os.path.dirname(frame.f_code.co_filename)
  960. with RequestHandler._template_loader_lock:
  961. if template_path not in RequestHandler._template_loaders:
  962. loader = self.create_template_loader(template_path)
  963. RequestHandler._template_loaders[template_path] = loader
  964. else:
  965. loader = RequestHandler._template_loaders[template_path]
  966. t = loader.load(template_name)
  967. namespace = self.get_template_namespace()
  968. namespace.update(kwargs)
  969. return t.generate(**namespace)
  970. def get_template_namespace(self) -> Dict[str, Any]:
  971. """Returns a dictionary to be used as the default template namespace.
  972. May be overridden by subclasses to add or modify values.
  973. The results of this method will be combined with additional
  974. defaults in the `tornado.template` module and keyword arguments
  975. to `render` or `render_string`.
  976. """
  977. namespace = dict(
  978. handler=self,
  979. request=self.request,
  980. current_user=self.current_user,
  981. locale=self.locale,
  982. _=self.locale.translate,
  983. pgettext=self.locale.pgettext,
  984. static_url=self.static_url,
  985. xsrf_form_html=self.xsrf_form_html,
  986. reverse_url=self.reverse_url,
  987. )
  988. namespace.update(self.ui)
  989. return namespace
  990. def create_template_loader(self, template_path: str) -> template.BaseLoader:
  991. """Returns a new template loader for the given path.
  992. May be overridden by subclasses. By default returns a
  993. directory-based loader on the given path, using the
  994. ``autoescape`` and ``template_whitespace`` application
  995. settings. If a ``template_loader`` application setting is
  996. supplied, uses that instead.
  997. """
  998. settings = self.application.settings
  999. if "template_loader" in settings:
  1000. return settings["template_loader"]
  1001. kwargs = {}
  1002. if "autoescape" in settings:
  1003. # autoescape=None means "no escaping", so we have to be sure
  1004. # to only pass this kwarg if the user asked for it.
  1005. kwargs["autoescape"] = settings["autoescape"]
  1006. if "template_whitespace" in settings:
  1007. kwargs["whitespace"] = settings["template_whitespace"]
  1008. return template.Loader(template_path, **kwargs)
  1009. def flush(self, include_footers: bool = False) -> "Future[None]":
  1010. """Flushes the current output buffer to the network.
  1011. .. versionchanged:: 4.0
  1012. Now returns a `.Future` if no callback is given.
  1013. .. versionchanged:: 6.0
  1014. The ``callback`` argument was removed.
  1015. """
  1016. assert self.request.connection is not None
  1017. chunk = b"".join(self._write_buffer)
  1018. self._write_buffer = []
  1019. if not self._headers_written:
  1020. self._headers_written = True
  1021. for transform in self._transforms:
  1022. assert chunk is not None
  1023. (
  1024. self._status_code,
  1025. self._headers,
  1026. chunk,
  1027. ) = transform.transform_first_chunk(
  1028. self._status_code, self._headers, chunk, include_footers
  1029. )
  1030. # Ignore the chunk and only write the headers for HEAD requests
  1031. if self.request.method == "HEAD":
  1032. chunk = b""
  1033. # Finalize the cookie headers (which have been stored in a side
  1034. # object so an outgoing cookie could be overwritten before it
  1035. # is sent).
  1036. if hasattr(self, "_new_cookie"):
  1037. for cookie in self._new_cookie.values():
  1038. self.add_header("Set-Cookie", cookie.OutputString(None))
  1039. start_line = httputil.ResponseStartLine("", self._status_code, self._reason)
  1040. return self.request.connection.write_headers(
  1041. start_line, self._headers, chunk
  1042. )
  1043. else:
  1044. for transform in self._transforms:
  1045. chunk = transform.transform_chunk(chunk, include_footers)
  1046. # Ignore the chunk and only write the headers for HEAD requests
  1047. if self.request.method != "HEAD":
  1048. return self.request.connection.write(chunk)
  1049. else:
  1050. future = Future() # type: Future[None]
  1051. future.set_result(None)
  1052. return future
  1053. def finish(self, chunk: Optional[Union[str, bytes, dict]] = None) -> "Future[None]":
  1054. """Finishes this response, ending the HTTP request.
  1055. Passing a ``chunk`` to ``finish()`` is equivalent to passing that
  1056. chunk to ``write()`` and then calling ``finish()`` with no arguments.
  1057. Returns a `.Future` which may optionally be awaited to track the sending
  1058. of the response to the client. This `.Future` resolves when all the response
  1059. data has been sent, and raises an error if the connection is closed before all
  1060. data can be sent.
  1061. .. versionchanged:: 5.1
  1062. Now returns a `.Future` instead of ``None``.
  1063. """
  1064. if self._finished:
  1065. raise RuntimeError("finish() called twice")
  1066. if chunk is not None:
  1067. self.write(chunk)
  1068. # Automatically support ETags and add the Content-Length header if
  1069. # we have not flushed any content yet.
  1070. if not self._headers_written:
  1071. if (
  1072. self._status_code == 200
  1073. and self.request.method in ("GET", "HEAD")
  1074. and "Etag" not in self._headers
  1075. ):
  1076. self.set_etag_header()
  1077. if self.check_etag_header():
  1078. self._write_buffer = []
  1079. self.set_status(304)
  1080. if self._status_code in (204, 304) or (100 <= self._status_code < 200):
  1081. assert not self._write_buffer, (
  1082. "Cannot send body with %s" % self._status_code
  1083. )
  1084. self._clear_representation_headers()
  1085. elif "Content-Length" not in self._headers:
  1086. content_length = sum(len(part) for part in self._write_buffer)
  1087. self.set_header("Content-Length", content_length)
  1088. assert self.request.connection is not None
  1089. # Now that the request is finished, clear the callback we
  1090. # set on the HTTPConnection (which would otherwise prevent the
  1091. # garbage collection of the RequestHandler when there
  1092. # are keepalive connections)
  1093. self.request.connection.set_close_callback(None) # type: ignore
  1094. future = self.flush(include_footers=True)
  1095. self.request.connection.finish()
  1096. self._log()
  1097. self._finished = True
  1098. self.on_finish()
  1099. self._break_cycles()
  1100. return future
  1101. def detach(self) -> iostream.IOStream:
  1102. """Take control of the underlying stream.
  1103. Returns the underlying `.IOStream` object and stops all
  1104. further HTTP processing. Intended for implementing protocols
  1105. like websockets that tunnel over an HTTP handshake.
  1106. This method is only supported when HTTP/1.1 is used.
  1107. .. versionadded:: 5.1
  1108. """
  1109. self._finished = True
  1110. # TODO: add detach to HTTPConnection?
  1111. return self.request.connection.detach() # type: ignore
  1112. def _break_cycles(self) -> None:
  1113. # Break up a reference cycle between this handler and the
  1114. # _ui_module closures to allow for faster GC on CPython.
  1115. self.ui = None # type: ignore
  1116. def send_error(self, status_code: int = 500, **kwargs: Any) -> None:
  1117. """Sends the given HTTP error code to the browser.
  1118. If `flush()` has already been called, it is not possible to send
  1119. an error, so this method will simply terminate the response.
  1120. If output has been written but not yet flushed, it will be discarded
  1121. and replaced with the error page.
  1122. Override `write_error()` to customize the error page that is returned.
  1123. Additional keyword arguments are passed through to `write_error`.
  1124. """
  1125. if self._headers_written:
  1126. gen_log.error("Cannot send error response after headers written")
  1127. if not self._finished:
  1128. # If we get an error between writing headers and finishing,
  1129. # we are unlikely to be able to finish due to a
  1130. # Content-Length mismatch. Try anyway to release the
  1131. # socket.
  1132. try:
  1133. self.finish()
  1134. except Exception:
  1135. gen_log.error("Failed to flush partial response", exc_info=True)
  1136. return
  1137. self.clear()
  1138. reason = kwargs.get("reason")
  1139. if "exc_info" in kwargs:
  1140. exception = kwargs["exc_info"][1]
  1141. if isinstance(exception, HTTPError) and exception.reason:
  1142. reason = exception.reason
  1143. self.set_status(status_code, reason=reason)
  1144. try:
  1145. if status_code != 304:
  1146. self.write_error(status_code, **kwargs)
  1147. except Exception:
  1148. app_log.error("Uncaught exception in write_error", exc_info=True)
  1149. if not self._finished:
  1150. self.finish()
  1151. def write_error(self, status_code: int, **kwargs: Any) -> None:
  1152. """Override to implement custom error pages.
  1153. ``write_error`` may call `write`, `render`, `set_header`, etc
  1154. to produce output as usual.
  1155. If this error was caused by an uncaught exception (including
  1156. HTTPError), an ``exc_info`` triple will be available as
  1157. ``kwargs["exc_info"]``. Note that this exception may not be
  1158. the "current" exception for purposes of methods like
  1159. ``sys.exc_info()`` or ``traceback.format_exc``.
  1160. """
  1161. if self.settings.get("serve_traceback") and "exc_info" in kwargs:
  1162. # in debug mode, try to send a traceback
  1163. self.set_header("Content-Type", "text/plain")
  1164. for line in traceback.format_exception(*kwargs["exc_info"]):
  1165. self.write(line)
  1166. self.finish()
  1167. else:
  1168. self.finish(
  1169. "<html><title>%(code)d: %(message)s</title>"
  1170. "<body>%(code)d: %(message)s</body></html>"
  1171. % {"code": status_code, "message": escape.xhtml_escape(self._reason)}
  1172. )
  1173. @property
  1174. def locale(self) -> tornado.locale.Locale:
  1175. """The locale for the current session.
  1176. Determined by either `get_user_locale`, which you can override to
  1177. set the locale based on, e.g., a user preference stored in a
  1178. database, or `get_browser_locale`, which uses the ``Accept-Language``
  1179. header.
  1180. .. versionchanged: 4.1
  1181. Added a property setter.
  1182. """
  1183. if not hasattr(self, "_locale"):
  1184. loc = self.get_user_locale()
  1185. if loc is not None:
  1186. self._locale = loc
  1187. else:
  1188. self._locale = self.get_browser_locale()
  1189. assert self._locale
  1190. return self._locale
  1191. @locale.setter
  1192. def locale(self, value: tornado.locale.Locale) -> None:
  1193. self._locale = value
  1194. def get_user_locale(self) -> Optional[tornado.locale.Locale]:
  1195. """Override to determine the locale from the authenticated user.
  1196. If None is returned, we fall back to `get_browser_locale()`.
  1197. This method should return a `tornado.locale.Locale` object,
  1198. most likely obtained via a call like ``tornado.locale.get("en")``
  1199. """
  1200. return None
  1201. def get_browser_locale(self, default: str = "en_US") -> tornado.locale.Locale:
  1202. """Determines the user's locale from ``Accept-Language`` header.
  1203. See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
  1204. """
  1205. if "Accept-Language" in self.request.headers:
  1206. languages = self.request.headers["Accept-Language"].split(",")
  1207. locales = []
  1208. for language in languages:
  1209. parts = language.strip().split(";")
  1210. if len(parts) > 1 and parts[1].strip().startswith("q="):
  1211. try:
  1212. score = float(parts[1].strip()[2:])
  1213. if score < 0:
  1214. raise ValueError()
  1215. except (ValueError, TypeError):
  1216. score = 0.0
  1217. else:
  1218. score = 1.0
  1219. if score > 0:
  1220. locales.append((parts[0], score))
  1221. if locales:
  1222. locales.sort(key=lambda pair: pair[1], reverse=True)
  1223. codes = [loc[0] for loc in locales]
  1224. return locale.get(*codes)
  1225. return locale.get(default)
  1226. @property
  1227. def current_user(self) -> Any:
  1228. """The authenticated user for this request.
  1229. This is set in one of two ways:
  1230. * A subclass may override `get_current_user()`, which will be called
  1231. automatically the first time ``self.current_user`` is accessed.
  1232. `get_current_user()` will only be called once per request,
  1233. and is cached for future access::
  1234. def get_current_user(self):
  1235. user_cookie = self.get_signed_cookie("user")
  1236. if user_cookie:
  1237. return json.loads(user_cookie)
  1238. return None
  1239. * It may be set as a normal variable, typically from an overridden
  1240. `prepare()`::
  1241. @gen.coroutine
  1242. def prepare(self):
  1243. user_id_cookie = self.get_signed_cookie("user_id")
  1244. if user_id_cookie:
  1245. self.current_user = yield load_user(user_id_cookie)
  1246. Note that `prepare()` may be a coroutine while `get_current_user()`
  1247. may not, so the latter form is necessary if loading the user requires
  1248. asynchronous operations.
  1249. The user object may be any type of the application's choosing.
  1250. """
  1251. if not hasattr(self, "_current_user"):
  1252. self._current_user = self.get_current_user()
  1253. return self._current_user
  1254. @current_user.setter
  1255. def current_user(self, value: Any) -> None:
  1256. self._current_user = value
  1257. def get_current_user(self) -> Any:
  1258. """Override to determine the current user from, e.g., a cookie.
  1259. This method may not be a coroutine.
  1260. """
  1261. return None
  1262. def get_login_url(self) -> str:
  1263. """Override to customize the login URL based on the request.
  1264. By default, we use the ``login_url`` application setting.
  1265. """
  1266. self.require_setting("login_url", "@tornado.web.authenticated")
  1267. return self.application.settings["login_url"]
  1268. def get_template_path(self) -> Optional[str]:
  1269. """Override to customize template path for each handler.
  1270. By default, we use the ``template_path`` application setting.
  1271. Return None to load templates relative to the calling file.
  1272. """
  1273. return self.application.settings.get("template_path")
  1274. @property
  1275. def xsrf_token(self) -> bytes:
  1276. """The XSRF-prevention token for the current user/session.
  1277. To prevent cross-site request forgery, we set an '_xsrf' cookie
  1278. and include the same '_xsrf' value as an argument with all POST
  1279. requests. If the two do not match, we reject the form submission
  1280. as a potential forgery.
  1281. See http://en.wikipedia.org/wiki/Cross-site_request_forgery
  1282. This property is of type `bytes`, but it contains only ASCII
  1283. characters. If a character string is required, there is no
  1284. need to base64-encode it; just decode the byte string as
  1285. UTF-8.
  1286. .. versionchanged:: 3.2.2
  1287. The xsrf token will now be have a random mask applied in every
  1288. request, which makes it safe to include the token in pages
  1289. that are compressed. See http://breachattack.com for more
  1290. information on the issue fixed by this change. Old (version 1)
  1291. cookies will be converted to version 2 when this method is called
  1292. unless the ``xsrf_cookie_version`` `Application` setting is
  1293. set to 1.
  1294. .. versionchanged:: 4.3
  1295. The ``xsrf_cookie_kwargs`` `Application` setting may be
  1296. used to supply additional cookie options (which will be
  1297. passed directly to `set_cookie`). For example,
  1298. ``xsrf_cookie_kwargs=dict(httponly=True, secure=True)``
  1299. will set the ``secure`` and ``httponly`` flags on the
  1300. ``_xsrf`` cookie.
  1301. """
  1302. if not hasattr(self, "_xsrf_token"):
  1303. version, token, timestamp = self._get_raw_xsrf_token()
  1304. output_version = self.settings.get("xsrf_cookie_version", 2)
  1305. cookie_kwargs = self.settings.get("xsrf_cookie_kwargs", {})
  1306. if output_version == 1:
  1307. self._xsrf_token = binascii.b2a_hex(token)
  1308. elif output_version == 2:
  1309. mask = os.urandom(4)
  1310. self._xsrf_token = b"|".join(
  1311. [
  1312. b"2",
  1313. binascii.b2a_hex(mask),
  1314. binascii.b2a_hex(_websocket_mask(mask, token)),
  1315. utf8(str(int(timestamp))),
  1316. ]
  1317. )
  1318. else:
  1319. raise ValueError("unknown xsrf cookie version %d", output_version)
  1320. if version is None:
  1321. if self.current_user and "expires_days" not in cookie_kwargs:
  1322. cookie_kwargs["expires_days"] = 30
  1323. cookie_name = self.settings.get("xsrf_cookie_name", "_xsrf")
  1324. self.set_cookie(cookie_name, self._xsrf_token, **cookie_kwargs)
  1325. return self._xsrf_token
  1326. def _get_raw_xsrf_token(self) -> Tuple[Optional[int], bytes, float]:
  1327. """Read or generate the xsrf token in its raw form.
  1328. The raw_xsrf_token is a tuple containing:
  1329. * version: the version of the cookie from which this token was read,
  1330. or None if we generated a new token in this request.
  1331. * token: the raw token data; random (non-ascii) bytes.
  1332. * timestamp: the time this token was generated (will not be accurate
  1333. for version 1 cookies)
  1334. """
  1335. if not hasattr(self, "_raw_xsrf_token"):
  1336. cookie_name = self.settings.get("xsrf_cookie_name", "_xsrf")
  1337. cookie = self.get_cookie(cookie_name)
  1338. if cookie:
  1339. version, token, timestamp = self._decode_xsrf_token(cookie)
  1340. else:
  1341. version, token, timestamp = None, None, None
  1342. if token is None:
  1343. version = None
  1344. token = os.urandom(16)
  1345. timestamp = time.time()
  1346. assert token is not None
  1347. assert timestamp is not None
  1348. self._raw_xsrf_token = (version, token, timestamp)
  1349. return self._raw_xsrf_token
  1350. def _decode_xsrf_token(
  1351. self, cookie: str
  1352. ) -> Tuple[Optional[int], Optional[bytes], Optional[float]]:
  1353. """Convert a cookie string into a the tuple form returned by
  1354. _get_raw_xsrf_token.
  1355. """
  1356. try:
  1357. m = _signed_value_version_re.match(utf8(cookie))
  1358. if m:
  1359. version = int(m.group(1))
  1360. if version == 2:
  1361. _, mask_str, masked_token, timestamp_str = cookie.split("|")
  1362. mask = binascii.a2b_hex(utf8(mask_str))
  1363. token = _websocket_mask(mask, binascii.a2b_hex(utf8(masked_token)))
  1364. timestamp = int(timestamp_str)
  1365. return version, token, timestamp
  1366. else:
  1367. # Treat unknown versions as not present instead of failing.
  1368. raise Exception("Unknown xsrf cookie version")
  1369. else:
  1370. version = 1
  1371. try:
  1372. token = binascii.a2b_hex(utf8(cookie))
  1373. except (binascii.Error, TypeError):
  1374. token = utf8(cookie)
  1375. # We don't have a usable timestamp in older versions.
  1376. timestamp = int(time.time())
  1377. return (version, token, timestamp)
  1378. except Exception:
  1379. # Catch exceptions and return nothing instead of failing.
  1380. gen_log.debug("Uncaught exception in _decode_xsrf_token", exc_info=True)
  1381. return None, None, None
  1382. def check_xsrf_cookie(self) -> None:
  1383. """Verifies that the ``_xsrf`` cookie matches the ``_xsrf`` argument.
  1384. To prevent cross-site request forgery, we set an ``_xsrf``
  1385. cookie and include the same value as a non-cookie
  1386. field with all ``POST`` requests. If the two do not match, we
  1387. reject the form submission as a potential forgery.
  1388. The ``_xsrf`` value may be set as either a form field named ``_xsrf``
  1389. or in a custom HTTP header named ``X-XSRFToken`` or ``X-CSRFToken``
  1390. (the latter is accepted for compatibility with Django).
  1391. See http://en.wikipedia.org/wiki/Cross-site_request_forgery
  1392. .. versionchanged:: 3.2.2
  1393. Added support for cookie version 2. Both versions 1 and 2 are
  1394. supported.
  1395. """
  1396. # Prior to release 1.1.1, this check was ignored if the HTTP header
  1397. # ``X-Requested-With: XMLHTTPRequest`` was present. This exception
  1398. # has been shown to be insecure and has been removed. For more
  1399. # information please see
  1400. # http://www.djangoproject.com/weblog/2011/feb/08/security/
  1401. # http://weblog.rubyonrails.org/2011/2/8/csrf-protection-bypass-in-ruby-on-rails
  1402. input_token = (
  1403. self.get_argument("_xsrf", None)
  1404. or self.request.headers.get("X-Xsrftoken")
  1405. or self.request.headers.get("X-Csrftoken")
  1406. )
  1407. if not input_token:
  1408. raise HTTPError(403, "'_xsrf' argument missing from POST")
  1409. _, token, _ = self._decode_xsrf_token(input_token)
  1410. _, expected_token, _ = self._get_raw_xsrf_token()
  1411. if not token:
  1412. raise HTTPError(403, "'_xsrf' argument has invalid format")
  1413. if not hmac.compare_digest(utf8(token), utf8(expected_token)):
  1414. raise HTTPError(403, "XSRF cookie does not match POST argument")
  1415. def xsrf_form_html(self) -> str:
  1416. """An HTML ``<input/>`` element to be included with all POST forms.
  1417. It defines the ``_xsrf`` input value, which we check on all POST
  1418. requests to prevent cross-site request forgery. If you have set
  1419. the ``xsrf_cookies`` application setting, you must include this
  1420. HTML within all of your HTML forms.
  1421. In a template, this method should be called with ``{% module
  1422. xsrf_form_html() %}``
  1423. See `check_xsrf_cookie()` above for more information.
  1424. """
  1425. return (
  1426. '<input type="hidden" name="_xsrf" value="'
  1427. + escape.xhtml_escape(self.xsrf_token)
  1428. + '"/>'
  1429. )
  1430. def static_url(
  1431. self, path: str, include_host: Optional[bool] = None, **kwargs: Any
  1432. ) -> str:
  1433. """Returns a static URL for the given relative static file path.
  1434. This method requires you set the ``static_path`` setting in your
  1435. application (which specifies the root directory of your static
  1436. files).
  1437. This method returns a versioned url (by default appending
  1438. ``?v=<signature>``), which allows the static files to be
  1439. cached indefinitely. This can be disabled by passing
  1440. ``include_version=False`` (in the default implementation;
  1441. other static file implementations are not required to support
  1442. this, but they may support other options).
  1443. By default this method returns URLs relative to the current
  1444. host, but if ``include_host`` is true the URL returned will be
  1445. absolute. If this handler has an ``include_host`` attribute,
  1446. that value will be used as the default for all `static_url`
  1447. calls that do not pass ``include_host`` as a keyword argument.
  1448. """
  1449. self.require_setting("static_path", "static_url")
  1450. get_url = self.settings.get(
  1451. "static_handler_class", StaticFileHandler
  1452. ).make_static_url
  1453. if include_host is None:
  1454. include_host = getattr(self, "include_host", False)
  1455. if include_host:
  1456. base = self.request.protocol + "://" + self.request.host
  1457. else:
  1458. base = ""
  1459. return base + get_url(self.settings, path, **kwargs)
  1460. def require_setting(self, name: str, feature: str = "this feature") -> None:
  1461. """Raises an exception if the given app setting is not defined."""
  1462. if not self.application.settings.get(name):
  1463. raise Exception(
  1464. "You must define the '%s' setting in your "
  1465. "application to use %s" % (name, feature)
  1466. )
  1467. def reverse_url(self, name: str, *args: Any) -> str:
  1468. """Alias for `Application.reverse_url`."""
  1469. return self.application.reverse_url(name, *args)
  1470. def compute_etag(self) -> Optional[str]:
  1471. """Computes the etag header to be used for this request.
  1472. By default uses a hash of the content written so far.
  1473. May be overridden to provide custom etag implementations,
  1474. or may return None to disable tornado's default etag support.
  1475. """
  1476. hasher = hashlib.sha1()
  1477. for part in self._write_buffer:
  1478. hasher.update(part)
  1479. return '"%s"' % hasher.hexdigest()
  1480. def set_etag_header(self) -> None:
  1481. """Sets the response's Etag header using ``self.compute_etag()``.
  1482. Note: no header will be set if ``compute_etag()`` returns ``None``.
  1483. This method is called automatically when the request is finished.
  1484. """
  1485. etag = self.compute_etag()
  1486. if etag is not None:
  1487. self.set_header("Etag", etag)
  1488. def check_etag_header(self) -> bool:
  1489. """Checks the ``Etag`` header against requests's ``If-None-Match``.
  1490. Returns ``True`` if the request's Etag matches and a 304 should be
  1491. returned. For example::
  1492. self.set_etag_header()
  1493. if self.check_etag_header():
  1494. self.set_status(304)
  1495. return
  1496. This method is called automatically when the request is finished,
  1497. but may be called earlier for applications that override
  1498. `compute_etag` and want to do an early check for ``If-None-Match``
  1499. before completing the request. The ``Etag`` header should be set
  1500. (perhaps with `set_etag_header`) before calling this method.
  1501. """
  1502. computed_etag = utf8(self._headers.get("Etag", ""))
  1503. # Find all weak and strong etag values from If-None-Match header
  1504. # because RFC 7232 allows multiple etag values in a single header.
  1505. etags = re.findall(
  1506. rb'\*|(?:W/)?"[^"]*"', utf8(self.request.headers.get("If-None-Match", ""))
  1507. )
  1508. if not computed_etag or not etags:
  1509. return False
  1510. match = False
  1511. if etags[0] == b"*":
  1512. match = True
  1513. else:
  1514. # Use a weak comparison when comparing entity-tags.
  1515. def val(x: bytes) -> bytes:
  1516. return x[2:] if x.startswith(b"W/") else x
  1517. for etag in etags:
  1518. if val(etag) == val(computed_etag):
  1519. match = True
  1520. break
  1521. return match
  1522. async def _execute(
  1523. self, transforms: List["OutputTransform"], *args: bytes, **kwargs: bytes
  1524. ) -> None:
  1525. """Executes this request with the given output transforms."""
  1526. self._transforms = transforms
  1527. try:
  1528. if self.request.method not in self.SUPPORTED_METHODS:
  1529. raise HTTPError(405)
  1530. # If we're not in stream_request_body mode, this is the place where we parse the body.
  1531. if not _has_stream_request_body(self.__class__):
  1532. try:
  1533. self.request._parse_body()
  1534. except httputil.HTTPInputError as e:
  1535. raise HTTPError(400, "Invalid body: %s" % e) from e
  1536. self.path_args = [self.decode_argument(arg) for arg in args]
  1537. self.path_kwargs = {
  1538. k: self.decode_argument(v, name=k) for (k, v) in kwargs.items()
  1539. }
  1540. # If XSRF cookies are turned on, reject form submissions without
  1541. # the proper cookie
  1542. if self.request.method not in (
  1543. "GET",
  1544. "HEAD",
  1545. "OPTIONS",
  1546. ) and self.application.settings.get("xsrf_cookies"):
  1547. self.check_xsrf_cookie()
  1548. result = self.prepare()
  1549. if result is not None:
  1550. result = await result # type: ignore
  1551. if self._prepared_future is not None:
  1552. # Tell the Application we've finished with prepare()
  1553. # and are ready for the body to arrive.
  1554. future_set_result_unless_cancelled(self._prepared_future, None)
  1555. if self._finished:
  1556. return
  1557. if _has_stream_request_body(self.__class__):
  1558. # In streaming mode request.body is a Future that signals
  1559. # the body has been completely received. The Future has no
  1560. # result; the data has been passed to self.data_received
  1561. # instead.
  1562. try:
  1563. await self.request._body_future
  1564. except iostream.StreamClosedError:
  1565. return
  1566. method = getattr(self, self.request.method.lower())
  1567. result = method(*self.path_args, **self.path_kwargs)
  1568. if result is not None:
  1569. result = await result
  1570. if self._auto_finish and not self._finished:
  1571. self.finish()
  1572. except Exception as e:
  1573. try:
  1574. self._handle_request_exception(e)
  1575. except Exception:
  1576. app_log.error("Exception in exception handler", exc_info=True)
  1577. finally:
  1578. # Unset result to avoid circular references
  1579. result = None
  1580. if self._prepared_future is not None and not self._prepared_future.done():
  1581. # In case we failed before setting _prepared_future, do it
  1582. # now (to unblock the HTTP server). Note that this is not
  1583. # in a finally block to avoid GC issues prior to Python 3.4.
  1584. self._prepared_future.set_result(None)
  1585. def data_received(self, chunk: bytes) -> Optional[Awaitable[None]]:
  1586. """Implement this method to handle streamed request data.
  1587. Requires the `.stream_request_body` decorator.
  1588. May be a coroutine for flow control.
  1589. """
  1590. raise NotImplementedError()
  1591. def _log(self) -> None:
  1592. """Logs the current request.
  1593. Sort of deprecated since this functionality was moved to the
  1594. Application, but left in place for the benefit of existing apps
  1595. that have overridden this method.
  1596. """
  1597. self.application.log_request(self)
  1598. def _request_summary(self) -> str:
  1599. return "{} {} ({})".format(
  1600. self.request.method,
  1601. self.request.uri,
  1602. self.request.remote_ip,
  1603. )
  1604. def _handle_request_exception(self, e: BaseException) -> None:
  1605. if isinstance(e, Finish):
  1606. # Not an error; just finish the request without logging.
  1607. if not self._finished:
  1608. self.finish(*e.args)
  1609. return
  1610. try:
  1611. self.log_exception(*sys.exc_info())
  1612. except Exception:
  1613. # An error here should still get a best-effort send_error()
  1614. # to avoid leaking the connection.
  1615. app_log.error("Error in exception logger", exc_info=True)
  1616. if self._finished:
  1617. # Extra errors after the request has been finished should
  1618. # be logged, but there is no reason to continue to try and
  1619. # send a response.
  1620. return
  1621. if isinstance(e, HTTPError):
  1622. self.send_error(e.status_code, exc_info=sys.exc_info())
  1623. else:
  1624. self.send_error(500, exc_info=sys.exc_info())
  1625. def log_exception(
  1626. self,
  1627. typ: "Optional[Type[BaseException]]",
  1628. value: Optional[BaseException],
  1629. tb: Optional[TracebackType],
  1630. ) -> None:
  1631. """Override to customize logging of uncaught exceptions.
  1632. By default logs instances of `HTTPError` as warnings without
  1633. stack traces (on the ``tornado.general`` logger), and all
  1634. other exceptions as errors with stack traces (on the
  1635. ``tornado.application`` logger).
  1636. .. versionadded:: 3.1
  1637. """
  1638. if isinstance(value, HTTPError):
  1639. log_message = value.get_message()
  1640. if log_message:
  1641. format = "%d %s: %s"
  1642. args = [value.status_code, self._request_summary(), log_message]
  1643. gen_log.warning(format, *args)
  1644. else:
  1645. app_log.error(
  1646. "Uncaught exception %s\n%r",
  1647. self._request_summary(),
  1648. self.request,
  1649. exc_info=(typ, value, tb), # type: ignore
  1650. )
  1651. def _ui_module(self, name: str, module: Type["UIModule"]) -> Callable[..., str]:
  1652. def render(*args, **kwargs) -> str: # type: ignore
  1653. if not hasattr(self, "_active_modules"):
  1654. self._active_modules = {} # type: Dict[str, UIModule]
  1655. if name not in self._active_modules:
  1656. self._active_modules[name] = module(self)
  1657. rendered = self._active_modules[name].render(*args, **kwargs)
  1658. return _unicode(rendered)
  1659. return render
  1660. def _ui_method(self, method: Callable[..., str]) -> Callable[..., str]:
  1661. return lambda *args, **kwargs: method(self, *args, **kwargs)
  1662. def _clear_representation_headers(self) -> None:
  1663. # 304 responses should not contain representation metadata
  1664. # headers (defined in
  1665. # https://tools.ietf.org/html/rfc7231#section-3.1)
  1666. # not explicitly allowed by
  1667. # https://tools.ietf.org/html/rfc7232#section-4.1
  1668. headers = ["Content-Encoding", "Content-Language", "Content-Type"]
  1669. for h in headers:
  1670. self.clear_header(h)
  1671. _RequestHandlerType = TypeVar("_RequestHandlerType", bound=RequestHandler)
  1672. def stream_request_body(cls: Type[_RequestHandlerType]) -> Type[_RequestHandlerType]:
  1673. """Apply to `RequestHandler` subclasses to enable streaming body support.
  1674. This decorator implies the following changes:
  1675. * `.HTTPServerRequest.body` is undefined, and body arguments will not
  1676. be included in `RequestHandler.get_argument`.
  1677. * `RequestHandler.prepare` is called when the request headers have been
  1678. read instead of after the entire body has been read.
  1679. * The subclass must define a method ``data_received(self, data):``, which
  1680. will be called zero or more times as data is available. Note that
  1681. if the request has an empty body, ``data_received`` may not be called.
  1682. * ``prepare`` and ``data_received`` may return Futures (such as via
  1683. ``@gen.coroutine``, in which case the next method will not be called
  1684. until those futures have completed.
  1685. * The regular HTTP method (``post``, ``put``, etc) will be called after
  1686. the entire body has been read.
  1687. See the `file receiver demo <https://github.com/tornadoweb/tornado/tree/stable/demos/file_upload/>`_
  1688. for example usage.
  1689. """ # noqa: E501
  1690. if not issubclass(cls, RequestHandler):
  1691. raise TypeError("expected subclass of RequestHandler, got %r", cls)
  1692. cls._stream_request_body = True
  1693. return cls
  1694. def _has_stream_request_body(cls: Type[RequestHandler]) -> bool:
  1695. if not issubclass(cls, RequestHandler):
  1696. raise TypeError("expected subclass of RequestHandler, got %r", cls)
  1697. return cls._stream_request_body
  1698. def removeslash(
  1699. method: Callable[..., Optional[Awaitable[None]]],
  1700. ) -> Callable[..., Optional[Awaitable[None]]]:
  1701. """Use this decorator to remove trailing slashes from the request path.
  1702. For example, a request to ``/foo/`` would redirect to ``/foo`` with this
  1703. decorator. Your request handler mapping should use a regular expression
  1704. like ``r'/foo/*'`` in conjunction with using the decorator.
  1705. """
  1706. @functools.wraps(method)
  1707. def wrapper( # type: ignore
  1708. self: RequestHandler, *args, **kwargs
  1709. ) -> Optional[Awaitable[None]]:
  1710. if self.request.path.endswith("/"):
  1711. if self.request.method in ("GET", "HEAD"):
  1712. uri = self.request.path.rstrip("/")
  1713. if uri: # don't try to redirect '/' to ''
  1714. if self.request.query:
  1715. uri += "?" + self.request.query
  1716. self.redirect(uri, permanent=True)
  1717. return None
  1718. else:
  1719. raise HTTPError(404)
  1720. return method(self, *args, **kwargs)
  1721. return wrapper
  1722. def addslash(
  1723. method: Callable[..., Optional[Awaitable[None]]],
  1724. ) -> Callable[..., Optional[Awaitable[None]]]:
  1725. """Use this decorator to add a missing trailing slash to the request path.
  1726. For example, a request to ``/foo`` would redirect to ``/foo/`` with this
  1727. decorator. Your request handler mapping should use a regular expression
  1728. like ``r'/foo/?'`` in conjunction with using the decorator.
  1729. """
  1730. @functools.wraps(method)
  1731. def wrapper( # type: ignore
  1732. self: RequestHandler, *args, **kwargs
  1733. ) -> Optional[Awaitable[None]]:
  1734. if not self.request.path.endswith("/"):
  1735. if self.request.method in ("GET", "HEAD"):
  1736. uri = self.request.path + "/"
  1737. if self.request.query:
  1738. uri += "?" + self.request.query
  1739. self.redirect(uri, permanent=True)
  1740. return None
  1741. raise HTTPError(404)
  1742. return method(self, *args, **kwargs)
  1743. return wrapper
  1744. class _ApplicationRouter(ReversibleRuleRouter):
  1745. """Routing implementation used internally by `Application`.
  1746. Provides a binding between `Application` and `RequestHandler`.
  1747. This implementation extends `~.routing.ReversibleRuleRouter` in a couple of ways:
  1748. * it allows to use `RequestHandler` subclasses as `~.routing.Rule` target and
  1749. * it allows to use a list/tuple of rules as `~.routing.Rule` target.
  1750. ``process_rule`` implementation will substitute this list with an appropriate
  1751. `_ApplicationRouter` instance.
  1752. """
  1753. def __init__(
  1754. self, application: "Application", rules: Optional[_RuleList] = None
  1755. ) -> None:
  1756. assert isinstance(application, Application)
  1757. self.application = application
  1758. super().__init__(rules)
  1759. def process_rule(self, rule: Rule) -> Rule:
  1760. rule = super().process_rule(rule)
  1761. if isinstance(rule.target, (list, tuple)):
  1762. rule.target = _ApplicationRouter(
  1763. self.application, rule.target # type: ignore
  1764. )
  1765. return rule
  1766. def get_target_delegate(
  1767. self, target: Any, request: httputil.HTTPServerRequest, **target_params: Any
  1768. ) -> Optional[httputil.HTTPMessageDelegate]:
  1769. if isclass(target) and issubclass(target, RequestHandler):
  1770. return self.application.get_handler_delegate(
  1771. request, target, **target_params
  1772. )
  1773. return super().get_target_delegate(target, request, **target_params)
  1774. class Application(ReversibleRouter):
  1775. r"""A collection of request handlers that make up a web application.
  1776. Instances of this class are callable and can be passed directly to
  1777. HTTPServer to serve the application::
  1778. application = web.Application([
  1779. (r"/", MainPageHandler),
  1780. ])
  1781. http_server = httpserver.HTTPServer(application)
  1782. http_server.listen(8080)
  1783. The constructor for this class takes in a list of `~.routing.Rule`
  1784. objects or tuples of values corresponding to the arguments of
  1785. `~.routing.Rule` constructor: ``(matcher, target, [target_kwargs], [name])``,
  1786. the values in square brackets being optional. The default matcher is
  1787. `~.routing.PathMatches`, so ``(regexp, target)`` tuples can also be used
  1788. instead of ``(PathMatches(regexp), target)``.
  1789. A common routing target is a `RequestHandler` subclass, but you can also
  1790. use lists of rules as a target, which create a nested routing configuration::
  1791. application = web.Application([
  1792. (HostMatches("example.com"), [
  1793. (r"/", MainPageHandler),
  1794. (r"/feed", FeedHandler),
  1795. ]),
  1796. ])
  1797. In addition to this you can use nested `~.routing.Router` instances,
  1798. `~.httputil.HTTPMessageDelegate` subclasses and callables as routing targets
  1799. (see `~.routing` module docs for more information).
  1800. When we receive requests, we iterate over the list in order and
  1801. instantiate an instance of the first request class whose regexp
  1802. matches the request path. The request class can be specified as
  1803. either a class object or a (fully-qualified) name.
  1804. A dictionary may be passed as the third element (``target_kwargs``)
  1805. of the tuple, which will be used as keyword arguments to the handler's
  1806. constructor and `~RequestHandler.initialize` method. This pattern
  1807. is used for the `StaticFileHandler` in this example (note that a
  1808. `StaticFileHandler` can be installed automatically with the
  1809. static_path setting described below)::
  1810. application = web.Application([
  1811. (r"/static/(.*)", web.StaticFileHandler, {"path": "/var/www"}),
  1812. ])
  1813. We support virtual hosts with the `add_handlers` method, which takes in
  1814. a host regular expression as the first argument::
  1815. application.add_handlers(r"www\.myhost\.com", [
  1816. (r"/article/([0-9]+)", ArticleHandler),
  1817. ])
  1818. If there's no match for the current request's host, then ``default_host``
  1819. parameter value is matched against host regular expressions.
  1820. .. warning::
  1821. Applications that do not use TLS may be vulnerable to :ref:`DNS
  1822. rebinding <dnsrebinding>` attacks. This attack is especially
  1823. relevant to applications that only listen on ``127.0.0.1`` or
  1824. other private networks. Appropriate host patterns must be used
  1825. (instead of the default of ``r'.*'``) to prevent this risk. The
  1826. ``default_host`` argument must not be used in applications that
  1827. may be vulnerable to DNS rebinding.
  1828. You can serve static files by sending the ``static_path`` setting
  1829. as a keyword argument. We will serve those files from the
  1830. ``/static/`` URI (this is configurable with the
  1831. ``static_url_prefix`` setting), and we will serve ``/favicon.ico``
  1832. and ``/robots.txt`` from the same directory. A custom subclass of
  1833. `StaticFileHandler` can be specified with the
  1834. ``static_handler_class`` setting.
  1835. .. versionchanged:: 4.5
  1836. Integration with the new `tornado.routing` module.
  1837. """
  1838. def __init__(
  1839. self,
  1840. handlers: Optional[_RuleList] = None,
  1841. default_host: Optional[str] = None,
  1842. transforms: Optional[List[Type["OutputTransform"]]] = None,
  1843. **settings: Any,
  1844. ) -> None:
  1845. if transforms is None:
  1846. self.transforms = [] # type: List[Type[OutputTransform]]
  1847. if settings.get("compress_response") or settings.get("gzip"):
  1848. self.transforms.append(GZipContentEncoding)
  1849. else:
  1850. self.transforms = transforms
  1851. self.default_host = default_host
  1852. self.settings = settings
  1853. self.ui_modules = {
  1854. "linkify": _linkify,
  1855. "xsrf_form_html": _xsrf_form_html,
  1856. "Template": TemplateModule,
  1857. }
  1858. self.ui_methods = {} # type: Dict[str, Callable[..., str]]
  1859. self._load_ui_modules(settings.get("ui_modules", {}))
  1860. self._load_ui_methods(settings.get("ui_methods", {}))
  1861. if self.settings.get("static_path"):
  1862. path = self.settings["static_path"]
  1863. handlers = list(handlers or [])
  1864. static_url_prefix = settings.get("static_url_prefix", "/static/")
  1865. static_handler_class = settings.get(
  1866. "static_handler_class", StaticFileHandler
  1867. )
  1868. static_handler_args = settings.get("static_handler_args", {})
  1869. static_handler_args["path"] = path
  1870. for pattern in [
  1871. re.escape(static_url_prefix) + r"(.*)",
  1872. r"/(favicon\.ico)",
  1873. r"/(robots\.txt)",
  1874. ]:
  1875. handlers.insert(0, (pattern, static_handler_class, static_handler_args))
  1876. if self.settings.get("debug"):
  1877. self.settings.setdefault("autoreload", True)
  1878. self.settings.setdefault("compiled_template_cache", False)
  1879. self.settings.setdefault("static_hash_cache", False)
  1880. self.settings.setdefault("serve_traceback", True)
  1881. self.wildcard_router = _ApplicationRouter(self, handlers)
  1882. self.default_router = _ApplicationRouter(
  1883. self, [Rule(AnyMatches(), self.wildcard_router)]
  1884. )
  1885. # Automatically reload modified modules
  1886. if self.settings.get("autoreload"):
  1887. from tornado import autoreload
  1888. autoreload.start()
  1889. def listen(
  1890. self,
  1891. port: int,
  1892. address: Optional[str] = None,
  1893. *,
  1894. family: socket.AddressFamily = socket.AF_UNSPEC,
  1895. backlog: int = tornado.netutil._DEFAULT_BACKLOG,
  1896. flags: Optional[int] = None,
  1897. reuse_port: bool = False,
  1898. **kwargs: Any,
  1899. ) -> HTTPServer:
  1900. """Starts an HTTP server for this application on the given port.
  1901. This is a convenience alias for creating an `.HTTPServer` object and
  1902. calling its listen method. Keyword arguments not supported by
  1903. `HTTPServer.listen <.TCPServer.listen>` are passed to the `.HTTPServer`
  1904. constructor. For advanced uses (e.g. multi-process mode), do not use
  1905. this method; create an `.HTTPServer` and call its
  1906. `.TCPServer.bind`/`.TCPServer.start` methods directly.
  1907. Note that after calling this method you still need to call
  1908. ``IOLoop.current().start()`` (or run within ``asyncio.run``) to start
  1909. the server.
  1910. Returns the `.HTTPServer` object.
  1911. .. versionchanged:: 4.3
  1912. Now returns the `.HTTPServer` object.
  1913. .. versionchanged:: 6.2
  1914. Added support for new keyword arguments in `.TCPServer.listen`,
  1915. including ``reuse_port``.
  1916. """
  1917. server = HTTPServer(self, **kwargs)
  1918. server.listen(
  1919. port,
  1920. address=address,
  1921. family=family,
  1922. backlog=backlog,
  1923. flags=flags,
  1924. reuse_port=reuse_port,
  1925. )
  1926. return server
  1927. def add_handlers(self, host_pattern: str, host_handlers: _RuleList) -> None:
  1928. """Appends the given handlers to our handler list.
  1929. Host patterns are processed sequentially in the order they were
  1930. added. All matching patterns will be considered.
  1931. """
  1932. host_matcher = HostMatches(host_pattern)
  1933. rule = Rule(host_matcher, _ApplicationRouter(self, host_handlers))
  1934. self.default_router.rules.insert(-1, rule)
  1935. if self.default_host is not None:
  1936. self.wildcard_router.add_rules(
  1937. [(DefaultHostMatches(self, host_matcher.host_pattern), host_handlers)]
  1938. )
  1939. def add_transform(self, transform_class: Type["OutputTransform"]) -> None:
  1940. self.transforms.append(transform_class)
  1941. def _load_ui_methods(self, methods: Any) -> None:
  1942. if isinstance(methods, types.ModuleType):
  1943. self._load_ui_methods({n: getattr(methods, n) for n in dir(methods)})
  1944. elif isinstance(methods, list):
  1945. for m in methods:
  1946. self._load_ui_methods(m)
  1947. else:
  1948. for name, fn in methods.items():
  1949. if (
  1950. not name.startswith("_")
  1951. and hasattr(fn, "__call__")
  1952. and name[0].lower() == name[0]
  1953. ):
  1954. self.ui_methods[name] = fn
  1955. def _load_ui_modules(self, modules: Any) -> None:
  1956. if isinstance(modules, types.ModuleType):
  1957. self._load_ui_modules({n: getattr(modules, n) for n in dir(modules)})
  1958. elif isinstance(modules, list):
  1959. for m in modules:
  1960. self._load_ui_modules(m)
  1961. else:
  1962. assert isinstance(modules, dict)
  1963. for name, cls in modules.items():
  1964. try:
  1965. if issubclass(cls, UIModule):
  1966. self.ui_modules[name] = cls
  1967. except TypeError:
  1968. pass
  1969. def __call__(
  1970. self, request: httputil.HTTPServerRequest
  1971. ) -> Optional[Awaitable[None]]:
  1972. # Legacy HTTPServer interface
  1973. dispatcher = self.find_handler(request)
  1974. return dispatcher.execute()
  1975. def find_handler(
  1976. self, request: httputil.HTTPServerRequest, **kwargs: Any
  1977. ) -> "_HandlerDelegate":
  1978. route = self.default_router.find_handler(request)
  1979. if route is not None:
  1980. return cast("_HandlerDelegate", route)
  1981. if self.settings.get("default_handler_class"):
  1982. return self.get_handler_delegate(
  1983. request,
  1984. self.settings["default_handler_class"],
  1985. self.settings.get("default_handler_args", {}),
  1986. )
  1987. return self.get_handler_delegate(request, ErrorHandler, {"status_code": 404})
  1988. def get_handler_delegate(
  1989. self,
  1990. request: httputil.HTTPServerRequest,
  1991. target_class: Type[RequestHandler],
  1992. target_kwargs: Optional[Dict[str, Any]] = None,
  1993. path_args: Optional[List[bytes]] = None,
  1994. path_kwargs: Optional[Dict[str, bytes]] = None,
  1995. ) -> "_HandlerDelegate":
  1996. """Returns `~.httputil.HTTPMessageDelegate` that can serve a request
  1997. for application and `RequestHandler` subclass.
  1998. :arg httputil.HTTPServerRequest request: current HTTP request.
  1999. :arg RequestHandler target_class: a `RequestHandler` class.
  2000. :arg dict target_kwargs: keyword arguments for ``target_class`` constructor.
  2001. :arg list path_args: positional arguments for ``target_class`` HTTP method that
  2002. will be executed while handling a request (``get``, ``post`` or any other).
  2003. :arg dict path_kwargs: keyword arguments for ``target_class`` HTTP method.
  2004. """
  2005. return _HandlerDelegate(
  2006. self, request, target_class, target_kwargs, path_args, path_kwargs
  2007. )
  2008. def reverse_url(self, name: str, *args: Any) -> str:
  2009. """Returns a URL path for handler named ``name``
  2010. The handler must be added to the application as a named `URLSpec`.
  2011. Args will be substituted for capturing groups in the `URLSpec` regex.
  2012. They will be converted to strings if necessary, encoded as utf8,
  2013. and url-escaped.
  2014. """
  2015. reversed_url = self.default_router.reverse_url(name, *args)
  2016. if reversed_url is not None:
  2017. return reversed_url
  2018. raise KeyError("%s not found in named urls" % name)
  2019. def log_request(self, handler: RequestHandler) -> None:
  2020. """Writes a completed HTTP request to the logs.
  2021. By default writes to the python root logger. To change
  2022. this behavior either subclass Application and override this method,
  2023. or pass a function in the application settings dictionary as
  2024. ``log_function``.
  2025. """
  2026. if "log_function" in self.settings:
  2027. self.settings["log_function"](handler)
  2028. return
  2029. if handler.get_status() < 400:
  2030. log_method = access_log.info
  2031. elif handler.get_status() < 500:
  2032. log_method = access_log.warning
  2033. else:
  2034. log_method = access_log.error
  2035. request_time = 1000.0 * handler.request.request_time()
  2036. log_method(
  2037. "%d %s %.2fms",
  2038. handler.get_status(),
  2039. handler._request_summary(),
  2040. request_time,
  2041. )
  2042. class _HandlerDelegate(httputil.HTTPMessageDelegate):
  2043. def __init__(
  2044. self,
  2045. application: Application,
  2046. request: httputil.HTTPServerRequest,
  2047. handler_class: Type[RequestHandler],
  2048. handler_kwargs: Optional[Dict[str, Any]],
  2049. path_args: Optional[List[bytes]],
  2050. path_kwargs: Optional[Dict[str, bytes]],
  2051. ) -> None:
  2052. self.application = application
  2053. self.connection = request.connection
  2054. self.request = request
  2055. self.handler_class = handler_class
  2056. self.handler_kwargs = handler_kwargs or {}
  2057. self.path_args = path_args or []
  2058. self.path_kwargs = path_kwargs or {}
  2059. self.chunks = [] # type: List[bytes]
  2060. self.stream_request_body = _has_stream_request_body(self.handler_class)
  2061. def headers_received(
  2062. self,
  2063. start_line: Union[httputil.RequestStartLine, httputil.ResponseStartLine],
  2064. headers: httputil.HTTPHeaders,
  2065. ) -> Optional[Awaitable[None]]:
  2066. if self.stream_request_body:
  2067. self.request._body_future = Future()
  2068. return self.execute()
  2069. return None
  2070. def data_received(self, data: bytes) -> Optional[Awaitable[None]]:
  2071. if self.stream_request_body:
  2072. return self.handler.data_received(data)
  2073. else:
  2074. self.chunks.append(data)
  2075. return None
  2076. def finish(self) -> None:
  2077. if self.stream_request_body:
  2078. future_set_result_unless_cancelled(self.request._body_future, None)
  2079. else:
  2080. # Note that the body gets parsed in RequestHandler._execute so it can be in
  2081. # the right exception handler scope.
  2082. self.request.body = b"".join(self.chunks)
  2083. self.execute()
  2084. def on_connection_close(self) -> None:
  2085. if self.stream_request_body:
  2086. self.handler.on_connection_close()
  2087. else:
  2088. self.chunks = None # type: ignore
  2089. def execute(self) -> Optional[Awaitable[None]]:
  2090. # If template cache is disabled (usually in the debug mode),
  2091. # re-compile templates and reload static files on every
  2092. # request so you don't need to restart to see changes
  2093. if not self.application.settings.get("compiled_template_cache", True):
  2094. with RequestHandler._template_loader_lock:
  2095. for loader in RequestHandler._template_loaders.values():
  2096. loader.reset()
  2097. if not self.application.settings.get("static_hash_cache", True):
  2098. static_handler_class = self.application.settings.get(
  2099. "static_handler_class", StaticFileHandler
  2100. )
  2101. static_handler_class.reset()
  2102. self.handler = self.handler_class(
  2103. self.application, self.request, **self.handler_kwargs
  2104. )
  2105. transforms = [t(self.request) for t in self.application.transforms]
  2106. if self.stream_request_body:
  2107. self.handler._prepared_future = Future()
  2108. # Note that if an exception escapes handler._execute it will be
  2109. # trapped in the Future it returns (which we are ignoring here,
  2110. # leaving it to be logged when the Future is GC'd).
  2111. # However, that shouldn't happen because _execute has a blanket
  2112. # except handler, and we cannot easily access the IOLoop here to
  2113. # call add_future (because of the requirement to remain compatible
  2114. # with WSGI)
  2115. fut = gen.convert_yielded(
  2116. self.handler._execute(transforms, *self.path_args, **self.path_kwargs)
  2117. )
  2118. fut.add_done_callback(lambda f: f.result())
  2119. # If we are streaming the request body, then execute() is finished
  2120. # when the handler has prepared to receive the body. If not,
  2121. # it doesn't matter when execute() finishes (so we return None)
  2122. return self.handler._prepared_future
  2123. class HTTPError(Exception):
  2124. """An exception that will turn into an HTTP error response.
  2125. Raising an `HTTPError` is a convenient alternative to calling
  2126. `RequestHandler.send_error` since it automatically ends the
  2127. current function.
  2128. To customize the response sent with an `HTTPError`, override
  2129. `RequestHandler.write_error`.
  2130. :arg int status_code: HTTP status code. Must be listed in
  2131. `httplib.responses <http.client.responses>` unless the ``reason``
  2132. keyword argument is given.
  2133. :arg str log_message: Message to be written to the log for this error
  2134. (will not be shown to the user unless the `Application` is in debug
  2135. mode). May contain ``%s``-style placeholders, which will be filled
  2136. in with remaining positional parameters.
  2137. :arg str reason: Keyword-only argument. The HTTP "reason" phrase
  2138. to pass in the status line along with ``status_code`` (for example,
  2139. the "Not Found" in ``HTTP/1.1 404 Not Found``). Normally
  2140. determined automatically from ``status_code``, but can be used
  2141. to use a non-standard numeric code. This is not a general-purpose
  2142. error message.
  2143. """
  2144. def __init__(
  2145. self,
  2146. status_code: int = 500,
  2147. log_message: Optional[str] = None,
  2148. *args: Any,
  2149. **kwargs: Any,
  2150. ) -> None:
  2151. self.status_code = status_code
  2152. self._log_message = log_message
  2153. self.args = args
  2154. self.reason = kwargs.get("reason", None)
  2155. @property
  2156. def log_message(self) -> Optional[str]:
  2157. """
  2158. A backwards compatible way of accessing log_message.
  2159. """
  2160. if self._log_message and not self.args:
  2161. return self._log_message.replace("%", "%%")
  2162. return self._log_message
  2163. def get_message(self) -> Optional[str]:
  2164. if self._log_message and self.args:
  2165. return self._log_message % self.args
  2166. return self._log_message
  2167. def __str__(self) -> str:
  2168. message = "HTTP %d: %s" % (
  2169. self.status_code,
  2170. self.reason or httputil.responses.get(self.status_code, "Unknown"),
  2171. )
  2172. log_message = self.get_message()
  2173. if log_message:
  2174. return message + " (" + log_message + ")"
  2175. else:
  2176. return message
  2177. class Finish(Exception):
  2178. """An exception that ends the request without producing an error response.
  2179. When `Finish` is raised in a `RequestHandler`, the request will
  2180. end (calling `RequestHandler.finish` if it hasn't already been
  2181. called), but the error-handling methods (including
  2182. `RequestHandler.write_error`) will not be called.
  2183. If `Finish()` was created with no arguments, the pending response
  2184. will be sent as-is. If `Finish()` was given an argument, that
  2185. argument will be passed to `RequestHandler.finish()`.
  2186. This can be a more convenient way to implement custom error pages
  2187. than overriding ``write_error`` (especially in library code)::
  2188. if self.current_user is None:
  2189. self.set_status(401)
  2190. self.set_header('WWW-Authenticate', 'Basic realm="something"')
  2191. raise Finish()
  2192. .. versionchanged:: 4.3
  2193. Arguments passed to ``Finish()`` will be passed on to
  2194. `RequestHandler.finish`.
  2195. """
  2196. pass
  2197. class MissingArgumentError(HTTPError):
  2198. """Exception raised by `RequestHandler.get_argument`.
  2199. This is a subclass of `HTTPError`, so if it is uncaught a 400 response
  2200. code will be used instead of 500 (and a stack trace will not be logged).
  2201. .. versionadded:: 3.1
  2202. """
  2203. def __init__(self, arg_name: str) -> None:
  2204. super().__init__(400, "Missing argument %s" % arg_name)
  2205. self.arg_name = arg_name
  2206. class ErrorHandler(RequestHandler):
  2207. """Generates an error response with ``status_code`` for all requests."""
  2208. def initialize(self, status_code: int) -> None:
  2209. self.set_status(status_code)
  2210. def prepare(self) -> None:
  2211. raise HTTPError(self._status_code)
  2212. def check_xsrf_cookie(self) -> None:
  2213. # POSTs to an ErrorHandler don't actually have side effects,
  2214. # so we don't need to check the xsrf token. This allows POSTs
  2215. # to the wrong url to return a 404 instead of 403.
  2216. pass
  2217. class RedirectHandler(RequestHandler):
  2218. """Redirects the client to the given URL for all GET requests.
  2219. You should provide the keyword argument ``url`` to the handler, e.g.::
  2220. application = web.Application([
  2221. (r"/oldpath", web.RedirectHandler, {"url": "/newpath"}),
  2222. ])
  2223. `RedirectHandler` supports regular expression substitutions. E.g., to
  2224. swap the first and second parts of a path while preserving the remainder::
  2225. application = web.Application([
  2226. (r"/(.*?)/(.*?)/(.*)", web.RedirectHandler, {"url": "/{1}/{0}/{2}"}),
  2227. ])
  2228. The final URL is formatted with `str.format` and the substrings that match
  2229. the capturing groups. In the above example, a request to "/a/b/c" would be
  2230. formatted like::
  2231. str.format("/{1}/{0}/{2}", "a", "b", "c") # -> "/b/a/c"
  2232. Use Python's :ref:`format string syntax <formatstrings>` to customize how
  2233. values are substituted.
  2234. .. versionchanged:: 4.5
  2235. Added support for substitutions into the destination URL.
  2236. .. versionchanged:: 5.0
  2237. If any query arguments are present, they will be copied to the
  2238. destination URL.
  2239. """
  2240. def initialize(self, url: str, permanent: bool = True) -> None:
  2241. self._url = url
  2242. self._permanent = permanent
  2243. def get(self, *args: Any, **kwargs: Any) -> None:
  2244. to_url = self._url.format(*args, **kwargs)
  2245. if self.request.query_arguments:
  2246. # TODO: figure out typing for the next line.
  2247. to_url = httputil.url_concat(
  2248. to_url,
  2249. list(httputil.qs_to_qsl(self.request.query_arguments)), # type: ignore
  2250. )
  2251. self.redirect(to_url, permanent=self._permanent)
  2252. class StaticFileHandler(RequestHandler):
  2253. """A simple handler that can serve static content from a directory.
  2254. A `StaticFileHandler` is configured automatically if you pass the
  2255. ``static_path`` keyword argument to `Application`. This handler
  2256. can be customized with the ``static_url_prefix``, ``static_handler_class``,
  2257. and ``static_handler_args`` settings.
  2258. To map an additional path to this handler for a static data directory
  2259. you would add a line to your application like::
  2260. application = web.Application([
  2261. (r"/content/(.*)", web.StaticFileHandler, {"path": "/var/www"}),
  2262. ])
  2263. The handler constructor requires a ``path`` argument, which specifies the
  2264. local root directory of the content to be served.
  2265. Note that a capture group in the regex is required to parse the value for
  2266. the ``path`` argument to the get() method (different than the constructor
  2267. argument above); see `URLSpec` for details.
  2268. To serve a file like ``index.html`` automatically when a directory is
  2269. requested, set ``static_handler_args=dict(default_filename="index.html")``
  2270. in your application settings, or add ``default_filename`` as an initializer
  2271. argument for your ``StaticFileHandler``.
  2272. To maximize the effectiveness of browser caching, this class supports
  2273. versioned urls (by default using the argument ``?v=``). If a version
  2274. is given, we instruct the browser to cache this file indefinitely.
  2275. `make_static_url` (also available as `RequestHandler.static_url`) can
  2276. be used to construct a versioned url.
  2277. This handler is intended primarily for use in development and light-duty
  2278. file serving; for heavy traffic it will be more efficient to use
  2279. a dedicated static file server (such as nginx or Apache). We support
  2280. the HTTP ``Accept-Ranges`` mechanism to return partial content (because
  2281. some browsers require this functionality to be present to seek in
  2282. HTML5 audio or video).
  2283. **Subclassing notes**
  2284. This class is designed to be extensible by subclassing, but because
  2285. of the way static urls are generated with class methods rather than
  2286. instance methods, the inheritance patterns are somewhat unusual.
  2287. Be sure to use the ``@classmethod`` decorator when overriding a
  2288. class method. Instance methods may use the attributes ``self.path``
  2289. ``self.absolute_path``, and ``self.modified``.
  2290. Subclasses should only override methods discussed in this section;
  2291. overriding other methods is error-prone. Overriding
  2292. ``StaticFileHandler.get`` is particularly problematic due to the
  2293. tight coupling with ``compute_etag`` and other methods.
  2294. To change the way static urls are generated (e.g. to match the behavior
  2295. of another server or CDN), override `make_static_url`, `parse_url_path`,
  2296. `get_cache_time`, and/or `get_version`.
  2297. To replace all interaction with the filesystem (e.g. to serve
  2298. static content from a database), override `get_content`,
  2299. `get_content_size`, `get_modified_time`, `get_absolute_path`, and
  2300. `validate_absolute_path`.
  2301. .. versionchanged:: 3.1
  2302. Many of the methods for subclasses were added in Tornado 3.1.
  2303. """
  2304. CACHE_MAX_AGE = 86400 * 365 * 10 # 10 years
  2305. _static_hashes = {} # type: Dict[str, Optional[str]]
  2306. _lock = threading.Lock() # protects _static_hashes
  2307. def initialize(self, path: str, default_filename: Optional[str] = None) -> None:
  2308. self.root = path
  2309. self.default_filename = default_filename
  2310. @classmethod
  2311. def reset(cls) -> None:
  2312. with cls._lock:
  2313. cls._static_hashes = {}
  2314. def head(self, path: str) -> Awaitable[None]:
  2315. return self.get(path, include_body=False)
  2316. async def get(self, path: str, include_body: bool = True) -> None:
  2317. # Set up our path instance variables.
  2318. self.path = self.parse_url_path(path)
  2319. del path # make sure we don't refer to path instead of self.path again
  2320. absolute_path = self.get_absolute_path(self.root, self.path)
  2321. self.absolute_path = self.validate_absolute_path(self.root, absolute_path)
  2322. if self.absolute_path is None:
  2323. return
  2324. self.modified = self.get_modified_time()
  2325. self.set_headers()
  2326. if self.should_return_304():
  2327. self.set_status(304)
  2328. return
  2329. request_range = None
  2330. range_header = self.request.headers.get("Range")
  2331. if range_header:
  2332. # As per RFC 2616 14.16, if an invalid Range header is specified,
  2333. # the request will be treated as if the header didn't exist.
  2334. request_range = httputil._parse_request_range(range_header)
  2335. size = self.get_content_size()
  2336. if request_range:
  2337. start, end = request_range
  2338. if start is not None and start < 0:
  2339. start += size
  2340. if start < 0:
  2341. start = 0
  2342. if (
  2343. start is not None
  2344. and (start >= size or (end is not None and start >= end))
  2345. ) or end == 0:
  2346. # As per RFC 2616 14.35.1, a range is not satisfiable only: if
  2347. # the first requested byte is equal to or greater than the
  2348. # content, or when a suffix with length 0 is specified.
  2349. # https://tools.ietf.org/html/rfc7233#section-2.1
  2350. # A byte-range-spec is invalid if the last-byte-pos value is present
  2351. # and less than the first-byte-pos.
  2352. self.set_status(416) # Range Not Satisfiable
  2353. self.set_header("Content-Type", "text/plain")
  2354. self.set_header("Content-Range", f"bytes */{size}")
  2355. return
  2356. if end is not None and end > size:
  2357. # Clients sometimes blindly use a large range to limit their
  2358. # download size; cap the endpoint at the actual file size.
  2359. end = size
  2360. # Note: only return HTTP 206 if less than the entire range has been
  2361. # requested. Not only is this semantically correct, but Chrome
  2362. # refuses to play audio if it gets an HTTP 206 in response to
  2363. # ``Range: bytes=0-``.
  2364. if size != (end or size) - (start or 0):
  2365. self.set_status(206) # Partial Content
  2366. self.set_header(
  2367. "Content-Range", httputil._get_content_range(start, end, size)
  2368. )
  2369. else:
  2370. start = end = None
  2371. if start is not None and end is not None:
  2372. content_length = end - start
  2373. elif end is not None:
  2374. content_length = end
  2375. elif start is not None:
  2376. content_length = size - start
  2377. else:
  2378. content_length = size
  2379. self.set_header("Content-Length", content_length)
  2380. if include_body:
  2381. content = self.get_content(self.absolute_path, start, end)
  2382. if isinstance(content, bytes):
  2383. content = [content]
  2384. for chunk in content:
  2385. try:
  2386. self.write(chunk)
  2387. await self.flush()
  2388. except iostream.StreamClosedError:
  2389. return
  2390. else:
  2391. assert self.request.method == "HEAD"
  2392. def compute_etag(self) -> Optional[str]:
  2393. """Sets the ``Etag`` header based on static url version.
  2394. This allows efficient ``If-None-Match`` checks against cached
  2395. versions, and sends the correct ``Etag`` for a partial response
  2396. (i.e. the same ``Etag`` as the full file).
  2397. .. versionadded:: 3.1
  2398. """
  2399. assert self.absolute_path is not None
  2400. version_hash = self._get_cached_version(self.absolute_path)
  2401. if not version_hash:
  2402. return None
  2403. return f'"{version_hash}"'
  2404. def set_headers(self) -> None:
  2405. """Sets the content and caching headers on the response.
  2406. .. versionadded:: 3.1
  2407. """
  2408. self.set_header("Accept-Ranges", "bytes")
  2409. self.set_etag_header()
  2410. if self.modified is not None:
  2411. self.set_header("Last-Modified", self.modified)
  2412. content_type = self.get_content_type()
  2413. if content_type:
  2414. self.set_header("Content-Type", content_type)
  2415. cache_time = self.get_cache_time(self.path, self.modified, content_type)
  2416. if cache_time > 0:
  2417. self.set_header(
  2418. "Expires",
  2419. datetime.datetime.now(datetime.timezone.utc)
  2420. + datetime.timedelta(seconds=cache_time),
  2421. )
  2422. self.set_header("Cache-Control", "max-age=" + str(cache_time))
  2423. self.set_extra_headers(self.path)
  2424. def should_return_304(self) -> bool:
  2425. """Returns True if the headers indicate that we should return 304.
  2426. .. versionadded:: 3.1
  2427. """
  2428. # If client sent If-None-Match, use it, ignore If-Modified-Since
  2429. if self.request.headers.get("If-None-Match"):
  2430. return self.check_etag_header()
  2431. # Check the If-Modified-Since, and don't send the result if the
  2432. # content has not been modified
  2433. ims_value = self.request.headers.get("If-Modified-Since")
  2434. if ims_value is not None:
  2435. try:
  2436. if_since = email.utils.parsedate_to_datetime(ims_value)
  2437. except Exception:
  2438. return False
  2439. if if_since.tzinfo is None:
  2440. if_since = if_since.replace(tzinfo=datetime.timezone.utc)
  2441. assert self.modified is not None
  2442. if if_since >= self.modified:
  2443. return True
  2444. return False
  2445. @classmethod
  2446. def get_absolute_path(cls, root: str, path: str) -> str:
  2447. """Returns the absolute location of ``path`` relative to ``root``.
  2448. ``root`` is the path configured for this `StaticFileHandler`
  2449. (in most cases the ``static_path`` `Application` setting).
  2450. This class method may be overridden in subclasses. By default
  2451. it returns a filesystem path, but other strings may be used
  2452. as long as they are unique and understood by the subclass's
  2453. overridden `get_content`.
  2454. .. versionadded:: 3.1
  2455. """
  2456. abspath = os.path.abspath(os.path.join(root, path))
  2457. return abspath
  2458. def validate_absolute_path(self, root: str, absolute_path: str) -> Optional[str]:
  2459. """Validate and return the absolute path.
  2460. ``root`` is the configured path for the `StaticFileHandler`,
  2461. and ``path`` is the result of `get_absolute_path`
  2462. This is an instance method called during request processing,
  2463. so it may raise `HTTPError` or use methods like
  2464. `RequestHandler.redirect` (return None after redirecting to
  2465. halt further processing). This is where 404 errors for missing files
  2466. are generated.
  2467. This method may modify the path before returning it, but note that
  2468. any such modifications will not be understood by `make_static_url`.
  2469. In instance methods, this method's result is available as
  2470. ``self.absolute_path``.
  2471. .. versionadded:: 3.1
  2472. """
  2473. # os.path.abspath strips a trailing /.
  2474. # We must add it back to `root` so that we only match files
  2475. # in a directory named `root` instead of files starting with
  2476. # that prefix.
  2477. root = os.path.abspath(root)
  2478. if not root.endswith(os.path.sep):
  2479. # abspath always removes a trailing slash, except when
  2480. # root is '/'. This is an unusual case, but several projects
  2481. # have independently discovered this technique to disable
  2482. # Tornado's path validation and (hopefully) do their own,
  2483. # so we need to support it.
  2484. root += os.path.sep
  2485. # The trailing slash also needs to be temporarily added back
  2486. # the requested path so a request to root/ will match.
  2487. if not (absolute_path + os.path.sep).startswith(root):
  2488. raise HTTPError(403, "%s is not in root static directory", self.path)
  2489. if os.path.isdir(absolute_path) and self.default_filename is not None:
  2490. # need to look at the request.path here for when path is empty
  2491. # but there is some prefix to the path that was already
  2492. # trimmed by the routing
  2493. if not self.request.path.endswith("/"):
  2494. if self.request.path.startswith("//"):
  2495. # A redirect with two initial slashes is a "protocol-relative" URL.
  2496. # This means the next path segment is treated as a hostname instead
  2497. # of a part of the path, making this effectively an open redirect.
  2498. # Reject paths starting with two slashes to prevent this.
  2499. # This is only reachable under certain configurations.
  2500. raise HTTPError(
  2501. 403, "cannot redirect path with two initial slashes"
  2502. )
  2503. self.redirect(self.request.path + "/", permanent=True)
  2504. return None
  2505. absolute_path = os.path.join(absolute_path, self.default_filename)
  2506. if not os.path.exists(absolute_path):
  2507. raise HTTPError(404)
  2508. if not os.path.isfile(absolute_path):
  2509. raise HTTPError(403, "%s is not a file", self.path)
  2510. return absolute_path
  2511. @classmethod
  2512. def get_content(
  2513. cls, abspath: str, start: Optional[int] = None, end: Optional[int] = None
  2514. ) -> Generator[bytes, None, None]:
  2515. """Retrieve the content of the requested resource which is located
  2516. at the given absolute path.
  2517. This class method may be overridden by subclasses. Note that its
  2518. signature is different from other overridable class methods
  2519. (no ``settings`` argument); this is deliberate to ensure that
  2520. ``abspath`` is able to stand on its own as a cache key.
  2521. This method should either return a byte string or an iterator
  2522. of byte strings. The latter is preferred for large files
  2523. as it helps reduce memory fragmentation.
  2524. .. versionadded:: 3.1
  2525. """
  2526. with open(abspath, "rb") as file:
  2527. if start is not None:
  2528. file.seek(start)
  2529. if end is not None:
  2530. remaining = end - (start or 0) # type: Optional[int]
  2531. else:
  2532. remaining = None
  2533. while True:
  2534. chunk_size = 64 * 1024
  2535. if remaining is not None and remaining < chunk_size:
  2536. chunk_size = remaining
  2537. chunk = file.read(chunk_size)
  2538. if chunk:
  2539. if remaining is not None:
  2540. remaining -= len(chunk)
  2541. yield chunk
  2542. else:
  2543. if remaining is not None:
  2544. assert remaining == 0
  2545. return
  2546. @classmethod
  2547. def get_content_version(cls, abspath: str) -> str:
  2548. """Returns a version string for the resource at the given path.
  2549. This class method may be overridden by subclasses. The
  2550. default implementation is a SHA-512 hash of the file's contents.
  2551. .. versionadded:: 3.1
  2552. """
  2553. data = cls.get_content(abspath)
  2554. hasher = hashlib.sha512()
  2555. if isinstance(data, bytes):
  2556. hasher.update(data)
  2557. else:
  2558. for chunk in data:
  2559. hasher.update(chunk)
  2560. return hasher.hexdigest()
  2561. def _stat(self) -> os.stat_result:
  2562. assert self.absolute_path is not None
  2563. if not hasattr(self, "_stat_result"):
  2564. self._stat_result = os.stat(self.absolute_path)
  2565. return self._stat_result
  2566. def get_content_size(self) -> int:
  2567. """Retrieve the total size of the resource at the given path.
  2568. This method may be overridden by subclasses.
  2569. .. versionadded:: 3.1
  2570. .. versionchanged:: 4.0
  2571. This method is now always called, instead of only when
  2572. partial results are requested.
  2573. """
  2574. stat_result = self._stat()
  2575. return stat_result.st_size
  2576. def get_modified_time(self) -> Optional[datetime.datetime]:
  2577. """Returns the time that ``self.absolute_path`` was last modified.
  2578. May be overridden in subclasses. Should return a `~datetime.datetime`
  2579. object or None.
  2580. .. versionadded:: 3.1
  2581. .. versionchanged:: 6.4
  2582. Now returns an aware datetime object instead of a naive one.
  2583. Subclasses that override this method may return either kind.
  2584. """
  2585. stat_result = self._stat()
  2586. # NOTE: Historically, this used stat_result[stat.ST_MTIME],
  2587. # which truncates the fractional portion of the timestamp. It
  2588. # was changed from that form to stat_result.st_mtime to
  2589. # satisfy mypy (which disallows the bracket operator), but the
  2590. # latter form returns a float instead of an int. For
  2591. # consistency with the past (and because we have a unit test
  2592. # that relies on this), we truncate the float here, although
  2593. # I'm not sure that's the right thing to do.
  2594. modified = datetime.datetime.fromtimestamp(
  2595. int(stat_result.st_mtime), datetime.timezone.utc
  2596. )
  2597. return modified
  2598. def get_content_type(self) -> str:
  2599. """Returns the ``Content-Type`` header to be used for this request.
  2600. .. versionadded:: 3.1
  2601. """
  2602. assert self.absolute_path is not None
  2603. mime_type, encoding = mimetypes.guess_type(self.absolute_path)
  2604. # per RFC 6713, use the appropriate type for a gzip compressed file
  2605. if encoding == "gzip":
  2606. return "application/gzip"
  2607. # As of 2015-07-21 there is no bzip2 encoding defined at
  2608. # http://www.iana.org/assignments/media-types/media-types.xhtml
  2609. # So for that (and any other encoding), use octet-stream.
  2610. elif encoding is not None:
  2611. return "application/octet-stream"
  2612. elif mime_type is not None:
  2613. return mime_type
  2614. # if mime_type not detected, use application/octet-stream
  2615. else:
  2616. return "application/octet-stream"
  2617. def set_extra_headers(self, path: str) -> None:
  2618. """For subclass to add extra headers to the response"""
  2619. pass
  2620. def get_cache_time(
  2621. self, path: str, modified: Optional[datetime.datetime], mime_type: str
  2622. ) -> int:
  2623. """Override to customize cache control behavior.
  2624. Return a positive number of seconds to make the result
  2625. cacheable for that amount of time or 0 to mark resource as
  2626. cacheable for an unspecified amount of time (subject to
  2627. browser heuristics).
  2628. By default returns cache expiry of 10 years for resources requested
  2629. with ``v`` argument.
  2630. """
  2631. return self.CACHE_MAX_AGE if "v" in self.request.arguments else 0
  2632. @classmethod
  2633. def make_static_url(
  2634. cls, settings: Dict[str, Any], path: str, include_version: bool = True
  2635. ) -> str:
  2636. """Constructs a versioned url for the given path.
  2637. This method may be overridden in subclasses (but note that it
  2638. is a class method rather than an instance method). Subclasses
  2639. are only required to implement the signature
  2640. ``make_static_url(cls, settings, path)``; other keyword
  2641. arguments may be passed through `~RequestHandler.static_url`
  2642. but are not standard.
  2643. ``settings`` is the `Application.settings` dictionary. ``path``
  2644. is the static path being requested. The url returned should be
  2645. relative to the current host.
  2646. ``include_version`` determines whether the generated URL should
  2647. include the query string containing the version hash of the
  2648. file corresponding to the given ``path``.
  2649. """
  2650. url = settings.get("static_url_prefix", "/static/") + path
  2651. if not include_version:
  2652. return url
  2653. version_hash = cls.get_version(settings, path)
  2654. if not version_hash:
  2655. return url
  2656. return f"{url}?v={version_hash}"
  2657. def parse_url_path(self, url_path: str) -> str:
  2658. """Converts a static URL path into a filesystem path.
  2659. ``url_path`` is the path component of the URL with
  2660. ``static_url_prefix`` removed. The return value should be
  2661. filesystem path relative to ``static_path``.
  2662. This is the inverse of `make_static_url`.
  2663. """
  2664. if os.path.sep != "/":
  2665. url_path = url_path.replace("/", os.path.sep)
  2666. return url_path
  2667. @classmethod
  2668. def get_version(cls, settings: Dict[str, Any], path: str) -> Optional[str]:
  2669. """Generate the version string to be used in static URLs.
  2670. ``settings`` is the `Application.settings` dictionary and ``path``
  2671. is the relative location of the requested asset on the filesystem.
  2672. The returned value should be a string, or ``None`` if no version
  2673. could be determined.
  2674. .. versionchanged:: 3.1
  2675. This method was previously recommended for subclasses to override;
  2676. `get_content_version` is now preferred as it allows the base
  2677. class to handle caching of the result.
  2678. """
  2679. abs_path = cls.get_absolute_path(settings["static_path"], path)
  2680. return cls._get_cached_version(abs_path)
  2681. @classmethod
  2682. def _get_cached_version(cls, abs_path: str) -> Optional[str]:
  2683. with cls._lock:
  2684. hashes = cls._static_hashes
  2685. if abs_path not in hashes:
  2686. try:
  2687. hashes[abs_path] = cls.get_content_version(abs_path)
  2688. except Exception:
  2689. gen_log.error("Could not open static file %r", abs_path)
  2690. hashes[abs_path] = None
  2691. hsh = hashes.get(abs_path)
  2692. if hsh:
  2693. return hsh
  2694. return None
  2695. class FallbackHandler(RequestHandler):
  2696. """A `RequestHandler` that wraps another HTTP server callback.
  2697. The fallback is a callable object that accepts an
  2698. `~.httputil.HTTPServerRequest`, such as an `Application` or
  2699. `tornado.wsgi.WSGIContainer`. This is most useful to use both
  2700. Tornado ``RequestHandlers`` and WSGI in the same server. Typical
  2701. usage::
  2702. wsgi_app = tornado.wsgi.WSGIContainer(
  2703. django.core.handlers.wsgi.WSGIHandler())
  2704. application = tornado.web.Application([
  2705. (r"/foo", FooHandler),
  2706. (r".*", FallbackHandler, dict(fallback=wsgi_app)),
  2707. ])
  2708. """
  2709. def initialize(
  2710. self, fallback: Callable[[httputil.HTTPServerRequest], None]
  2711. ) -> None:
  2712. self.fallback = fallback
  2713. def prepare(self) -> None:
  2714. self.fallback(self.request)
  2715. self._finished = True
  2716. self.on_finish()
  2717. class OutputTransform:
  2718. """A transform modifies the result of an HTTP request (e.g., GZip encoding)
  2719. Applications are not expected to create their own OutputTransforms
  2720. or interact with them directly; the framework chooses which transforms
  2721. (if any) to apply.
  2722. """
  2723. def __init__(self, request: httputil.HTTPServerRequest) -> None:
  2724. pass
  2725. def transform_first_chunk(
  2726. self,
  2727. status_code: int,
  2728. headers: httputil.HTTPHeaders,
  2729. chunk: bytes,
  2730. finishing: bool,
  2731. ) -> Tuple[int, httputil.HTTPHeaders, bytes]:
  2732. return status_code, headers, chunk
  2733. def transform_chunk(self, chunk: bytes, finishing: bool) -> bytes:
  2734. return chunk
  2735. class GZipContentEncoding(OutputTransform):
  2736. """Applies the gzip content encoding to the response.
  2737. See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.11
  2738. .. versionchanged:: 4.0
  2739. Now compresses all mime types beginning with ``text/``, instead
  2740. of just a whitelist. (the whitelist is still used for certain
  2741. non-text mime types).
  2742. """
  2743. # Whitelist of compressible mime types (in addition to any types
  2744. # beginning with "text/").
  2745. CONTENT_TYPES = {
  2746. "application/javascript",
  2747. "application/x-javascript",
  2748. "application/xml",
  2749. "application/atom+xml",
  2750. "application/json",
  2751. "application/xhtml+xml",
  2752. "image/svg+xml",
  2753. }
  2754. # Python's GzipFile defaults to level 9, while most other gzip
  2755. # tools (including gzip itself) default to 6, which is probably a
  2756. # better CPU/size tradeoff.
  2757. GZIP_LEVEL = 6
  2758. # Responses that are too short are unlikely to benefit from gzipping
  2759. # after considering the "Content-Encoding: gzip" header and the header
  2760. # inside the gzip encoding.
  2761. # Note that responses written in multiple chunks will be compressed
  2762. # regardless of size.
  2763. MIN_LENGTH = 1024
  2764. def __init__(self, request: httputil.HTTPServerRequest) -> None:
  2765. self._gzipping = "gzip" in request.headers.get("Accept-Encoding", "")
  2766. def _compressible_type(self, ctype: str) -> bool:
  2767. return ctype.startswith("text/") or ctype in self.CONTENT_TYPES
  2768. def transform_first_chunk(
  2769. self,
  2770. status_code: int,
  2771. headers: httputil.HTTPHeaders,
  2772. chunk: bytes,
  2773. finishing: bool,
  2774. ) -> Tuple[int, httputil.HTTPHeaders, bytes]:
  2775. # TODO: can/should this type be inherited from the superclass?
  2776. if "Vary" in headers:
  2777. headers["Vary"] += ", Accept-Encoding"
  2778. else:
  2779. headers["Vary"] = "Accept-Encoding"
  2780. if self._gzipping:
  2781. ctype = _unicode(headers.get("Content-Type", "")).split(";")[0]
  2782. self._gzipping = (
  2783. self._compressible_type(ctype)
  2784. and (not finishing or len(chunk) >= self.MIN_LENGTH)
  2785. and ("Content-Encoding" not in headers)
  2786. )
  2787. if self._gzipping:
  2788. headers["Content-Encoding"] = "gzip"
  2789. self._gzip_value = BytesIO()
  2790. self._gzip_file = gzip.GzipFile(
  2791. mode="w", fileobj=self._gzip_value, compresslevel=self.GZIP_LEVEL
  2792. )
  2793. chunk = self.transform_chunk(chunk, finishing)
  2794. if "Content-Length" in headers:
  2795. # The original content length is no longer correct.
  2796. # If this is the last (and only) chunk, we can set the new
  2797. # content-length; otherwise we remove it and fall back to
  2798. # chunked encoding.
  2799. if finishing:
  2800. headers["Content-Length"] = str(len(chunk))
  2801. else:
  2802. del headers["Content-Length"]
  2803. return status_code, headers, chunk
  2804. def transform_chunk(self, chunk: bytes, finishing: bool) -> bytes:
  2805. if self._gzipping:
  2806. self._gzip_file.write(chunk)
  2807. if finishing:
  2808. self._gzip_file.close()
  2809. else:
  2810. self._gzip_file.flush()
  2811. chunk = self._gzip_value.getvalue()
  2812. self._gzip_value.truncate(0)
  2813. self._gzip_value.seek(0)
  2814. return chunk
  2815. def authenticated(
  2816. method: Callable[..., Optional[Awaitable[None]]],
  2817. ) -> Callable[..., Optional[Awaitable[None]]]:
  2818. """Decorate methods with this to require that the user be logged in.
  2819. If the user is not logged in, they will be redirected to the configured
  2820. `login url <RequestHandler.get_login_url>`.
  2821. If you configure a login url with a query parameter, Tornado will
  2822. assume you know what you're doing and use it as-is. If not, it
  2823. will add a `next` parameter so the login page knows where to send
  2824. you once you're logged in.
  2825. """
  2826. @functools.wraps(method)
  2827. def wrapper( # type: ignore
  2828. self: RequestHandler, *args, **kwargs
  2829. ) -> Optional[Awaitable[None]]:
  2830. if not self.current_user:
  2831. if self.request.method in ("GET", "HEAD"):
  2832. url = self.get_login_url()
  2833. if "?" not in url:
  2834. if urllib.parse.urlsplit(url).scheme:
  2835. # if login url is absolute, make next absolute too
  2836. next_url = self.request.full_url()
  2837. else:
  2838. assert self.request.uri is not None
  2839. next_url = self.request.uri
  2840. url += "?" + urlencode(dict(next=next_url))
  2841. self.redirect(url)
  2842. return None
  2843. raise HTTPError(403)
  2844. return method(self, *args, **kwargs)
  2845. return wrapper
  2846. class UIModule:
  2847. """A re-usable, modular UI unit on a page.
  2848. UI modules often execute additional queries, and they can include
  2849. additional CSS and JavaScript that will be included in the output
  2850. page, which is automatically inserted on page render.
  2851. Subclasses of UIModule must override the `render` method.
  2852. """
  2853. def __init__(self, handler: RequestHandler) -> None:
  2854. self.handler = handler
  2855. self.request = handler.request
  2856. self.ui = handler.ui
  2857. self.locale = handler.locale
  2858. @property
  2859. def current_user(self) -> Any:
  2860. return self.handler.current_user
  2861. def render(self, *args: Any, **kwargs: Any) -> Union[str, bytes]:
  2862. """Override in subclasses to return this module's output."""
  2863. raise NotImplementedError()
  2864. def embedded_javascript(self) -> Optional[str]:
  2865. """Override to return a JavaScript string
  2866. to be embedded in the page."""
  2867. return None
  2868. def javascript_files(self) -> Optional[Iterable[str]]:
  2869. """Override to return a list of JavaScript files needed by this module.
  2870. If the return values are relative paths, they will be passed to
  2871. `RequestHandler.static_url`; otherwise they will be used as-is.
  2872. """
  2873. return None
  2874. def embedded_css(self) -> Optional[str]:
  2875. """Override to return a CSS string
  2876. that will be embedded in the page."""
  2877. return None
  2878. def css_files(self) -> Optional[Iterable[str]]:
  2879. """Override to returns a list of CSS files required by this module.
  2880. If the return values are relative paths, they will be passed to
  2881. `RequestHandler.static_url`; otherwise they will be used as-is.
  2882. """
  2883. return None
  2884. def html_head(self) -> Optional[str]:
  2885. """Override to return an HTML string that will be put in the <head/>
  2886. element.
  2887. """
  2888. return None
  2889. def html_body(self) -> Optional[str]:
  2890. """Override to return an HTML string that will be put at the end of
  2891. the <body/> element.
  2892. """
  2893. return None
  2894. def render_string(self, path: str, **kwargs: Any) -> bytes:
  2895. """Renders a template and returns it as a string."""
  2896. return self.handler.render_string(path, **kwargs)
  2897. class _linkify(UIModule):
  2898. def render(self, text: str, **kwargs: Any) -> str:
  2899. return escape.linkify(text, **kwargs)
  2900. class _xsrf_form_html(UIModule):
  2901. def render(self) -> str:
  2902. return self.handler.xsrf_form_html()
  2903. class TemplateModule(UIModule):
  2904. """UIModule that simply renders the given template.
  2905. {% module Template("foo.html") %} is similar to {% include "foo.html" %},
  2906. but the module version gets its own namespace (with kwargs passed to
  2907. Template()) instead of inheriting the outer template's namespace.
  2908. Templates rendered through this module also get access to UIModule's
  2909. automatic JavaScript/CSS features. Simply call set_resources
  2910. inside the template and give it keyword arguments corresponding to
  2911. the methods on UIModule: {{ set_resources(js_files=static_url("my.js")) }}
  2912. Note that these resources are output once per template file, not once
  2913. per instantiation of the template, so they must not depend on
  2914. any arguments to the template.
  2915. """
  2916. def __init__(self, handler: RequestHandler) -> None:
  2917. super().__init__(handler)
  2918. # keep resources in both a list and a dict to preserve order
  2919. self._resource_list = [] # type: List[Dict[str, Any]]
  2920. self._resource_dict = {} # type: Dict[str, Dict[str, Any]]
  2921. def render(self, path: str, **kwargs: Any) -> bytes:
  2922. def set_resources(**kwargs) -> str: # type: ignore
  2923. if path not in self._resource_dict:
  2924. self._resource_list.append(kwargs)
  2925. self._resource_dict[path] = kwargs
  2926. else:
  2927. if self._resource_dict[path] != kwargs:
  2928. raise ValueError(
  2929. "set_resources called with different "
  2930. "resources for the same template"
  2931. )
  2932. return ""
  2933. return self.render_string(path, set_resources=set_resources, **kwargs)
  2934. def _get_resources(self, key: str) -> Iterable[str]:
  2935. return (r[key] for r in self._resource_list if key in r)
  2936. def embedded_javascript(self) -> str:
  2937. return "\n".join(self._get_resources("embedded_javascript"))
  2938. def javascript_files(self) -> Iterable[str]:
  2939. result = []
  2940. for f in self._get_resources("javascript_files"):
  2941. if isinstance(f, (unicode_type, bytes)):
  2942. result.append(f)
  2943. else:
  2944. result.extend(f)
  2945. return result
  2946. def embedded_css(self) -> str:
  2947. return "\n".join(self._get_resources("embedded_css"))
  2948. def css_files(self) -> Iterable[str]:
  2949. result = []
  2950. for f in self._get_resources("css_files"):
  2951. if isinstance(f, (unicode_type, bytes)):
  2952. result.append(f)
  2953. else:
  2954. result.extend(f)
  2955. return result
  2956. def html_head(self) -> str:
  2957. return "".join(self._get_resources("html_head"))
  2958. def html_body(self) -> str:
  2959. return "".join(self._get_resources("html_body"))
  2960. class _UIModuleNamespace:
  2961. """Lazy namespace which creates UIModule proxies bound to a handler."""
  2962. def __init__(
  2963. self, handler: RequestHandler, ui_modules: Dict[str, Type[UIModule]]
  2964. ) -> None:
  2965. self.handler = handler
  2966. self.ui_modules = ui_modules
  2967. def __getitem__(self, key: str) -> Callable[..., str]:
  2968. return self.handler._ui_module(key, self.ui_modules[key])
  2969. def __getattr__(self, key: str) -> Callable[..., str]:
  2970. try:
  2971. return self[key]
  2972. except KeyError as e:
  2973. raise AttributeError(str(e))
  2974. def create_signed_value(
  2975. secret: _CookieSecretTypes,
  2976. name: str,
  2977. value: Union[str, bytes],
  2978. version: Optional[int] = None,
  2979. clock: Optional[Callable[[], float]] = None,
  2980. key_version: Optional[int] = None,
  2981. ) -> bytes:
  2982. if version is None:
  2983. version = DEFAULT_SIGNED_VALUE_VERSION
  2984. if clock is None:
  2985. clock = time.time
  2986. timestamp = utf8(str(int(clock())))
  2987. value = base64.b64encode(utf8(value))
  2988. if version == 1:
  2989. assert not isinstance(secret, dict)
  2990. signature = _create_signature_v1(secret, name, value, timestamp)
  2991. value = b"|".join([value, timestamp, signature])
  2992. return value
  2993. elif version == 2:
  2994. # The v2 format consists of a version number and a series of
  2995. # length-prefixed fields "%d:%s", the last of which is a
  2996. # signature, all separated by pipes. All numbers are in
  2997. # decimal format with no leading zeros. The signature is an
  2998. # HMAC-SHA256 of the whole string up to that point, including
  2999. # the final pipe.
  3000. #
  3001. # The fields are:
  3002. # - format version (i.e. 2; no length prefix)
  3003. # - key version (integer, default is 0)
  3004. # - timestamp (integer seconds since epoch)
  3005. # - name (not encoded; assumed to be ~alphanumeric)
  3006. # - value (base64-encoded)
  3007. # - signature (hex-encoded; no length prefix)
  3008. def format_field(s: Union[str, bytes]) -> bytes:
  3009. return utf8("%d:" % len(s)) + utf8(s)
  3010. to_sign = b"|".join(
  3011. [
  3012. b"2",
  3013. format_field(str(key_version or 0)),
  3014. format_field(timestamp),
  3015. format_field(name),
  3016. format_field(value),
  3017. b"",
  3018. ]
  3019. )
  3020. if isinstance(secret, dict):
  3021. assert (
  3022. key_version is not None
  3023. ), "Key version must be set when sign key dict is used"
  3024. assert version >= 2, "Version must be at least 2 for key version support"
  3025. secret = secret[key_version]
  3026. signature = _create_signature_v2(secret, to_sign)
  3027. return to_sign + signature
  3028. else:
  3029. raise ValueError("Unsupported version %d" % version)
  3030. # A leading version number in decimal
  3031. # with no leading zeros, followed by a pipe.
  3032. _signed_value_version_re = re.compile(rb"^([1-9][0-9]*)\|(.*)$")
  3033. def _get_version(value: bytes) -> int:
  3034. # Figures out what version value is. Version 1 did not include an
  3035. # explicit version field and started with arbitrary base64 data,
  3036. # which makes this tricky.
  3037. m = _signed_value_version_re.match(value)
  3038. if m is None:
  3039. version = 1
  3040. else:
  3041. try:
  3042. version = int(m.group(1))
  3043. if version > 999:
  3044. # Certain payloads from the version-less v1 format may
  3045. # be parsed as valid integers. Due to base64 padding
  3046. # restrictions, this can only happen for numbers whose
  3047. # length is a multiple of 4, so we can treat all
  3048. # numbers up to 999 as versions, and for the rest we
  3049. # fall back to v1 format.
  3050. version = 1
  3051. except ValueError:
  3052. version = 1
  3053. return version
  3054. def decode_signed_value(
  3055. secret: _CookieSecretTypes,
  3056. name: str,
  3057. value: Union[None, str, bytes],
  3058. max_age_days: float = 31,
  3059. clock: Optional[Callable[[], float]] = None,
  3060. min_version: Optional[int] = None,
  3061. ) -> Optional[bytes]:
  3062. if clock is None:
  3063. clock = time.time
  3064. if min_version is None:
  3065. min_version = DEFAULT_SIGNED_VALUE_MIN_VERSION
  3066. if min_version > 2:
  3067. raise ValueError("Unsupported min_version %d" % min_version)
  3068. if not value:
  3069. return None
  3070. value = utf8(value)
  3071. version = _get_version(value)
  3072. if version < min_version:
  3073. return None
  3074. if version == 1:
  3075. assert not isinstance(secret, dict)
  3076. return _decode_signed_value_v1(secret, name, value, max_age_days, clock)
  3077. elif version == 2:
  3078. return _decode_signed_value_v2(secret, name, value, max_age_days, clock)
  3079. else:
  3080. return None
  3081. def _decode_signed_value_v1(
  3082. secret: Union[str, bytes],
  3083. name: str,
  3084. value: bytes,
  3085. max_age_days: float,
  3086. clock: Callable[[], float],
  3087. ) -> Optional[bytes]:
  3088. parts = utf8(value).split(b"|")
  3089. if len(parts) != 3:
  3090. return None
  3091. signature = _create_signature_v1(secret, name, parts[0], parts[1])
  3092. if not hmac.compare_digest(parts[2], signature):
  3093. gen_log.warning("Invalid cookie signature %r", value)
  3094. return None
  3095. timestamp = int(parts[1])
  3096. if timestamp < clock() - max_age_days * 86400:
  3097. gen_log.warning("Expired cookie %r", value)
  3098. return None
  3099. if timestamp > clock() + 31 * 86400:
  3100. # _cookie_signature does not hash a delimiter between the
  3101. # parts of the cookie, so an attacker could transfer trailing
  3102. # digits from the payload to the timestamp without altering the
  3103. # signature. For backwards compatibility, sanity-check timestamp
  3104. # here instead of modifying _cookie_signature.
  3105. gen_log.warning("Cookie timestamp in future; possible tampering %r", value)
  3106. return None
  3107. if parts[1].startswith(b"0"):
  3108. gen_log.warning("Tampered cookie %r", value)
  3109. return None
  3110. try:
  3111. return base64.b64decode(parts[0])
  3112. except Exception:
  3113. return None
  3114. def _decode_fields_v2(value: bytes) -> Tuple[int, bytes, bytes, bytes, bytes]:
  3115. def _consume_field(s: bytes) -> Tuple[bytes, bytes]:
  3116. length, _, rest = s.partition(b":")
  3117. n = int(length)
  3118. field_value = rest[:n]
  3119. # In python 3, indexing bytes returns small integers; we must
  3120. # use a slice to get a byte string as in python 2.
  3121. if rest[n : n + 1] != b"|":
  3122. raise ValueError("malformed v2 signed value field")
  3123. rest = rest[n + 1 :]
  3124. return field_value, rest
  3125. rest = value[2:] # remove version number
  3126. key_version, rest = _consume_field(rest)
  3127. timestamp, rest = _consume_field(rest)
  3128. name_field, rest = _consume_field(rest)
  3129. value_field, passed_sig = _consume_field(rest)
  3130. return int(key_version), timestamp, name_field, value_field, passed_sig
  3131. def _decode_signed_value_v2(
  3132. secret: _CookieSecretTypes,
  3133. name: str,
  3134. value: bytes,
  3135. max_age_days: float,
  3136. clock: Callable[[], float],
  3137. ) -> Optional[bytes]:
  3138. try:
  3139. (
  3140. key_version,
  3141. timestamp_bytes,
  3142. name_field,
  3143. value_field,
  3144. passed_sig,
  3145. ) = _decode_fields_v2(value)
  3146. except ValueError:
  3147. return None
  3148. signed_string = value[: -len(passed_sig)]
  3149. if isinstance(secret, dict):
  3150. try:
  3151. secret = secret[key_version]
  3152. except KeyError:
  3153. return None
  3154. expected_sig = _create_signature_v2(secret, signed_string)
  3155. if not hmac.compare_digest(passed_sig, expected_sig):
  3156. return None
  3157. if name_field != utf8(name):
  3158. return None
  3159. timestamp = int(timestamp_bytes)
  3160. if timestamp < clock() - max_age_days * 86400:
  3161. # The signature has expired.
  3162. return None
  3163. try:
  3164. return base64.b64decode(value_field)
  3165. except Exception:
  3166. return None
  3167. def get_signature_key_version(value: Union[str, bytes]) -> Optional[int]:
  3168. value = utf8(value)
  3169. version = _get_version(value)
  3170. if version < 2:
  3171. return None
  3172. try:
  3173. key_version, _, _, _, _ = _decode_fields_v2(value)
  3174. except ValueError:
  3175. return None
  3176. return key_version
  3177. def _create_signature_v1(secret: Union[str, bytes], *parts: Union[str, bytes]) -> bytes:
  3178. hash = hmac.new(utf8(secret), digestmod=hashlib.sha1)
  3179. for part in parts:
  3180. hash.update(utf8(part))
  3181. return utf8(hash.hexdigest())
  3182. def _create_signature_v2(secret: Union[str, bytes], s: bytes) -> bytes:
  3183. hash = hmac.new(utf8(secret), digestmod=hashlib.sha256)
  3184. hash.update(utf8(s))
  3185. return utf8(hash.hexdigest())
  3186. def is_absolute(path: str) -> bool:
  3187. return any(path.startswith(x) for x in ["/", "http:", "https:"])