예제 #1
0
def get_httpmessage(headers):
    if six.PY3:
        return http.client.parse_headers(BytesIO(headers))
    msg = HTTPMessage(BytesIO(headers))
    msg.fp.seek(0)
    msg.readheaders()
    return msg
예제 #2
0
파일: compat.py 프로젝트: IvanMalison/vcrpy
def get_httpmessage(headers):
    if six.PY3:
        return http.client.parse_headers(BytesIO(headers))
    msg = HTTPMessage(BytesIO(headers))
    msg.fp.seek(0)
    msg.readheaders()
    return msg
def parse_icap_response(data):
    with closing(BytesIO(data)) as fp:
        icap_status = fp.readline()
        icap_msg = HTTPMessage(fp)
        icap_msg.status = icap_status
        enc_header = icap_msg.get('encapsulated')
        enc_offsets = [offset.strip() for offset in enc_header.split(',')]
        enc_msg_1 = parse_enc(fp, enc_offsets[:1])
        enc_msg_2 = parse_enc(fp, enc_offsets[1:])
        chunks = fp.read()
    return Message(icap_msg, enc_msg_1, enc_msg_2, chunks)
예제 #4
0
파일: __init__.py 프로젝트: ghotiv/vcrpy
def parse_headers_backwards_compat(header_dict):
    """
    In vcr 0.6.0, I changed the cassettes to store
    headers as a list instead of a dict.  This method
    parses the old dictionary-style headers for
    backwards-compatability reasons.
    """
    msg = HTTPMessage(BytesIO(""))
    for key, val in header_dict.items():
        msg.addheader(key, val)
        msg.headers.append("{0}:{1}".format(key, val))
    return msg
예제 #5
0
 def __init__(self, rfile, method, uri, protocol):
     HTTPMessage.__init__(self, rfile)
     self.method = method
     self.uri = uri
     parsed_uri = urlparse(uri)
     self.abs_path = parsed_uri.path
     self.protocol = protocol
     self.preview = self.get('preview') and int(self.get('preview'))
     self.http_request = None
     self.http_response = None
     self.preview_chunks = []
     self.send_continue_after_preview = None
     self.null_body = True
     self.eof = False
예제 #6
0
def fake_urlopen(req, **kw):
    """
    Behave like six.moves.urllib.request.urlopen().

    Return the contents of local fixture files on disk instead.
    """
    o = urlparse(req.get_full_url())
    localfile = os.path.join(FIXTURES_DIR, o.netloc, o.path[1:])
    # Try reading the file, and handle some special cases if we get an error.
    try:
        with open(localfile):
            pass
    except IOError as e:
        # Raise HTTP 404 errors for non-existent files.
        if e.errno == errno.ENOENT:
            url = req.get_full_url()
            headers = HTTPMessage(StringIO(u''))
            raise HTTPError(url, 404, 'Not Found', headers, None)
        # If URL looked like a directory ("/"), open the file instead.
        elif e.errno == errno.ENOTDIR:
            localfile = localfile.rstrip('/')
        # If localfile's a directory, look for a matching ".body" file instead.
        elif e.errno == errno.EISDIR:
            localfile = localfile.rstrip('/') + '.body'
        else:
            raise

    response = urlopen('file://' + localfile)

    return response
def parse_msg(fp):
    status = fp.readline()
    msg = HTTPMessage(fp)
    msg.status = status
    return msg