@@ -54,8 +54,19 @@ static string rfc1123Time()
5454 return DateTimeStrFormat (" %a, %d %b %Y %H:%M:%S +0000" , GetTime ());
5555}
5656
57- string HTTPReply (int nStatus, const string& strMsg, bool keepalive,
58- bool headersOnly, const char *contentType)
57+ static const char *httpStatusDescription (int nStatus)
58+ {
59+ switch (nStatus) {
60+ case HTTP_OK: return " OK" ;
61+ case HTTP_BAD_REQUEST: return " Bad Request" ;
62+ case HTTP_FORBIDDEN: return " Forbidden" ;
63+ case HTTP_NOT_FOUND: return " Not Found" ;
64+ case HTTP_INTERNAL_SERVER_ERROR: return " Internal Server Error" ;
65+ default : return " " ;
66+ }
67+ }
68+
69+ string HTTPError (int nStatus, bool keepalive, bool headersOnly)
5970{
6071 if (nStatus == HTTP_UNAUTHORIZED)
6172 return strprintf (" HTTP/1.0 401 Authorization Required\r\n "
@@ -75,20 +86,13 @@ string HTTPReply(int nStatus, const string& strMsg, bool keepalive,
7586 " <BODY><H1>401 Unauthorized.</H1></BODY>\r\n "
7687 " </HTML>\r\n " , rfc1123Time (), FormatFullVersion ());
7788
78- const char *cStatus;
79- if (nStatus == HTTP_OK) cStatus = " OK" ;
80- else if (nStatus == HTTP_BAD_REQUEST) cStatus = " Bad Request" ;
81- else if (nStatus == HTTP_FORBIDDEN) cStatus = " Forbidden" ;
82- else if (nStatus == HTTP_NOT_FOUND) cStatus = " Not Found" ;
83- else if (nStatus == HTTP_INTERNAL_SERVER_ERROR) cStatus = " Internal Server Error" ;
84- else cStatus = " " ;
85-
86- bool useInternalContent = false ;
87- if (nStatus != HTTP_OK) {
88- contentType = " text/plain" ;
89- useInternalContent = true ;
90- }
89+ return HTTPReply (nStatus, httpStatusDescription (nStatus), keepalive,
90+ headersOnly, " text/plain" );
91+ }
9192
93+ string HTTPReply (int nStatus, const string& strMsg, bool keepalive,
94+ bool headersOnly, const char *contentType)
95+ {
9296 return strprintf (
9397 " HTTP/1.1 %d %s\r\n "
9498 " Date: %s\r\n "
@@ -99,14 +103,14 @@ string HTTPReply(int nStatus, const string& strMsg, bool keepalive,
99103 " \r\n "
100104 " %s" ,
101105 nStatus,
102- cStatus ,
106+ httpStatusDescription (nStatus) ,
103107 rfc1123Time (),
104108 keepalive ? " keep-alive" : " close" ,
105- strMsg.size (),
109+ (headersOnly ? 0 : strMsg.size () ),
106110 contentType,
107111 FormatFullVersion (),
108- (headersOnly ? " " :
109- (useInternalContent ? cStatus : strMsg. c_str ())) );
112+ (headersOnly ? " " : strMsg. c_str ())
113+ );
110114}
111115
112116bool ReadHTTPRequestLine (std::basic_istream<char >& stream, int &proto,
0 commit comments