Skip to content

Commit

Permalink
fix some bugs and make some clare
Browse files Browse the repository at this point in the history
  • Loading branch information
LiuRicky committed Mar 1, 2020
1 parent 364279f commit 717f9f5
Show file tree
Hide file tree
Showing 22 changed files with 329 additions and 25 deletions.
4 changes: 2 additions & 2 deletions Epoll.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include "Channel.h"
#include "Http_conn.h"
//#include "Http_conn.h"
#include "Epoll.h"
#include "MemoryPool.h"

typedef std::shared_ptr<Epoll> SP_Epoll;

Epoll::Epoll()
: events(EVENTS)
: events(EVENTS)
{
epollfd=Epoll_create(MAXFDS);
}
Expand Down
5 changes: 2 additions & 3 deletions Epoll.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ class Channel;
class Http_conn;
typedef std::shared_ptr<Channel> SP_Channel;

std::vector<SE> events;
std::unordered_map<int,SP_Channel> Channelmap;

class Epoll{
public:
Epoll();
Expand All @@ -26,4 +23,6 @@ class Epoll{
void poll(std::vector<SP_Channel>& req);
private:
int epollfd;
std::vector<SE> events;
std::unordered_map<int,SP_Channel> Channelmap;
};
Binary file added Epoll.o
Binary file not shown.
3 changes: 0 additions & 3 deletions EventLoop.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#include "EventLoop.h"
#include "Epoll.h"
#include "TimeManger.h"
#include "MutexLock.h"

bool EventLoop::quit = false;

Expand Down
9 changes: 4 additions & 5 deletions EventLoop.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
#include <queue>
#include <memory>
#include <functional>

class MutexLock;
class Channel;
class Epoll;
class TimerManager;
#include "MutexLock.h"
#include "Epoll.h"
#include "TimeManager.h"
#include "Channel.h"

typedef std::function<void()> Functor;
typedef std::shared_ptr<Channel> SP_Channel;
Expand Down
198 changes: 198 additions & 0 deletions Http_conn.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
#include "Http_conn.h"
#include "Packet.h"
#include "EventLoop.h"
#include "Conf.h"
// #include "MemoryPool.h"
#include "Mimetype.h"
#include "Logging.h"

typedef shared_ptr<Http_conn> SP_Http_conn;

Http_conn::Http_conn(SP_Channel Channel)
: channel(Channel),
storage(getconf().getstorage()),
parsestate(PARSE_METHOD),
pos(0),
inbuffer(""),
path(""),
filetype(""),
keepalive(false),
size(0)
{
handleparse[0] = bind(&Http_conn::parseError, this);
handleparse[1] = bind(&Http_conn::parseMethod, this);
handleparse[2] = bind(&Http_conn::parseHeader, this);
handleparse[3] = bind(&Http_conn::parseSuccess, this);
channel->setReadhandler(bind(&Http_conn::parse, this));
}

Http_conn::~Http_conn(){

}

void Http_conn::initmsg(){
path = "";
filetype = "";
keepalive = false;
}

bool Http_conn::Read(string& msg, string str){
//find the first str in msg
int next = inbuffer.find(str, pos);
if(string::npos == next){
return false;
}
msg = inbuffer.substr(pos, next-pos);
pos = next + str.length();
return true;
}

PARSESTATE Http_conn::parseMethod(){
string msg;
if(!Read(msg, " /")){
return PARSE_ERROR;
}
else if(msg == "GET"){
method = METHOD_GET;
}
else if(msg == "POST"){
method = METHOD_POST;
}
else{
return PARSE_ERROR;
}
struct stat sbuf;
if(path == " "){
path = "index.html";
}
if(stat((storage+path).c_str(), &sbuf) < 0){
LOG << "no file";
parsestate = PARSE_ERROR;
return PARSE_ERROR;
}
size = sbuf.st_size;
if(!Read(msg, "\r\n")){
return PARSE_ERROR;
}
else if(msg == "HTTP/1.0"){
version = HTTP_10;
}
else if(msg == "HTTP/1.1"){
version = HTTP_11;
}
else{
return PARSE_ERROR;
}
return PARSE_HEADER;
}

PARSESTATE Http_conn::parseHeader(){
if(inbuffer[pos] == '\r' && inbuffer[pos+1] == '\n'){
return PARSE_SUCCESS;
}
string key, value;
if(!Read(key,": ")){
return PARSE_ERROR;
}
if(!Read(value, "\r\n")){
return PARSE_ERROR;
}
header[key] = value;
return PARSE_HEADER;
}

PARSESTATE Http_conn::parseError(){
LOG<<"parse error";
inbuffer="";
pos=0;
initmsg();
header.clear();
channel->setRevents(EPOLLOUT|EPOLLET);
channel->getLoop().lock()->updatePoller(channel);
channel->setWritehandler(bind(&Http_conn::handleError,this,400,"Bad Request"));
return PARSE_METHOD;
}

PARSESTATE Http_conn::parseSuccess(){
channel->setRevents(EPOLLOUT|EPOLLET);
channel->getLoop().lock()->updatePoller(channel);
inbuffer=inbuffer.substr(pos+2);
pos=0;
if(HTTP_11==version && header.find("Connection")!=header.end() &&
("Keep-Alive"==header["Connection"] || "keep-alive"==header["Connection"]))
keepalive=true;
int dot_pos=path.find('.');
if(string::npos==dot_pos)
filetype=Mimetype::getMime("default");
else
filetype=Mimetype::getMime(path.substr(dot_pos));
channel->setWritehandler(bind(&Http_conn::send,this));
header.clear();
return PARSE_METHOD;
}

void Http_conn::parse(){
bool zero = false;
int readsum;
readsum = readn(channel->getFd(), inbuffer, zero);
if(readsum < 0 || zero){//close
initmsg();
channel->setDeleted(true);
channel->getLoop().lock()->addTimer(channel, 0);
return;
}
while(inbuffer.length() && ~inbuffer.find("\r\n", pos)){
parsestate = handleparse[parsestate]();
}
}

void Http_conn::send(){
string outbuffer = "";
if(method == METHOD_GET){
if(keepalive){
outbuffer = "HTTP/1.1 200 OK\r\n";
outbuffer += string("Connection: Keep-Alive\r\n");
channel->getLoop().lock()->addTimer(channel, getconf().getkeep_alived());
}
else{
outbuffer = "HTTP/1.1 200 OK\r\n";
channel->getLoop().lock()->addTimer(channel,0);
}
outbuffer += "Content-Type: " + filetype + "\r\n";
outbuffer += "Content-Length: " + to_string(size) + "\r\n";
outbuffer += "Server: ChiServer\r\n";
outbuffer += "\r\n"; //last line
//initializing cache here
}
else{
LOG << "method dont implement yet";
}
const char* buffer = outbuffer.c_str();
if(!writen(channel->getFd(), buffer, outbuffer.length())){
LOG << "writen error";
}
initmsg();
channel->setRevents(EPOLLIN | EPOLLET);
channel->getLoop().lock()->updatePoller(channel);
}

void Http_conn::handleError(int errornum,string msg){//error 404
string body = "<html><title>error</title>";
body += "<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head>";
body += "<body bgcolor=\"ffffff\">";
body += to_string(errornum) + msg;
body += "<hr><em> ChiServer</em>\n</body></html>";
string outbuffer = "HTTP/1.1 " + to_string(errornum) + msg + "\r\n";
outbuffer += "Content-Type: text/html\r\n";
outbuffer += "Connection: Close\r\n";
outbuffer += "Content-Length: " + to_string(body.size()) + "\r\n";
outbuffer += "Server: ChiServer\r\n";;
outbuffer += "\r\n";
outbuffer +=body;
const char *buffer=outbuffer.c_str();
if(!writen(channel->getFd(),buffer,outbuffer.length())){
LOG<<"writen error";
}
channel->setRevents(EPOLLIN|EPOLLET);
channel->getLoop().lock()->updatePoller(channel);
}
47 changes: 47 additions & 0 deletions Http_conn.h
Original file line number Diff line number Diff line change
@@ -1 +1,48 @@
#pragma once

#include <string>
#include <unordered_map>
#include <iostream>
//#include <sys/mman.h>
#include <memory>
#include <functional>

using namespace std;

enum METHOD{METHOD_GET,METHOD_POST};
enum HTTPVERSION{HTTP_10,HTTP_11};
enum PARSESTATE{PARSE_ERROR,PARSE_METHOD,PARSE_HEADER,PARSE_SUCCESS};

class Channel;
typedef shared_ptr<Channel> SP_Channel;

class Http_conn{
public:
Http_conn(SP_Channel channel);
~Http_conn();

private:
typedef function<PARSESTATE()> CallBack;
CallBack handleparse[4];
SP_Channel channel;
bool keepalive;
int pos;
int size;
string inbuffer;
string storage;
string path;
string filetype;
METHOD method;
HTTPVERSION version;
PARSESTATE parsestate;
unordered_map<string, string> header;
PARSESTATE parseMethod();
PARSESTATE parseHeader();
PARSESTATE parseError();
PARSESTATE parseSuccess();
void parse();
void send();
bool Read(string& msg, string str);
void initmsg();
void handleError(int errornum, string msg);
};
1 change: 0 additions & 1 deletion LFUCache.cc → LFUCache.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "LFUCache.h"
#include "MutexLock.h"
#include "Conf.h"
#include "MemoryPool.h"

Expand Down
3 changes: 1 addition & 2 deletions LFUCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

#include <unordered_map>
#include <string>
#include "MutexLock.h"

using std::string;

class MutexLock;

template<typename T>
class Node{
private:
Expand Down
Binary file added LFUCache.o
Binary file not shown.
1 change: 1 addition & 0 deletions Logging.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#pragma once
16 changes: 15 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,28 @@ GSTD := g++ -std=c++11
OPT := -lpthread

all:


lfu: config memorypool
$(GSTD) -c LFUCache.cpp $(OPT)

http:
$(GSTD) -c Http_conn.cpp $(OPT)

epoll: packet
$(GSTD) -c Epoll.cpp $(OPT)

packet:
$(GSTD) -c Packet.cpp $(OPT)

config:
$(GSTD) -c Conf.cpp $(OPT)

memorypool:
$(GSTD) -c MemoryPool.cpp $(OPT)

mimetype:
$(GSTD) -c Mimetype.cpp $(OPT)

thread:
$(GSTD) -c Thread.cpp $(OPT)

Expand Down
3 changes: 1 addition & 2 deletions MemoryPool.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "MemoryPool.h"
#include "Epoll.h"
#include "TimerManager.h"
#include "MutexLock.h"


MemoryPool::MemoryPool(){

Expand Down
2 changes: 1 addition & 1 deletion MemoryPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
#include <stdarg.h>
#include <utility>
#include <functional>
#include "MutexLock.h"

#define BlockSize 4096

class MutexLock;

/*
* used to allocate memory
Expand Down
Binary file modified MemoryPool.o
Binary file not shown.
Loading

0 comments on commit 717f9f5

Please sign in to comment.