2015-06-08 03:23:56 +00:00
|
|
|
import contextlib
|
2014-10-24 01:01:34 +00:00
|
|
|
import sys
|
|
|
|
import os
|
2015-06-07 01:18:33 +00:00
|
|
|
import itertools
|
2014-10-25 06:50:48 +00:00
|
|
|
import hashlib
|
2015-04-29 20:03:26 +00:00
|
|
|
import Queue
|
2014-10-25 03:43:01 +00:00
|
|
|
import random
|
2015-04-29 20:03:26 +00:00
|
|
|
import select
|
2015-03-14 00:54:47 +00:00
|
|
|
import time
|
2015-04-28 22:02:16 +00:00
|
|
|
import threading
|
2015-03-14 00:54:47 +00:00
|
|
|
|
|
|
|
import OpenSSL.crypto
|
|
|
|
|
2015-06-01 16:45:17 +00:00
|
|
|
from netlib import tcp, http, http2, certutils, websockets
|
2014-10-25 02:30:54 +00:00
|
|
|
|
2015-05-02 04:17:00 +00:00
|
|
|
import language.http
|
|
|
|
import language.websockets
|
2015-05-30 05:43:01 +00:00
|
|
|
from . import utils, log
|
2012-06-24 09:10:10 +00:00
|
|
|
|
2015-06-08 08:45:17 +00:00
|
|
|
import logging
|
|
|
|
logging.getLogger("hpack").setLevel(logging.WARNING)
|
|
|
|
|
2014-10-25 02:30:54 +00:00
|
|
|
|
2015-04-30 02:42:54 +00:00
|
|
|
class PathocError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-06-18 09:07:33 +00:00
|
|
|
class SSLInfo(object):
|
2015-06-18 16:12:11 +00:00
|
|
|
|
2015-06-01 16:45:17 +00:00
|
|
|
def __init__(self, certchain, cipher, alp):
|
|
|
|
self.certchain, self.cipher, self.alp = certchain, cipher, alp
|
2014-03-02 00:45:35 +00:00
|
|
|
|
2015-04-19 06:04:27 +00:00
|
|
|
def __str__(self):
|
|
|
|
parts = [
|
2015-06-16 11:52:41 +00:00
|
|
|
"Application Layer Protocol: %s" % self.alp,
|
2015-05-30 00:03:13 +00:00
|
|
|
"Cipher: %s, %s bit, %s" % self.cipher,
|
2015-04-19 06:04:27 +00:00
|
|
|
"SSL certificate chain:"
|
|
|
|
]
|
|
|
|
for i in self.certchain:
|
|
|
|
parts.append("\tSubject: ")
|
|
|
|
for cn in i.get_subject().get_components():
|
2015-05-30 00:03:13 +00:00
|
|
|
parts.append("\t\t%s=%s" % cn)
|
2015-04-19 06:04:27 +00:00
|
|
|
parts.append("\tIssuer: ")
|
|
|
|
for cn in i.get_issuer().get_components():
|
2015-05-30 00:03:13 +00:00
|
|
|
parts.append("\t\t%s=%s" % cn)
|
2015-04-19 06:04:27 +00:00
|
|
|
parts.extend(
|
|
|
|
[
|
2015-05-30 00:03:13 +00:00
|
|
|
"\tVersion: %s" % i.get_version(),
|
|
|
|
"\tValidity: %s - %s" % (
|
2015-04-19 06:04:27 +00:00
|
|
|
i.get_notBefore(), i.get_notAfter()
|
|
|
|
),
|
2015-05-30 00:03:13 +00:00
|
|
|
"\tSerial: %s" % i.get_serial_number(),
|
|
|
|
"\tAlgorithm: %s" % i.get_signature_algorithm()
|
2015-04-19 06:04:27 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
pk = i.get_pubkey()
|
|
|
|
types = {
|
|
|
|
OpenSSL.crypto.TYPE_RSA: "RSA",
|
|
|
|
OpenSSL.crypto.TYPE_DSA: "DSA"
|
|
|
|
}
|
|
|
|
t = types.get(pk.type(), "Uknown")
|
2015-05-30 00:03:13 +00:00
|
|
|
parts.append("\tPubkey: %s bit %s" % (pk.bits(), t))
|
2015-04-19 06:04:27 +00:00
|
|
|
s = certutils.SSLCert(i)
|
|
|
|
if s.altnames:
|
2015-05-30 00:03:13 +00:00
|
|
|
parts.append("\tSANs: %s" % " ".join(s.altnames))
|
2015-04-19 06:04:27 +00:00
|
|
|
return "\n".join(parts)
|
|
|
|
|
2014-03-02 00:45:35 +00:00
|
|
|
|
2015-06-18 09:07:33 +00:00
|
|
|
class Response(object):
|
2015-06-18 16:12:11 +00:00
|
|
|
|
2014-10-25 03:20:23 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
httpversion,
|
|
|
|
status_code,
|
|
|
|
msg,
|
|
|
|
headers,
|
|
|
|
content,
|
|
|
|
sslinfo
|
|
|
|
):
|
|
|
|
self.httpversion, self.status_code = httpversion, status_code
|
|
|
|
self.msg = msg
|
2013-02-26 20:07:16 +00:00
|
|
|
self.headers, self.content = headers, content
|
2014-03-02 00:45:35 +00:00
|
|
|
self.sslinfo = sslinfo
|
2013-02-26 20:07:16 +00:00
|
|
|
|
2013-03-02 03:57:00 +00:00
|
|
|
def __repr__(self):
|
2015-05-30 00:03:13 +00:00
|
|
|
return "Response(%s - %s)" % (self.status_code, self.msg)
|
2013-03-02 03:57:00 +00:00
|
|
|
|
2014-03-02 00:45:35 +00:00
|
|
|
|
2015-04-28 22:02:16 +00:00
|
|
|
class WebsocketFrameReader(threading.Thread):
|
2015-06-18 16:12:11 +00:00
|
|
|
|
2015-05-30 00:03:13 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
rfile,
|
|
|
|
logfp,
|
|
|
|
showresp,
|
|
|
|
hexdump,
|
2015-06-08 03:23:56 +00:00
|
|
|
ws_read_limit,
|
|
|
|
timeout
|
|
|
|
):
|
2015-04-28 22:02:16 +00:00
|
|
|
threading.Thread.__init__(self)
|
2015-06-08 03:23:56 +00:00
|
|
|
self.timeout = timeout
|
2015-04-29 20:03:26 +00:00
|
|
|
self.ws_read_limit = ws_read_limit
|
2015-04-30 02:42:54 +00:00
|
|
|
self.logfp = logfp
|
2015-04-30 01:59:10 +00:00
|
|
|
self.showresp = showresp
|
2015-04-30 02:42:54 +00:00
|
|
|
self.hexdump = hexdump
|
2015-06-05 04:03:17 +00:00
|
|
|
self.rfile = rfile
|
2015-04-29 20:03:26 +00:00
|
|
|
self.terminate = Queue.Queue()
|
2015-06-05 04:03:17 +00:00
|
|
|
self.frames_queue = Queue.Queue()
|
2015-04-28 22:02:16 +00:00
|
|
|
|
2015-04-30 02:42:54 +00:00
|
|
|
def log(self, rfile):
|
2015-05-30 05:43:01 +00:00
|
|
|
return log.Log(
|
2015-04-30 02:42:54 +00:00
|
|
|
self.logfp,
|
|
|
|
self.hexdump,
|
|
|
|
rfile if self.showresp else None,
|
|
|
|
None
|
|
|
|
)
|
|
|
|
|
2015-06-08 03:23:56 +00:00
|
|
|
@contextlib.contextmanager
|
|
|
|
def terminator(self):
|
|
|
|
yield
|
|
|
|
self.frames_queue.put(None)
|
|
|
|
|
2015-04-28 22:02:16 +00:00
|
|
|
def run(self):
|
2015-06-08 03:23:56 +00:00
|
|
|
starttime = time.time()
|
|
|
|
with self.terminator():
|
|
|
|
while True:
|
|
|
|
if self.ws_read_limit == 0:
|
|
|
|
return
|
2015-06-18 16:05:09 +00:00
|
|
|
r, _, _ = select.select([self.rfile], [], [], 0.05)
|
2015-06-08 03:57:29 +00:00
|
|
|
delta = time.time() - starttime
|
|
|
|
if not r and self.timeout and delta > self.timeout:
|
2015-06-08 03:23:56 +00:00
|
|
|
return
|
|
|
|
try:
|
|
|
|
self.terminate.get_nowait()
|
|
|
|
return
|
|
|
|
except Queue.Empty:
|
|
|
|
pass
|
|
|
|
for rfile in r:
|
|
|
|
with self.log(rfile) as log:
|
2015-05-04 22:59:37 +00:00
|
|
|
frm = websockets.Frame.from_file(self.rfile)
|
2015-06-08 03:23:56 +00:00
|
|
|
self.frames_queue.put(frm)
|
|
|
|
log("<< %s" % frm.header.human_readable())
|
|
|
|
if self.ws_read_limit is not None:
|
|
|
|
self.ws_read_limit -= 1
|
|
|
|
starttime = time.time()
|
2015-04-28 22:02:16 +00:00
|
|
|
|
|
|
|
|
2012-06-24 09:10:10 +00:00
|
|
|
class Pathoc(tcp.TCPClient):
|
2015-06-18 16:12:11 +00:00
|
|
|
|
2014-10-25 02:30:54 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
address,
|
2015-04-19 06:04:27 +00:00
|
|
|
|
|
|
|
# SSL
|
2014-10-25 02:30:54 +00:00
|
|
|
ssl=None,
|
|
|
|
sni=None,
|
2015-06-11 14:13:22 +00:00
|
|
|
sslversion='SSLv23',
|
2014-10-25 02:30:54 +00:00
|
|
|
clientcert=None,
|
2015-04-19 06:04:27 +00:00
|
|
|
ciphers=None,
|
|
|
|
|
2015-06-01 16:14:21 +00:00
|
|
|
# HTTP/2
|
|
|
|
use_http2=False,
|
|
|
|
http2_skip_connection_preface=False,
|
2015-06-18 16:12:11 +00:00
|
|
|
http2_framedump=False,
|
2015-06-01 16:14:21 +00:00
|
|
|
|
2015-04-29 20:03:26 +00:00
|
|
|
# Websockets
|
2015-06-18 16:12:11 +00:00
|
|
|
ws_read_limit=None,
|
2015-04-29 20:03:26 +00:00
|
|
|
|
2015-06-08 03:23:56 +00:00
|
|
|
# Network
|
2015-06-18 16:12:11 +00:00
|
|
|
timeout=None,
|
2015-06-08 03:23:56 +00:00
|
|
|
|
2015-04-19 06:04:27 +00:00
|
|
|
# Output control
|
2015-06-18 16:12:11 +00:00
|
|
|
showreq=False,
|
|
|
|
showresp=False,
|
|
|
|
explain=False,
|
|
|
|
hexdump=False,
|
|
|
|
ignorecodes=(),
|
|
|
|
ignoretimeout=False,
|
|
|
|
showsummary=False,
|
|
|
|
fp=sys.stdout
|
2015-04-20 03:42:33 +00:00
|
|
|
):
|
2015-04-19 06:04:27 +00:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
"""
|
2014-01-28 18:28:20 +00:00
|
|
|
tcp.TCPClient.__init__(self, address)
|
2015-06-08 08:45:17 +00:00
|
|
|
|
2013-01-03 21:37:26 +00:00
|
|
|
self.ssl, self.sni = ssl, sni
|
2013-01-20 09:37:43 +00:00
|
|
|
self.clientcert = clientcert
|
2014-02-27 05:33:48 +00:00
|
|
|
self.sslversion = utils.SSLVERSIONS[sslversion]
|
|
|
|
self.ciphers = ciphers
|
2015-04-19 20:56:47 +00:00
|
|
|
self.sslinfo = None
|
2013-01-03 21:37:26 +00:00
|
|
|
|
2015-06-01 16:14:21 +00:00
|
|
|
self.use_http2 = use_http2
|
|
|
|
self.http2_skip_connection_preface = http2_skip_connection_preface
|
2015-06-11 14:13:22 +00:00
|
|
|
self.http2_framedump = http2_framedump
|
2015-06-01 16:14:21 +00:00
|
|
|
|
2015-04-29 20:03:26 +00:00
|
|
|
self.ws_read_limit = ws_read_limit
|
|
|
|
|
2015-06-08 03:23:56 +00:00
|
|
|
self.timeout = timeout
|
|
|
|
|
2015-04-19 06:04:27 +00:00
|
|
|
self.showreq = showreq
|
|
|
|
self.showresp = showresp
|
|
|
|
self.explain = explain
|
|
|
|
self.hexdump = hexdump
|
|
|
|
self.ignorecodes = ignorecodes
|
|
|
|
self.ignoretimeout = ignoretimeout
|
|
|
|
self.showsummary = showsummary
|
|
|
|
self.fp = fp
|
|
|
|
|
2015-04-29 20:03:26 +00:00
|
|
|
self.ws_framereader = None
|
|
|
|
|
2015-06-08 08:45:17 +00:00
|
|
|
if self.use_http2:
|
2015-06-08 13:28:19 +00:00
|
|
|
if not OpenSSL._util.lib.Cryptography_HAS_ALPN: # pragma: nocover
|
|
|
|
print >> sys.stderr, "HTTP/2 requires ALPN support. Please use OpenSSL >= 1.0.2."
|
|
|
|
print >> sys.stderr, "Pathoc might not be working as expected without ALPN."
|
2015-06-08 08:45:17 +00:00
|
|
|
self.protocol = http2.HTTP2Protocol(self)
|
|
|
|
else:
|
|
|
|
# TODO: create HTTP or Websockets protocol
|
|
|
|
self.protocol = None
|
|
|
|
|
|
|
|
self.settings = language.Settings(
|
2015-06-18 16:12:11 +00:00
|
|
|
is_client=True,
|
|
|
|
staticdir=os.getcwd(),
|
|
|
|
unconstrained_file_access=True,
|
|
|
|
request_host=self.address.host,
|
|
|
|
protocol=self.protocol,
|
2015-06-08 08:45:17 +00:00
|
|
|
)
|
|
|
|
|
2015-04-30 01:59:10 +00:00
|
|
|
def log(self):
|
2015-05-30 05:43:01 +00:00
|
|
|
return log.Log(
|
2015-04-30 02:42:54 +00:00
|
|
|
self.fp,
|
|
|
|
self.hexdump,
|
|
|
|
self.rfile if self.showresp else None,
|
|
|
|
self.wfile if self.showreq else None,
|
|
|
|
)
|
2015-04-30 01:59:10 +00:00
|
|
|
|
2013-12-15 05:42:58 +00:00
|
|
|
def http_connect(self, connect_to):
|
|
|
|
self.wfile.write(
|
2015-05-30 00:03:13 +00:00
|
|
|
'CONNECT %s:%s HTTP/1.1\r\n' % tuple(connect_to) +
|
2014-10-25 02:30:54 +00:00
|
|
|
'\r\n'
|
|
|
|
)
|
2013-12-15 05:42:58 +00:00
|
|
|
self.wfile.flush()
|
|
|
|
l = self.rfile.readline()
|
2013-03-02 03:57:00 +00:00
|
|
|
if not l:
|
|
|
|
raise PathocError("Proxy CONNECT failed")
|
|
|
|
parsed = http.parse_response_line(l)
|
|
|
|
if not parsed[1] == 200:
|
2015-04-20 03:42:33 +00:00
|
|
|
raise PathocError(
|
2015-05-30 00:03:13 +00:00
|
|
|
"Proxy CONNECT failed: %s - %s" % (parsed[1], parsed[2])
|
2015-04-20 03:42:33 +00:00
|
|
|
)
|
2014-01-19 05:20:01 +00:00
|
|
|
http.read_headers(self.rfile)
|
2013-01-05 07:29:46 +00:00
|
|
|
|
2015-04-19 03:55:22 +00:00
|
|
|
def connect(self, connect_to=None, showssl=False, fp=sys.stdout):
|
2013-01-05 07:29:46 +00:00
|
|
|
"""
|
2015-04-20 03:42:33 +00:00
|
|
|
connect_to: A (host, port) tuple, which will be connected to with
|
|
|
|
an HTTP CONNECT request.
|
2013-01-05 07:29:46 +00:00
|
|
|
"""
|
2015-06-01 16:45:17 +00:00
|
|
|
if self.use_http2 and not self.ssl:
|
2015-06-08 13:28:19 +00:00
|
|
|
raise NotImplementedError("HTTP2 without SSL is not supported.")
|
2015-06-01 16:45:17 +00:00
|
|
|
|
2013-01-03 21:37:26 +00:00
|
|
|
tcp.TCPClient.connect(self)
|
2015-06-01 16:45:17 +00:00
|
|
|
|
2013-01-05 07:29:46 +00:00
|
|
|
if connect_to:
|
2013-12-15 05:42:58 +00:00
|
|
|
self.http_connect(connect_to)
|
2015-06-01 16:45:17 +00:00
|
|
|
|
2014-03-02 00:45:35 +00:00
|
|
|
self.sslinfo = None
|
2013-01-03 21:37:26 +00:00
|
|
|
if self.ssl:
|
|
|
|
try:
|
2015-06-08 08:45:17 +00:00
|
|
|
alpn_protos = [b'http1.1'] # TODO: move to a new HTTP1 protocol
|
2015-06-01 16:45:17 +00:00
|
|
|
if self.use_http2:
|
2015-06-08 08:45:17 +00:00
|
|
|
alpn_protos.append(http2.HTTP2Protocol.ALPN_PROTO_H2)
|
2015-06-01 16:45:17 +00:00
|
|
|
|
2014-02-27 05:33:48 +00:00
|
|
|
self.convert_to_ssl(
|
2014-10-25 02:30:54 +00:00
|
|
|
sni=self.sni,
|
|
|
|
cert=self.clientcert,
|
|
|
|
method=self.sslversion,
|
2015-06-01 16:45:17 +00:00
|
|
|
cipher_list=self.ciphers,
|
|
|
|
alpn_protos=alpn_protos
|
2014-10-25 02:30:54 +00:00
|
|
|
)
|
2015-05-30 00:03:13 +00:00
|
|
|
except tcp.NetLibError as v:
|
2013-01-03 21:37:26 +00:00
|
|
|
raise PathocError(str(v))
|
2015-06-01 16:45:17 +00:00
|
|
|
|
2014-03-02 00:45:35 +00:00
|
|
|
self.sslinfo = SSLInfo(
|
2014-10-25 02:30:54 +00:00
|
|
|
self.connection.get_peer_cert_chain(),
|
2015-06-01 16:45:17 +00:00
|
|
|
self.get_current_cipher(),
|
|
|
|
self.get_alpn_proto_negotiated()
|
2014-10-25 02:30:54 +00:00
|
|
|
)
|
2015-04-19 03:55:22 +00:00
|
|
|
if showssl:
|
2015-04-19 06:04:27 +00:00
|
|
|
print >> fp, str(self.sslinfo)
|
2015-06-01 16:45:17 +00:00
|
|
|
|
|
|
|
if self.use_http2:
|
2015-06-08 08:45:17 +00:00
|
|
|
self.protocol.check_alpn()
|
2015-06-01 16:45:17 +00:00
|
|
|
if not self.http2_skip_connection_preface:
|
2015-06-11 14:13:22 +00:00
|
|
|
self.protocol.perform_client_connection_preface()
|
2015-06-01 16:45:17 +00:00
|
|
|
|
2015-06-08 03:57:29 +00:00
|
|
|
if self.timeout:
|
|
|
|
self.settimeout(self.timeout)
|
2014-03-02 00:45:35 +00:00
|
|
|
|
2015-04-30 01:59:10 +00:00
|
|
|
def _resp_summary(self, resp):
|
2015-05-30 00:03:13 +00:00
|
|
|
return "<< %s %s: %s bytes" % (
|
2015-04-19 06:04:27 +00:00
|
|
|
resp.status_code, utils.xrepr(resp.msg), len(resp.content)
|
2014-10-25 02:30:54 +00:00
|
|
|
)
|
2012-09-25 23:07:22 +00:00
|
|
|
|
2015-04-29 20:03:26 +00:00
|
|
|
def stop(self):
|
2015-04-30 21:41:07 +00:00
|
|
|
if self.ws_framereader:
|
|
|
|
self.ws_framereader.terminate.put(None)
|
2015-04-29 20:03:26 +00:00
|
|
|
|
2015-06-05 04:03:17 +00:00
|
|
|
def wait(self, timeout=0.01, finish=True):
|
|
|
|
"""
|
|
|
|
A generator that yields frames until Pathoc terminates.
|
|
|
|
|
|
|
|
timeout: If specified None may be yielded instead if timeout is
|
|
|
|
reached. If timeout is None, wait forever. If timeout is 0, return
|
|
|
|
immedately if nothing is on the queue.
|
|
|
|
|
|
|
|
finish: If true, consume messages until the reader shuts down.
|
|
|
|
Otherwise, return None on timeout.
|
|
|
|
"""
|
2015-04-29 20:03:26 +00:00
|
|
|
if self.ws_framereader:
|
2015-05-30 00:03:13 +00:00
|
|
|
while True:
|
2015-04-29 20:03:26 +00:00
|
|
|
try:
|
2015-06-05 04:03:17 +00:00
|
|
|
frm = self.ws_framereader.frames_queue.get(
|
2015-06-18 16:12:11 +00:00
|
|
|
timeout=timeout,
|
|
|
|
block=True if timeout != 0 else False
|
2015-06-05 04:03:17 +00:00
|
|
|
)
|
|
|
|
except Queue.Empty:
|
|
|
|
if finish:
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
if frm is None:
|
2015-04-29 20:03:26 +00:00
|
|
|
self.ws_framereader.join()
|
|
|
|
return
|
2015-06-05 04:03:17 +00:00
|
|
|
yield frm
|
2015-04-29 20:03:26 +00:00
|
|
|
|
2015-04-28 22:02:16 +00:00
|
|
|
def websocket_send_frame(self, r):
|
|
|
|
"""
|
|
|
|
Sends a single websocket frame.
|
|
|
|
"""
|
2015-04-30 01:59:10 +00:00
|
|
|
with self.log() as log:
|
2015-05-30 00:03:13 +00:00
|
|
|
log(">> %s" % r)
|
2015-06-08 04:25:33 +00:00
|
|
|
language.serve(r, self.wfile, self.settings)
|
|
|
|
self.wfile.flush()
|
2015-04-28 22:02:16 +00:00
|
|
|
|
2015-06-08 04:25:33 +00:00
|
|
|
def websocket_start(self, r):
|
2015-04-28 22:02:16 +00:00
|
|
|
"""
|
|
|
|
Performs an HTTP request, and attempts to drop into websocket
|
|
|
|
connection.
|
|
|
|
"""
|
2015-04-30 02:42:54 +00:00
|
|
|
resp = self.http(r)
|
|
|
|
if resp.status_code == 101:
|
|
|
|
self.ws_framereader = WebsocketFrameReader(
|
|
|
|
self.rfile,
|
|
|
|
self.fp,
|
|
|
|
self.showresp,
|
|
|
|
self.hexdump,
|
2015-06-08 03:23:56 +00:00
|
|
|
self.ws_read_limit,
|
|
|
|
self.timeout
|
2015-04-30 02:42:54 +00:00
|
|
|
)
|
|
|
|
self.ws_framereader.start()
|
2015-04-28 22:02:16 +00:00
|
|
|
return resp
|
|
|
|
|
|
|
|
def http(self, r):
|
2012-06-29 22:51:13 +00:00
|
|
|
"""
|
2015-04-19 06:04:27 +00:00
|
|
|
Performs a single request.
|
2012-09-25 22:12:30 +00:00
|
|
|
|
2015-05-02 04:53:48 +00:00
|
|
|
r: A language.http.Request object, or a string representing one
|
|
|
|
request.
|
2012-09-26 22:56:06 +00:00
|
|
|
|
2015-04-28 22:02:16 +00:00
|
|
|
Returns Response if we have a non-ignored response.
|
2015-04-19 20:56:47 +00:00
|
|
|
|
|
|
|
May raise http.HTTPError, tcp.NetLibError
|
2012-06-29 22:51:13 +00:00
|
|
|
"""
|
2015-04-30 01:59:10 +00:00
|
|
|
with self.log() as log:
|
2015-05-30 00:03:13 +00:00
|
|
|
log(">> %s" % r)
|
2015-04-30 01:59:10 +00:00
|
|
|
resp, req = None, None
|
|
|
|
try:
|
|
|
|
req = language.serve(r, self.wfile, self.settings)
|
|
|
|
self.wfile.flush()
|
2015-06-08 08:45:17 +00:00
|
|
|
|
|
|
|
if self.use_http2:
|
|
|
|
status_code, headers, body = self.protocol.read_response()
|
|
|
|
resp = Response("HTTP/2", status_code, "", headers, body, self.sslinfo)
|
|
|
|
else:
|
|
|
|
resp = list(
|
|
|
|
http.read_response(
|
|
|
|
self.rfile,
|
|
|
|
req["method"],
|
|
|
|
None
|
|
|
|
)
|
2015-04-30 01:59:10 +00:00
|
|
|
)
|
2015-06-08 08:45:17 +00:00
|
|
|
resp.append(self.sslinfo)
|
|
|
|
resp = Response(*resp)
|
2015-06-18 16:12:11 +00:00
|
|
|
except http.HttpError as v:
|
2015-06-08 02:01:04 +00:00
|
|
|
log("Invalid server response: %s" % v)
|
|
|
|
raise
|
2015-04-30 01:59:10 +00:00
|
|
|
except tcp.NetLibTimeout:
|
|
|
|
if self.ignoretimeout:
|
|
|
|
log("Timeout (ignored)")
|
|
|
|
return None
|
2015-06-08 02:01:04 +00:00
|
|
|
log("Timeout")
|
2015-04-30 01:59:10 +00:00
|
|
|
raise
|
|
|
|
finally:
|
2015-04-30 02:42:54 +00:00
|
|
|
if resp:
|
|
|
|
log(self._resp_summary(resp))
|
|
|
|
if resp.status_code in self.ignorecodes:
|
|
|
|
log.suppress()
|
2015-04-30 01:59:10 +00:00
|
|
|
return resp
|
2014-03-02 06:04:56 +00:00
|
|
|
|
2015-04-28 22:02:16 +00:00
|
|
|
def request(self, r):
|
|
|
|
"""
|
|
|
|
Performs a single request.
|
|
|
|
|
2015-06-05 00:04:40 +00:00
|
|
|
r: A language.message.Messsage object, or a string representing
|
|
|
|
one.
|
2015-04-28 22:02:16 +00:00
|
|
|
|
|
|
|
Returns Response if we have a non-ignored response.
|
|
|
|
|
|
|
|
May raise http.HTTPError, tcp.NetLibError
|
|
|
|
"""
|
|
|
|
if isinstance(r, basestring):
|
2015-06-08 08:45:17 +00:00
|
|
|
r = language.parse_pathoc(r, self.use_http2).next()
|
|
|
|
|
2015-05-02 04:17:00 +00:00
|
|
|
if isinstance(r, language.http.Request):
|
2015-04-28 22:02:16 +00:00
|
|
|
if r.ws:
|
2015-06-08 04:25:33 +00:00
|
|
|
return self.websocket_start(r)
|
2015-04-28 22:02:16 +00:00
|
|
|
else:
|
|
|
|
return self.http(r)
|
2015-05-02 04:17:00 +00:00
|
|
|
elif isinstance(r, language.websockets.WebsocketFrame):
|
2015-04-28 22:02:16 +00:00
|
|
|
self.websocket_send_frame(r)
|
2015-06-08 08:45:17 +00:00
|
|
|
elif isinstance(r, language.http2.Request):
|
|
|
|
return self.http(r)
|
|
|
|
# elif isinstance(r, language.http2.Frame):
|
|
|
|
# TODO: do something
|
2015-04-28 22:02:16 +00:00
|
|
|
|
2014-03-02 06:04:56 +00:00
|
|
|
|
2015-05-30 00:03:13 +00:00
|
|
|
def main(args): # pragma: nocover
|
2014-10-25 06:50:48 +00:00
|
|
|
memo = set([])
|
2014-10-26 05:16:47 +00:00
|
|
|
trycount = 0
|
2015-04-29 20:03:26 +00:00
|
|
|
p = None
|
2014-10-24 04:12:54 +00:00
|
|
|
try:
|
2014-10-25 04:58:59 +00:00
|
|
|
cnt = 0
|
2015-05-30 00:03:13 +00:00
|
|
|
while True:
|
2015-03-14 00:54:47 +00:00
|
|
|
if cnt == args.repeat and args.repeat != 0:
|
|
|
|
break
|
|
|
|
if args.wait and cnt != 0:
|
|
|
|
time.sleep(args.wait)
|
2014-10-26 05:16:47 +00:00
|
|
|
|
2014-10-25 04:58:59 +00:00
|
|
|
cnt += 1
|
2015-06-07 01:18:33 +00:00
|
|
|
playlist = itertools.chain(*args.requests)
|
2014-10-25 06:50:48 +00:00
|
|
|
if args.random:
|
2015-06-07 01:18:33 +00:00
|
|
|
playlist = random.choice(args.requests)
|
2014-10-24 04:12:54 +00:00
|
|
|
p = Pathoc(
|
|
|
|
(args.host, args.port),
|
2015-06-18 16:12:11 +00:00
|
|
|
ssl=args.ssl,
|
|
|
|
sni=args.sni,
|
|
|
|
sslversion=args.sslversion,
|
|
|
|
clientcert=args.clientcert,
|
|
|
|
ciphers=args.ciphers,
|
|
|
|
use_http2=args.use_http2,
|
|
|
|
http2_skip_connection_preface=args.http2_skip_connection_preface,
|
|
|
|
http2_framedump=args.http2_framedump,
|
|
|
|
showreq=args.showreq,
|
|
|
|
showresp=args.showresp,
|
|
|
|
explain=args.explain,
|
|
|
|
hexdump=args.hexdump,
|
|
|
|
ignorecodes=args.ignorecodes,
|
|
|
|
timeout=args.timeout,
|
|
|
|
ignoretimeout=args.ignoretimeout,
|
|
|
|
showsummary=True
|
2014-10-24 04:12:54 +00:00
|
|
|
)
|
2014-10-26 05:16:47 +00:00
|
|
|
trycount = 0
|
2014-10-24 04:12:54 +00:00
|
|
|
try:
|
2015-04-19 03:55:22 +00:00
|
|
|
p.connect(args.connect_to, args.showssl)
|
2015-05-30 00:03:13 +00:00
|
|
|
except tcp.NetLibError as v:
|
2015-03-14 00:54:47 +00:00
|
|
|
print >> sys.stderr, str(v)
|
|
|
|
continue
|
2015-05-30 00:03:13 +00:00
|
|
|
except PathocError as v:
|
2014-10-24 04:12:54 +00:00
|
|
|
print >> sys.stderr, str(v)
|
|
|
|
sys.exit(1)
|
2014-10-25 03:43:01 +00:00
|
|
|
for spec in playlist:
|
2015-06-07 01:18:33 +00:00
|
|
|
if args.explain or args.memo:
|
|
|
|
spec = spec.freeze(p.settings)
|
|
|
|
if args.memo:
|
|
|
|
h = hashlib.sha256(spec.spec()).digest()
|
|
|
|
if h not in memo:
|
|
|
|
trycount = 0
|
|
|
|
memo.add(h)
|
|
|
|
else:
|
|
|
|
trycount += 1
|
|
|
|
if trycount > args.memolimit:
|
|
|
|
print >> sys.stderr, "Memo limit exceeded..."
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
continue
|
2015-04-19 06:04:27 +00:00
|
|
|
try:
|
|
|
|
ret = p.request(spec)
|
|
|
|
if ret and args.oneshot:
|
|
|
|
return
|
2015-06-05 04:03:17 +00:00
|
|
|
# We consume the queue when we can, so it doesn't build up.
|
2015-06-18 16:05:09 +00:00
|
|
|
for i_ in p.wait(timeout=0, finish=False):
|
2015-06-05 04:03:17 +00:00
|
|
|
pass
|
2015-05-30 00:03:13 +00:00
|
|
|
except (http.HttpError, tcp.NetLibError) as v:
|
2015-04-30 21:41:07 +00:00
|
|
|
break
|
2015-06-18 16:05:09 +00:00
|
|
|
for i_ in p.wait(timeout=0.01, finish=True):
|
2015-06-05 04:03:17 +00:00
|
|
|
pass
|
2014-10-24 04:12:54 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
2015-04-29 20:03:26 +00:00
|
|
|
if p:
|
|
|
|
p.stop()
|