A simple HTTP library
Fork of http://www.scumways.com/happyhttp/happyhttp.html
HappyHTTP is a simple C++ library for issuing HTTP requests and processing responses.
- Simple to integrate - just drop in the .h and .cpp files
- Easy-to-use interface (example)
- Non-blocking operation, suitable for use in game update loops
- Supports pipelining. You can issue multiple requests without waiting for responses.
- Licensed under the zlib/libpng license.
- Cross-platform (Linux, OSX, Windows)
The interface is based loosely on Python's httplib.
All HappyHTTP code is kept within the happyhttp::
namespace
To issue and process a HTTP request, the basic steps are:
- Create a connection object
- Set up callbacks for handling responses
- Issue request(s)
- 'pump' the connection at regular intervals. As responses are received, the callbacks will be invoked.
For more examples, see test.cpp.
static int count=0;
// invoked when response headers have been received
void OnBegin( const happyhttp::Response* r, void* userdata )
{
printf( "BEGIN (%d %s)\n", r->getstatus(), r->getreason() );
count = 0;
}
// invoked to process response body data (may be called multiple times)
void OnData( const happyhttp::Response* r, void* userdata, const unsigned char* data, int n )
{
fwrite( data,1,n, stdout );
count += n;
}
// invoked when response is complete
void OnComplete( const happyhttp::Response* r, void* userdata )
{
printf( "COMPLETE (%d bytes)\n", count );
}
void TestGET()
{
happyhttp::Connection conn( "www.scumways.com", 80 );
conn.setcallbacks( OnBegin, OnData, OnComplete, 0 );
conn.request( "GET", "/happyhttp/test.php" );
while( conn.outstanding() )
conn.pump();
}
Constructor. Specifies host and port, but connection isn't made until
request is issued or connect()
is called.
Destructor. If connection is open, it'll be closed, and any outstanding requests will be discarded.
void setcallbacks( ResponseBegin_CB begincb,
ResponseData_CB datacb,
ResponseComplete_CB completecb,
void* userdata )
Set up the response handling callbacks. These will be invoked during
calls to pump()
.
begincb
- called when the responses headers have been received
datacb
- called repeatedly to handle body data
completecb
- response is completed
userdata
- passed as a param to all callbacks.
Don't need to call connect()
explicitly as issuing a request will call
it automatically if needed. But it could block (for name lookup etc), so
you might prefer to call it in advance.
close connection, discarding any pending requests.
Update the connection (non-blocking) Just keep calling this regularly to service outstanding requests. As responses are received, the callbacks will be invoked.
Returns true if any requests are still outstanding.
void request( const char* method, const char* url,
const char* headers[],
const unsigned char* body,
int bodysize )
High-level request interface. Issues a request.
method
- "GET", "POST" etc...url
- eg "/index.html"headers
- array of name/value pairs, terminated by a null-ptrbody, bodysize
- specify body data of request (eg values for a form)
(part of low-level request interface) Begin a request
- method is "GET", "POST" etc...
- url is only path part: eg "/index.html"
(part of low-level request interface) Add a header to the request (call after putrequest() )
(part of low-level request interface) Indicate that your are finished adding headers and the request can be issued.
(part of low-level request interface) send body data if any. To be called after endheaders()
Invoked when all the headers for a response have been received.
The Response object can be queried to determine status and header
values.
userdata
is the same value that was passed in toConnection::setcallbacks()
.
typedef void (*ResponseData_CB)( const Response* r, void* userdata, const unsigned char* data, int numbytes )
This callback is invoked to pass out data from the body of the response. It may be called multiple times, or not at all (if there is no body).
Once a response is completed, this callback is invoked. When the callback returns, the respsonse object will be destroyed.
When a callback is invoked, a response object is passed to it. The following member functions can be used to query the response:
retrieve the value of a header (returns 0 if not present)
Get the HTTP status code returned by the server
Get the HTTP response reason string returned by the server
If an error occurs, a Wobbly
is thrown. The Wobbly::what()
method
returns a text description.
- Proxy support
- URL manipulation functions
- Improve error text (and maybe some more exception types?)
- HTTP 2 Support
- Modern C++ implementation (smart pointers, etc.)
HappyHTTP is licensed under the zlib/libpng license.
You are free to use this library however you wish, but if you make changes, please send a patch!
If you use it, let us know.