Pyjo.Content - HTTP content base class

import Pyjo.Content

class MyContent(Pyjo.Content.object):
    @property
    def body_contains(self, chunk):
        ...

    @property
    def body_size(self):
        ...

    def get_body_chunk(self, offset):
        ...

Pyjo.Content is an abstract base class for HTTP content based on RFC 7230 and RFC 7231.

Events

Pyjo.Content inherits all events from Pyjo.EventEmitter and can emit the following new ones.

body

@content.on
def body(content):
    ...

Emitted once all headers have been parsed and the body starts.

@content.on
def body(content):
    if content.headers.header('X-No-MultiPart'):
        content.auto_upgrade = False

drain

@content.on
def drain(content, offset):
    ...

Emitted once all data has been written.

@content.on
def drain(content, offset):
    content.write_chunk(time.time())

read

@content.on
def read(content, chunk):
    ...

Emitted when a new chunk of content arrives.

content.unsubscribe('read')

@content.on
def read(content, chunk):
    print("Streaming: {0}".format(chunk))

Classess

class Pyjo.Content.Pyjo_Content(**kwargs)

Pyjo.Content inherits all methods from Pyjo.EventEmitter and implements the following new ones.

auto_decompress = None
boolean = content.auto_decompress
content.auto_decompress = boolean

Decompress content automatically if is_compressed is true.

auto_relax = None
boolean = content.auto_relax
content.auto_relax = boolean

Try to detect when relaxed parsing is necessary.

body_contains(*args, **kwargs)
boolean = content.body_contains(b'foo bar baz')

Check if content contains a specific string. Meant to be overloaded in a subclass.

body_size
size = content.body_size

Content size in bytes. Meant to be overloaded in a subclass.

boundary
boundary = content.boundary

Extract multipart boundary from Content-Type header.

build_body()
bstring = content.build_body()

Render whole body.

build_headers()
bstring = content.build_headers()

Render all headers.

charset
charset = content.charset

Extract charset from Content-Type header.

clone()
clone = content.clone()

Clone content if possible, otherwise return None.

expect_close = None
boolean = content.expect_close
content.expect_close = boolean

Expect a response that is terminated with a connection close.

generate_body_chunk(offset)
chunk = content.generate_body_chunk(0)

Generate dynamic content.

get_body_chunk(*args, **kwargs)
chunk = content.get_body_chunk(0)

Get a chunk of content starting from a specific position. Meant to be overloaded in a subclass.

get_header_chunk(offset)
chunk = content.get_header_chunk(13)

Get a chunk of the headers starting from a specific position.

header_size
size = content.header_size

Size of headers in bytes.

headers = None
headers = content.headers
content.headers = Pyjo.Headers.new()

Content headers, defaults to a Pyjo.Headers object.

is_chunked
boolean = content.is_chunked

Check if content is chunked.

is_compressed
boolean = content.is_compressed

Check if content is gzip compressed.

is_dynamic
boolean = content.is_dynamic

Check if content will be dynamically generated, which prevents clone() from working.

is_finished
boolean = content.is_finished

Check if parser is finished.

is_limit_exceeded
boolean = content.is_limit_exceeded

Check if buffer has exceeded max_buffer_size.

is_multipart
false = content.is_multipart

False.

is_parsing_body
boolean = content.is_parsing_body

Check if body parsing started yet.

leftovers
chunk = content.leftovers

Get leftover data from content parser.

max_buffer_size = None
size = content.max_buffer_size
content.max_buffer_size = 1024

Maximum size in bytes of buffer for content parser, defaults to the value of the PYJO_MAX_BUFFER_SIZE` environment variable or ``262144 (256KB).

max_leftover_size = None
size = content.max_leftover_size
content.max_leftover_size = 1024

Maximum size in bytes of buffer for pipelined HTTP requests, defaults to the value of the PYJO_MAX_LEFTOVER_SIZE` environment variable or ``262144 (256KB).

parse(chunk=None)
content = content.parse(b"Content-Length: 12\x0d\x0a\x0d\x0aHello World!")

Parse content chunk.

parse_body(body)
content = content.parse_body(b'Hi!')

Parse body chunk and skip headers.

progress
size = content.progress

Size of content already received from message in bytes.

relaxed = None
boolean = content.relaxed
content.relaxed = boolean

Activate relaxed parsing for responses that are terminated with a connection close.

skip_body = None
boolean = content.skip_body
content.skip_body = boolean

Skip body parsing and finish after headers.

to_bytes()
bstring = content.to_bytes()

Turn content into a bytes string, suitable for HTTP messages.

write(chunk=None, cb=None)
content = content.write(chunk)
content = content.write(chunk, cb)

Write dynamic content non-blocking, the optional drain callback will be invoked once all data has been written.

write_chunk(chunk=None, cb=None)
content = content.write_chunk(chunk)
content = content.write_chunk(chunk, cb)

Write dynamic content non-blocking with chunked transfer encoding, the optional drain callback will be invoked once all data has been written.

Pyjo.Content.object

alias of Pyjo_Content