-
Notifications
You must be signed in to change notification settings - Fork 0
/
myhttpd.cpp
225 lines (198 loc) · 4.56 KB
/
myhttpd.cpp
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include "HttpServer.h"
#include "HttpRequest.h"
#include "HttpResponse.h"
#include <muduo/net/EventLoop.h>
#include <muduo/base/Logging.h>
#include "Util.h"
#include <sys/types.h>
#include <sys/wait.h>
#include <iostream>
#include <map>
#include <string>
using namespace std;
void onRequest(const HttpRequest& req, HttpResponse* resp)
{
int cgi=0;//on - off
if(req.method()!=HttpRequest::GET&&req.method()!=HttpRequest::POST)
{
resp->setStatusCode(HttpResponse::CODE_501);
resp->setStatusMessage("Method Not Implemented");
resp->setContentType("text/html");
resp->setBody("<HTML><HEAD><TITLE>Method Not Implemented</TITLE></HEAD><BODY><P>HTTP request method not supported.</BODY></HTML>");
return ;
}
if(req.method()==HttpRequest::POST||
(req.method()==HttpRequest::GET&&!req.query().empty()) )
{
cgi=1;
resp->setCgi(cgi);
}
if(!cgi)
{
string path=Util::ConstructPath(req.path());
string extent=Util::GetExtent(path);
string contentType="";
Util::GetContentType(extent,contentType);
string content=Util::GetContent(path);
if(content.empty())
{
resp->setStatusCode(HttpResponse::CODE_404);
resp->setStatusMessage("Not Found");
}
else
{
resp->setBody(content);
resp->setStatusCode(HttpResponse::CODE_200);
resp->setStatusMessage("OK");
resp->setContentType(contentType);
}
return ;
}
//CGI SCRIPT
#if 1
int cgi_output[2];
int cgi_input[2];
pid_t pid;
int status;
char c;
//POST
if(req.method()==HttpRequest::POST)
{
// cout<<req.getBody()<<endl;
// cout<<"xxxx"<<req.getHeader("Content-Length")<<endl;
int contentLengthValue=std::stoi(req.getHeader("Content-Length"));
string databody=req.getBody();
resp->setStatusCode(HttpResponse::CODE_200);
resp->setStatusMessage("OK");
if(pipe(cgi_output)<0)
{
printf("pipe error\r\n");
return ;
}
if(pipe(cgi_input)<0)
{
printf("pipe error\r\n");
return ;
}
if ( (pid = fork()) < 0 )
{
printf("fork error\r\n");
return;
}
if(pid==0)
{
char meth_env[255];
char length_env[255];
string Path(req.path().begin()+1,req.path().end());
sprintf(meth_env, "REQUEST_METHOD=%s", req.methodString());
sprintf(length_env, "CONTENT_LENGTH=%d", contentLengthValue);
putenv(meth_env);
putenv(length_env);
dup2(cgi_output[1], 1);
dup2(cgi_input[0], 0);
close(cgi_output[0]);
close(cgi_input[1]);
execl(Path.c_str(), Path.c_str(), NULL);
exit(0);
}
else
{
string cgibody;
close(cgi_output[1]);
close(cgi_input[0]);
for(int i=0;i<databody.size();++i)
{
write(cgi_input[1],&databody[i] ,1 );
}
// write(cgi_input[1],databody.c_str() ,databody.size() );
#if 1
while (read(cgi_output[0], &c, 1) > 0)
{
cgibody+=c;
}
#endif
close(cgi_output[0]);
close(cgi_input[1]);
resp->setBody(cgibody);
waitpid(pid, &status, 0);
}
}//POST END
else if(req.method()==HttpRequest::GET)//GET
{
resp->setStatusCode(HttpResponse::CODE_200);
resp->setStatusMessage("OK");
if(pipe(cgi_output)<0)
{
printf("pipe error\r\n");
return ;
}
if(pipe(cgi_input)<0)
{
printf("pipe error\r\n");
return ;
}
if ( (pid = fork()) < 0 )
{
printf("fork error\r\n");
return;
}
if(pid==0)
{
char meth_env[255];
char query_env[255];
string Path(req.path().begin()+1,req.path().end());
string Query(req.query().begin()+1,req.query().end());
sprintf(meth_env, "REQUEST_METHOD=%s", req.methodString());
sprintf(query_env, "QUERY_STRING=%s", Query.c_str());
dup2(cgi_output[1], 1);
dup2(cgi_input[0], 0);
close(cgi_output[0]);
close(cgi_input[1]);
putenv(meth_env);
putenv(query_env);
execl(Path.c_str(), Path.c_str(), NULL);
exit(0);
}
else
{
string cgibody;
close(cgi_output[1]);
close(cgi_input[0]);
#if 1
while (read(cgi_output[0], &c, 1) > 0)
{
cgibody+=c;
}
#endif
close(cgi_output[0]);
close(cgi_input[1]);
resp->setBody(cgibody);
waitpid(pid, &status, 0);
}
}//GET END
else
{
// Bad Request!
//FIXME:
return ;
}
#endif
//cgi
}
int main(int argc, char* argv[])
{
int numThreads = 0;
if (argc > 1)
{
//benchmark = true;
muduo::Logger::setLogLevel(muduo::Logger::WARN);
numThreads = atoi(argv[1]);
}
muduo::net::EventLoop loop;
HttpServer server(&loop, muduo::net::InetAddress(8000) );
server.setHttpCallback(onRequest);
server.setThreadNum(numThreads);
server.start();
loop.loop();
return 0;
}