Skip to content

Commit

Permalink
Merge branch 'master' into revamped_setup
Browse files Browse the repository at this point in the history
  • Loading branch information
The-EDev committed Oct 2, 2021
2 parents de9e860 + 6b71f92 commit 72e33c7
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 40 deletions.
6 changes: 6 additions & 0 deletions docs/guides/routes.md
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
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
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
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 @@ -421,47 +422,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

0 comments on commit 72e33c7

Please sign in to comment.