

def httpRequest(url, payload, headers, method='POST', timeout=DEFAULT_TIMEOUT, ctx_factory=None):
    if (type(url) is not str):
        e = HTTPRequestError(('URL must be string, not %s' % type(url)))
        return defer.fail(e)
    if (not url.startswith('http')):
        e = HTTPRequestError(('URL does not start with http (URL %s)' % url))
        return defer.fail(e)
    log.msg((' -- Sending Payload to %s --\n%s\n -- END. Sending Payload --' % (url, payload)), system=LOG_SYSTEM, payload=True)
    (scheme, netloc, _, _, _, _) = twhttp.urlparse(url)
    if (not (':' in netloc)):
        host = netloc
        port = (80 if (scheme == 'http') else 443)
    else:
        (host, s_port) = netloc.split(':', 1)
        port = int(s_port)
    factory = twclient.HTTPClientFactory(url, method, postdata=payload, timeout=timeout)
    factory.noisy = False
    factory.protocol.handleStatus_204 = (lambda _: None)
    factory.headers['host'] = ((host + ':') + str(port))
    factory.headers['User-Agent'] = 'OpenNSA/Twisted'
    for (header, value) in headers.items():
        factory.headers[value] = value
    if (scheme == 'https'):
        if (ctx_factory is None):
            return defer.fail(HTTPRequestError('Cannot perform https request without context factory'))
        reactor.connectSSL(host, port, factory, ctx_factory)
    else:
        reactor.connectTCP(host, port, factory)

    def invocationError(err):
        if isinstance(err.value, ConnectionClosed):
            pass
        elif isinstance(err.value, WebError):
            data = err.value.response
            log.msg((' -- Received Reply (fault) --\n%s\n -- END. Received Reply (fault) --' % data), system=LOG_SYSTEM, payload=True)
            return err
        elif isinstance(err.value, ConnectionRefusedError):
            log.msg(('Connection refused for %s:%i. Request URL: %s' % (host, port, url)), system=LOG_SYSTEM)
            return err
        else:
            return err

    def logReply(data):
        log.msg((' -- Received Reply --\n%s\n -- END. Received Reply --' % data), system=LOG_SYSTEM, payload=True)
        return data
    factory.deferred.addCallbacks(logReply, invocationError)
    return factory.deferred

dataset/ETHPy150Open NORDUnet/opennsa/opennsa/protocols/shared/httpclient.py httpRequest/VarMisuse@283/392 `header`->`value`