Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 29 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ The most basic example of creating a server and handling a requests for the path

class hello_world_resource : public http_resource {
public:
const std::shared_ptr<http_response> render(const http_request&) {
std::shared_ptr<http_response> render(const http_request&) {
return std::shared_ptr<http_response>(new string_response("Hello, World!"));
}
};
Expand Down Expand Up @@ -219,17 +219,17 @@ In all these 3 cases libhttpserver would provide a standard HTTP response to the

using namespace httpserver;

const std::shared_ptr<http_response> not_found_custom(const http_request& req) {
std::shared_ptr<http_response> not_found_custom(const http_request& req) {
return std::shared_ptr<string_response>(new string_response("Not found custom", 404, "text/plain"));
}

const std::shared_ptr<http_response> not_allowed_custom(const http_request& req) {
std::shared_ptr<http_response> not_allowed_custom(const http_request& req) {
return std::shared_ptr<string_response>(new string_response("Not allowed custom", 405, "text/plain"));
}

class hello_world_resource : public http_resource {
public:
const std::shared_ptr<http_response> render(const http_request&) {
std::shared_ptr<http_response> render(const http_request&) {
return std::shared_ptr<http_response>(new string_response("Hello, World!"));
}
};
Expand Down Expand Up @@ -275,7 +275,7 @@ You can also check this example on [github](https://github.com/etr/libhttpserver

class hello_world_resource : public http_resource {
public:
const std::shared_ptr<http_response> render(const http_request&) {
std::shared_ptr<http_response> render(const http_request&) {
return std::shared_ptr<http_response>(new string_response("Hello, World!"));
}
};
Expand Down Expand Up @@ -321,7 +321,7 @@ You can also check this example on [github](https://github.com/etr/libhttpserver

class hello_world_resource : public http_resource {
public:
const std::shared_ptr<http_response> render(const http_request&) {
std::shared_ptr<http_response> render(const http_request&) {
return std::shared_ptr<http_response>(new string_response("Hello, World!"));
}
};
Expand Down Expand Up @@ -391,15 +391,15 @@ Once a webserver is created, you can manage its execution through the following
The `http_resource` class represents a logical collection of HTTP methods that will be associated to a URL when registered on the webserver. The class is **designed for extension** and it is where most of your code should ideally live. When the webserver matches a request against a resource (see: [resource registration](#registering-resources)), the method correspondent to the one in the request (GET, POST, etc..) (see below) is called on the resource.

Given this, the `http_resource` class contains the following extensible methods (also called `handlers` or `render methods`):
* _**const std::shared_ptr<http_response>** http_resource::render_GET(**const http_request&** req):_ Invoked on an HTTP GET request.
* _**const std::shared_ptr<http_response>** http_resource::render_POST(**const http_request&** req):_ Invoked on an HTTP POST request.
* _**const std::shared_ptr<http_response>** http_resource::render_PUT(**const http_request&** req):_ Invoked on an HTTP PUT request.
* _**const std::shared_ptr<http_response>** http_resource::render_HEAD(**const http_request&** req):_ Invoked on an HTTP HEAD request.
* _**const std::shared_ptr<http_response>** http_resource::render_DELETE(**const http_request&** req):_ Invoked on an HTTP DELETE request.
* _**const std::shared_ptr<http_response>** http_resource::render_TRACE(**const http_request&** req):_ Invoked on an HTTP TRACE request.
* _**const std::shared_ptr<http_response>** http_resource::render_OPTIONS(**const http_request&** req):_ Invoked on an HTTP OPTIONS request.
* _**const std::shared_ptr<http_response>** http_resource::render_CONNECT(**const http_request&** req):_ Invoked on an HTTP CONNECT request.
* _**const std::shared_ptr<http_response>** http_resource::render(**const http_request&** req):_ Invoked as a backup method if the matching method is not implemented. It can be used whenever you want all the invocations on a URL to activate the same behavior regardless of the HTTP method requested. The default implementation of the `render` method returns an empty response with a `404`.
* _**std::shared_ptr<http_response>** http_resource::render_GET(**const http_request&** req):_ Invoked on an HTTP GET request.
* _**std::shared_ptr<http_response>** http_resource::render_POST(**const http_request&** req):_ Invoked on an HTTP POST request.
* _**std::shared_ptr<http_response>** http_resource::render_PUT(**const http_request&** req):_ Invoked on an HTTP PUT request.
* _**std::shared_ptr<http_response>** http_resource::render_HEAD(**const http_request&** req):_ Invoked on an HTTP HEAD request.
* _**std::shared_ptr<http_response>** http_resource::render_DELETE(**const http_request&** req):_ Invoked on an HTTP DELETE request.
* _**std::shared_ptr<http_response>** http_resource::render_TRACE(**const http_request&** req):_ Invoked on an HTTP TRACE request.
* _**std::shared_ptr<http_response>** http_resource::render_OPTIONS(**const http_request&** req):_ Invoked on an HTTP OPTIONS request.
* _**std::shared_ptr<http_response>** http_resource::render_CONNECT(**const http_request&** req):_ Invoked on an HTTP CONNECT request.
* _**std::shared_ptr<http_response>** http_resource::render(**const http_request&** req):_ Invoked as a backup method if the matching method is not implemented. It can be used whenever you want all the invocations on a URL to activate the same behavior regardless of the HTTP method requested. The default implementation of the `render` method returns an empty response with a `404`.

#### Example of implementation of render methods
```cpp
Expand All @@ -409,11 +409,11 @@ Given this, the `http_resource` class contains the following extensible methods

class hello_world_resource : public http_resource {
public:
const std::shared_ptr<http_response> render_GET(const http_request&) {
std::shared_ptr<http_response> render_GET(const http_request&) {
return std::shared_ptr<http_response>(new string_response("GET: Hello, World!"));
}

const std::shared_ptr<http_response> render(const http_request&) {
std::shared_ptr<http_response> render(const http_request&) {
return std::shared_ptr<http_response>(new string_response("OTHER: Hello, World!"));
}
};
Expand Down Expand Up @@ -449,7 +449,7 @@ The base `http_resource` class has a set of methods that can be used to allow an

class hello_world_resource : public http_resource {
public:
const std::shared_ptr<http_response> render(const http_request&) {
std::shared_ptr<http_response> render(const http_request&) {
return std::shared_ptr<http_response>(new string_response("Hello, World!"));
}
};
Expand Down Expand Up @@ -496,21 +496,21 @@ There are essentially four ways to specify an endpoint string:

class hello_world_resource : public http_resource {
public:
const std::shared_ptr<http_response> render(const http_request&) {
std::shared_ptr<http_response> render(const http_request&) {
return std::shared_ptr<http_response>(new string_response("Hello, World!"));
}
};

class handling_multiple_resource : public http_resource {
public:
const std::shared_ptr<http_response> render(const http_request& req) {
std::shared_ptr<http_response> render(const http_request& req) {
return std::shared_ptr<http_response>(new string_response("Your URL: " + req.get_path()));
}
};

class url_args_resource : public http_resource {
public:
const std::shared_ptr<http_response> render(const http_request& req) {
std::shared_ptr<http_response> render(const http_request& req) {
return std::shared_ptr<http_response>(new string_response("ARGS: " + req.get_arg("arg1") + " and " + req.get_arg("arg2")));
}
};
Expand Down Expand Up @@ -596,7 +596,7 @@ Details on the `http::file_info` structure.

class hello_world_resource : public http_resource {
public:
const std::shared_ptr<http_response> render(const http_request& req) {
std::shared_ptr<http_response> render(const http_request& req) {
return std::shared_ptr<http_response>(new string_response("Hello: " + req.get_arg("name")));
}
};
Expand Down Expand Up @@ -649,7 +649,7 @@ The `http_response` class offers an additional set of methods to "decorate" your

class hello_world_resource : public http_resource {
public:
const std::shared_ptr<http_response> render(const http_request&) {
std::shared_ptr<http_response> render(const http_request&) {
std::shared_ptr<http_response> response = std::shared_ptr<http_response>(new string_response("Hello, World!"));
response->with_header("MyHeader", "MyValue");
return response;
Expand Down Expand Up @@ -707,7 +707,7 @@ Examples of valid IPs include:

class hello_world_resource : public http_resource {
public:
const std::shared_ptr<http_response> render(const http_request&) {
std::shared_ptr<http_response> render(const http_request&) {
return std::shared_ptr<http_response>(new string_response("Hello, World!"));
}
};
Expand Down Expand Up @@ -750,7 +750,7 @@ Client certificate authentication uses a X.509 certificate from the client. This

class user_pass_resource : public httpserver::http_resource {
public:
const std::shared_ptr<http_response> render_GET(const http_request& req) {
std::shared_ptr<http_response> render_GET(const http_request& req) {
if (req.get_user() != "myuser" || req.get_pass() != "mypass") {
return std::shared_ptr<basic_auth_fail_response>(new basic_auth_fail_response("FAIL", "test@example.com"));
}
Expand Down Expand Up @@ -786,7 +786,7 @@ You can also check this example on [github](https://github.com/etr/libhttpserver

class digest_resource : public httpserver::http_resource {
public:
const std::shared_ptr<http_response> render_GET(const http_request& req) {
std::shared_ptr<http_response> render_GET(const http_request& req) {
if (req.get_digested_user() == "") {
return std::shared_ptr<digest_auth_fail_response>(new digest_auth_fail_response("FAIL", "test@example.com", MY_OPAQUE, true));
}
Expand Down Expand Up @@ -835,7 +835,7 @@ libhttpserver provides a set of constants to help you develop your HTTP server.

class file_response_resource : public http_resource {
public:
const std::shared_ptr<http_response> render_GET(const http_request& req) {
std::shared_ptr<http_response> render_GET(const http_request& req) {
return std::shared_ptr<file_response>(new file_response("test_content", 200, "text/plain"));
}
};
Expand Down Expand Up @@ -878,7 +878,7 @@ You can also check this example on [github](https://github.com/etr/libhttpserver

class deferred_resource : public http_resource {
public:
const std::shared_ptr<http_response> render_GET(const http_request& req) {
std::shared_ptr<http_response> render_GET(const http_request& req) {
return std::shared_ptr<deferred_response<void> >(new deferred_response<void>(test_callback, nullptr, "cycle callback response"));
}
};
Expand Down Expand Up @@ -935,7 +935,7 @@ You can also check this example on [github](https://github.com/etr/libhttpserver

class deferred_resource : public http_resource {
public:
const std::shared_ptr<http_response> render_GET(const http_request& req) {
std::shared_ptr<http_response> render_GET(const http_request& req) {
std::shared_ptr<std::atomic<int> > closure_data(new std::atomic<int>(counter++));
return std::shared_ptr<deferred_response<std::atomic<int> > >(new deferred_response<std::atomic<int> >(test_callback, closure_data, "cycle callback response"));
}
Expand Down
2 changes: 1 addition & 1 deletion examples/allowing_disallowing_methods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

class hello_world_resource : public httpserver::http_resource {
public:
const std::shared_ptr<httpserver::http_response> render(const httpserver::http_request&) {
std::shared_ptr<httpserver::http_response> render(const httpserver::http_request&) {
return std::shared_ptr<httpserver::http_response>(new httpserver::string_response("Hello, World!"));
}
};
Expand Down
2 changes: 1 addition & 1 deletion examples/basic_authentication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

class user_pass_resource : public httpserver::http_resource {
public:
const std::shared_ptr<httpserver::http_response> render_GET(const httpserver::http_request& req) {
std::shared_ptr<httpserver::http_response> render_GET(const httpserver::http_request& req) {
if (req.get_user() != "myuser" || req.get_pass() != "mypass") {
return std::shared_ptr<httpserver::basic_auth_fail_response>(new httpserver::basic_auth_fail_response("FAIL", "test@example.com"));
}
Expand Down
2 changes: 1 addition & 1 deletion examples/benchmark_nodelay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class hello_world_resource : public httpserver::http_resource {
resp(resp) {
}

const std::shared_ptr<httpserver::http_response> render(const httpserver::http_request&) {
std::shared_ptr<httpserver::http_response> render(const httpserver::http_request&) {
return resp;
}

Expand Down
2 changes: 1 addition & 1 deletion examples/benchmark_select.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class hello_world_resource : public httpserver::http_resource {
resp(resp) {
}

const std::shared_ptr<httpserver::http_response> render(const httpserver::http_request&) {
std::shared_ptr<httpserver::http_response> render(const httpserver::http_request&) {
return resp;
}

Expand Down
2 changes: 1 addition & 1 deletion examples/benchmark_threads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class hello_world_resource : public httpserver::http_resource {
resp(resp) {
}

const std::shared_ptr<httpserver::http_response> render(const httpserver::http_request&) {
std::shared_ptr<httpserver::http_response> render(const httpserver::http_request&) {
return resp;
}

Expand Down
2 changes: 1 addition & 1 deletion examples/custom_access_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void custom_access_log(const std::string& url) {

class hello_world_resource : public httpserver::http_resource {
public:
const std::shared_ptr<httpserver::http_response> render(const httpserver::http_request&) {
std::shared_ptr<httpserver::http_response> render(const httpserver::http_request&) {
return std::shared_ptr<httpserver::http_response>(new httpserver::string_response("Hello, World!"));
}
};
Expand Down
6 changes: 3 additions & 3 deletions examples/custom_error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@

#include <httpserver.hpp>

const std::shared_ptr<httpserver::http_response> not_found_custom(const httpserver::http_request&) {
std::shared_ptr<httpserver::http_response> not_found_custom(const httpserver::http_request&) {
return std::shared_ptr<httpserver::string_response>(new httpserver::string_response("Not found custom", 404, "text/plain"));
}

const std::shared_ptr<httpserver::http_response> not_allowed_custom(const httpserver::http_request&) {
std::shared_ptr<httpserver::http_response> not_allowed_custom(const httpserver::http_request&) {
return std::shared_ptr<httpserver::string_response>(new httpserver::string_response("Not allowed custom", 405, "text/plain"));
}

class hello_world_resource : public httpserver::http_resource {
public:
const std::shared_ptr<httpserver::http_response> render(const httpserver::http_request&) {
std::shared_ptr<httpserver::http_response> render(const httpserver::http_request&) {
return std::shared_ptr<httpserver::http_response>(new httpserver::string_response("Hello, World!"));
}
};
Expand Down
2 changes: 1 addition & 1 deletion examples/deferred_with_accumulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ ssize_t test_callback(std::shared_ptr<std::atomic<int> > closure_data, char* buf

class deferred_resource : public httpserver::http_resource {
public:
const std::shared_ptr<httpserver::http_response> render_GET(const httpserver::http_request&) {
std::shared_ptr<httpserver::http_response> render_GET(const httpserver::http_request&) {
std::shared_ptr<std::atomic<int> > closure_data(new std::atomic<int>(counter++));
return std::shared_ptr<httpserver::deferred_response<std::atomic<int> > >(new httpserver::deferred_response<std::atomic<int> >(test_callback, closure_data, "cycle callback response"));
}
Expand Down
2 changes: 1 addition & 1 deletion examples/digest_authentication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

class digest_resource : public httpserver::http_resource {
public:
const std::shared_ptr<httpserver::http_response> render_GET(const httpserver::http_request& req) {
std::shared_ptr<httpserver::http_response> render_GET(const httpserver::http_request& req) {
if (req.get_digested_user() == "") {
return std::shared_ptr<httpserver::digest_auth_fail_response>(new httpserver::digest_auth_fail_response("FAIL", "test@example.com", MY_OPAQUE, true));
} else {
Expand Down
4 changes: 2 additions & 2 deletions examples/file_upload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

class file_upload_resource : public httpserver::http_resource {
public:
const std::shared_ptr<httpserver::http_response> render_GET(const httpserver::http_request&) {
std::shared_ptr<httpserver::http_response> render_GET(const httpserver::http_request&) {
std::string get_response = "<html>\n";
get_response += " <body>\n";
get_response += " <form method=\"POST\" enctype=\"multipart/form-data\">\n";
Expand All @@ -40,7 +40,7 @@ class file_upload_resource : public httpserver::http_resource {
return std::shared_ptr<httpserver::http_response>(new httpserver::string_response(get_response, 200, "text/html"));
}

const std::shared_ptr<httpserver::http_response> render_POST(const httpserver::http_request& req) {
std::shared_ptr<httpserver::http_response> render_POST(const httpserver::http_request& req) {
std::string post_response = "<html>\n";
post_response += "<head>\n";
post_response += " <style>\n";
Expand Down
4 changes: 2 additions & 2 deletions examples/handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@

class hello_world_resource : public httpserver::http_resource {
public:
const std::shared_ptr<httpserver::http_response> render_GET(const httpserver::http_request&) {
std::shared_ptr<httpserver::http_response> render_GET(const httpserver::http_request&) {
return std::shared_ptr<httpserver::http_response>(new httpserver::string_response("GET: Hello, World!"));
}

const std::shared_ptr<httpserver::http_response> render(const httpserver::http_request&) {
std::shared_ptr<httpserver::http_response> render(const httpserver::http_request&) {
return std::shared_ptr<httpserver::http_response>(new httpserver::string_response("OTHER: Hello, World!"));
}
};
Expand Down
2 changes: 1 addition & 1 deletion examples/hello_with_get_arg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

class hello_world_resource : public httpserver::http_resource {
public:
const std::shared_ptr<httpserver::http_response> render(const httpserver::http_request& req) {
std::shared_ptr<httpserver::http_response> render(const httpserver::http_request& req) {
return std::shared_ptr<httpserver::http_response>(new httpserver::string_response("Hello: " + req.get_arg("name")));
}
};
Expand Down
4 changes: 2 additions & 2 deletions examples/hello_world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@

class hello_world_resource : public httpserver::http_resource {
public:
const std::shared_ptr<httpserver::http_response> render(const httpserver::http_request&);
std::shared_ptr<httpserver::http_response> render(const httpserver::http_request&);
void set_some_data(const std::string &s) {data = s;}
std::string data;
};

// Using the render method you are able to catch each type of request you receive
const std::shared_ptr<httpserver::http_response> hello_world_resource::render(const httpserver::http_request& req) {
std::shared_ptr<httpserver::http_response> hello_world_resource::render(const httpserver::http_request& req) {
// It is possible to store data inside the resource object that can be altered through the requests
std::cout << "Data was: " << data << std::endl;
std::string datapar = req.get_arg("data");
Expand Down
Loading