Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added status codes enum #230

Merged
merged 2 commits into from
Oct 2, 2021
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions docs/guides/routes.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ class a : public crow::returnable
}
}
```
<br><br>

## Response codes
**Introduced in: `master`**<br><br>

instead of assigning a response code, you can use the `crow::status` enum, for example you can replace `crow::response(200)` with `crow::response(crow::status::OK)`

##Catchall routes
**Introduced in: `v0.3`**<br><br>
Expand Down
10 changes: 10 additions & 0 deletions examples/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ int main()
return crow::response(os.str());
});

// Same as above, but using crow::status
CROW_ROUTE(app,"/hello/<int>")
([](int count){
if (count > 100)
return crow::response(crow::status::BAD_REQUEST);
std::ostringstream os;
os << count << " bottles of beer!";
return crow::response(os.str());
});

// To see it in action submit {ip}:18080/add/1/2 and you should receive 3 (exciting, isn't it)
CROW_ROUTE(app,"/add/<int>/<int>")
([](crow::response& res, int a, int b){
Expand Down
44 changes: 44 additions & 0 deletions include/crow/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,50 @@ namespace crow
// should not add an item below this line: used for array count
};

enum status
{
CONTINUE = 100,
SWITCHING_PROTOCOLS = 101,

OK = 200,
CREATED = 201,
ACCEPTED = 202,
NON_AUTHORITATIVE_INFORMATION = 203,
NO_CONTENT = 204,
RESET_CONTENT = 205,
PARTIAL_CONTENT = 206,

MULTIPLE_CHOICES = 300,
MOVED_PERMANENTLY = 301,
FOUND = 302,
SEE_OTHER = 303,
NOT_MODIFIED = 304,
TEMPORARY_REDIRECT = 307,
PERMANENT_REDIRECT = 308,

BAD_REQUEST = 400,
UNAUTHORIZED = 401,
FORBIDDEN = 403,
NOT_FOUND = 404,
METHOD_NOT_ALLOWED = 405,
PROXY_AUTHENTICATION_REQUIRED = 407,
CONFLICT = 409,
GONE = 410,
PAYLOAD_TOO_LARGE = 413,
UNSUPPORTED_MEDIA_TYPE = 415,
RANGE_NOT_SATISFIABLE = 416,
EXPECTATION_FAILED = 417,
PRECONDITION_REQUIRED = 428,
TOO_MANY_REQUESTS = 429,
UNAVAILABLE_FOR_LEGAL_REASONS = 451,

INTERNAL_SERVER_ERROR = 500,
NOT_IMPLEMENTED = 501,
BAD_GATEWAY = 502,
SERVICE_UNAVAILABLE = 503,
VARIANT_ALSO_NEGOTIATES = 506
};

inline std::string method_name(HTTPMethod method)
{
switch(method)
Expand Down
1 change: 1 addition & 0 deletions include/crow/http_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ namespace crow
return;
}

// Keep in sync with common.h/status
static std::unordered_map<int, std::string> statusCodes = {
{100, "HTTP/1.1 100 Continue\r\n"},
The-EDev marked this conversation as resolved.
Show resolved Hide resolved
{101, "HTTP/1.1 101 Switching Protocols\r\n"},
Expand Down