Update status code

This commit is contained in:
antao 2018-09-06 19:06:25 +08:00
parent b2d0595c60
commit 6fbfe28506
9 changed files with 117 additions and 53 deletions

View File

@ -271,7 +271,7 @@ void create_view::newViewSourceFile(std::ofstream &file,const std::string &class
}
file<<"\tauto res = HttpResponse::newHttpResponse();\n";
file<<"\tres->setStatusCode(HttpResponse::k200Ok);\n";
file<<"\tres->setStatusCode(HttpResponse::k200OK);\n";
file<<"#ifdef CONTENT_TYPE\n";
file<<"\tres->setContentTypeCode(CONTENT_TYPE);\n";
file<<"#else\n";

View File

@ -5,7 +5,7 @@ void TestController::asyncHandleHttpRequest(const HttpRequestPtr& req,const std:
//write your application logic here
auto resp=HttpResponse::newHttpResponse();
LOG_DEBUG<<"!!!!!!!!!!1";
resp->setStatusCode(HttpResponse::k200Ok);
resp->setStatusCode(HttpResponse::k200OK);
resp->setContentTypeCode(CT_TEXT_HTML);
resp->setBody("hello!!!");
callback(resp);

View File

@ -63,16 +63,47 @@ namespace drogon
{
public:
enum HttpStatusCode {
kUnknown,
k101 = 101,
k200Ok = 200,
k204NoContent = 204,
k301MovedPermanently = 301,
k302Found = 302,
k304NotModified = 304,
k400BadRequest = 400,
k404NotFound = 404,
k405MethodNotAllowed = 405,
kUnknown=0,
k100Continue=100,
k101SwitchingProtocols=101,
k200OK=200,
k201Created=201,
k202Accepted=202,
k203NonAuthoritativeInformation=203,
k204NoContent=204,
k205ResetContent=205,
k206PartialContent=206,
k300MultipleChoices=300,
k301MovedPermanently=301,
k302Found=302,
k303SeeOther=303,
k304NotModified=304,
k305UseProxy=305,
k307TemporaryRedirect=307,
k400BadRequest=400,
k401Unauthorized=401,
k402PaymentRequired=402,
k403Forbidden=403,
k404NotFound=404,
k405MethodNotAllowed=405,
k406NotAcceptable=406,
k407ProxyAuthenticationRequired=407,
k408RequestTimeout=408,
k409Conflict=409,
k410Gone=410,
k411LengthRequired=411,
k412PreconditionFailed=412,
k413RequestEntityTooLarge=413,
k414RequestURITooLarge=414,
k415UnsupportedMediaType=415,
k416Requestedrangenotsatisfiable=416,
k417ExpectationFailed=417,
k500InternalServerError=500,
k501NotImplemented=501,
k502BadGateway=502,
k503ServiceUnavailable=503,
k504GatewayTimeout=504,
k505HTTPVersionnotsupported=505,
};
enum Version {

View File

@ -434,7 +434,7 @@ void HttpAppFrameworkImpl::onNewWebsockRequest(const HttpRequestPtr& req,
SHA1(reinterpret_cast<const unsigned char *>(wsKey.c_str()), wsKey.length(), accKey);
auto base64Key=base64Encode(accKey,SHA_DIGEST_LENGTH);
auto resp=HttpResponse::newHttpResponse();
resp->setStatusCode(HttpResponse::k101,"Switching Protocols");
resp->setStatusCode(HttpResponse::k101SwitchingProtocols);
resp->addHeader("Upgrade","websocket");
resp->addHeader("Connection","Upgrade");
resp->addHeader("Sec-WebSocket-Accept",base64Key);
@ -710,7 +710,7 @@ void HttpAppFrameworkImpl::readSendFile(const std::string& filePath,const HttpRe
pbuf->sgetn (&str[0],size);
infile.close();
resp->setStatusCode(HttpResponse::k200Ok);
resp->setStatusCode(HttpResponse::k200OK);
LOG_INFO << "file len:" << str.length();
resp->setBody(std::move(str));

View File

@ -209,29 +209,9 @@ bool HttpContext::processResponseLine(const char *begin, const char *end)
std::string status_code(start, space - start);
std::string status_message(space + 1, end - space - 1);
LOG_TRACE << status_code << " " << status_message;
switch (atoi(status_code.c_str()))
{
case 200:
response_.setStatusCode(HttpResponse::k200Ok, status_message);
break;
case 301:
response_.setStatusCode(HttpResponse::k301MovedPermanently, status_message);
break;
case 302:
response_.setStatusCode(HttpResponse::k302Found, status_message);
break;
case 304:
response_.setStatusCode(HttpResponse::k304NotModified, status_message);
break;
case 400:
response_.setStatusCode(HttpResponse::k400BadRequest, status_message);
break;
case 404:
response_.setStatusCode(HttpResponse::k404NotFound, status_message);
break;
default:
return false;
}
auto code=atoi(status_code.c_str());
response_.setStatusCode(HttpResponse::HttpStatusCode(code),status_message);
return true;
}
return false;

View File

@ -208,7 +208,7 @@ namespace drogon
void addHeader(const char* start, const char* colon, const char* end)
{
std::string field(start, colon);
//field name is case-insensitive.so we transform it to lower;
//field name is case-insensitive.so we transform it to lower;(rfc2616-4.2)
std::transform(field.begin(),field.end(),field.begin(),::tolower);
++colon;
while (colon < end && isspace(*colon)) {

View File

@ -34,7 +34,7 @@ using namespace drogon;
HttpResponsePtr HttpResponse::newHttpResponse()
{
auto res = std::make_shared<HttpResponseImpl>();
res->setStatusCode(HttpResponse::k200Ok);
res->setStatusCode(HttpResponse::k200OK);
res->setContentTypeCode(CT_TEXT_HTML);
return res;
}
@ -42,7 +42,7 @@ HttpResponsePtr HttpResponse::newHttpResponse()
HttpResponsePtr HttpResponse::newHttpJsonResponse(const Json::Value &data)
{
auto res=std::make_shared<HttpResponseImpl>();
res->setStatusCode(HttpResponse::k200Ok);
res->setStatusCode(HttpResponse::k200OK);
res->setContentTypeCode(CT_APPLICATION_JSON);
Json::StreamWriterBuilder builder;
builder["commentStyle"] = "None";
@ -213,33 +213,86 @@ const std::string HttpResponseImpl::web_content_type_to_string(uint8_t contentty
const std::string HttpResponseImpl::web_response_code_to_string(int code)
{
switch(code) {
case 100:
return "Continue";
case 101:
return "Switching Protocols";
case 200:
return "OK";
case 201:
return "Created";
case 202:
return "Accepted";
case 203:
return "Non-Authoritative Information";
case 204:
return "No Content";
case 205:
return "Reset Content";
case 206:
return "Partial Content";
case 300:
return "Multiple Choices";
case 301:
return "Moved Permanently";
case 302:
return "Found";
case 303:
return "See Other";
case 304:
return "Not Modified";
case 305:
return "Use Proxy";
case 307:
return "Temporary Redirect";
case 400:
return "Bad Request";
case 401:
return "Unauthorized";
case 402:
return "Payment Required";
case 403:
return "Forbidden";
case 404:
return "Not Found";
case 405:
return "Method Not Allowed";
case 406:
return "Not Acceptable";
case 407:
return "Proxy Authentication Required";
case 408:
return "Request Time-out";
case 409:
return "Conflict";
case 410:
return "Gone";
case 411:
return "Length Required";
case 412:
return "Preconditions Failed";
return "Precondition Failed";
case 413:
return "Request Entity Too Large";
case 414:
return "Request-URI Too Large";
case 415:
return "Unsupported Media Type";
case 416:
return "Requested range not satisfiable";
case 417:
return "Expectation Failed";
case 500:
return "Internal Server Error";
case 501:
return "Not Implemented";
case 502:
return "Bad Gateway";
case 503:
return "Service Unavailable";
case 504:
return "Gateway Time-out";
case 505:
return "HTTP Version not supported";
default:
if(code >= 100 && code < 200)
return "Informational";

View File

@ -125,7 +125,7 @@ void HttpServer::onMessage(const TcpConnectionPtr& conn,
auto wsConn=std::make_shared<WebSocketConnectionImpl>(conn);
newWebsocketCallback_(context->request(),[=](const HttpResponsePtr &resp) mutable
{
if(resp->statusCode()==HttpResponse::k101)
if(resp->statusCode()==HttpResponse::k101SwitchingProtocols)
{
context->setWebsockConnection(wsConn);
}
@ -166,7 +166,7 @@ void HttpServer::onRequest(const TcpConnectionPtr& conn, const HttpRequestPtr& r
if(!response)
return;
response->setCloseConnection(_close);
//if the request method is HEAD,remove the body of response
//if the request method is HEAD,remove the body of response(rfc2616-9.4)
if(_isHeadMethod)
response->setBody(std::string());
if(response->getContentTypeCode()<CT_APPLICATION_OCTET_STREAM&&

View File

@ -25,7 +25,7 @@ HttpResponsePtr NotFound::genHttpResponse(const HttpViewData& NotFound_view_data
NotFound_tmp_stream << "<!-- a padding to disable MSIE and Chrome friendly error page -->\n";
NotFound_tmp_stream << "<!-- a padding to disable MSIE and Chrome friendly error page -->\n";
auto res = HttpResponse::newHttpResponse();
res->setStatusCode(HttpResponse::k200Ok);
res->setStatusCode(HttpResponse::k200OK);
#ifdef CONTENT_TYPE
res->setContentTypeCode(CONTENT_TYPE);
#else