Browse Source

Fix httpserver request resolve bug on linux

xcbosa-itx 2 years ago
parent
commit
1e9f75d7d3
4 changed files with 17 additions and 61 deletions
  1. 2 1
      controller/EntryController.cpp
  2. 14 58
      httpserver/ClientConnection.cpp
  3. 1 1
      main.cpp
  4. 0 1
      utils/TextResponseData.cpp

+ 2 - 1
controller/EntryController.cpp

@@ -16,7 +16,8 @@ using namespace configor;
 namespace xc::controller {
 
     ResponseData *EntryController(RequestData request) {
-        bool isUserLogin = user::isLogin(request.getCookie("Token"));
+        string cookieToken = request.getCookie("Token");
+        bool isUserLogin = user::isLogin(cookieToken);
         auto resp = new TemplateResponseData({
             If(isUserLogin, {
                 ContentGeneratorReference("PortListController", request)

+ 14 - 58
httpserver/ClientConnection.cpp

@@ -35,7 +35,7 @@ namespace xc {
 
             this->clRead = clRead;
             this->clWrite = clWrite;
-            char *requestBuff = (char *)::malloc(urlRequestBuffSize);
+            char *requestBuff = (char *)::calloc(1, urlRequestBuffSize);
             this->requestBuff = requestBuff;
 
             ::fgets(requestBuff, urlRequestBuffSize, clRead);
@@ -62,66 +62,22 @@ namespace xc {
                 map<string, string> headers;
                 ostringstream body;
 
-                bool isHeader = true;
-                bool lastLineIsEmpty = false;
-                bool lastLineEmptyAndZero = false;
                 while (bzero(requestBuff, urlRequestBuffSize), ::fgets(requestBuff, urlRequestBuffSize, clRead)) {
-                    if (method == "GET") {
-                        if (strcmp(requestBuff, "\r\n") == 0) {
-                            break;
-                        }
-                    }
-                    int len = ::strlen(requestBuff);
-                    char *lineBuff = (char *) ::malloc(len + 1), *pLineBuff = lineBuff;
-                    bool leftHasContext = false;
-                    for (int i = 0; i < len; i++) {
-                        if (requestBuff[i] == '\r' || requestBuff[i] == '\n') continue;
-                        if (requestBuff[i] == ' ' || requestBuff[i] == '\t') {
-                            if (leftHasContext) {
-                                *pLineBuff++ = requestBuff[i];
-                            }
-                        } else {
-                            leftHasContext = true;
-                            *pLineBuff++ = requestBuff[i];
-                        }
-                    }
-                    if (leftHasContext) {
-                        pLineBuff--;
-                        while (pLineBuff >= lineBuff && (*pLineBuff == ' ' || *pLineBuff == '\t')) {
-                            *pLineBuff = 0; // 从右往左,删掉右边的空白符
-                            pLineBuff--;
-                        }
-                    }
-                    if (::strlen(lineBuff) == 0) {
-                        // TODO: Fix Linux POST Method
-                        isHeader = false;
-                        lastLineIsEmpty = true;
-                        if (lastLineEmptyAndZero) {
-                            break;
-                        } else {
-                            lastLineEmptyAndZero = false;
-                        }
-                        if (method == "GET") {
-                            break;
-                        }
-                        continue;
+                    string line(requestBuff);
+
+                    replace_all(line, "\r\n", "");
+                    trim(line);
+                    if (line.length() == 0) {
+                        break; // TODO: POST Body not read.
                     }
-                    if (lineBuff[0] == '0') {
-                        if (lastLineIsEmpty) {
-                            lastLineEmptyAndZero = true;
-                            lastLineIsEmpty = false;
-                        }
+
+                    auto lineParts = split(line, ":");
+                    for (auto &it : lineParts) {
+                        trim(it);
                     }
-                    if (isHeader) {
-                        char *headerValue;
-                        char *headerName = strtok_r(lineBuff, ":", &headerValue);
-                        if (headerName != nullptr && headerValue != nullptr) {
-                            string name(headerName);
-                            string value(headerValue);
-                            headers[name] = value;
-                        }
-                    } else {
-                        body << lineBuff << endl;
+
+                    if (lineParts.size() == 2) {
+                        headers[lineParts[0]] = lineParts[1];
                     }
                 }
 

+ 1 - 1
main.cpp

@@ -138,6 +138,6 @@ int main(int argc, char **argv) {
     cmdLine.workerLoop();
 
     while (true) {
-        sleep(1000);
+        sleep(1);
     }
 }

+ 0 - 1
utils/TextResponseData.cpp

@@ -63,7 +63,6 @@ namespace xc {
                     oss << it.first;
                     oss << "=";
                     oss << it.second;
-                    oss << "; Path=/";
                     string str = oss.str();
                     ::fprintf(fp, "%s: %s\r\n", "Set-Cookie", str.c_str());
                 }