Skip to content

Instantly share code, notes, and snippets.

@sethhall
Created November 3, 2012 03:11
Show Gist options
  • Save sethhall/4005673 to your computer and use it in GitHub Desktop.
Save sethhall/4005673 to your computer and use it in GitHub Desktop.
Example Bro HTTP API
module ActiveHTTP;
export {
## The default timeout for HTTP requests.
const default_request_timeout = 1min &redef;
## The default HTTP method/verb to use for requests.
const default_method = "GET" &redef;
type Request: record {
## The URL being requested.
url: string;
## The HTTP method/verb to use for the request.
method: string &default=default_method;
## Post data to send to the server. Don't URL encode
## data in this field, it will be done internally.
post_data: string &optional;
## Arbitrary headers to pass to the server. Some headers
## will be included by libCurl.
custom_headers: table[string] of string &optional;
## Timeout for the request.
timeout: interval &default=default_request_timeout;
};
type Response: record {
## Numeric response code from the server.
code: count;
## String response messgae from the server.
msg: string;
## Full body of the response.
body: string &optional;
## All headers returned by the server.
headers: table[string] of string &optional;
};
## Perform an HTTP request according to the :bro:type:`Request` record.
## This is an asynchronous function and must be called within a "when"
## statement.
##
## req: A record instance representing all options for an HTTP request.
##
## Returns: A record with the full response message.
global request: function(req: Request): Response;
}
## Example...
event bro_init()
{
when ( local resp = ActiveHTTP::request([$url="http://www.google.com/"]) )
{
print resp$body;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment