轻松分享
设计实现http服务端程序,能够提供浏览器客户端进行文件的上传,下载,浏览功能
Linux 服务器 , g++ 编译器 , vim编辑器 , gdb 调试器 , make工具
class TcpSocket {
private:
int _sockfd;
public:
bool SocketInit();
bool Accept(TcpSocket &sock);
bool RecvPeek(std::string &buf);
bool Recv(std::string &buf, int len);
}
Class Epoll {
private:
int _epfd;
public:
bool EpollInit();
bool EpollAdd(TcpSocket &sock);
bool EpollDel(TcpSocket &sock);
bool EpollWait(std::vector &list, int timeout = 3000);
}
typedef void (*TaskHandler_t)(int data);
class ThreadTask {
private:
int _data;
TaskHandler_t _handler;
public:
void TaskAdd(int data, TaskHandler_t handler);
void TaskRun();
}
class ThreadPool {
private:
std::queue _queue;
int _max_queue;
pthread_mutex_t _mutex;
pthread_cond_t _cond_pro;
pthread_cond_t _cond_con;
private:
int _max_thread;
static void *thr_start(void *arg);
public:
ThreadPool(int max_queue, int max_thread);
bool ThreadInit();
bool TaskPush(ThreadTask &task);
}
//HTTP请求类, 解析HTTP的请求 , 将解析结果放入实例化对象中
class HttpRequest {
public:
std::string _method;
std::string _path;
std::unordered_map _param;
std::unordered_map _headers;
std::string _body;
private:
bool RecvHeader(std::string header);
bool FirstLineParse(std::string &line);
bool PathIsLegal();
public:
int RequestParse(TcpSocket &sock);
}
//HTTP响应类
class HttpResponse {
public:
int _status;
std::unordered_map _headers;
std::string _body;
private:
std::string GetDesc();
public:
bool ErrorResponse();
bool NormalResponse(TcpSocket &sock);
}
class Server
{
private:
Tcpsocket lst_sock;
Epoll epoll;
ThreadPool poll;
public:
static void ThreadHandler(int data){
TcpSocket sock;
sock.SetFd(data) ;
HttpRequest req;
int status = req.RequestParse(sock);
if(status != 200)
{
rsp.status = status;
rsp.ErrorProcess(); }
HttpResponse rsp;
HttpProcess(req, rsp);
rsp.NormalProcess(sock);
}
bool HttpProcess(HttpReaquest &req , HttpResponse &rsp);
源码
本项目就是一个实现http服务器, 然后用这个服务器来存储文件, 提供下载,上传功能, 通过这个项目, 我感觉自身的知识掌握还不牢固, 知识面比较狭窄, 需要巩固学习的东西还有很多, 希望自己今后多多加油~~