def render(self, resrc): # this is called once a Resource has been found to serve the request; in our # case the Resource in question will normally be a JsonResource. # create a LogContext for this request request_id = self.get_request_id() logcontext = self.logcontext = LoggingContext(request_id) logcontext.request = request_id # override the Server header which is set by twisted self.setHeader("Server", self.site.server_version_string) with PreserveLoggingContext(self.logcontext): # we start the request metrics timer here with an initial stab # at the servlet name. For most requests that name will be # JsonResource (or a subclass), and JsonResource._async_render # will update it once it picks a servlet. servlet_name = resrc.__class__.__name__ self._started_processing(servlet_name) Request.render(self, resrc) # record the arrival of the request *after* # dispatching to the handler, so that the handler # can update the servlet name in the request # metrics requests_counter.labels(self.get_method(), self.request_metrics.name).inc()
def render(self, resrc): # this is called once a Resource has been found to serve the request; in our # case the Resource in question will normally be a JsonResource. # create a LogContext for this request request_id = self.get_request_id() self.logcontext = LoggingContext(request_id, request=request_id) # override the Server header which is set by twisted self.setHeader("Server", self.site.server_version_string) with PreserveLoggingContext(self.logcontext): # we start the request metrics timer here with an initial stab # at the servlet name. For most requests that name will be # JsonResource (or a subclass), and JsonResource._async_render # will update it once it picks a servlet. servlet_name = resrc.__class__.__name__ self._started_processing(servlet_name) Request.render(self, resrc) # record the arrival of the request *after* # dispatching to the handler, so that the handler # can update the servlet name in the request # metrics requests_counter.labels(self.get_method(), self.request_metrics.name).inc()
def _render_request(self, request: Request) -> Awaitable[MagicMock]: # Patch out the email sending so we can investigate the resulting email. with patch("sydent.sms.openmarket.OpenMarketSMS.sendTextSMS" ) as sendTextSMS: # We can't use AsyncMock until Python 3.8. Instead, mock the # function as returning a future. f = asyncio.Future() f.set_result(MagicMock()) sendTextSMS.return_value = f request.render(self.sydent.servlets.msisdnRequestCode) return sendTextSMS
def render(self, resource): """Renders identified resource and write response""" CORE.info('Rendering resource...') try: body = resource.render(self) except NotAuthenticated: body = None self.setResponseCode(BAD_REQUEST_UNAUTH) # HTTP FIXME except UnsupportedMethod: Request.render(self, resource) # use twisteds error handling return if body is NOT_DONE_YET: return self.respond(body)
def render(self, resrc: Resource) -> None: # this is called once a Resource has been found to serve the request; in our # case the Resource in question will normally be a JsonResource. # create a LogContext for this request request_id = self.get_request_id() self.logcontext = LoggingContext( request_id, request=ContextRequest( request_id=request_id, ip_address=self.getClientAddress().host, site_tag=self.synapse_site.site_tag, # The requester is going to be unknown at this point. requester=None, authenticated_entity=None, method=self.get_method(), url=self.get_redacted_uri(), protocol=self.clientproto.decode("ascii", errors="replace"), user_agent=get_request_user_agent(self), ), ) # override the Server header which is set by twisted self.setHeader("Server", self.synapse_site.server_version_string) with PreserveLoggingContext(self.logcontext): # we start the request metrics timer here with an initial stab # at the servlet name. For most requests that name will be # JsonResource (or a subclass), and JsonResource._async_render # will update it once it picks a servlet. servlet_name = resrc.__class__.__name__ self._started_processing(servlet_name) Request.render(self, resrc) # record the arrival of the request *after* # dispatching to the handler, so that the handler # can update the servlet name in the request # metrics requests_counter.labels(self.get_method(), self.request_metrics.name).inc()
def render(self, resource): message = None result = '' method = self.getHeader('X-UMC-Method') _ = self.getSession(Translation)._ # TODO: this runs in the module process so it can be a global try: self.site.initialize(self) return Request.render(self, resource) except UMC_OptionSanitizeError as exc: self.setResponseCode(409) # Conflict # HTTP FIXME message = exc.message result = exc.body except (UMC_OptionTypeError, UMC_OptionMissing, UMC_CommandError) as exc: self.setResponseCode(400) message = { UMC_OptionTypeError: _('An option passed to %s has the wrong type: %s') % (method, exc), UMC_OptionMissing: _('One or more options to %s are missing: %s') % (method, exc), UMC_CommandError: _('The command has failed: %s') % (exc, ) }.get(exc.__class__) # TODO: subclasses? except BaseException as exc: self.setResponseCode(MODULE_ERR_COMMAND_FAILED) # HTTP FIXME message = _("Execution of command '%(command)s' has failed:\n\n%(text)s") % { 'command': self.path, 'text': unicode(traceback.format_exc()) } if isinstance(message, unicode): message = message.encode('UTF-8') MODULE.process(message) message = json.dumps(message) result = json.dumps(result) self.setHeader('X-UMC-Message', message) self.setHeader('Content-Length', b'%d' % len(result)) self.write(result) self.finish()
def render(self, resrc): # override the Server header which is set by twisted self.setHeader("Server", self.site.server_version_string) return Request.render(self, resrc)
def render(self, resource): # NOTE(kgriffs): Override render, instead of process, to ensure # that we have the last say in the value of the headers. self._inject_headers() Request.render(self, resource)