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 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
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
82 changes: 42 additions & 40 deletions include/crow/http_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "crow/http_parser_merged.h"

#include "crow/common.h"
#include "crow/parser.h"
#include "crow/http_response.h"
#include "crow/logging.h"
Expand Down Expand Up @@ -418,47 +419,48 @@ 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"},
{101, "HTTP/1.1 101 Switching Protocols\r\n"},

{200, "HTTP/1.1 200 OK\r\n"},
{201, "HTTP/1.1 201 Created\r\n"},
{202, "HTTP/1.1 202 Accepted\r\n"},
{203, "HTTP/1.1 203 Non-Authoritative Information\r\n"},
{204, "HTTP/1.1 204 No Content\r\n"},
{205, "HTTP/1.1 205 Reset Content\r\n"},
{206, "HTTP/1.1 206 Partial Content\r\n"},

{300, "HTTP/1.1 300 Multiple Choices\r\n"},
{301, "HTTP/1.1 301 Moved Permanently\r\n"},
{302, "HTTP/1.1 302 Found\r\n"},
{303, "HTTP/1.1 303 See Other\r\n"},
{304, "HTTP/1.1 304 Not Modified\r\n"},
{307, "HTTP/1.1 307 Temporary Redirect\r\n"},
{308, "HTTP/1.1 308 Permanent Redirect\r\n"},

{400, "HTTP/1.1 400 Bad Request\r\n"},
{401, "HTTP/1.1 401 Unauthorized\r\n"},
{403, "HTTP/1.1 403 Forbidden\r\n"},
{404, "HTTP/1.1 404 Not Found\r\n"},
{405, "HTTP/1.1 405 Method Not Allowed\r\n"},
{407, "HTTP/1.1 407 Proxy Authentication Required\r\n"},
{409, "HTTP/1.1 409 Conflict\r\n"},
{410, "HTTP/1.1 410 Gone\r\n"},
{413, "HTTP/1.1 413 Payload Too Large\r\n"},
{415, "HTTP/1.1 415 Unsupported Media Type\r\n"},
{416, "HTTP/1.1 416 Range Not Satisfiable\r\n"},
{417, "HTTP/1.1 417 Expectation Failed\r\n"},
{428, "HTTP/1.1 428 Precondition Required\r\n"},
{429, "HTTP/1.1 429 Too Many Requests\r\n"},
{451, "HTTP/1.1 451 Unavailable For Legal Reasons\r\n"},

{500, "HTTP/1.1 500 Internal Server Error\r\n"},
{501, "HTTP/1.1 501 Not Implemented\r\n"},
{502, "HTTP/1.1 502 Bad Gateway\r\n"},
{503, "HTTP/1.1 503 Service Unavailable\r\n"},
{506, "HTTP/1.1 506 Variant Also Negotiates\r\n"},
{status::CONTINUE, "HTTP/1.1 100 Continue\r\n"},
{status::SWITCHING_PROTOCOLS, "HTTP/1.1 101 Switching Protocols\r\n"},

{status::OK, "HTTP/1.1 200 OK\r\n"},
{status::CREATED, "HTTP/1.1 201 Created\r\n"},
{status::ACCEPTED, "HTTP/1.1 202 Accepted\r\n"},
{status::NON_AUTHORITATIVE_INFORMATION, "HTTP/1.1 203 Non-Authoritative Information\r\n"},
{status::NO_CONTENT, "HTTP/1.1 204 No Content\r\n"},
{status::RESET_CONTENT, "HTTP/1.1 205 Reset Content\r\n"},
{status::PARTIAL_CONTENT, "HTTP/1.1 206 Partial Content\r\n"},

{status::MULTIPLE_CHOICES, "HTTP/1.1 300 Multiple Choices\r\n"},
{status::MOVED_PERMANENTLY, "HTTP/1.1 301 Moved Permanently\r\n"},
{status::FOUND, "HTTP/1.1 302 Found\r\n"},
{status::SEE_OTHER, "HTTP/1.1 303 See Other\r\n"},
{status::NOT_MODIFIED, "HTTP/1.1 304 Not Modified\r\n"},
{status::TEMPORARY_REDIRECT, "HTTP/1.1 307 Temporary Redirect\r\n"},
{status::PERMANENT_REDIRECT, "HTTP/1.1 308 Permanent Redirect\r\n"},

{status::BAD_REQUEST, "HTTP/1.1 400 Bad Request\r\n"},
{status::UNAUTHORIZED, "HTTP/1.1 401 Unauthorized\r\n"},
{status::FORBIDDEN, "HTTP/1.1 403 Forbidden\r\n"},
{status::NOT_FOUND, "HTTP/1.1 404 Not Found\r\n"},
{status::METHOD_NOT_ALLOWED, "HTTP/1.1 405 Method Not Allowed\r\n"},
{status::PROXY_AUTHENTICATION_REQUIRED, "HTTP/1.1 407 Proxy Authentication Required\r\n"},
{status::CONFLICT, "HTTP/1.1 409 Conflict\r\n"},
{status::GONE, "HTTP/1.1 410 Gone\r\n"},
{status::PAYLOAD_TOO_LARGE, "HTTP/1.1 413 Payload Too Large\r\n"},
{status::UNSUPPORTED_MEDIA_TYPE, "HTTP/1.1 415 Unsupported Media Type\r\n"},
{status::RANGE_NOT_SATISFIABLE, "HTTP/1.1 416 Range Not Satisfiable\r\n"},
{status::EXPECTATION_FAILED, "HTTP/1.1 417 Expectation Failed\r\n"},
{status::PRECONDITION_REQUIRED, "HTTP/1.1 428 Precondition Required\r\n"},
{status::TOO_MANY_REQUESTS, "HTTP/1.1 429 Too Many Requests\r\n"},
{status::UNAVAILABLE_FOR_LEGAL_REASONS, "HTTP/1.1 451 Unavailable For Legal Reasons\r\n"},

{status::INTERNAL_SERVER_ERROR, "HTTP/1.1 500 Internal Server Error\r\n"},
{status::NOT_IMPLEMENTED, "HTTP/1.1 501 Not Implemented\r\n"},
{status::BAD_GATEWAY, "HTTP/1.1 502 Bad Gateway\r\n"},
{status::SERVICE_UNAVAILABLE, "HTTP/1.1 503 Service Unavailable\r\n"},
{status::VARIANT_ALSO_NEGOTIATES, "HTTP/1.1 506 Variant Also Negotiates\r\n"},
};

static std::string seperator = ": ";
Expand Down