Package core :: Module http :: Class HttpServerResponse

[hide private]
[frames] | no frames]

# Class HttpServerResponse

source code

             object --+
                      |
    streams.WriteStream --+
                          |
                         HttpServerResponse

Encapsulates a server-side HTTP response.

An instance of this class is created and associated to every instance of HttpServerRequest that is created.

It allows the developer to control the HTTP response that is sent back to the client for the corresponding HTTP request. It contains methods that allow HTTP headers and trailers to be set, and for a body to be written out to the response.

It also allows a file to be streamed by the kernel directly from disk to the outgoing HTTP connection, bypassing user space altogether (where supported by the underlying operating system). This is a very efficient way of serving files from the server since buffers do not have to be read one by one from the file and written to the outgoing socket.

Instance Methods | [hide private]

__init__(self, java_obj) | source code

get_status_code(self) Get the status code of the response. | source code

set_status_code(self, code) Set the status code of the response. | source code

get_status_message(self) Get the status message the goes with status code | source code

set_status_message(self, message) Set the status message for a response | source code

headers(self) Get a copy of the reponse headers as a dictionary | source code

put_header(self, key, value) Inserts a header into the response. | source code

trailers(self) Get a copy of the trailers as a dictionary | source code

put_trailer(self, key, value) Inserts a trailer into the response. | source code

write_buffer(self, buffer, handler=None) Write a buffer to the response. | source code

write_str(self, str, enc="UTF-8", handler=None) Write a String to the response. | source code

send_file(self, path) Tell the kernel to stream a file directly from disk to the outgoing connection, bypassing userspace altogether (where supported by the underlying operating system. | source code

set_chunked(self, val) Sets whether this response uses HTTP chunked encoding or not. | source code

get_chunked(self) Get whether this response uses HTTP chunked encoding or not. | source code

end(self, data=None) Ends the response. | source code

close(self) Close the underlying TCP connection | source code

Inherited from `streams.WriteStream`: `drain_handler`, `exception_handler`, `set_write_queue_max_size`, `write_queue_full` Inherited from `streams.WriteStream` (private): `_to_write_stream`

Class Variables | [hide private]

| status_code = `property(get_status_code, set_status_code)`
| status_message = `property(get_status_message, set_status_message)`
| chunked = `property(get_chunked, set_chunked)`
Inherited from `streams.WriteStream`: `write_queue_max_size`

Method Details | [hide private]

### __init__(self, java_obj)
(Constructor)

source code

Overrides: object.__init__
  (inherited documentation)

### set_status_code(self, code)

source code

Set the status code of the response. Default is 200

### headers(self)

source code

Get a copy of the reponse headers as a dictionary

Decorators:
  - `@property`

### put_header(self, key, value)

source code

Inserts a header into the response.

Keyword arguments:

Parameters:
  - `key` - The header key
  - `value` - The header value.
Returns:
  HttpServerResponse so multiple operations can be chained.

### trailers(self)

source code

Get a copy of the trailers as a dictionary

Decorators:
  - `@property`

### put_trailer(self, key, value)

source code

Inserts a trailer into the response.

Keyword arguments:

Parameters:
  - `key` - The header key
  - `value` - The header value.
Returns:
  HttpServerResponse so multiple operations can be chained.

### write_buffer(self, buffer, handler=None)

source code

Write a buffer to the response. The handler (if supplied) will be called when the buffer has actually been written to the wire.

Keyword arguments:

Parameters:
  - `buffer` - The buffer to write
  - `handler` - The handler to be called when writing has been completed. It is wrapped in a DoneHandler (default None)
Returns:
  a HttpServerResponse so multiple operations can be chained.
Overrides: streams.WriteStream.write_buffer

### write_str(self, str, enc="UTF-8", handler=None)

source code

Write a String to the response. The handler will be called when the String has actually been written to the wire.

Keyword arguments:

Parameters:
  - `str` - The string to write
  - `enc` - Encoding to use.
  - `handler` - The handler to be called when writing has been completed. It is wrapped in a DoneHandler (default None)
Returns:
  a HttpServerResponse so multiple operations can be chained.

### send_file(self, path)

source code

Tell the kernel to stream a file directly from disk to the outgoing connection, bypassing userspace altogether (where supported by the underlying operating system. This is a very efficient way to serve files.

Keyword arguments:

Parameters:
  - `path` - Path to file to send.
Returns:
  a HttpServerResponse so multiple operations can be chained.

### set_chunked(self, val)

source code

Sets whether this response uses HTTP chunked encoding or not.

Keyword arguments:

Parameters:
  - `val` - If val is true, this response will use HTTP chunked encoding, and each call to write to the body will correspond to a new HTTP chunk sent on the wire. If chunked encoding is used the HTTP header 'Transfer-Encoding' with a value of 'Chunked' will be automatically inserted in the response.

    If chunked is false, this response will not use HTTP chunked encoding, and therefore if any data is written the body of the response, the total size of that data must be set in the 'Content-Length' header before any data is written to the response body. An HTTP chunked response is typically used when you do not know the total size of the request body up front.

Returns:
  a HttpServerResponse so multiple operations can be chained.

### end(self, data=None)

source code

Ends the response. If no data has been written to the response body, the actual response won't get written until this method gets called. Once the response has ended, it cannot be used any more, and if keep alive is true the underlying connection will be closed.

Keywords arguments

Parameters:
  - `data` - Optional String or Buffer to write before ending the response