|
@@ -3,7 +3,8 @@
|
|
|
//
|
|
|
|
|
|
#include "ClientConnection.h"
|
|
|
-#include "../webui.h"
|
|
|
+
|
|
|
+using namespace std;
|
|
|
|
|
|
namespace xc {
|
|
|
namespace httpserver {
|
|
@@ -11,20 +12,75 @@ namespace xc {
|
|
|
ClientConnection::ClientConnection(int sockFd, struct sockaddr_in address) {
|
|
|
this->sockFd = sockFd;
|
|
|
this->address = address;
|
|
|
+ this->clRead = nullptr;
|
|
|
+ this->clWrite = nullptr;
|
|
|
+ this->requestBuff = nullptr;
|
|
|
}
|
|
|
|
|
|
void ClientConnection::workAndDestroy() {
|
|
|
- thread(workLoop);
|
|
|
+ thread(&ClientConnection::workLoop, this).detach();
|
|
|
}
|
|
|
|
|
|
void ClientConnection::workLoop() {
|
|
|
::FILE *clRead = fdopen(this->sockFd, "r");
|
|
|
::FILE *clWrite = fdopen(dup(this->sockFd), "w");
|
|
|
- char smallBuf[smallBuffSize];
|
|
|
- ::fgets(smallBuf, smallBuffSize, clRead);
|
|
|
- if (::strstr(smallBuf, "HTTP/") == NULL) {
|
|
|
-
|
|
|
+ this->clRead = clRead;
|
|
|
+ this->clWrite = clWrite;
|
|
|
+ char *requestBuff = (char *)::malloc(urlRequestBuffSize);
|
|
|
+ this->requestBuff = requestBuff;
|
|
|
+ ::fgets(requestBuff, urlRequestBuffSize, clRead);
|
|
|
+
|
|
|
+ if (::strstr(requestBuff, "HTTP/") == NULL) {
|
|
|
+ conf::errorPage400.writeTo(clWrite);
|
|
|
+ cleanUpAndDestroy();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ string method, url;
|
|
|
+
|
|
|
+ char *ptr, *p;
|
|
|
+ ptr = strtok_r(requestBuff, " ", &p);
|
|
|
+ if (ptr != nullptr) {
|
|
|
+ method = string(ptr);
|
|
|
+ ptr = strtok_r(NULL, " ", &p);
|
|
|
+ if (ptr != nullptr) {
|
|
|
+ url = string(ptr);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (method == "GET1") {
|
|
|
+
|
|
|
+ } else if (method == "POST1") {
|
|
|
+
|
|
|
+ } else {
|
|
|
+ conf::errorPage.applyReplacements(400, {
|
|
|
+ Replacement("errorCode", "400"),
|
|
|
+ Replacement("errorMessage", "未知的协议 " + method)
|
|
|
+ }).writeTo(clWrite);
|
|
|
+ cleanUpAndDestroy();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ ResponseData(200, string(requestBuff)).writeTo(clWrite);
|
|
|
+ cleanUpAndDestroy();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ void ClientConnection::cleanUpAndDestroy() {
|
|
|
+ if (this->clRead) {
|
|
|
+ ::fclose(this->clRead);
|
|
|
+ this->clRead = nullptr;
|
|
|
+ }
|
|
|
+ if (this->clWrite) {
|
|
|
+ ::fclose(this->clWrite);
|
|
|
+ this->clWrite = nullptr;
|
|
|
+ }
|
|
|
+ if (this->requestBuff) {
|
|
|
+ ::free(this->requestBuff);
|
|
|
+ this->requestBuff = nullptr;
|
|
|
}
|
|
|
+ ::close(this->sockFd);
|
|
|
+ delete this;
|
|
|
}
|
|
|
|
|
|
} // xc
|