diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8712434 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Panu P + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/httpcpp.h b/httpcpp.h index 2e18b72..32f760b 100644 --- a/httpcpp.h +++ b/httpcpp.h @@ -20,9 +20,9 @@ class HttpRequestHandler; class HttpResponseHandler; /** - * HttpRequest provides access to data of an HTTP reqeust. In general cases, - * AsyncHttpServer creates objects of this class automatically and provides - * them in methods of HttpReqestHandler, which you inherit in order to build + * HttpRequest provides access to data of an HTTP request. In general cases, + * AsyncHttpServer creates objects of this class automatically and provides + * them in methods of HttpReqestHandler, which you inherit in order to build * your own handler. * * TODO: (1) Code from_sequence() to parse more data, like headers, and make @@ -32,32 +32,32 @@ class HttpRequest { friend class AsyncHttpServer; friend class HttpRequestHandler; private: - string method; - string path; - string body; + string method; + string path; + string body; AsyncHttpServer* server; int fd; bool done; protected: /** - * Parses the sequence and returns a request if successful or NULL if + * Parses the sequence and returns a request if successful or NULL if * not. The caller MUST delete the request when no longer used. * * @param sequence the sequence to be parsed into an HttpRequest object */ static HttpRequest* from_sequence(const string& sequence); - /** + /** * Constructor. * - * @param method the method of the request + * @param method the method of the request * @param path the path of the request * @param body the body of the request */ - HttpRequest(const string& method, const string& path, + HttpRequest(const string& method, const string& path, const string& body=""); public: /** - * Returns the method. + * Returns the method. */ const string& get_method(); /** @@ -65,20 +65,20 @@ class HttpRequest { */ const string& get_path(); /** - * Returns the body. + * Returns the body. */ const string& get_body(); }; /** - * HttpResponse provides access to data of an HTTP response. In general cases, + * HttpResponse provides access to data of an HTTP response. In general cases, * AsyncHttpClient creates objects of this class automatically and provides - * them in methods of HttpResponseHandler, which you inherit in order to build + * them in methods of HttpResponseHandler, which you inherit in order to build * your own handler. * * TODO: (1) Code from_sequence() to parse more data, like headers, and make * them available via getting methods, e.g. get_headers("XYZ"). - * (2) Make from_sequence() more flexible because it always expects + * (2) Make from_sequence() more flexible because it always expects * Content-Length at the moment. */ class HttpResponse { @@ -88,22 +88,22 @@ class HttpResponse { int code; string body; protected: - /** - * Returns the sequence of the resonse as an HTTP sequence. + /** + * Returns the sequence of the resonse as an HTTP sequence. * * @param code the code of the response * @param body the body of the response */ static const string to_sequence(int code, const string& body=""); /** - * Parses the sequence and returns a response if successful or NULL if - * not. The caller MUST delete the response when no longer used. + * Parses the sequence and returns a response if successful or NULL if + * not. The caller MUST delete the response when no longer used. * - * @param sequence the sequence to be parsed into an HttpResponse object + * @param sequence the sequence to be parsed into an HttpResponse object */ static HttpResponse* from_sequence(const string& sequence); - /** - * Constructor. + /** + * Constructor. * * @param code the code of the response * @param body the body of the response @@ -111,18 +111,18 @@ class HttpResponse { HttpResponse(const int& code, const string& body=""); public: /** - * Returns the code. + * Returns the code. */ const int& get_code(); /** - * Returns the body. + * Returns the body. */ const string& get_body(); }; /** - * HttpRequestHandler handles HTTP requests on the server side. All handlers of - * AsyncHttpServer must inherit this class and should implement the supported + * HttpRequestHandler handles HTTP requests on the server side. All handlers of + * AsyncHttpServer must inherit this class and should implement the supported * methods accordingly. */ class HttpRequestHandler { @@ -130,12 +130,12 @@ class HttpRequestHandler { protected: /** * Replies to the client of the request with the code and the body. - * + * * @param request the HTTP request to reply to * @param code the code of the response * @param body the body of the response */ - void reply(HttpRequest* const request, const int& code, + void reply(HttpRequest* const request, const int& code, const string& body=""); public: /** @@ -146,24 +146,24 @@ class HttpRequestHandler { * Called when a HTTP GET request is available. The caller should * always manage to reply the request using method reply(). * - * @param request the HTTP request - * @param args the arguments associated with the regex of the handler + * @param request the HTTP request + * @param args the arguments associated with the regex of the handler */ - virtual void get(HttpRequest* const request, + virtual void get(HttpRequest* const request, const vector& args); /** - * Called when a HTTP POST request is available. The caller should + * Called when a HTTP POST request is available. The caller should * always manage to reply the request using method reply(). * - * @param request the HTTP request - * @param args the arguments associated with the regex of the handler + * @param request the HTTP request + * @param args the arguments associated with the regex of the handler */ - virtual void post(HttpRequest* const request, + virtual void post(HttpRequest* const request, const vector& args); }; /** - * HttpResponseHandler handles HTTP responses on the client side. All handlers + * HttpResponseHandler handles HTTP responses on the client side. All handlers * of AsyncHttpClient must inherit this class and implement method on_receive(). */ class HttpResponseHandler { @@ -173,7 +173,7 @@ class HttpResponseHandler { */ virtual ~HttpResponseHandler() {} /** - * Called when an HTTP response is available. + * Called when an HTTP response is available. * * @param response the HTTP response */ @@ -182,7 +182,7 @@ class HttpResponseHandler { /** * IOHandler handles IO events. It is the parent class of AsyncHttpClient and - * AsyncHttpServer. In general, you should not need to use this class. + * AsyncHttpServer. In general, you should not need to use this class. */ class IOHandler { protected: @@ -200,21 +200,21 @@ class IOHandler { */ virtual ~IOHandler() {} /** - * Called when network data from the file descriptor is available. + * Called when network data from the file descriptor is available. * - * @param fd the associted file descriptor + * @param fd the associated file descriptor */ virtual void on_read(const int& fd) = 0; /** - * Called when network buffer of the file descriptor is available. + * Called when network buffer of the file descriptor is available. * - * @param fd the associted file descriptor + * @param fd the associated file descriptor */ virtual void on_write(const int& fd) = 0; /** - * Called when the file descriptor is closed unexpectedly. + * Called when the file descriptor is closed unexpectedly. * - * @param fd the associted file descriptor + * @param fd the associated file descriptor */ virtual void on_close(const int& fd) = 0; }; @@ -229,15 +229,15 @@ class AsyncHttpClient : public IOHandler { map handlers; protected: /** - * Called when network data from the file descriptor is available. + * Called when network data from the file descriptor is available. * - * @param fd the associted file descriptor + * @param fd the associated file descriptor */ void on_read(const int& fd); /** - * Called when network buffer of the file descriptor is available. + * Called when network buffer of the file descriptor is available. * - * @param fd the associted file descriptor + * @param fd the associated file descriptor */ void on_write(const int& fd); /** @@ -255,7 +255,7 @@ class AsyncHttpClient : public IOHandler { AsyncHttpClient(IOLoop* const loop=NULL); /** * Makes a request and handles the response by the handler. Note that, - * unlike AsyncHttpServer, this class deletes (de-allocate the memory + * unlike AsyncHttpServer, this class deletes (de-allocate the memory * of) the handler after it is called. Raises an exception if an error * occurs. * @@ -267,12 +267,12 @@ class AsyncHttpClient : public IOHandler { * @param handler the handler to call when the response is received */ void fetch(const string& host, const int& port, const string& method, - const string& path, const string& body, + const string& path, const string& body, HttpResponseHandler* const handler); }; /** - * AsyncHttpServer is an async HTTP server driven by an IO loop. + * AsyncHttpServer is an async HTTP server driven by an IO loop. */ class AsyncHttpServer : public IOHandler { friend class HttpRequestHandler; @@ -282,7 +282,7 @@ class AsyncHttpServer : public IOHandler { vector > handlers; protected: /** - * Returns the handler whose pattern of interest matches the path + * Returns the handler whose pattern of interest matches the path * and NULL if the handler is not found. * * @param path the path of the request @@ -297,27 +297,27 @@ class AsyncHttpServer : public IOHandler { vector get_arguments(const string& path); /** * Writes to the client of the file descriptor the response. - * + * * @param fd the associated file descriptor * @param code the code of the response * @param body the body of the response */ void reply(const int& fd, const int& code, const string& body=""); /** - * Called when network data from the file descriptor is available. + * Called when network data from the file descriptor is available. * * @param fd the associated file descriptor */ void on_read(const int& fd); /** - * Called when network buffer of the file descriptor is available. + * Called when network buffer of the file descriptor is available. * * @param fd the associated file descriptor */ void on_write(const int& fd); /** - * Called when the file descriptor is closed unexpectedly. - * + * Called when the file descriptor is closed unexpectedly. + * * @param fd the associated file descriptor */ void on_close(const int& fd); @@ -331,7 +331,7 @@ class AsyncHttpServer : public IOHandler { */ AsyncHttpServer(const int& port, IOLoop* const loop=NULL); /** - * Destructor. + * Destructor. */ ~AsyncHttpServer(); /** @@ -342,10 +342,10 @@ class AsyncHttpServer : public IOHandler { * @param pattern the pattern associated with the handler * @param handler the request handler for reqeusts matching the pattern */ - void add_handler(const string& pattern, + void add_handler(const string& pattern, HttpRequestHandler* const handler); /** - * Removes the first found handler of the pattern and returns the + * Removes the first found handler of the pattern and returns the * removed handler or NULL if no handler is removed. * * @param pattern the pattern associated with the handler @@ -354,7 +354,7 @@ class AsyncHttpServer : public IOHandler { }; /** - * IOLoop wraps epoll Edge Triggered and notifies registered handlers of network + * IOLoop wraps epoll Edge Triggered and notifies registered handlers of network * events. Examples of handlers are AsyncHttpClient and AsyncHttpServer. * * TODO: (1) IOLoop runs forever at the moment. Maybe, adding timeout in start(). @@ -366,19 +366,19 @@ class IOLoop { static IOLoop* loop; public: /** - * Constructor. + * Constructor. */ IOLoop(); /** - * Sets the handler for either read events or write events on the file - * descriptor and returns the previously set handler or NULL if no - * handler was previously set. + * Sets the handler for either read events or write events on the file + * descriptor and returns the previously set handler or NULL if no + * handler was previously set. * * @param fd the associated file descriptor * @param handler the handler to notify of read events * @param mode 'r' for read events or else for write events */ - IOHandler* set_handler(const int& fd, IOHandler* const handler, + IOHandler* set_handler(const int& fd, IOHandler* const handler, char mode='r'); /** * Unsets the handler for all events on the file descriptor and returns @@ -388,11 +388,11 @@ class IOLoop { */ IOHandler* unset_handler(const int& fd); /** - * Starts the I/O loop forever. + * Starts the I/O loop forever. */ void start(); /** - * Returns the singleton instance of the I/O loop. + * Returns the singleton instance of the I/O loop. */ static IOLoop* instance(); };