Skip to content

Commit 4bd6036

Browse files
committed
Merge TASK-022: snake_case render_* overrides on http_resource
2 parents 8da6215 + 22f1c02 commit 4bd6036

37 files changed

Lines changed: 246 additions & 187 deletions

README.md

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -561,14 +561,14 @@ Once a webserver is created, you can manage its execution through the following
561561
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.
562562

563563
Given this, the `http_resource` class contains the following extensible methods (also called `handlers` or `render methods`):
564-
* _**std::shared_ptr<http_response>** http_resource::render_GET(**const http_request&** req):_ Invoked on an HTTP GET request.
565-
* _**std::shared_ptr<http_response>** http_resource::render_POST(**const http_request&** req):_ Invoked on an HTTP POST request.
566-
* _**std::shared_ptr<http_response>** http_resource::render_PUT(**const http_request&** req):_ Invoked on an HTTP PUT request.
567-
* _**std::shared_ptr<http_response>** http_resource::render_HEAD(**const http_request&** req):_ Invoked on an HTTP HEAD request.
568-
* _**std::shared_ptr<http_response>** http_resource::render_DELETE(**const http_request&** req):_ Invoked on an HTTP DELETE request.
569-
* _**std::shared_ptr<http_response>** http_resource::render_TRACE(**const http_request&** req):_ Invoked on an HTTP TRACE request.
570-
* _**std::shared_ptr<http_response>** http_resource::render_OPTIONS(**const http_request&** req):_ Invoked on an HTTP OPTIONS request.
571-
* _**std::shared_ptr<http_response>** http_resource::render_CONNECT(**const http_request&** req):_ Invoked on an HTTP CONNECT request.
564+
* _**std::shared_ptr<http_response>** http_resource::render_get(**const http_request&** req):_ Invoked on an HTTP GET request.
565+
* _**std::shared_ptr<http_response>** http_resource::render_post(**const http_request&** req):_ Invoked on an HTTP POST request.
566+
* _**std::shared_ptr<http_response>** http_resource::render_put(**const http_request&** req):_ Invoked on an HTTP PUT request.
567+
* _**std::shared_ptr<http_response>** http_resource::render_head(**const http_request&** req):_ Invoked on an HTTP HEAD request.
568+
* _**std::shared_ptr<http_response>** http_resource::render_delete(**const http_request&** req):_ Invoked on an HTTP DELETE request.
569+
* _**std::shared_ptr<http_response>** http_resource::render_trace(**const http_request&** req):_ Invoked on an HTTP TRACE request.
570+
* _**std::shared_ptr<http_response>** http_resource::render_options(**const http_request&** req):_ Invoked on an HTTP OPTIONS request.
571+
* _**std::shared_ptr<http_response>** http_resource::render_connect(**const http_request&** req):_ Invoked on an HTTP CONNECT request.
572572
* _**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`.
573573

574574
#### Example of implementation of render methods
@@ -579,7 +579,7 @@ Given this, the `http_resource` class contains the following extensible methods
579579

580580
class hello_world_resource : public http_resource {
581581
public:
582-
std::shared_ptr<http_response> render_GET(const http_request&) {
582+
std::shared_ptr<http_response> render_get(const http_request&) {
583583
return std::shared_ptr<http_response>(new string_response("GET: Hello, World!"));
584584
}
585585

@@ -913,7 +913,7 @@ You can also check this example on [github](https://github.com/etr/libhttpserver
913913

914914
class image_resource : public http_resource {
915915
public:
916-
std::shared_ptr<http_response> render_GET(const http_request&) {
916+
std::shared_ptr<http_response> render_get(const http_request&) {
917917
// binary_data could come from a camera capture, image library, etc.
918918
std::string binary_data = get_image_bytes_from_camera();
919919

@@ -1014,7 +1014,7 @@ Client certificate authentication uses a X.509 certificate from the client. This
10141014

10151015
class user_pass_resource : public httpserver::http_resource {
10161016
public:
1017-
std::shared_ptr<http_response> render_GET(const http_request& req) {
1017+
std::shared_ptr<http_response> render_get(const http_request& req) {
10181018
if (req.get_user() != "myuser" || req.get_pass() != "mypass") {
10191019
return std::shared_ptr<basic_auth_fail_response>(new basic_auth_fail_response("FAIL", "test@example.com"));
10201020
}
@@ -1060,7 +1060,7 @@ You can also use `check_digest_auth_digest` to verify against a pre-computed HA1
10601060

10611061
class digest_resource : public httpserver::http_resource {
10621062
public:
1063-
std::shared_ptr<http_response> render_GET(const http_request& req) {
1063+
std::shared_ptr<http_response> render_get(const http_request& req) {
10641064
if (req.get_digested_user() == "") {
10651065
return std::make_shared<digest_auth_fail_response>("FAIL", "test@example.com", MY_OPAQUE, true,
10661066
http_utils::http_ok, http_utils::text_plain, http_utils::digest_algorithm::MD5);
@@ -1112,14 +1112,14 @@ libhttpserver provides a centralized authentication mechanism that runs a single
11121112
// Resources no longer need authentication logic
11131113
class hello_resource : public http_resource {
11141114
public:
1115-
std::shared_ptr<http_response> render_GET(const http_request&) {
1115+
std::shared_ptr<http_response> render_get(const http_request&) {
11161116
return std::make_shared<string_response>("Hello, authenticated user!", 200, "text/plain");
11171117
}
11181118
};
11191119

11201120
class health_resource : public http_resource {
11211121
public:
1122-
std::shared_ptr<http_response> render_GET(const http_request&) {
1122+
std::shared_ptr<http_response> render_get(const http_request&) {
11231123
return std::make_shared<string_response>("OK", 200, "text/plain");
11241124
}
11251125
};
@@ -1185,7 +1185,7 @@ To enable client certificate authentication, configure your webserver with:
11851185
11861186
class secure_resource : public http_resource {
11871187
public:
1188-
std::shared_ptr<http_response> render_GET(const http_request& req) {
1188+
std::shared_ptr<http_response> render_get(const http_request& req) {
11891189
// Check if client provided a certificate
11901190
if (!req.has_client_certificate()) {
11911191
return std::make_shared<string_response>(
@@ -1409,7 +1409,7 @@ Additionally, the following utility methods are available:
14091409

14101410
class file_response_resource : public http_resource {
14111411
public:
1412-
std::shared_ptr<http_response> render_GET(const http_request& req) {
1412+
std::shared_ptr<http_response> render_get(const http_request& req) {
14131413
return std::shared_ptr<file_response>(new file_response("test_content", 200, "text/plain"));
14141414
}
14151415
};
@@ -1452,7 +1452,7 @@ You can also check this example on [github](https://github.com/etr/libhttpserver
14521452

14531453
class deferred_resource : public http_resource {
14541454
public:
1455-
std::shared_ptr<http_response> render_GET(const http_request& req) {
1455+
std::shared_ptr<http_response> render_get(const http_request& req) {
14561456
return std::shared_ptr<deferred_response<void> >(new deferred_response<void>(test_callback, nullptr, "cycle callback response"));
14571457
}
14581458
};
@@ -1509,7 +1509,7 @@ You can also check this example on [github](https://github.com/etr/libhttpserver
15091509
15101510
class deferred_resource : public http_resource {
15111511
public:
1512-
std::shared_ptr<http_response> render_GET(const http_request& req) {
1512+
std::shared_ptr<http_response> render_get(const http_request& req) {
15131513
std::shared_ptr<std::atomic<int> > closure_data(new std::atomic<int>(counter++));
15141514
return std::shared_ptr<deferred_response<std::atomic<int> > >(new deferred_response<std::atomic<int> >(test_callback, closure_data, "cycle callback response"));
15151515
}
@@ -1539,13 +1539,13 @@ You can also check this example on [github](https://github.com/etr/libhttpserver
15391539

15401540
class no_content_resource : public http_resource {
15411541
public:
1542-
std::shared_ptr<http_response> render_DELETE(const http_request&) {
1542+
std::shared_ptr<http_response> render_delete(const http_request&) {
15431543
// Return a 204 No Content response with no body
15441544
return std::make_shared<empty_response>(
15451545
http::http_utils::http_no_content);
15461546
}
15471547

1548-
std::shared_ptr<http_response> render_HEAD(const http_request&) {
1548+
std::shared_ptr<http_response> render_head(const http_request&) {
15491549
// Return a HEAD-only response with headers but no body
15501550
auto response = std::make_shared<empty_response>(
15511551
http::http_utils::http_ok,
@@ -1580,7 +1580,7 @@ You can also check this example on [github](https://github.com/etr/libhttpserver
15801580
15811581
class iovec_resource : public http_resource {
15821582
public:
1583-
std::shared_ptr<http_response> render_GET(const http_request&) {
1583+
std::shared_ptr<http_response> render_get(const http_request&) {
15841584
// Build a response from multiple separate buffers without copying
15851585
std::vector<std::string> parts;
15861586
parts.push_back("{\"header\": \"value\", ");
@@ -1619,7 +1619,7 @@ You can also check this example on [github](https://github.com/etr/libhttpserver
16191619

16201620
class pipe_resource : public http_resource {
16211621
public:
1622-
std::shared_ptr<http_response> render_GET(const http_request&) {
1622+
std::shared_ptr<http_response> render_get(const http_request&) {
16231623
int pipefd[2];
16241624
if (pipe(pipefd) == -1) {
16251625
return std::make_shared<string_response>("pipe failed", 500);
@@ -1704,7 +1704,7 @@ You can also check this example on [github](https://github.com/etr/libhttpserver
17041704

17051705
class hello_resource : public http_resource {
17061706
public:
1707-
std::shared_ptr<http_response> render_GET(const http_request&) {
1707+
std::shared_ptr<http_response> render_get(const http_request&) {
17081708
return std::make_shared<string_response>("Hello, World!");
17091709
}
17101710
};
@@ -1748,7 +1748,7 @@ You can also check this example on [github](https://github.com/etr/libhttpserver
17481748

17491749
class hello_resource : public http_resource {
17501750
public:
1751-
std::shared_ptr<http_response> render_GET(const http_request&) {
1751+
std::shared_ptr<http_response> render_get(const http_request&) {
17521752
return std::make_shared<string_response>("Hello from external event loop!");
17531753
}
17541754
};
@@ -1793,7 +1793,7 @@ You can also check this example on [github](https://github.com/etr/libhttpserver
17931793
17941794
class hello_resource : public http_resource {
17951795
public:
1796-
std::shared_ptr<http_response> render_GET(const http_request&) {
1796+
std::shared_ptr<http_response> render_get(const http_request&) {
17971797
return std::make_shared<string_response>("Hello, turbo world!");
17981798
}
17991799
};

examples/basic_authentication.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
class user_pass_resource : public httpserver::http_resource {
2727
public:
28-
std::shared_ptr<httpserver::http_response> render_GET(const httpserver::http_request& req) {
28+
std::shared_ptr<httpserver::http_response> render_get(const httpserver::http_request& req) {
2929
if (req.get_user() != "myuser" || req.get_pass() != "mypass") {
3030
return std::make_shared<httpserver::http_response>(
3131
httpserver::http_response::unauthorized("Basic", "test@example.com", "FAIL"));

examples/binary_buffer_response.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static std::string generate_png_data() {
5858

5959
class image_resource : public httpserver::http_resource {
6060
public:
61-
std::shared_ptr<httpserver::http_response> render_GET(const httpserver::http_request&) {
61+
std::shared_ptr<httpserver::http_response> render_get(const httpserver::http_request&) {
6262
// Build binary content as a std::string. The string can contain any
6363
// bytes — it is not limited to printable characters or null-terminated
6464
// C strings. The size is tracked internally by std::string::size().

examples/centralized_authentication.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ using httpserver::create_webserver;
3131
// Simple resource that doesn't need to handle auth itself
3232
class hello_resource : public http_resource {
3333
public:
34-
std::shared_ptr<http_response> render_GET(const http_request&) {
34+
std::shared_ptr<http_response> render_get(const http_request&) {
3535
return std::make_shared<http_response>(http_response::string("Hello, authenticated user!"));
3636
}
3737
};
3838

3939
class health_resource : public http_resource {
4040
public:
41-
std::shared_ptr<http_response> render_GET(const http_request&) {
41+
std::shared_ptr<http_response> render_get(const http_request&) {
4242
return std::make_shared<http_response>(http_response::string("OK"));
4343
}
4444
};

examples/client_cert_auth.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ std::set<std::string> allowed_fingerprints;
6767
// Resource that requires client certificate authentication
6868
class secure_resource : public httpserver::http_resource {
6969
public:
70-
std::shared_ptr<httpserver::http_response> render_GET(const httpserver::http_request& req) {
70+
std::shared_ptr<httpserver::http_response> render_get(const httpserver::http_request& req) {
7171
// Check if client provided a certificate
7272
if (!req.has_client_certificate()) {
7373
return std::make_shared<httpserver::http_response>(httpserver::http_response::string("Client certificate required").with_status(httpserver::http::http_utils::http_unauthorized));
@@ -123,7 +123,7 @@ class secure_resource : public httpserver::http_resource {
123123
// Public resource that shows certificate info but doesn't require it
124124
class info_resource : public httpserver::http_resource {
125125
public:
126-
std::shared_ptr<httpserver::http_response> render_GET(const httpserver::http_request& req) {
126+
std::shared_ptr<httpserver::http_response> render_get(const httpserver::http_request& req) {
127127
std::string response;
128128

129129
if (req.has_client_certificate()) {

examples/daemon_info.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
class hello_resource : public httpserver::http_resource {
2727
public:
28-
std::shared_ptr<httpserver::http_response> render_GET(const httpserver::http_request&) {
28+
std::shared_ptr<httpserver::http_response> render_get(const httpserver::http_request&) {
2929
return std::make_shared<httpserver::http_response>(httpserver::http_response::string("Hello, World!"));
3030
}
3131
};

examples/deferred_with_accumulator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ ssize_t test_callback(std::shared_ptr<std::atomic<int> > closure_data, char* buf
6161

6262
class deferred_resource : public httpserver::http_resource {
6363
public:
64-
std::shared_ptr<httpserver::http_response> render_GET(const httpserver::http_request&) {
64+
std::shared_ptr<httpserver::http_response> render_get(const httpserver::http_request&) {
6565
std::shared_ptr<std::atomic<int> > closure_data(new std::atomic<int>(counter++));
6666
std::string initial = "cycle callback response";
6767
return std::make_shared<httpserver::http_response>(

examples/digest_authentication.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
class digest_resource : public httpserver::http_resource {
2828
public:
29-
std::shared_ptr<httpserver::http_response> render_GET(const httpserver::http_request& req) {
29+
std::shared_ptr<httpserver::http_response> render_get(const httpserver::http_request& req) {
3030
using httpserver::http::http_utils;
3131
if (req.get_digested_user() == "") {
3232
return std::make_shared<httpserver::http_response>(httpserver::http_response::unauthorized("Digest", "test@example.com", "FAIL"));

examples/empty_response_example.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626

2727
class no_content_resource : public httpserver::http_resource {
2828
public:
29-
std::shared_ptr<httpserver::http_response> render_DELETE(const httpserver::http_request&) {
29+
std::shared_ptr<httpserver::http_response> render_delete(const httpserver::http_request&) {
3030
// Return a 204 No Content response with no body
3131
return std::make_shared<httpserver::http_response>(
3232
httpserver::http_response::empty());
3333
}
3434

35-
std::shared_ptr<httpserver::http_response> render_HEAD(const httpserver::http_request&) {
35+
std::shared_ptr<httpserver::http_response> render_head(const httpserver::http_request&) {
3636
// Return a HEAD-only response with headers but no body
3737
auto response = std::make_shared<httpserver::http_response>(
3838
httpserver::http_response::empty(MHD_RF_HEAD_ONLY_RESPONSE)

examples/external_event_loop.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void signal_handler(int) {
3333

3434
class hello_resource : public httpserver::http_resource {
3535
public:
36-
std::shared_ptr<httpserver::http_response> render_GET(const httpserver::http_request&) {
36+
std::shared_ptr<httpserver::http_response> render_get(const httpserver::http_request&) {
3737
return std::make_shared<httpserver::http_response>(httpserver::http_response::string("Hello from external event loop!"));
3838
}
3939
};

0 commit comments

Comments
 (0)