123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- //
- // Created by xcbosa on 2023/1/28.
- //
- #include "ClientConnection.h"
- using namespace std;
- namespace xc {
- namespace httpserver {
- 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(&ClientConnection::workLoop, this).detach();
- }
- void ClientConnection::workLoop() {
- ::FILE *clRead = fdopen(this->sockFd, "r");
- ::FILE *clWrite = fdopen(dup(this->sockFd), "w");
- 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
- } // httpserver
|