Skip to content

Commit f1c3d9d

Browse files
committed
Fixed #2
1 parent 37c6ce4 commit f1c3d9d

File tree

3 files changed

+14
-61
lines changed

3 files changed

+14
-61
lines changed

src/wsjcpp_light_web_http_response.cpp

Lines changed: 8 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,6 @@
1919
// enum for http responses
2020
std::map<int, std::string> *WSJCppLightWebHttpResponse::g_mapReponseDescription = nullptr;
2121

22-
// deprecated
23-
std::string WSJCppLightWebHttpResponse::RESP_OK = "HTTP/1.1 200 OK";
24-
std::string WSJCppLightWebHttpResponse::RESP_BAD_REQUEST = "HTTP/1.1 400 Bad Request";
25-
std::string WSJCppLightWebHttpResponse::RESP_FORBIDDEN = "HTTP/1.1 403 Forbidden";
26-
std::string WSJCppLightWebHttpResponse::RESP_NOT_FOUND = "HTTP/1.1 404 Not Found";
27-
std::string WSJCppLightWebHttpResponse::RESP_PAYLOAD_TOO_LARGE = "HTTP/1.1 413 Payload Too Large";
28-
std::string WSJCppLightWebHttpResponse::RESP_INTERNAL_SERVER_ERROR = "HTTP/1.1 500 Internal Server Error";
29-
std::string WSJCppLightWebHttpResponse::RESP_NOT_IMPLEMENTED = "HTTP/1.1 501 Not Implemented";
30-
3122
// ----------------------------------------------------------------------
3223

3324
WSJCppLightWebHttpResponse::WSJCppLightWebHttpResponse(int nSockFd) {
@@ -41,6 +32,7 @@ WSJCppLightWebHttpResponse::WSJCppLightWebHttpResponse(int nSockFd) {
4132
WSJCppLightWebHttpResponse::g_mapReponseDescription->insert(std::pair<int, std::string>(413, "HTTP/1.1 413 Payload Too Large"));
4233
WSJCppLightWebHttpResponse::g_mapReponseDescription->insert(std::pair<int, std::string>(500, "HTTP/1.1 500 Internal Server Error"));
4334
WSJCppLightWebHttpResponse::g_mapReponseDescription->insert(std::pair<int, std::string>(501, "HTTP/1.1 501 Not Implemented"));
35+
WSJCppLightWebHttpResponse::g_mapReponseDescription->insert(std::pair<int, std::string>(408, "HTTP/1.1 408 Request Time-out"));
4436
}
4537

4638
m_nSockFd = nSockFd;
@@ -103,6 +95,13 @@ WSJCppLightWebHttpResponse &WSJCppLightWebHttpResponse::notImplemented() {
10395

10496
// ----------------------------------------------------------------------
10597

98+
WSJCppLightWebHttpResponse &WSJCppLightWebHttpResponse::requestTimeout() {
99+
m_nResponseCode = 408;
100+
return *this;
101+
}
102+
103+
// ----------------------------------------------------------------------
104+
106105
WSJCppLightWebHttpResponse &WSJCppLightWebHttpResponse::noCache() {
107106
m_sCacheControl = "no-cache, no-store, must-revalidate";
108107
return *this;
@@ -209,33 +208,6 @@ void WSJCppLightWebHttpResponse::sendOptions(const std::string &sOptions) {
209208

210209
// ----------------------------------------------------------------------
211210

212-
void WSJCppLightWebHttpResponse::sendRequestTimeOut() {
213-
std::string sResponse = "I don't understand you! Are you just a machine? Or maybe hacker?";
214-
/*
215-
HTTP/1.0 408 Request Time-out
216-
Cache-Control: no-cache
217-
Connection: close
218-
Content-Type: text/html
219-
220-
<html><body><h1>408 Request Time-out</h1>
221-
Your browser didn't send a complete request in time.
222-
</body></html>
223-
*/
224-
225-
if (m_bClosed) {
226-
WSJCppLog::warn(TAG, "Already sended response");
227-
return;
228-
}
229-
m_bClosed = true;
230-
231-
WSJCppLog::info(TAG, "\nResponse: \n>>>\n" + sResponse + "\n<<<");
232-
233-
send(m_nSockFd, sResponse.c_str(), sResponse.length(),0);
234-
close(m_nSockFd);
235-
}
236-
237-
// ----------------------------------------------------------------------
238-
239211
void WSJCppLightWebHttpResponse::sendFile(const std::string &sFilePath) {
240212

241213
// read data from file
@@ -279,21 +251,7 @@ void WSJCppLightWebHttpResponse::sendBuffer(const std::string &sFilePath, const
279251
return;
280252
}
281253
m_bClosed = true;
282-
283-
// #if __APPLE__
284-
// send(m_nSockFd, sResponse.c_str(), sResponse.length(), SO_NOSIGPIPE);
285-
// send(m_nSockFd, pData, nSize, SO_NOSIGPIPE);
286-
// // #if
287-
// // TARGET_OS_MAC
288-
289-
// #else
290-
// SO_NOSIGPIPE
291254
write(m_nSockFd, sResponse.c_str(), sResponse.length());
292255
write(m_nSockFd, pBuffer, nBufferSize);
293-
294-
// send(m_nSockFd, sResponse.c_str(), sResponse.length(), MSG_CONFIRM);
295-
// send(m_nSockFd, pBuffer, nBufferSize, MSG_CONFIRM);
296-
// #endif
297-
298256
close(m_nSockFd);
299257
}

src/wsjcpp_light_web_http_response.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,6 @@
2323
class WSJCppLightWebHttpResponse {
2424
public:
2525
static std::map<int, std::string> *g_mapReponseDescription;
26-
27-
// enum for http responses
28-
static std::string RESP_OK;
29-
static std::string RESP_BAD_REQUEST;
30-
static std::string RESP_FORBIDDEN;
31-
static std::string RESP_NOT_FOUND;
32-
static std::string RESP_PAYLOAD_TOO_LARGE;
33-
static std::string RESP_INTERNAL_SERVER_ERROR;
34-
static std::string RESP_NOT_IMPLEMENTED;
3526

3627
WSJCppLightWebHttpResponse(int nSockFd);
3728

@@ -42,14 +33,14 @@ class WSJCppLightWebHttpResponse {
4233
WSJCppLightWebHttpResponse &payloadTooLarge();
4334
WSJCppLightWebHttpResponse &internalServerError();
4435
WSJCppLightWebHttpResponse &notImplemented();
36+
WSJCppLightWebHttpResponse &requestTimeout();
4537

4638
WSJCppLightWebHttpResponse &noCache();
4739
WSJCppLightWebHttpResponse &cacheSec(int nCacheSec);
4840

4941
void sendText(const std::string &sBody);
5042
void sendEmpty();
5143
void sendOptions(const std::string &sOptions);
52-
void sendRequestTimeOut();
5344
void sendFile(const std::string &sFilePath);
5445
void sendBuffer(const std::string &sFilePath, const char *pBuffer, const int nBufferSize);
5546

src/wsjcpp_light_web_server.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,11 @@ void WSJCppLightWebHttpThreadWorker::run() {
173173
WSJCppLog::info(TAG, "\nRequest: \n>>>\n" + sRequest + "\n<<<");
174174

175175
if (bErrorRead) {
176-
pResponse->sendRequestTimeOut();
176+
pResponse->requestTimeout().noCache().sendText(
177+
"<html><body><h1>408 Request Time-out</h1>"
178+
"Your browser didn't send a complete request in time."
179+
"</body></html>"
180+
);
177181
} else if (pInfo->requestType() == "OPTIONS") {
178182
pResponse->ok().sendOptions("OPTIONS, GET, POST");
179183
} else if (pInfo->requestType() != "GET" && pInfo->requestType() != "POST") {

0 commit comments

Comments
 (0)