def test_access_control(self): v = base.TokValue.parseString("<path")[0] with tutils.tmpdir() as t: p = os.path.join(t, "path") with open(p, "wb") as f: f.write(b"x" * 10000) assert v.get_generator(language.Settings(staticdir=t)) v = base.TokValue.parseString("<path2")[0] tutils.raises( exceptions.FileAccessDenied, v.get_generator, language.Settings(staticdir=t) ) tutils.raises( "access disabled", v.get_generator, language.Settings() ) v = base.TokValue.parseString("</outside")[0] tutils.raises( "outside", v.get_generator, language.Settings(staticdir=t) )
def _test_messages(self, specs, message_klass): for i in specs: wf = parse_request(i) assert isinstance(wf, message_klass) assert wf assert wf.values(language.Settings()) assert wf.resolve(language.Settings()) spec = wf.spec() wf2 = parse_request(spec) assert wf2.spec() == spec
def test_websocket(self): r = parse_request('ws:/path/') res = r.resolve(language.Settings()) assert res.method.string().lower() == b"get" assert res.tok(http.Path).value.val == b"/path/" assert res.tok(http.Method).value.val.lower() == b"get" assert http.get_header(b"Upgrade", res.headers).value.val == b"websocket" r = parse_request('ws:put:/path/') res = r.resolve(language.Settings()) assert r.method.string().lower() == b"put" assert res.tok(http.Path).value.val == b"/path/" assert res.tok(http.Method).value.val.lower() == b"put" assert http.get_header(b"Upgrade", res.headers).value.val == b"websocket"
def tval(self, requests, showreq=False, showresp=False, explain=False, showssl=False, hexdump=False, timeout=None, ignorecodes=(), ignoretimeout=None, showsummary=True): s = StringIO() c = pathoc.Pathoc(("127.0.0.1", self.d.port), ssl=self.ssl, showreq=showreq, showresp=showresp, explain=explain, hexdump=hexdump, ignorecodes=ignorecodes, ignoretimeout=ignoretimeout, showsummary=showsummary, fp=s) c.connect(showssl=showssl, fp=s) if timeout: c.settimeout(timeout) for i in requests: r = language.parse_pathoc(i).next() if explain: r = r.freeze(language.Settings()) try: c.request(r) except NetlibException: pass return s.getvalue()
def test_access_control(self, tmpdir): v = base.TokValue.parseString("<path")[0] f = tmpdir.join("path") f.write(b"x" * 10000) assert v.get_generator(language.Settings(staticdir=str(tmpdir))) v = base.TokValue.parseString("<path2")[0] with pytest.raises(exceptions.FileAccessDenied): v.get_generator(language.Settings(staticdir=str(tmpdir))) with pytest.raises(Exception, match="access disabled"): v.get_generator(language.Settings()) v = base.TokValue.parseString("</outside")[0] with pytest.raises(Exception, match="outside"): v.get_generator(language.Settings(staticdir=str(tmpdir)))
def test_caseless_literal(): class CL(base.CaselessLiteral): TOK = "foo" v = CL("foo") assert v.expr() assert v.values(language.Settings())
def test_intfield(): class TT(base.IntField): preamble = "t" names = { "one": 1, "two": 2, "three": 3 } max = 4 e = TT.expr() v = e.parseString("tone")[0] assert v.value == 1 assert v.spec() == "tone" assert v.values(language.Settings()) v = e.parseString("t1")[0] assert v.value == 1 assert v.spec() == "t1" v = e.parseString("t4")[0] assert v.value == 4 assert v.spec() == "t4" with pytest.raises(Exception, match="can't exceed"): e.parseString("t5")
def test_render(self): s = BytesIO() r = parse_request("GET:'/foo'") assert language.serve( r, s, language.Settings(request_host="foo.com") )
def __init__( self, addr, ssl=False, ssloptions=None, craftanchor=DEFAULT_CRAFT_ANCHOR, staticdir=None, anchors=(), sizelimit=None, nocraft=False, nohang=False, timeout=None, logreq=False, logresp=False, explain=False, hexdump=False, http2_framedump=False, webdebug=False, logfp=sys.stdout, ): """ addr: (address, port) tuple. If port is 0, a free port will be automatically chosen. ssloptions: an SSLOptions object. craftanchor: URL prefix specifying the path under which to anchor response generation. staticdir: path to a directory of static resources, or None. anchors: List of (regex object, language.Request object) tuples, or None. sizelimit: Limit size of served data. nocraft: Disable response crafting. nohang: Disable pauses. """ tcp.TCPServer.__init__(self, addr) self.ssl = ssl self.ssloptions = ssloptions or SSLOptions() self.staticdir = staticdir self.craftanchor = craftanchor self.sizelimit = sizelimit self.nocraft = nocraft self.nohang = nohang self.timeout, self.logreq = timeout, logreq self.logresp, self.hexdump = logresp, hexdump self.http2_framedump = http2_framedump self.explain = explain self.logfp = logfp self.log = [] self.logid = 0 self.anchors = anchors self.settings = language.Settings( staticdir=self.staticdir ) self.loglock = threading.Lock()
def tval(self, requests, timeout=None, showssl=False, **kwargs): s = io.StringIO() c = pathoc.Pathoc(("127.0.0.1", self.d.port), ssl=self.ssl, fp=s, **kwargs) with c.connect(showssl=showssl, fp=s): if timeout: c.settimeout(timeout) for i in requests: r = next(language.parse_pathoc(i)) if kwargs.get("explain"): r = r.freeze(language.Settings()) try: c.request(r) except exceptions.NetlibException: pass self.d.wait_for_silence() return s.getvalue()
def test_freeze(self): v = base.TokValueGenerate(100, "b", "ascii") f = v.freeze(language.Settings()) assert len(f.val) == 100
def render(r, settings=language.Settings()): r = r.resolve(settings) s = io.BytesIO() assert language.serve(r, s, settings) return s.getvalue()
def default_settings(): return language.Settings(request_host="foo.com", protocol=netlib.http.http2.HTTP2Protocol( tcp.TCPClient(('localhost', 1234))))
def __init__( self, address, # SSL ssl=None, sni=None, ssl_version=tls.DEFAULT_METHOD, ssl_options=tls.DEFAULT_OPTIONS, clientcert=None, ciphers=None, # HTTP/2 use_http2=False, http2_skip_connection_preface=False, http2_framedump=False, # Websockets ws_read_limit=None, # Network timeout=None, # Output control showreq=False, showresp=False, explain=False, hexdump=False, ignorecodes=(), ignoretimeout=False, showsummary=False, fp=sys.stdout): """ spec: A request specification showreq: Print requests showresp: Print responses explain: Print request explanation showssl: Print info on SSL connection hexdump: When printing requests or responses, use hex dump output showsummary: Show a summary of requests ignorecodes: Sequence of return codes to ignore """ tcp.TCPClient.__init__(self, address) self.ssl, self.sni = ssl, sni self.clientcert = clientcert self.ssl_version = ssl_version self.ssl_options = ssl_options self.ciphers = ciphers self.sslinfo = None self.use_http2 = use_http2 self.http2_skip_connection_preface = http2_skip_connection_preface self.http2_framedump = http2_framedump self.ws_read_limit = ws_read_limit self.timeout = timeout self.showreq = showreq self.showresp = showresp self.explain = explain self.hexdump = hexdump self.ignorecodes = ignorecodes self.ignoretimeout = ignoretimeout self.showsummary = showsummary self.fp = fp self.ws_framereader = None if self.use_http2: self.protocol = http2.HTTP2StateProtocol( self, dump_frames=self.http2_framedump) else: self.protocol = net_http.http1 self.settings = language.Settings( is_client=True, staticdir=os.getcwd(), unconstrained_file_access=True, request_host=self.address[0], protocol=self.protocol, )
def test_websockets(self): r = next(language.parse_pathod("ws")) with pytest.raises(Exception, match="No websocket key"): r.resolve(language.Settings()) res = r.resolve(language.Settings(websocket_key=b"foo")) assert res.status_code.string() == b"101"
def test_websockets(self): r = next(language.parse_pathod("ws")) tutils.raises("no websocket key", r.resolve, language.Settings()) res = r.resolve(language.Settings(websocket_key=b"foo")) assert res.status_code.string() == b"101"
def __init__( self, address, # SSL ssl=None, sni=None, ssl_version=tcp.SSL_DEFAULT_METHOD, ssl_options=tcp.SSL_DEFAULT_OPTIONS, clientcert=None, ciphers=None, # HTTP/2 use_http2=False, http2_skip_connection_preface=False, http2_framedump=False, # Websockets ws_read_limit=None, # Network timeout=None, # Output control showreq=False, showresp=False, explain=False, hexdump=False, ignorecodes=(), ignoretimeout=False, showsummary=False, fp=sys.stdout): """ spec: A request specification showreq: Print requests showresp: Print responses explain: Print request explanation showssl: Print info on SSL connection hexdump: When printing requests or responses, use hex dump output showsummary: Show a summary of requests ignorecodes: Sequence of return codes to ignore """ tcp.TCPClient.__init__(self, address) self.ssl, self.sni = ssl, sni self.clientcert = clientcert self.ssl_version = ssl_version self.ssl_options = ssl_options self.ciphers = ciphers self.sslinfo = None self.use_http2 = use_http2 self.http2_skip_connection_preface = http2_skip_connection_preface self.http2_framedump = http2_framedump self.ws_read_limit = ws_read_limit self.timeout = timeout self.showreq = showreq self.showresp = showresp self.explain = explain self.hexdump = hexdump self.ignorecodes = ignorecodes self.ignoretimeout = ignoretimeout self.showsummary = showsummary self.fp = fp self.ws_framereader = None if self.use_http2: if not tcp.HAS_ALPN: # pragma: no cover log.write_raw( self.fp, "HTTP/2 requires ALPN support. " "Please use OpenSSL >= 1.0.2. " "Pathoc might not be working as expected without ALPN.", timestamp=False) self.protocol = http2.HTTP2Protocol( self, dump_frames=self.http2_framedump) else: self.protocol = http1 self.settings = language.Settings( is_client=True, staticdir=os.getcwd(), unconstrained_file_access=True, request_host=self.address.host, protocol=self.protocol, )
def test_path_generator(self): r = parse_request("GET:@100").freeze(language.Settings()) assert len(r.spec()) > 100
def test_freeze(self): r = parse_request("GET:/:b@100").freeze(language.Settings()) assert len(r.spec()) > 100
def default_settings(): return language.Settings( request_host="foo.com", protocol=HTTP2StateProtocol(tcp.TCPClient(('localhost', 1234))) )
def testlen(x): s = BytesIO() x = next(x) language.serve(x, s, language.Settings()) assert x.length(language.Settings()) == len(s.getvalue())
def testlen(x): s = cStringIO.StringIO() x = x.next() language.serve(x, s, language.Settings()) assert x.length(language.Settings()) == len(s.getvalue())