Ejemplo n.º 1
0
def forge_request(uri='https://www.globaleaks.org/',
                  headers={},
                  method=b'GET',
                  client_addr=None):
    """
    Creates a twisted.web.Request compliant request that is from an external
    IP address.
    """
    _, host, path, query, frag = urlparse.urlsplit(uri)

    x = host.split(':')
    if len(x) > 1:
        port = int(x[1])
    else:
        port = 80

    ret = DummyRequest([''])
    ret.method = method
    ret.uri = uri
    ret.path = path
    ret._serverName = bytes(host)

    if client_addr is None:
        ret.client = IPv4Address('TCP', '1.2.3.4', 12345)
    else:
        ret.client = client_addr

    def getHost():
        return IPv4Address('TCP', '127.0.0.1', port)

    ret.getHost = getHost

    def notifyFinish():
        return Deferred()

    ret.notifyFinish = notifyFinish

    for k, v in headers.iteritems():
        ret.requestHeaders.setRawHeaders(bytes(k), [bytes(v)])

    ret.headers = ret.getAllHeaders()

    return ret
Ejemplo n.º 2
0
def forge_request(uri=b'https://www.globaleaks.org/',
                  headers=None, body='', client_addr=None, method=b'GET'):
    """
    Creates a twisted.web.Request compliant request that is from an external
    IP address.
    """
    if headers is None:
        headers = {}

    _, host, path, query, frag = urlsplit(uri)

    x = host.split(b':')
    if len(x) > 1:
        port = int(x[1])
    else:
        port = 80

    request = DummyRequest([b''])
    request.tid = 1
    request.method = method
    request.uri = uri
    request.path = path
    request._serverName = host

    request.code = 200
    request.client_ip = b'127.0.0.1'
    request.client_proto = b'https'
    request.client_using_tor = False

    def getResponseBody():
        # Ugh, hack. Twisted returns this all as bytes, and we want it as str
        if isinstance(request.written[0], binary_type):
            return b''.join(request.written)
        else:
            return ''.join(request.written)

    request.getResponseBody = getResponseBody

    if client_addr is None:
        request.client = IPv4Address('TCP', b'1.2.3.4', 12345)
    else:
        request.client = client_addr

    def getHost():
        return IPv4Address('TCP', b'127.0.0.1', port)

    request.getHost = getHost

    def notifyFinish():
        return Deferred()

    request.notifyFinish = notifyFinish

    request.requestHeaders.setRawHeaders('host', [b'127.0.0.1'])
    request.requestHeaders.setRawHeaders('user-agent', [b'NSA Agent'])

    for k, v in headers.items():
        request.requestHeaders.setRawHeaders(k, [v])

    request.headers = request.getAllHeaders()

    class fakeBody(object):
        def read(self):
            if isinstance(body, dict):
                ret = json.dumps(body)
            else:
                ret = body

            if isinstance(ret, text_type):
                ret = ret.encode('utf-8')

            return ret

        def close(self):
            pass

    request.content = fakeBody()

    return request
Ejemplo n.º 3
0
def forge_request(uri=b'https://www.globaleaks.org/',
                  headers=None, body='', client_addr=None, method=b'GET'):
    """
    Creates a twisted.web.Request compliant request that is from an external
    IP address.
    """
    if headers is None:
        headers = {}

    _, host, path, query, frag = urlsplit(uri)

    x = host.split(b':')
    if len(x) > 1:
        port = int(x[1])
    else:
        port = 80

    request = DummyRequest([b''])
    request.tid = 1
    request.method = method
    request.uri = uri
    request.path = path
    request._serverName = host

    request.code = 200
    request.client_ip = b'127.0.0.1'
    request.client_proto = b'https'
    request.client_using_tor = False

    def getResponseBody():
        # Ugh, hack. Twisted returns this all as bytes, and we want it as str
        if isinstance(request.written[0], binary_type):
            return b''.join(request.written)
        else:
            return ''.join(request.written)

    request.getResponseBody = getResponseBody

    if client_addr is None:
        request.client = IPv4Address('TCP', b'1.2.3.4', 12345)
    else:
        request.client = client_addr

    def getHost():
        return IPv4Address('TCP', b'127.0.0.1', port)

    request.getHost = getHost

    def notifyFinish():
        return Deferred()

    request.notifyFinish = notifyFinish

    request.requestHeaders.setRawHeaders('host', [b'127.0.0.1'])
    request.requestHeaders.setRawHeaders('user-agent', [b'NSA Agent'])

    for k, v in headers.items():
        request.requestHeaders.setRawHeaders(k, [v])

    request.headers = request.getAllHeaders()

    class fakeBody(object):
        def read(self):
            if isinstance(body, dict):
                ret = json.dumps(body)
            else:
                ret = body

            if isinstance(ret, text_type):
                ret = ret.encode('utf-8')

            return ret

        def close(self):
            pass

    request.content = fakeBody()

    return request
Ejemplo n.º 4
0
def forge_request(uri='https://www.globaleaks.org/',
                  headers=None, body='', client_addr=None, method='GET',
                  handler_cls=None, attached_file={}):
    """
    Creates a twisted.web.Request compliant request that is from an external
    IP address.
    """
    if headers is None:
        headers = {}

    _, host, path, query, frag = urlparse.urlsplit(uri)

    x = host.split (':')
    if len(x) > 1:
        port = int(x[1])
    else:
        port = 80

    request = DummyRequest([''])
    request.method = method
    request.uri = uri
    request.path = path
    request._serverName = bytes(host)

    request.code = 200
    request.client_ip = '127.0.0.1'
    request.client_proto = 'https'
    request.client_using_tor = False

    def getResponseBody():
        return ''.join(request.written)

    request.getResponseBody = getResponseBody

    if client_addr is None:
        request.client = IPv4Address('TCP', '1.2.3.4', 12345)
    else:
        request.client = client_addr

    def getHost():
        return IPv4Address('TCP', '127.0.0.1', port)

    request.getHost = getHost

    def notifyFinish():
        return Deferred()

    request.notifyFinish = notifyFinish

    for k, v in headers.items():
        request.requestHeaders.setRawHeaders(bytes(k), [bytes(v)])

    request.headers = request.getAllHeaders()

    request.args = {}
    if attached_file is not None:
        request.args = {'file': [attached_file]}

    class fakeBody(object):
        def read(self):
            if isinstance(body, dict):
                return json.dumps(body)
            else:
                return body

        def close(self):
            pass

    request.content = fakeBody()

    return request