Makes an HTTP server accept connections on the specified socket. This may be
useful to create a socket and then fork multiple instances of an http server,
or when a socket has been communicated via file descriptor passing in
situations where an http servers does not have permissions to bind to a
low-numbered port.
Can be called multiple times to have the http server listen to
multiple different sockets.
Parameters:
http a pointer to an evhttp object
fd a socket fd that is ready for accepting connections
Returns:
0 on success, -1 on failure.
See also:
evhttp_bind_socket()
Like evhttp_accept_socket(), but returns a handle for referencing the
socket. The returned pointer is not valid after http is freed.
Parameters:
http a pointer to an evhttp object
fd a socket fd that is ready for accepting connections
Returns:
Handle for the socket on success, NULL on failure.
See also:
evhttp_accept_socket(),
evhttp_del_accept_socket()
Adds a header to a list of existing headers.
Parameters:
headers the evkeyvalq object to which to add a
header
key the name of the header
value the value belonging to the header
Returns:
0 on success, -1 otherwise.
See also:
evhttp_find_header(),
evhttp_clear_headers()
Add a server alias to an http object. The http object can be a virtual host or
the main server.
Parameters:
http the evhttp object
alias the alias to add
See also:
evhttp_add_remove_alias()
Adds a virtual host to the http server. A virtual host is a newly initialized
evhttp object that has request callbacks set on it via evhttp_set_cb()
or evhttp_set_gencb(). It most not have any listing sockets associated
with it.
If the virtual host has not been removed by the time that
evhttp_free() is called on the main http server, it will be
automatically freed, too.
It is possible to have hierarchical vhosts. For example: A vhost
with the pattern *.example.com may have other vhosts with patterns
foo.example.com and bar.example.com associated with it.
Parameters:
http the evhttp object to which to add a virtual
host
pattern the glob pattern against which the hostname is matched. The match
is case insensitive and follows otherwise regular shell matching.
vhost the virtual host to add the regular http server.
Returns:
0 on success, -1 on failure
See also:
evhttp_remove_virtual_host()
The most low-level evhttp_bind/accept method: takes an evconnlistener, and
returns an evhttp_bound_socket. The listener will be freed when the bound
socket is freed.
Binds an HTTP server on the specified address and port. Can be called multiple
times to bind the same http server to multiple different ports.
Parameters:
http a pointer to an evhttp object
address a string containing the IP address to listen(2) on
port the port number to listen on
Returns:
0 on success, -1 on failure.
See also:
evhttp_accept_socket()
Like evhttp_bind_socket(), but returns a handle for referencing the
socket. The returned pointer is not valid after http is freed.
Parameters:
http a pointer to an evhttp object
address a string containing the IP address to listen(2) on
port the port number to listen on
Returns:
Handle for the socket on success, NULL on failure.
See also:
evhttp_bind_socket(),
evhttp_del_accept_socket()
Get the raw file descriptor referenced by an evhttp_bound_socket.
Parameters:
bound_socket a handle returned by
evhttp_{bind,accept}_socket_with_handle
Returns:
the file descriptor used by the bound socket
See also:
evhttp_bind_socket_with_handle(),
evhttp_accept_socket_with_handle()
Cancels a pending HTTP request. Cancels an ongoing HTTP request. The callback
associated with this request is not executed and the request object is freed.
If the request is currently being processed, e.g. it is ongoing, the
corresponding evhttp_connection object is going to get reset.
A request cannot be canceled if its callback has executed already.
A request may be canceled reentrantly from its chunked callback.
Parameters:
req the evhttp_request to cancel; req becomes
invalid after this call.
Removes all headers from the header list.
Parameters:
headers the evkeyvalq object from which to remove
all headers
Create and return a connection object that can be used to for making HTTP
requests. The connection object tries to resolve address and establish the
connection when it is given an http request object.
Parameters:
base the event_base to use for handling the
connection
dnsbase the dns_base to use for resolving host names; if not specified
host name resolution will block.
bev a bufferevent to use for connecting to the server; if NULL, a
socket-based bufferevent will be created. This buffrevent will be freed when
the connection closes. It must have no fd set on it.
address the address to which to connect
port the port to connect to
Returns:
an evhttp_connection object that can be used for making
requests
Create and return a connection object that can be used to for making HTTP
requests. The connection object tries to resolve address and establish the
connection when it is given an http request object.
Parameters:
base the event_base to use for handling the
connection
dnsbase the dns_base to use for resolving host names; if not specified
host name resolution will block.
address the address to which to connect
port the port to connect to
Returns:
an evhttp_connection object that can be used for making
requests
Disowns a given connection object. Can be used to tell libevent to free the
connection object after the last request has completed or failed.
Get the remote address associated with this connection. extracted from
getpeername() OR from nameserver.
Returns:
NULL if getpeername() return non success, or connection
is not connected, otherwise it return pointer to struct sockaddr_storage
Get the remote address and port associated with this connection.
Set a callback for connection close.
Set connection flags.
See also:
EVHTTP_CON_*
Returns:
0 on success, otherwise non zero (for example if flag
doesn't supported).
Sets the delay before retrying requests on this connection. This is only used if
evhttp_connection_set_retries is used to make the number of retries at least
one. Each retry after the first is twice as long as the one before it.
Sets the timeout for events related to this connection. Takes a struct timeval.
Helper function to sort of decode a URI-encoded string. Unlike
evhttp_get_decoded_uri, it decodes all plus characters that appear
after the first question mark character, but no plusses that occur
before. This is not a good way to decode URIs in whole or in part.
The returned string must be freed by the caller
Deprecated
This function is deprecated; you probably want to use
evhttp_get_decoded_uri instead.
Parameters:
uri an encoded URI
Returns:
a newly allocated unencoded URI or NULL on failure
Makes an HTTP server stop accepting connections on the specified socket. This
may be useful when a socket has been sent via file descriptor passing and is
no longer needed by the current process.
If you created this bound socket with
evhttp_bind_socket_with_handle or evhttp_accept_socket_with_handle, this
function closes the fd you provided. If you created this bound socket with
evhttp_bind_listener, this function frees the listener you provided.
bound_socket is an invalid pointer after this call
returns.
Parameters:
http a pointer to an evhttp object
bound_socket a handle returned by
evhttp_{bind,accept}_socket_with_handle
See also:
evhttp_bind_socket_with_handle(),
evhttp_accept_socket_with_handle()
Helper function to encode a string for inclusion in a URI. All characters are
replaced by their hex-escaped (%22) equivalents, except for characters
explicitly unreserved by RFC3986 -- that is, ASCII alphanumeric characters,
hyphen, dot, underscore, and tilde.
The returned string must be freed by the caller.
Parameters:
str an unencoded string
Returns:
a newly allocated URI-encoded string or NULL on
failure
Finds the value belonging to a header.
Parameters:
headers the evkeyvalq object in which to find the
header
key the name of the header to find
Returns:
a pointer to the value for the header or NULL if the
header could not be found.
See also:
evhttp_add_header(),
evhttp_remove_header()
Applies the function specified in the first argument to all evhttp_bound_sockets
associated with 'http'. The user must not attempt to free or remove any
connections, sockets or listeners in the callback 'function'.
Parameters:
http pointer to an evhttp object
function function to apply to every bound socket
argument pointer value passed to function for every socket iterated
Free the previously created HTTP server. Works only if no requests are currently
being served.
Parameters:
http the evhttp server object to be freed
See also:
evhttp_start()
Escape HTML character entities in a string. Replaces <, >, ", ' and
& with <, >, ", ' and & correspondingly.
The returned string needs to be freed by the caller.
Parameters:
html an unescaped HTML string
Returns:
an escaped HTML string or NULL on error
Make an HTTP request over the specified connection. The connection gets
ownership of the request. On failure, the request object is no longer valid as
it has been freed.
Parameters:
evcon the evhttp_connection object over which to
send the request
req the previously created and configured request object
type the request type EVHTTP_REQ_GET, EVHTTP_REQ_POST, etc.
uri the URI associated with the request
Returns:
0 on success, -1 on failure
See also:
evhttp_cancel_request()
Create a new HTTP server.
Parameters:
base (optional) the event base to receive the HTTP
events
Returns:
a pointer to a newly initialized evhttp server
structure
See also:
evhttp_free()
Helper function to parse out arguments in a query. Parsing a URI like
http://foo.com/?q=test&s=some+thing
will result in two entries in the key value queue.
The first entry is: key='q', value='test' The second entry is:
key='s', value='some thing'
Deprecated
This function is deprecated as of Libevent 2.0.9. Use
evhttp_uri_parse and evhttp_parse_query_str instead.
Parameters:
uri the request URI
headers the head of the evkeyval queue
Returns:
0 on success, -1 on failure
Helper function to parse out arguments from the query portion of an HTTP URI.
Parsing a query string like
q=test&s=some+thing
will result in two entries in the key value queue.
The first entry is: key='q', value='test' The second entry is:
key='s', value='some thing'
Parameters:
query_parse the query portion of the URI
headers the head of the evkeyval queue
Returns:
0 on success, -1 on failure
Removes a header from a list of existing headers.
Parameters:
headers the evkeyvalq object from which to remove
a header
key the name of the header to remove
Returns:
0 if the header was removed, -1 otherwise.
See also:
evhttp_find_header(),
evhttp_add_header()
Remove a server alias from an http object.
Parameters:
http the evhttp object
alias the alias to remove
See also:
evhttp_add_server_alias()
Removes a virtual host from the http server.
Parameters:
http the evhttp object from which to remove the
virtual host
vhost the virtual host to remove from the regular http server.
Returns:
0 on success, -1 on failure
See also:
evhttp_add_virtual_host()
Frees the request object and removes associated events.
Returns the connection object associated with the request or NULL. The user
needs to either free the request explicitly or call
evhttp_send_reply_end().
Returns the host associated with the request. If a client sends an absolute URI,
the host part of that is preferred. Otherwise, the input headers are searched
for a Host: header. NULL is returned if no absolute URI or Host: header is
provided.
Creates a new request object that needs to be filled in with the request
parameters. The callback is executed when the request completed or an error
occurred.
Takes ownership of the request object. Can be used in a request callback to keep
onto the request until evhttp_request_free() is explicitly called by
the user.
Enable delivery of chunks to requestor.
Parameters:
cb will be called after every read of data with
the same argument as the completion callback. Will never be called on an empty
response. May drain the input buffer; it will be drained automatically on
return.
Set a callback for errors.
See also:
evhttp_request_error for error types.
On error, both the error callback and the regular callback will be
called, error callback is called before the regular callback.
Register callback for additional parsing of request headers.
Parameters:
cb will be called after receiving and parsing the
full header. It allows analyzing the header and possibly closing the
connection by returning a value < 0.
Set a callback to be called on request completion of evhttp_send_* function. The
callback function will be called on the completion of the request after the
output data has been written and before the evhttp_request object is
destroyed. This can be useful for tracking resources associated with a request
(ex: timing metrics).
Parameters:
req a request object
cb callback function that will be called on request completion
cb_arg an additional context argument for the callback
Send an HTML error message to the client.
Parameters:
req a request object
error the HTTP error code
reason a brief explanation of the error. If this is NULL, we'll just use
the standard meaning of the error code.
Send an HTML reply to the client. The body of the reply consists of the data in
databuf. After calling evhttp_send_reply() databuf will be empty, but
the buffer is still owned by the caller and needs to be deallocated by the
caller if necessary.
Parameters:
req a request object
code the HTTP response code to send
reason a brief message to send with the response code
databuf the body of the response
Send another data chunk as part of an ongoing chunked reply. The reply chunk
consists of the data in databuf. After calling
evhttp_send_reply_chunk() databuf will be empty, but the buffer is
still owned by the caller and needs to be deallocated by the caller if
necessary.
Parameters:
req a request object
databuf the data chunk to send as part of the reply.
Send another data chunk as part of an ongoing chunked reply. The reply chunk
consists of the data in databuf. After calling
evhttp_send_reply_chunk() databuf will be empty, but the buffer is
still owned by the caller and needs to be deallocated by the caller if
necessary.
Parameters:
req a request object
databuf the data chunk to send as part of the reply.
cb callback funcion
call back's argument.
Complete a chunked reply, freeing the request as appropriate.
Parameters:
req a request object
Initiate a reply that uses Transfer-Encoding chunked. This allows the caller to
stream the reply back to the client and is useful when either not all of the
reply data is immediately available or when sending very large replies.
The caller needs to supply data chunks with
evhttp_send_reply_chunk() and complete the reply by calling
evhttp_send_reply_end().
Parameters:
req a request object
code the HTTP response code to send
reason a brief message to send with the response code
Sets the what HTTP methods are supported in requests accepted by this server,
and passed to user callbacks. If not supported they will generate a '405
Method not allowed' response.
By default this includes the following methods: GET, POST, HEAD,
PUT, DELETE
Parameters:
http the http server on which to set the methods
methods bit mask constructed from evhttp_cmd_type values
Set a callback used to create new bufferevents for connections to a given evhttp
object. You can use this to override the default bufferevent type -- for
example, to make this evhttp object use SSL bufferevents rather than
unencrypted ones.
New bufferevents must be allocated with no fd set on them.
Parameters:
http the evhttp server object for which to set the
callback
cb the callback to invoke for incoming connections
arg an context argument for the callback
Set a callback for a specified URI.
Parameters:
http the http sever on which to set the callback
path the path for which to invoke the callback
cb the callback function that gets invoked on requesting path
cb_arg an additional context argument for the callback
Returns:
0 on success, -1 if the callback existed already, -2 on
failure
Set the value to use for the Content-Type header when none was provided. If the
content type string is NULL, the Content-Type header will not be automatically
added.
Parameters:
http the http server on which to set the default
content type
content_type the value for the Content-Type header
Set connection flags for HTTP server.
See also:
EVHTTP_SERVER_*
Returns:
0 on success, otherwise non zero (for example if flag
doesn't supported).
Set a callback for all requests that are not caught by specific callbacks.
Invokes the specified callback for all requests that do not match any of the
previously specified request paths. This is catchall for requests not
specifically configured with evhttp_set_cb().
Parameters:
http the evhttp server object for which to set the
callback
cb the callback to invoke for any unmatched requests
arg an context argument for the callback
Set the timeout for an HTTP request.
Parameters:
http an evhttp object
timeout_in_secs the timeout, in seconds
Set the timeout for an HTTP request.
Parameters:
http an evhttp object
tv the timeout, or NULL
Free all memory allocated for a parsed uri. Only use this for URIs generated by
evhttp_uri_parse.
Parameters:
uri container with parsed data
See also:
evhttp_uri_parse()
Return the host part of an evhttp_uri, or NULL if it has no host set. The host
may either be a regular hostname (conforming to the RFC 3986 'regname'
production), or an IPv4 address, or the empty string, or a bracketed IPv6
address, or a bracketed 'IP-Future' address.
Note that having a NULL host means that the URI has no authority
section, but having an empty-string host means that the URI has an authority
section with no host part. For example, 'mailto:user@example.com' has a host
of NULL, but 'file:///etc/motd' has a host of ''.
Return the port part of an evhttp_uri, or -1 if there is no port set.
Return the scheme of an evhttp_uri, or NULL if there is no scheme has been set
and the evhttp_uri contains a Relative-Ref.
Join together the uri parts from parsed data to form a URI-Reference. Note that
no escaping of reserved characters is done on the members of the evhttp_uri,
so the generated string might not be a valid URI unless the members of
evhttp_uri are themselves valid.
Parameters:
uri container with parsed data
buf destination buffer
limit destination buffer size
Returns:
an joined uri as string or NULL on error
See also:
evhttp_uri_parse()
Helper function to parse a URI-Reference as specified by RFC3986. This function
matches the URI-Reference production from RFC3986, which includes both URIs
like
scheme://[[userinfo]@]foo.com[:port]]/[path][?query][#fragment]
and relative-refs like
[path][?query][#fragment]
Any optional elements portions not present in the original URI are
left set to NULL in the resulting evhttp_uri. If no port is specified, the
port is set to -1.
Note that no decoding is performed on percent-escaped characters
in the string; if you want to parse them, use evhttp_uridecode or
evhttp_parse_query_str as appropriate.
Note also that most URI schemes will have additional constraints
that this function does not know about, and cannot check. For example,
mailto://www.example.com/cgi-bin/fortune.pl is not a reasonable mailto url,
http://www.example.com:99999/ is not a reasonable HTTP URL, and
ftp:username@example.com is not a reasonable FTP URL. Nevertheless, all of
these URLs conform to RFC3986, and this function accepts all of them as
valid.
Parameters:
source_uri the request URI
flags Zero or more EVHTTP_URI_* flags to affect the behavior of the
parser.
Returns:
uri container to hold parsed data, or NULL if there is
error
See also:
evhttp_uri_free()
Changes the flags set on a given URI. See EVHTTP_URI_* for a list of flags.
Set the fragment of an evhttp_uri, or clear the fragment if fragment==NULL. The
fragment should not include a leading '#'. Returns 0 on success, -1 if
fragment is not well-formed.
Set the host of an evhttp_uri, or clear the host if host==NULL. Returns 0 on
success, -1 if host is not well-formed.
Set the path of an evhttp_uri, or clear the path if path==NULL. Returns 0 on
success, -1 if path is not well-formed.
Set the port of an evhttp_uri, or clear the port if port==-1. Returns 0 on
success, -1 if port is not well-formed.
Set the query of an evhttp_uri, or clear the query if query==NULL. The query
should not include a leading '?'. Returns 0 on success, -1 if query is not
well-formed.
Set the scheme of an evhttp_uri, or clear the scheme if scheme==NULL. Returns 0
on success, -1 if scheme is not well-formed.
Set the userinfo of an evhttp_uri, or clear the userinfo if userinfo==NULL.
Returns 0 on success, -1 if userinfo is not well-formed.
Helper function to decode a URI-escaped string or HTTP parameter. If
'decode_plus' is 1, then we decode the string as an HTTP parameter value, and
convert all plus ('+') characters to spaces. If 'decode_plus' is 0, we leave
all plus characters unchanged.
The returned string must be freed by the caller.
Parameters:
uri a URI-encode encoded URI
decode_plus determines whether we convert '+' to space.
size_out if size_out is not NULL, *size_out is set to the size of the
returned string
Returns:
a newly allocated unencoded URI or NULL on failure
As evhttp_encode_uri, but if 'size' is nonnegative, treat the string as being
'size' bytes long. This allows you to encode strings that may contain 0-valued
bytes.
The returned string must be freed by the caller.
Parameters:
str an unencoded string
size the length of the string to encode, or -1 if the string is
NUL-terminated
space_to_plus if true, space characters in 'str' are encoded as +, not
%20.
Returns:
a newly allocate URI-encoded string, or NULL on
failure.