def start_response(status, headers): resp = HttpResponse() resp.status = status [resp.header(h).values.append(v) for h, v in headers] return reject(resp)
def start_response(status, headers): resp = HttpResponse() resp.status = status [resp.header(h).values.append(v) for h, v in headers] return reject(resp)
def on_request_head(self, req): url = str(req.url) frag_split = url.split('#', 2) query_split = frag_split[0].split('?', 2) path = query_split[0] if path.startswith('/'): path = path[1:] frag = frag_split[1] if len(frag_split) > 1 else '' query = query_split[1] if len(query_split) > 1 else '' target_path = os.path.join(self._root, path) if os.path.isdir(target_path): target_path = os.path.join(target_path, 'index.html') if not os.path.exists(target_path): return filtering.reply(_NOT_FOUND) resp = HttpResponse() resp.version = b'1.1' resp.status = '200 OK' resp.header('Server').values.append('pyrox/{}'.format(VERSION)) fin = open(target_path, 'r') return filtering.reply(resp, fin)
class HttpFilter(object): """ HttpFilter is a marker class that may be utilized for dynamic gathering of filter logic. """ pass """ Default return object. This should be configurable. """ _DEFAULT_REJECT_RESP = HttpResponse() _DEFAULT_REJECT_RESP.version = b'1.1' _DEFAULT_REJECT_RESP.status = '400 Bad Request' _DEFAULT_REJECT_RESP.header('Content-Length').values.append('0') """ Default filter action singletons. """ _DEFAULT_PASS_ACTION = FilterAction(NEXT_FILTER) _DEFAULT_CONSUME_ACTION = FilterAction(CONSUME) def consume(): """ Consumes the event and does not allow any further downstream filters to see it. This effectively halts execution of the filter chain but leaves the request to pass through the proxy. """
class HttpFilter(object): """ HttpFilter is a marker class that may be utilized for dynamic gathering of filter logic. """ pass """ Default return object. This should be configurable. """ _DEFAULT_REJECT_RESP = HttpResponse() _DEFAULT_REJECT_RESP.version = b'1.1' _DEFAULT_REJECT_RESP.status = '400 Bad Request' _DEFAULT_REJECT_RESP.header('Content-Length').values.append('0') """ Default filter action singletons. """ _DEFAULT_PASS_ACTION = FilterAction(NEXT_FILTER) _DEFAULT_CONSUME_ACTION = FilterAction(CONSUME) def consume(): """ Consumes the event and does not allow any further downstream filters to see it. This effectively halts execution of the filter chain but leaves the request to pass through the proxy. """ return _DEFAULT_CONSUME_ACTION
import os import pyrox.filtering as filtering from pyrox.http import HttpResponse from pyrox.about import VERSION _VERSION_STR = 'pyrox/{}'.format(VERSION) _NOT_FOUND = HttpResponse() _NOT_FOUND.version = b'1.1' _NOT_FOUND.status = '404 Not Found' _NOT_FOUND.header('Server').values.append(_VERSION_STR) class WebServer(filtering.HttpFilter): def __init__(self, root): self._root = root @filtering.handles_request_head def on_request_head(self, req): url = str(req.url) frag_split = url.split('#', 2) query_split = frag_split[0].split('?', 2) path = query_split[0] if path.startswith('/'): path = path[1:]
_LOG = get_logger(__name__) """ String representing a 0 length HTTP chunked encoding chunk. """ _CHUNK_CLOSE = b'0\r\n\r\n' """ Default return object on error. This should be configurable. """ _BAD_GATEWAY_RESP = HttpResponse() _BAD_GATEWAY_RESP.version = b'1.1' _BAD_GATEWAY_RESP.status = '502 Bad Gateway' _BAD_GATEWAY_RESP.header('Server').values.append('pyrox/{}'.format(VERSION)) _BAD_GATEWAY_RESP.header('Content-Length').values.append('0') """ Default return object on no route or upstream not responding. This should be configurable. """ _UPSTREAM_UNAVAILABLE = HttpResponse() _UPSTREAM_UNAVAILABLE.version = b'1.1' _UPSTREAM_UNAVAILABLE.status = '503 Service Unavailable' _UPSTREAM_UNAVAILABLE.header('Server').values.append('pyrox/{}'.format(VERSION)) _UPSTREAM_UNAVAILABLE.header('Content-Length').values.append('0') def _write_to_stream(stream, data, is_chunked, callback=None):
_LOG = get_logger(__name__) """ String representing a 0 length HTTP chunked encoding chunk. """ _CHUNK_CLOSE = b'0\r\n\r\n' """ Default return object on error. This should be configurable. """ _BAD_GATEWAY_RESP = HttpResponse() _BAD_GATEWAY_RESP.version = b'1.1' _BAD_GATEWAY_RESP.status = '502 Bad Gateway' _BAD_GATEWAY_RESP.header('Server').values.append('pyrox/{}'.format(VERSION)) _BAD_GATEWAY_RESP.header('Content-Length').values.append('0') """ Default return object on no route or upstream not responding. This should be configurable. """ _UPSTREAM_UNAVAILABLE = HttpResponse() _UPSTREAM_UNAVAILABLE.version = b'1.1' _UPSTREAM_UNAVAILABLE.status = '503 Service Unavailable' _UPSTREAM_UNAVAILABLE.header('Server').values.append('pyrox/{}'.format(VERSION)) _UPSTREAM_UNAVAILABLE.header('Content-Length').values.append('0') def _write_to_stream(stream, data, is_chunked, callback=None):