Module refinery.units.formats.httpresponse

Expand source code Browse git
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from http.client import HTTPResponse, IncompleteRead

from refinery.units import Unit, RefineryPartialResult
from refinery.lib.structures import MemoryFile


class SockWrapper(MemoryFile):
    def sendall(self, ___): pass
    def makefile(self, *_): return self


class httpresponse(Unit):
    """
    Parses HTTP response text, as you would obtain from a packet dump. This can be
    useful if chunked or compressed transfer encoding was used.
    """
    def process(self, data):
        with SockWrapper(data) as mock:
            mock.seek(0)
            parser = HTTPResponse(mock)
            parser.begin()
            try:
                return parser.read()
            except IncompleteRead as incomplete:
                msg = F'incomplete read: {len(incomplete.partial)} bytes processed, {incomplete.expected} more expected'
                raise RefineryPartialResult(msg, incomplete.partial) from incomplete

Classes

class SockWrapper (data=None, read_as_bytes=False, fileno=None, size_limit=None)

A thin wrapper around (potentially mutable) byte sequences which gives it the features of a file-like object.

Expand source code Browse git
class SockWrapper(MemoryFile):
    def sendall(self, ___): pass
    def makefile(self, *_): return self

Ancestors

Class variables

var read_as_bytes

Instance variables

var closed
Expand source code Browse git
@property
def closed(self) -> bool:
    return self._closed

Methods

def sendall(self, ___)
Expand source code Browse git
def sendall(self, ___): pass
def makefile(self, *_)
Expand source code Browse git
def makefile(self, *_): return self

Inherited members

class httpresponse

Parses HTTP response text, as you would obtain from a packet dump. This can be useful if chunked or compressed transfer encoding was used.

Expand source code Browse git
class httpresponse(Unit):
    """
    Parses HTTP response text, as you would obtain from a packet dump. This can be
    useful if chunked or compressed transfer encoding was used.
    """
    def process(self, data):
        with SockWrapper(data) as mock:
            mock.seek(0)
            parser = HTTPResponse(mock)
            parser.begin()
            try:
                return parser.read()
            except IncompleteRead as incomplete:
                msg = F'incomplete read: {len(incomplete.partial)} bytes processed, {incomplete.expected} more expected'
                raise RefineryPartialResult(msg, incomplete.partial) from incomplete

Ancestors

Class variables

var required_dependencies
var optional_dependencies

Inherited members