libonion
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Data Structures | Functions
Response. Write response data to client: headers, content body...

Data Structures

struct  onion_response_t
 The response. More...

Functions

int onion_response_flush (onion_response *res)
 Writes all buffered output waiting for sending.If header has not been sent yet (delayed), it uses a temporary buffer to send it now. This way header can use the buffer_size information to send the proper content-length, even when it wasnt properly set by programmer. Whith this information its possib to keep alive the connection on more cases.
ssize_t onion_response_write0 (onion_response *res, const char *data)
 Writes some data to the response. \0 ended string.
ssize_t onion_response_write_html_safe (onion_response *res, const char *data)
 Writes the given string to the res, but encodes the data using html entitiesThe encoding mens that <html> whould become <html>
onion_dictonion_response_get_headers (onion_response *res)
 Returns the headers dictionary, so user can add repeated headersOnly simple use case is to add several coockies; using normal set_header is not possible, but accessing the dictionary user can add repeated headers without problem.
void onion_response_add_cookie (onion_response *res, const char *cookiename, const char *cookievalue, time_t validity_t, const char *path, const char *domain, int flags)
 Sets a new cookie into the response.
onion_responseonion_response_t::onion_response_new (onion_request *req)
 Generates a new response object.
onion_connection_status onion_response_t::onion_response_free (onion_response *res)
 Frees the memory consumed by this object.
void onion_response_t::onion_response_set_header (onion_response *res, const char *key, const char *value)
 Adds a header to the response object.
void onion_response_t::onion_response_set_length (onion_response *res, size_t len)
 Sets the header length. Normally it should be through set_header, but as its very common and needs some procesing here is a shortcut.
void onion_response_t::onion_response_set_code (onion_response *res, int code)
 Sets the return code.
static void onion_response_t::write_header (onion_response *res, const char *key, const char *value, int flags)
 Helper that is called on each header, and writes the header.
int onion_response_t::onion_response_write_headers (onion_response *res)
 Writes all the header to the given response.
ssize_t onion_response_t::onion_response_write (onion_response *res, const char *data, size_t length)
 Write some response data.
ssize_t onion_response_t::onion_response_printf (onion_response *res, const char *fmt,...)
 Writes some data to the response. Using sprintf format strings.
ssize_t onion_response_t::onion_response_vprintf (onion_response *res, const char *fmt, va_list args)
 Writes some data to the response. Using sprintf format strings. va_list args version.
const char * onion_response_t::onion_response_code_description (int code)
 Returns a const char * string with the code description.

Detailed Description

Function Documentation

void onion_response_add_cookie ( onion_response res,
const char *  cookiename,
const char *  cookievalue,
time_t  validity_t,
const char *  path,
const char *  domain,
int  flags 
)

Sets a new cookie into the response.

Sets a new cookie.

Parameters
resResponse object
cookienameName for the cookie
cookievalueValue for the cookis
validity_tSeconds this cookie is valid (added to current datetime). -1 to do not expire, 0 to expire inmediatly.
pathCookie valid only for this path
DomainCookie valid only for this domain (www.example.com, or *.example.com).
flagsFlags from onion_cookie_flags_t, for example OC_SECURE or OC_HTTP_ONLY

If validity is 0, cookie is set to expire right now.

References OC_HTTP_ONLY, OC_SECURE, ONION_DEBUG, and onion_response_t::onion_response_set_header().

const char * onion_response_code_description ( int  code)
int onion_response_flush ( onion_response res)

Writes all buffered output waiting for sending.If header has not been sent yet (delayed), it uses a temporary buffer to send it now. This way header can use the buffer_size information to send the proper content-length, even when it wasnt properly set by programmer. Whith this information its possib to keep alive the connection on more cases.

Flushes remaining data on the buffer to the listen point.

References onion_response_t::buffer, onion_response_t::buffer_pos, onion_request_t::connection, onion_response_t::flags, onion_request_t::listen_point, OCS_CLOSE_CONNECTION, ONION_DEBUG0, ONION_ERROR, onion_response_t::onion_response_write(), onion_response_t::onion_response_write_headers(), ONION_WARNING, OR_CHUNKED, OR_HEADER_SENT, OR_SKIP_CONTENT, onion_response_t::request, onion_response_t::sent_bytes, onion_response_t::sent_bytes_total, and onion_listen_point_t::write.

Referenced by onion_empty_output_buffer(), onion_handler_t::onion_handler_handle(), onion_response_t::onion_response_free(), onion_response_t::onion_response_write(), onion_response_t::onion_response_write_headers(), onion_term_destination(), and onion_webdav_propfind().

onion_connection_status onion_response_free ( onion_response res)
onion_dict* onion_response_get_headers ( onion_response res)

Returns the headers dictionary, so user can add repeated headersOnly simple use case is to add several coockies; using normal set_header is not possible, but accessing the dictionary user can add repeated headers without problem.

Gets the headers dictionary.

References onion_response_t::headers.

onion_response * onion_response_new ( onion_request req)

Generates a new response object.

This response is generated from a request, and gets from there the writer and writer data.

Also fills some important data, as server Id, License type, and the default content type.

Default content type is HTML, as normally this is what is needed. This is nontheless just the default, and can be changed to any other with a call to:

onion_response_set_header(res, "Content-Type", my_type);

The response object must be freed with onion_response_free, which also returns the keep alive status.

onion_response objects are passed by onion internally to process the request, and should not be created by user normally. Nontheless the option exist.

Returns
An onion_response object for that request.

References onion_response_t::buffer_pos, onion_response_t::code, onion_response_t::flags, onion_response_t::headers, onion_response_t::length, OD_DUP_VALUE, ONION_DEBUG, onion_dict_t::onion_dict_add(), onion_dict_t::onion_dict_new(), onion_low_free(), onion_low_malloc(), onion_low_strdup(), onion_response_t::request, onion_response_t::sent_bytes, and onion_response_t::sent_bytes_total.

Referenced by onion_request_process().

ssize_t onion_response_printf ( onion_response res,
const char *  fmt,
  ... 
)
void onion_response_set_code ( onion_response res,
int  code 
)
void onion_response_set_header ( onion_response res,
const char *  key,
const char *  value 
)
void onion_response_set_length ( onion_response res,
size_t  len 
)
ssize_t onion_response_vprintf ( onion_response res,
const char *  fmt,
va_list  args 
)

Writes some data to the response. Using sprintf format strings. va_list args version.

Parameters
argsva_list of arguments

References ONION_ERROR, onion_low_free(), onion_low_scalar_malloc(), and onion_response_t::onion_response_write().

Referenced by onion_response_t::onion_response_printf().

ssize_t onion_response_write ( onion_response res,
const char *  data,
size_t  length 
)

Write some response data.

This is the main write data function. If the headers have not been sent yet, they are now.

It's internally used also by the write0 and printf versions.

Also it does some buffering, so data is not sent as written by code, but only in chunks. These chunks are when the response is finished, or when the internal buffer is full. This helps performance, and eases the programming on the user side.

If length is 0, forces the write of pending data.

Returns
The bytes written, normally just length. On error returns OCS_CLOSE_CONNECTION.

References onion_response_t::buffer, onion_response_t::buffer_pos, onion_response_t::flags, OCS_CLOSE_CONNECTION, ONION_DEBUG, onion_response_flush(), onion_response_t::onion_response_write_headers(), OR_HEADER_SENT, and OR_SKIP_CONTENT.

Referenced by onion_handler_auth_pam_handler(), onion_handler_static_handler(), onion_response_flush(), onion_response_t::onion_response_vprintf(), onion_response_write0(), onion_response_t::onion_response_write_headers(), onion_shortcut_response_extra_headers(), onion_shortcut_response_file(), onion_webdav_propfind(), onion_websocket_t::onion_websocket_new(), and onion_response_t::write_header().

ssize_t onion_response_write0 ( onion_response res,
const char *  data 
)
int onion_response_write_headers ( onion_response res)

Writes all the header to the given response.

It writes the headers and depending on the method, return OR_SKIP_CONTENT. this is set when in head mode. Handlers should react to this return by not trying to write more, but if they try this object will just skip those writtings.

Explicit calling to this function is not necessary, as as soon as the user calls any write function this will be performed.

As soon as the headers are written, any modification on them will be just ignored.

Returns
0 if should procced to normal data write, or OR_SKIP_CONTENT if should not write content.

References onion_response_t::buffer_pos, onion_response_t::code, CONNECTION_CHUNK_ENCODING, CONNECTION_CLOSE, CONNECTION_KEEP_ALIVE, CONNECTION_UPGRADE, onion_request_t::flags, onion_response_t::flags, onion_response_t::headers, ONION_DEBUG0, onion_dict_t::onion_dict_count(), onion_dict_t::onion_dict_preorder(), ONION_ERROR, onion_request_t::onion_request_keep_alive(), onion_response_t::onion_response_code_description(), onion_response_flush(), onion_response_t::onion_response_printf(), onion_response_t::onion_response_write(), OR_CHUNKED, OR_CONNECTION_UPGRADE, OR_HEAD, OR_HEADER_SENT, OR_HTTP11, OR_LENGTH_SET, OR_METHODS, OR_SKIP_CONTENT, onion_response_t::request, onion_response_t::sent_bytes, onion_request_t::session, onion_request_t::session_id, and onion_response_t::write_header().

Referenced by onion_handler_opack_handler(), onion_handler_static_handler(), onion_jpeg_response(), onion_png_response(), onion_response_flush(), onion_response_t::onion_response_free(), onion_response_t::onion_response_write(), onion_shortcut_response_extra_headers(), onion_shortcut_response_file(), onion_webdav_options(), onion_webdav_propfind(), onion_webdav_proppatch(), and onion_websocket_t::onion_websocket_new().

ssize_t onion_response_write_html_safe ( onion_response res,
const char *  data 
)

Writes the given string to the res, but encodes the data using html entitiesThe encoding mens that <html> whould become <html>

Writes some data to the response. \0 ended string, and encodes it if necesary into html entities to make it safe.

References onion_html_quote(), onion_low_free(), and onion_response_write0().

static void write_header ( onion_response res,
const char *  key,
const char *  value,
int  flags 
)

Helper that is called on each header, and writes the header.

References onion_response_t::onion_response_write(), and onion_response_write0().

Referenced by onion_response_t::onion_response_write_headers().