Skip to content

Commit

Permalink
🎨 adjust
Browse files Browse the repository at this point in the history
  • Loading branch information
markparticle committed Jun 24, 2020
1 parent 67b7710 commit bc87e3e
Show file tree
Hide file tree
Showing 22 changed files with 343 additions and 231 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
356 changes: 228 additions & 128 deletions src/http/httpconn.cpp → code/http/httpconn.cpp

Large diffs are not rendered by default.

57 changes: 23 additions & 34 deletions src/http/httpconn.h → code/http/httpconn.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@
#include <unordered_map>
#include <string>
#include <regex>
#include <iostream>
#include <algorithm>

#include "epoll.h"
#include "../log/log.h"
#include "../pool/threadpool.h"
#include "../pool/sqlconnRAII.h"
//#include "../timer/heaptimer.h"

class HttpConn {
public:
Expand All @@ -47,12 +48,6 @@ class HttpConn {
CONNEXT,
PATH,
};

enum CHECK_STATE {
REQUESTLINE = 0,
HEADER,
CONTENT,
};

enum HTTP_CODE {
NO_REQUEST = 0,
Expand All @@ -65,23 +60,22 @@ class HttpConn {
CLOSED_CONNECTION,
};

enum LINE_STATUS {
LINE_OK = 0,
LINE_BAD,
LINE_OPEN,
enum PARSE_STATE {
OK = 0,
CONTINUE,
ERROR,
};

struct Request {
std::string method, path, version;
std::string content;
int contentLen;
std::string method, path, version, body;
std::unordered_map<std::string, std::string> header;
//std::smatch pathMatch;
std::unordered_map<std::string, std::string> post;
};

static std::unordered_map<std::string, int> defaultRes;

public:
HttpConn();

~HttpConn();

void init(int sockFd, const sockaddr_in& addr);
Expand All @@ -91,8 +85,7 @@ class HttpConn {
void process();
void CloseConn();
bool IsClose() const;
// void InitMySQLResult();
// void InitResultFile();

static bool OpenLog() { return openLog; };

static Epoll* epollPtr;
Expand All @@ -103,16 +96,15 @@ class HttpConn {
static bool openLog;
static bool isET;

static bool UserVerify(const std::string& name, const std::string& pwd, bool isLogin);

int GetFd() const;
struct sockaddr_in GetAddr() const;
const char* GetIP() const;
int GetPort() const;
time_t GetExpires() const;
MYSQL* GetSql() const;

void SetExpires(time_t expires);
void BindSql(MYSQL * mysql);


private:
static const int PATH_LEN = 200;
static const int READ_BUFF_SIZE = 5000;
Expand All @@ -121,12 +113,13 @@ class HttpConn {
void Init_();
void Unmap_();

LINE_STATUS ParseLine_();
void GetRequestLine_();

HTTP_CODE ParseRequestLine_(std::string line);
HTTP_CODE ParseHeader_(std::string line);
HTTP_CODE ParseContent_(std::string line);
PARSE_STATE ParseRequestLine_(const std::string& line);
PARSE_STATE ParseHeader_(const std::string& line);
PARSE_STATE ParseContent_(const std::string& line);
HTTP_CODE DoRequest_();
void ParseFromUrlencoded_();

bool AddResponse_(const char* format,...);
bool AddStatusLine_(int status, const char* title);
Expand All @@ -137,8 +130,8 @@ class HttpConn {
bool AddLinger_();
bool AddBlinkLine_();

HTTP_CODE ParseHttpMsg_();
bool GenerateHttpMsg_(HTTP_CODE ret);
HTTP_CODE ParseRequest_();
bool GenerateResponse_(HTTP_CODE ret);

int fd_;
struct sockaddr_in addr_;
Expand All @@ -153,19 +146,15 @@ class HttpConn {
char* writeBuff_;
size_t writeIdx_;

CHECK_STATE checkState_;

bool isClose_;

char* fileAddr_;
int iovCount_;
struct stat fileStat_;
struct iovec iov_[2];

size_t bytesTosSend_;
size_t bytesToSend_;
size_t bytesHaveSend_;

MYSQL *mysql_;
};


Expand Down
File renamed without changes.
File renamed without changes.
10 changes: 6 additions & 4 deletions src/log/log.cpp → code/log/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ Log::Log() {
}

Log::~Log() {
delete[] buffer_;
if(writePID_ && writePID_->joinable()) {
while(!deque_->empty());
while(!deque_->empty()) {
deque_->flush();
};
deque_->Close();
writePID_->join();
delete writePID_;
Expand All @@ -35,6 +36,7 @@ Log::~Log() {
flush();
fclose(fp_);
}
delete[] buffer_;
}

int Log::getLevel() const {
Expand Down Expand Up @@ -65,7 +67,7 @@ void Log::init(int level = 1, const char* path, const char* suffix, int maxLines
strcpy(path_, path);
strcpy(suffix_, suffix);
char fileName[LOG_NAME_LEN] = {0};
snprintf(fileName, LOG_NAME_LEN - 1, "%s/%d_%02d_%02d%s",
snprintf(fileName, LOG_NAME_LEN - 1, "%s/%04d_%02d_%02d%s",
path_, t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, suffix_);
toDay_ = t.tm_mday;

Expand Down Expand Up @@ -119,7 +121,7 @@ void Log::write(int level, const char *format, ...) {
{
char newFile[LOG_NAME_LEN];
char tail[16] = {0};
snprintf(tail, 16, "%d_%02d_%02d", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday);
snprintf(tail, 16, "%04d_%02d_%02d", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday);

if (toDay_ != t.tm_mday)
{
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions src/main.cpp → code/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
* @copyleft GPL 2.0
*/
#include <unistd.h>
#include "webserver.h"
#include "server/webserver.h"

int main() {
//daemon(1, 0);
WebServer server(1314,
WebServer server(1316,
3306, "root", "root", "webserver",
10, 8, 3, true, true,
true, 0, 800);
true, 0, 0);
server.Init();
server.Start();
}
Expand Down
File renamed without changes.
16 changes: 10 additions & 6 deletions src/pool/sqlconnpool.cpp → code/pool/sqlconnpool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ SqlConnPool* SqlConnPool::GetInstance() {
return &connPool;
}

void SqlConnPool::Init(string host, string user,
string pwd, string dbName, int port,
void SqlConnPool::Init(char* host, char* user,
char* pwd, char* dbName, int port,
int connSize) {
sqlConfig_ = {host, user, pwd, dbName, port};
memcpy(sqlConfig_.host, host, strlen(host));
memcpy(sqlConfig_.user, host, strlen(user));
memcpy(sqlConfig_.pwd, host, strlen(pwd));
memcpy(sqlConfig_.dbName, host, strlen(dbName));
sqlConfig_.port = port;
for (int i = 0; i < connSize; i++)
{
MYSQL *sql = nullptr;
Expand All @@ -32,9 +36,9 @@ void SqlConnPool::Init(string host, string user,
LOG_ERROR("MySql init error!");
exit(1);
}
sql = mysql_real_connect(sql, sqlConfig_.host.c_str(),
sqlConfig_.user.c_str(), sqlConfig_.pwd.c_str(),
sqlConfig_.dbName.c_str(), sqlConfig_.port, nullptr, 0);
sql = mysql_real_connect(sql, sqlConfig_.host,
sqlConfig_.user, sqlConfig_.pwd,
sqlConfig_.dbName, sqlConfig_.port, nullptr, 0);
if (!sql) {
LOG_ERROR("MySql Connect error!");
exit(1);
Expand Down
12 changes: 6 additions & 6 deletions src/pool/sqlconnpool.h → code/pool/sqlconnpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ class SqlConnPool
void FreeConn(MYSQL * conn);
int GetFreeConnCount();

void Init(std::string host, std::string user,
std::string pwd, std::string dbName, int port,
void Init(char* host, char* user,
char* pwd, char* dbName, int port,
int connSize);

void ClosePool();
static bool OpenLog() { return openLog; };

struct SqlConfig {
std::string host;
std::string user;
std::string pwd;
std::string dbName;
char host[24];
char user[48];
char pwd[48];
char dbName[48];
int port;
};
static bool openLog;
Expand Down
File renamed without changes.
Loading

0 comments on commit bc87e3e

Please sign in to comment.