Repository navigation
Expand file tree
/
Copy pathMultiThread_httpd.c
More file actions
344 lines (321 loc) · 8.71 KB
/
Copy pathMultiThread_httpd.c
File metadata and controls
344 lines (321 loc) · 8.71 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//包括后续非 exec 逻辑
#include "MultiThread_httpd.h"
#define DEFAULT "src_html/default.html"
#define IMG "src_html"
#define CGI "src_cgi"
int times=0;
void echo_error(int fd,int _errno)
{
switch(_errno){
case 400://Bad Request //客户端请求有语法错误,不能被服务器所理解
break;
case 404:////请求资源不存在,eg:输入了错误的URL
//printf("*******404\n");
error_all(fd,404,"NOT_FIND!!");
break;
case 401://请求未经授权,这个状态代码必须和WWW-Authenticate报头域一起使用
break;
case 403://服务器收到请求,但是拒绝提供服务
break;
case 500:// Internal Server Error //服务器发生不可预期的错误
break;
case 503://Server Unavailable //服务器当前不能处理客户端的请求,一段时间后可能恢复正常
break;
default:
break;
}
close(fd);
}
void error_all(int fd,int err,char* reason)
{
char buf[_SIZE_]="";
char error[_SIZE_]="";
sprintf(buf,"HTTP/1.1 %d %s\r\n\r\n",err,reason);
sprintf(error," %d %s\n",err,reason);
// printf("err buf:%s\n",buf);
write(fd,buf,strlen(buf));
write(fd,"<html>\n",strlen("<html>\n"));
write(fd,"<head>\n",strlen("<head>\n"));
write(fd,"<h1> HELLO PPH!!!</h1>\n",strlen("<h1> HELLO PPH!!!</h1>\n"));
write(fd,"<h2>\n",strlen("<h2>\n"));
write(fd,error,strlen(error));
write(fd,"</h2>\n",strlen("</h2>\n"));
write(fd,"</head>\n",strlen("</head>\n"));
write(fd,"</html>\n",strlen("</html>\n"));
// echo_html(fd,"src_html/1.html",102400000);
}
int get_line(int sock_fd,char * line){
int index=0;
ssize_t size=1;
char ch=0;
while(ch!='\n'){
if((size=read(sock_fd,&ch,1))<0){
perror("read_get_line");
return -1;
}
if(ch=='\r'){
char tmp=0;
if(recv(sock_fd,&tmp,1,MSG_PEEK)>0){
if(tmp=='\n'){
line[index++]=tmp;
read(sock_fd,&ch,1);
continue;
}
}
}
if(index==1024){
line[1023]=0;
return -1;
}
line[index++]=ch;
}
line[index]=0;
printf("line :%d,%d,%d,%s",index,strlen(line),line[0],line);
if(strcmp(line,"\n")==0){
// printf("*****************");
return 0;
}
return 1;
}
void echo_html(int fd,const char* url,int fd_size ){
char buf[_SIZE_]="HTTP/1.1 200 OK\r\n\r\n";
if(url==NULL){
int rfd=-1;
off_t set=0;
ssize_t size=0;
if((rfd=open(DEFAULT, O_RDONLY))<0){
echo_error(fd,500);
exit(0);
}
// printf("%d %d\n",fd,rfd);
write(fd,buf,strlen(buf));
size=sendfile(fd,rfd,&set,fd_size);
if(size<0){
close(rfd);
printf("senfile error!\n");
return;
}
printf("size: %d\n",size);
close(rfd);
}else{
int rfd=-1;
off_t set=0;
ssize_t size=0;
//printf("url:%s\n",url);
if((rfd=open(url, O_RDONLY))<0){
echo_error(fd,500);
// exit(0);
}
write(fd,buf,strlen(buf));
size=sendfile(fd,rfd,NULL,fd_size);
if(size<0){
printf("senfile error!\n");
return;
}
// printf("size: %d\n",size);
close(rfd);
return;
}
}
//清除client_sock
int clear_buf(int fd)
{
char buf[_SIZE_]="";
ssize_t size=1;
size=read(fd,buf,_SIZE_);
if(size<0){
perror("clear_buf_read");
return -1;
}
return 0;
}
//获取post正文 参数
char* get_length(int fd,char* content_length)
{
int size=1;
int tag=0;
int index=0;
while(size!=0){//通过持续读取一行 直到读到空行结束
size=get_line(fd,content_length);
if(strncasecmp(content_length,"content-length: ",16)==0){
break;
}
if(size==-1){
printf("get line出错\n");
return NULL;
}
}
content_length[strlen(content_length)-1]=0;
strcpy(content_length,content_length+16);
printf("con end: %s\n",content_length);
return content_length;
}
void cgi_action(int fd,char* method,char* url ,char* parameter)
{
char env[20]="METHOD=";
char par[_SIZE_]="PARAMETER=";
int pwrite[2];
if((pipe(pwrite)<0)){
perror("pipe");
return;
}
strcat(env,method);
strcat(par,parameter);
printf(" act url:%s\n",url);
printf("parameter:%s\n",par);
if(putenv(env)<0){
perror("putenv");
return;
}
if(putenv(par)<0){
perror("putenv par");
return;
}
// printf("fork qian\n");
pid_t id=fork();
if(id<0){
perror("fork");
return;
}
else if(id==0){//子进程
close(pwrite[0]);
//printf("child\n");
if(dup2(pwrite[1],1)<0){
perror("dup2.1");
return;
}
if(dup2(fd,0)<0){
perror("dup2.2");
return;
}
if(execl(url,NULL)<0){
perror("execl");
printf("exit url:\n",url);
exit(-2);
}
}
else{//父进程
printf("father start\n");
close(pwrite[1]);
char buf[_SIZE_]="";
int count=0;
int i=0;
ssize_t size=1;
while(size>0){
size=read(pwrite[0],buf,_SIZE_);
if(size<0){
echo_error(fd,500);
break;
}
if(size==0)
break;
write(fd,buf,strlen(buf));
printf("if end?\n");
}
waitpid(0,NULL,0);
close(pwrite[0]);
}
}
void* http_action(void * client_sock){
int fd=(int)client_sock;
struct stat stat_buf;
char line[_SIZE_];
char* method=NULL;
char* version=NULL;
char* url=NULL;
char* parameter=NULL;//get方法 参数
char text[_SIZE_]="";
int cgi=0;
if(get_line(fd,line)==-1){
printf("this is a cache requset , so can't process!\n");
return NULL;
}
int index=strlen(line)-1;
//printf("%d \n",index);
//GET / HTTP/1.1
printf("%d\n index ",index);
while(index>0){//提取method url
if(line[index]==' '&&version==NULL){
version=((char*)line)+index+1;
line[index]=0;
}
if(line[index]==' '&&url==NULL){
url=line+index+1;
line[index]=0;
}
--index;
}
method=line;
char real_url[128]=CGI;
//判断请求方法
if(strcasecmp("POST",method)==0){
printf("real_url :%s\n",real_url);
strcat(real_url,url);
printf("start:%s %s %s",method,url ,version);
get_length(fd,text);
cgi_action(fd,method,real_url,text);
printf("fork ...end!\n");
}
else if(strcasecmp("GET",method)==0){
index=0;
while(url[index]){
if(url[index]=='?'){
cgi=1;
parameter=url+index+1;
url[index]=0;
printf(" get url:%s\n",url);
break;
}
++index;
}
if(cgi==1){
//cgi;
strcat(real_url,url);
printf("start:%s %s %s",method,url ,version);
cgi_action(fd,method,real_url,parameter);
printf("ret cgi\n");
}
else{
if(strcmp("/",url)==0){
if(stat(DEFAULT, &stat_buf)<0){
echo_error(fd,500);
close(fd);
return ;
}
echo_html(fd, NULL,stat_buf.st_size);
}
else{
char real_url[128]="src_html";
strcat(real_url,url);
printf("real_url :%s\n",real_url);
if(stat(real_url, &stat_buf)<0){
echo_error(fd,404);
//printf("times:%d end!\n",times++);
//close(fd);
//return;
}
else{
printf("fd size:%d",stat_buf.st_size);
echo_html(fd,real_url,stat_buf.st_size);
}
}
}
}
else{
echo_error(fd,403);
}
if(strcasecmp(method,"POST")==0){
printf("fork ...end!\n");
close(fd);
printf("times:%d end!\n",times++);
return;
}
if(clear_buf(fd)<0){
printf("clear error\n");
}
else
printf("clear success!\n");
printf("times:%d end!\n",times++);
close(fd);
return;
}