def test_formatting(self): """ ``_LogFormatter.json_access_log`` returns a JSON-encoded object with the usual http access log information as properties. """ when = 123456789 json_access_log = _LogFormatter( lambda: datetime.utcfromtimestamp(when), ).json_access_log ip = "192.0.2.1" channel = HTTPChannel() transport = StringTransport(peerAddress=IPv4Address("TCP", ip, 12345)) channel.makeConnection(transport) request = Request(channel) request.gotLength(None) request.requestReceived("GET", "/", "HTTP/1.1") event = json_access_log(datetimeToLogString(when), request) self.assertThat( loads(event), MatchesDict( dict( timestamp=Equals("1973-11-29T21:33:09"), ip=Equals(ip), method=Equals("GET"), uri=Equals("/"), protocol=Equals("HTTP/1.1"), code=Equals(200), length=Equals(None), referrer=Equals(None), agent=Equals(None), )), )
def __init__(self, counter, method, path, headers, content): channel = HTTPChannel() host = IPv4Address(b"TCP", b"127.0.0.1", 80) channel.makeConnection(StringTransport(hostAddress=host)) Request.__init__(self, channel, False) # An extra attribute for identifying this fake request self._counter = counter # Attributes a Request is supposed to have but we have to set ourselves # because the base class mixes together too much other logic with the # code that sets them. self.prepath = [] self.requestHeaders = headers self.content = BytesIO(content) self.requestReceived(method, path, b"HTTP/1.1") # requestReceived initializes the path attribute for us (but not # postpath). self.postpath = list(map(unquote, self.path[1:].split(b'/'))) # Our own notifyFinish / finish state because the inherited # implementation wants to write confusing stuff to the transport when # the request gets finished. self._finished = False self._finishedChannel = EventChannel() # Our own state for the response body so we don't have to dig it out of # the transport. self._responseBody = b""
def dataReceived(self, data): if self.protocol: # Pass the data off to the real protocol return self.protocol.dataReceived(data) # Try to determine the protocol requested if self.data: data = self.data + data self.data = data self.log.debug("Received %r", data) if len(data) >= 4: if "\0" in data: # Binary data; use DCSP self.switchProtocol(DaideServerProtocol()) self.transport.setTcpKeepAlive(True) elif data.startswith("DPP/"): self.switchProtocol(DppProtocol()) self.transport.setTcpKeepAlive(True) else: # Probably text; switch to HTTP proto = HTTPChannel() # Simulate Site.buildProtocol() site = self.factory.site proto.site = site proto.requestFactory = site.requestFactory proto.timeOut = site.timeOut self.switchProtocol(proto)
class YaybuChannel(channel.SSHChannel): name = 'session' def __init__(self, task): channel.SSHChannel.__init__(self) self.protocol = HTTPChannel() self.protocol.requestFactory = self.request_factory self.protocol.transport = self self.disconnecting = False self.task = task def request_factory(self): return YaybuRequest(self.task) def openFailed(self, reason): print 'echo failed', reason def channelOpen(self, ignoredData): self.data = '' d = self.conn.sendRequest(self, 'exec', common.NS('yaybu --remote -'), wantReply = 1) #d.addCallback(self._cbRequest) def _cbRequest(self, ignored): #self.write('hello conch\n') #self.conn.sendEOF(self) pass def dataReceived(self, data): self.protocol.dataReceived(data) def closed(self): self.loseConnection() reactor.stop()
def frombytes(cls, byte_string): """ Parse an AWS request from a byte string. @param byte_string: The request as parsed from a C{.req} file. @type byte_string: L{bytes} @return: A request object. @rtype: L{_AWSRequest} """ # Ensure there's a blank line so that status and header # parsing completes. blank_line = b'\n\n' if blank_line not in byte_string: byte_string += blank_line channel = HTTPChannel() channel.delimiter = b'\n' channel.makeConnection(StringTransport()) channel.dataReceived(byte_string) channel.connectionLost(ConnectionDone()) request = channel.requests[-1] method = request.method path = request.uri headers = dict(request.requestHeaders.getAllRawHeaders()) # what comes after the empty line is a body = byte_string.split(blank_line, 1)[-1] return cls(method, path, headers, body)
def __init__(self, task): channel.SSHChannel.__init__(self) self.protocol = HTTPChannel() self.protocol.requestFactory = self.request_factory self.protocol.transport = self self.disconnecting = False self.task = task
def test_client_sends_body(self): self.cl.post_json("testserv:8008", "foo/bar", timeout=10000, data={"a": "b"}) self.pump() clients = self.reactor.tcpClients self.assertEqual(len(clients), 1) client = clients[0][2].buildProtocol(None) server = HTTPChannel() client.makeConnection(FakeTransport(server, self.reactor)) server.makeConnection(FakeTransport(client, self.reactor)) self.pump(0.1) self.assertEqual(len(server.requests), 1) request = server.requests[0] content = request.content.read() self.assertEqual(content, b'{"a":"b"}')
def dataReceived(self, data): if data.startswith("<policy-file-request/>"): policy = ( '<?xml version="1.0"?><!DOCTYPE cross-domain-policy SYSTEM ' '"http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">' '<cross-domain-policy><allow-access-from domain="*" ' 'to-ports="*" /></cross-domain-policy>') self.transport.write(policy) self.transport.loseConnection() else: return HTTPChannel.dataReceived(self, data)
def allHeadersReceived(self): cert = self.transport.getPeerCertificate() der = OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_ASN1, cert.to_cryptography()) req = self.requests[-1] req.requestHeaders.removeHeader('X-User') req.requestHeaders.removeHeader('X-Cert') req.requestHeaders.addRawHeader( 'X-User', base64.b64encode(cert.get_subject().commonName)) req.requestHeaders.addRawHeader('X-Cert', base64.b64encode(der)) logging.info(repr(cert.get_subject().commonName)) logging.info( OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, cert.to_cryptography())) HTTPChannel.allHeadersReceived(self)
def connectionLost(self, reason): logging.debug("Connection lost from client: " + str(reason)) if (self.proxyConnection is not None): self.proxyConnection.transport.loseConnection() HTTPChannel.connectionLost(self, reason)
def connectionLost(self, reason): HTTPChannel.connectionLost(self, reason) self._connection_lost.callback(None)
def connectionMade(self, *args, **kwargs): HTTPChannel.connectionMade(self, *args, **kwargs) if self.site._setNoDelayOnConnect and \ hasattr(self.transport, 'setTcpNoDelay'): self.transport.setTcpNoDelay(True)
def __init__(self): self.proxyConnection = None HTTPChannel.__init__(self)
def connectionLost(self, reason): self.site.lostClient() HTTPChannel.connectionLost(self, reason)
def connectionLost(self, reason): HTTPChannel.connectionLost(self, reason) self.site._lost_client()
def __init__(self): HTTPChannel.__init__(self) self.content_type = None self.count_line_data = False self.co = self.dataCoroutine() self.co.next() # Start up the coroutine
def connectionLost(self, reason): print "connectionLost in MyHTTPChannel. channel is ", self HTTPChannel.connectionLost(self,reason)
def __init__(self, log): self.log, self.proxyConnection = log, None HTTPChannel.__init__(self)
def connectionMade(self): self.site.gotClient() HTTPChannel.connectionMade(self)
def allHeadersReceived(self): HTTPChannel.allHeadersReceived(self) req = self.requests[-1] if hasattr(req, "requestHeadersReceived"): req.requestHeadersReceived(self._command, self._path, self._version)
def connectionLost(self, reason): self.log.debug('Connection lost from client: %s', reason) if (self.proxyConnection is not None): self.proxyConnection.transport.loseConnection() HTTPChannel.connectionLost(self, reason)
def __init__(self): self.ssl_context = tssl.DefaultOpenSSLContextFactory('data/cert/key.pem', 'data/cert/cert.pem') HTTPChannel.__init__(self)
def __init__(self): HTTPChannel.__init__(self)
def connectionLost(self, reason): printStatus("Connection lost to: "+self.transport.getPeer().host) HTTPChannel.connectionLost(self, reason)
def connectionMade(self): print "connectionMade in MyHTTPChannel" HTTPChannel.connectionMade(self)
def dataReceived(self, data): if self.client: self.client.write(data) else: HTTPChannel.dataReceived(self, data)
def headerReceived(self, line): header = line.split(':')[0].lower() if header == "sec-websocket-key1" and not self._transferDecoder: HTTPChannel.headerReceived(self, "Content-Length: 8") HTTPChannel.headerReceived(self, line)
def __init__(self): self.proxyConnection = None self.requestFactory.getVerifier = lambda _: self.factory.verifier HTTPChannel.__init__(self)
def __init__(self): HTTPChannel.__init__(self) self.client = None
def connectionMade(self): HTTPChannel.connectionMade(self) self.site._got_client()
def rawDataReceived(self, data): if self.proxyConnection is None: HTTPChannel.rawDataReceived(self, data) else: self.proxyConnection.transport.write(data)
def connectionMade(self): HTTPChannel.connectionMade(self) self._connection_lost = defer.Deferred()