Skip to content
Open
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions tools/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4442,6 +4442,10 @@ struct server_res_generator : server_http_res {
status = 200;
data = safe_json_to_str(response_data);
}
void ok(const std::string & response_data) {
status = 200;
data = response_data;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's a safe solution as std::string can also be a valid json.

Instead, you can simply set the status and data directly in the handle_metrics without using ok() macro

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @ngxson Thank you for the guidance!

After thinking about it carefully, the situation you described with I don't think it's a safe solution as std::string can also be a valid json. does indeed exist.

I noticed that all handlers return by calling either error or ok to set the HTTP response. To make the code look more consistent, I avoided using this approach in the handler functions:

res->status = 200;
res->data = prometheus.str();

My original thinking was:

For JSON type return values, call this signature: ok(const json & response_data)
For string type return values, call this signature: ok(const std::string & response_data)

So I'm wondering which approach would be better - should I simply use:

res->status = 200;
res->data = prometheus.str();

Or should I define specific methods like:

void ok_json(const json & response_data);
void ok_text(const std::string & response_data);  
void ok_html(const std::string & response_data);

Or is there another approach you'd recommend?

I want to maintain consistency with the existing codebase while properly handling different response content types.

void error(const json & error_data) {
status = json_value(error_data, "code", 500);
data = safe_json_to_str({{ "error", error_data }});
Expand Down
Loading