|
|
@ -54,8 +54,19 @@ static string rfc1123Time() |
|
|
|
return DateTimeStrFormat("%a, %d %b %Y %H:%M:%S +0000", GetTime()); |
|
|
|
return DateTimeStrFormat("%a, %d %b %Y %H:%M:%S +0000", GetTime()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
string HTTPReply(int nStatus, const string& strMsg, bool keepalive, |
|
|
|
static const char *httpStatusDescription(int nStatus) |
|
|
|
bool headersOnly, const char *contentType) |
|
|
|
{ |
|
|
|
|
|
|
|
switch (nStatus) { |
|
|
|
|
|
|
|
case HTTP_OK: return "OK"; |
|
|
|
|
|
|
|
case HTTP_BAD_REQUEST: return "Bad Request"; |
|
|
|
|
|
|
|
case HTTP_FORBIDDEN: return "Forbidden"; |
|
|
|
|
|
|
|
case HTTP_NOT_FOUND: return "Not Found"; |
|
|
|
|
|
|
|
case HTTP_INTERNAL_SERVER_ERROR: return "Internal Server Error"; |
|
|
|
|
|
|
|
default: return ""; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string HTTPError(int nStatus, bool keepalive, bool headersOnly) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (nStatus == HTTP_UNAUTHORIZED) |
|
|
|
if (nStatus == HTTP_UNAUTHORIZED) |
|
|
|
return strprintf("HTTP/1.0 401 Authorization Required\r\n" |
|
|
|
return strprintf("HTTP/1.0 401 Authorization Required\r\n" |
|
|
@ -75,20 +86,13 @@ string HTTPReply(int nStatus, const string& strMsg, bool keepalive, |
|
|
|
"<BODY><H1>401 Unauthorized.</H1></BODY>\r\n" |
|
|
|
"<BODY><H1>401 Unauthorized.</H1></BODY>\r\n" |
|
|
|
"</HTML>\r\n", rfc1123Time(), FormatFullVersion()); |
|
|
|
"</HTML>\r\n", rfc1123Time(), FormatFullVersion()); |
|
|
|
|
|
|
|
|
|
|
|
const char *cStatus; |
|
|
|
return HTTPReply(nStatus, httpStatusDescription(nStatus), keepalive, |
|
|
|
if (nStatus == HTTP_OK) cStatus = "OK"; |
|
|
|
headersOnly, "text/plain"); |
|
|
|
else if (nStatus == HTTP_BAD_REQUEST) cStatus = "Bad Request"; |
|
|
|
|
|
|
|
else if (nStatus == HTTP_FORBIDDEN) cStatus = "Forbidden"; |
|
|
|
|
|
|
|
else if (nStatus == HTTP_NOT_FOUND) cStatus = "Not Found"; |
|
|
|
|
|
|
|
else if (nStatus == HTTP_INTERNAL_SERVER_ERROR) cStatus = "Internal Server Error"; |
|
|
|
|
|
|
|
else cStatus = ""; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool useInternalContent = false; |
|
|
|
|
|
|
|
if (nStatus != HTTP_OK) { |
|
|
|
|
|
|
|
contentType = "text/plain"; |
|
|
|
|
|
|
|
useInternalContent = true; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string HTTPReply(int nStatus, const string& strMsg, bool keepalive, |
|
|
|
|
|
|
|
bool headersOnly, const char *contentType) |
|
|
|
|
|
|
|
{ |
|
|
|
return strprintf( |
|
|
|
return strprintf( |
|
|
|
"HTTP/1.1 %d %s\r\n" |
|
|
|
"HTTP/1.1 %d %s\r\n" |
|
|
|
"Date: %s\r\n" |
|
|
|
"Date: %s\r\n" |
|
|
@ -99,14 +103,14 @@ string HTTPReply(int nStatus, const string& strMsg, bool keepalive, |
|
|
|
"\r\n" |
|
|
|
"\r\n" |
|
|
|
"%s", |
|
|
|
"%s", |
|
|
|
nStatus, |
|
|
|
nStatus, |
|
|
|
cStatus, |
|
|
|
httpStatusDescription(nStatus), |
|
|
|
rfc1123Time(), |
|
|
|
rfc1123Time(), |
|
|
|
keepalive ? "keep-alive" : "close", |
|
|
|
keepalive ? "keep-alive" : "close", |
|
|
|
strMsg.size(), |
|
|
|
(headersOnly ? 0 : strMsg.size()), |
|
|
|
contentType, |
|
|
|
contentType, |
|
|
|
FormatFullVersion(), |
|
|
|
FormatFullVersion(), |
|
|
|
(headersOnly ? "" : |
|
|
|
(headersOnly ? "" : strMsg.c_str()) |
|
|
|
(useInternalContent ? cStatus : strMsg.c_str()))); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool ReadHTTPRequestLine(std::basic_istream<char>& stream, int &proto, |
|
|
|
bool ReadHTTPRequestLine(std::basic_istream<char>& stream, int &proto, |
|
|
|