def handle(request, response=None): """Given a Request, return or raise a Response. """ if response is None: response = Response() mimetype, namespace, script, template = load(request) if namespace is None: response.body = template else: # Populate namespace. # =================== namespace.update(request.namespace) namespace['request'] = request namespace['response'] = response # Exec the script. # ================ if script: exec script in namespace response = namespace['response'] # Process the template. # ===================== # If template is None that means that that section was empty. if template is not None: response.body = template.generate(**namespace).encode(ENCODING).lstrip() # Set the mimetype. # ================= # Note that we guess based on the filesystem path, not the URL path. if response.headers.one('Content-Type') is None: if mimetype.startswith('text/'): mimetype += "; charset=" + ENCODING response.headers.set('Content-Type', mimetype) # Send it on back up the line. # ============================ return response
def handle(request, response=None): """Given a Request, return or raise a Response. """ if response is None: response = Response() mimetype, namespace, script, template = load(request) if namespace is None: response.body = template else: # Populate namespace. # =================== namespace.update(request.namespace) namespace['request'] = request namespace['response'] = response # Exec the script. # ================ if script: exec script in namespace response = namespace['response'] # Process the template. # ===================== # If template is None that means that that section was empty. if template is not None: response.body = template.generate( **namespace).encode(ENCODING).lstrip() # Set the mimetype. # ================= # Note that we guess based on the filesystem path, not the URL path. if response.headers.one('Content-Type') is None: if mimetype.startswith('text/'): mimetype += "; charset=" + ENCODING response.headers.set('Content-Type', mimetype) # Send it on back up the line. # ============================ return response
def test_handle_barely_working(): mk(('index.html', "Greetings, program!")) request = StubRequest('index.html') response = Response() handle(request, response) actual = response.body expected = "Greetings, program!" assert actual == expected, actual
def inbound(request): path = request.path if path in protected: # protect direct access raise Response(404) for mapping in mappings: # look for a match match = mapping.match(path) if match is not None: request.original_path = path if mapping.path == '': # redirect to trailing slash raise Response(301, headers={'Location': path + '/'}) #TODO qs else: # serve with explicit path request.path = mapping.path groups = match.groups() for key in range(len(groups)): # add [i] indices to url val = groups[key] if val is None: val = '' request.url[key] = val named = match.groupdict.items() for key, val in named: # add ['name'] indices to url if val is None: val = '' request.url[key] = val break # quit matching after first hit
def handle(request): """Handle a request for a websocket. """ if request.transport != 'xhr-polling': raise Response(404) org = request.headers.one('Origin') inq = Queue() outq = Queue() def wrap(request, inq, outq): handler(request, inq, outq) outq.put(WebSocketDisconnect()) fork(wrap, request, inq, outq) while True: try: log.debug("trying websocket thing") typ, val = first(receive=1, waits=[outq.wait_id]) log.debug(typ) log.debug(val) if typ == 'receive': assert val == '\x00' val = until('\xff')[:-1] if val == '': inq.put(WebSocketDisconnect()) else: inq.put(request) else: try: v = outq.get(waiting=False) except QueueEmpty: pass else: if type(v) is WebSocketDisconnect: send('\x00\xff') break else: send('\x00%s\xff' % response.to_http(request.version)) except ConnectionClosed: inq.put(WebSocketDisconnect()) raise ConnectionClosed("remote disconnected")
def test_to_diesel_raises_AttributeError(): # This is because _to_diesel actually pushes bits, but we have no loop response = Response() assert_raises(AttributeError, response._to_diesel, DieselReq)
def check(content): mk(('index.html', content)) request = StubRequest('index.html') response = Response() handle(request, response) return response.body
def xhr_polling_handler(request, inq, outq): while True: try: xhr_request = inq.get(timeout=0.5) except QueueTimeout: pass else: log.info("handling xhr long-polling request") log.info(ws_request) try: simplates.handle(request) except Response, response: pass except: response = Response(500, traceback.format_exc()) outq.put(response) def handle(request): """Handle a request for a websocket. """ if request.transport != 'xhr-polling': raise Response(404) org = request.headers.one('Origin') inq = Queue() outq = Queue() def wrap(request, inq, outq): handler(request, inq, outq)