-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandler.cpp
More file actions
130 lines (122 loc) · 3.79 KB
/
Copy pathHandler.cpp
File metadata and controls
130 lines (122 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Author: Broglie
* E-mail: yibo141@outlook.com
*/
#include "Handler.h"
Handler::Handler(const int connfd)
:_connfd(connfd)
{
_isClosed = false;
_AlreadyParse = false;
}
Handler::~Handler()
{
if(_request.connection!="keep-alive")
{
if(!_isClosed)
close(_connfd);//短链接释放
}
}
void Handler::handle()
{
//std::cout<<"_AlreadyParse "<<_AlreadyParse<<std::endl;
if(!_AlreadyParse)
{
close(_connfd);
_isClosed = true;
return;
}
//std::cout << "come here A" << std::endl;
if(_request.method != "GET")
{
sendErrorMsg("501", "Not Implemented",
"Server doesn't implement this method");
return;
}
parseURI();
//std::cout << "come here B" << std::endl;
struct stat fileInfo;
if(stat(_fileName.c_str(), &fileInfo) < 0)
{
std::cout << "404 Not found: Server couldn't find this file." << std::endl;
sendErrorMsg("404", "Not Found", "Server couldn't find this file");
return;
}
if(!(S_ISREG(fileInfo.st_mode)) || !(S_IRUSR & fileInfo.st_mode))
{
std::cout << "403 Forbidden: Server couldn't read this file." << std::endl;
sendErrorMsg("403", "Forbidden", "Server couldn't read this file");
return;
}
getFileType();
//std::cout << "come here C" << std::endl;
std::string msg = "HTTP/1.1 200 OK\r\n";
msg += "Server: Tiny Web Server\r\n";
msg += "Content-length: " + std::to_string(fileInfo.st_size) + "\r\n";
msg += "Content_type: " + _fileType + "\r\n\r\n";
_outputBuffer.append(msg.c_str(), msg.size());
int fd = open(_fileName.c_str(), O_RDONLY, 0);
_outputBuffer.readFd(fd);
_outputBuffer.sendFd(_connfd);
close(fd);
//std::cout << "come here D" << std::endl;
//shutdown(_connfd,SHUT_RDWR);//适合大量的短链接,可能不适合长连接unix p137
//close(_connfd);
//_isClosed = true;
}
bool Handler::receiveRequest()
{
if(_inputBuffer.readFd(_connfd) == 0)
return false;
std::string request = _inputBuffer.readAllAsString();
std::cout << "---------------------------Request---------------------------" << std::endl;
std::cout << request << std::endl;
std::cout << "-------------------------------------------------------------" << std::endl;
Parser p(request);
_request = p.getParseResult();
_AlreadyParse = true;
//std::cout<<"_AlreadyParse in request "<<_AlreadyParse<<std::endl;
return true;
}
void Handler::sendErrorMsg(const std::string &errorNum,
const std::string &shortMsg,
const std::string &longMsg)
{
std::string msg = "HTTP /1.0 " + errorNum + " " + shortMsg + "\r\n";
msg += "Content-type: text/html\r\n";
msg += "<html><title>Error</title>";
msg += "<body bgcolor=""ffffff"">\r\n";
msg += errorNum + ": " + shortMsg + "\r\n";
msg += "<p>" + longMsg + ": " + _fileName + "\r\n";
msg += "<hr><em>The Tiny Web server</em>\r\n";
_outputBuffer.append(msg.c_str(), msg.size());
_outputBuffer.sendFd(_connfd);
close(_connfd);
_isClosed = true;
}
void Handler::parseURI()
{
_fileName = ".";
if(_request.uri == "/")
_fileName += "/home.html";
}
void Handler::getFileType()
{
const char *name = _fileName.c_str();
if(strstr(name, ".html"))
_fileType = "text/html";
else if(strstr(name, ".pdf"))
_fileType = "application/pdf";
else if(strstr(name, ".png"))
_fileType = "image/png";
else if(strstr(name, ".gif"))
_fileType = "image/gif";
else if(strstr(name, ".jpg"))
_fileType = "image/jpg";
else if(strstr(name, ".jpeg"))
_fileType = "image/jpeg";
else if(strstr(name, ".css"))
_fileType = "test/css";
else
_fileType = "text/plain";
}