-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfork_server.c
More file actions
208 lines (178 loc) · 5.24 KB
/
fork_server.c
File metadata and controls
208 lines (178 loc) · 5.24 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <netdb.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#define PORT "80" /* Port to listen on */
#define BACKLOG 10 /* Passed to listen() */
#define BUFSIZE 8096
struct {
char *ext;
char *filetype;
} extensions [] = {
{"gif", "image/gif" },
{"jpg", "image/jpeg"},
{"jpeg","image/jpeg"},
{"png", "image/png" },
{"zip", "image/zip" },
{"gz", "image/gz" },
{"tar", "image/tar" },
{"htm", "text/html" },
{"html","text/html" },
{"exe","text/plain" },
{0,0}
};
/* Signal handler to reap zombie processes */
static void wait_for_child(int sig)
{
while (waitpid(-1, NULL, WNOHANG) > 0);
}
void handle(int newsock)
{
int j, file_fd, buflen, len;
long i, ret;
char * fstr;
static char buffer[BUFSIZE+1];
ret = read(newsock,buffer,BUFSIZE); /* 讀取瀏覽器要求 */
if (ret==0||ret==-1) {
/* 網路連線有問題,所以結束行程 */
exit(3);
}
/* 程式技巧:在讀取到的字串結尾補空字元,方便後續程式判斷結尾 */
if (ret>0&&ret<BUFSIZE)
buffer[ret] = 0;
else
buffer[0] = 0;
/* 移除換行字元 */
for (i=0;i<ret;i++)
if (buffer[i]=='\r'||buffer[i]=='\n')
buffer[i] = 0;
/* 只接受 GET 命令要求 */
if (strncmp(buffer,"GET ",4)&&strncmp(buffer,"get ",4))
exit(3);
/* 我們要把 GET /index.html HTTP/1.0 後面的 HTTP/1.0 用空字元隔開 */
for(i=4;i<BUFSIZE;i++) {
if(buffer[i] == ' ') {
buffer[i] = 0;
break;
}
}
/* 檔掉回上層目錄的路徑『..』 */
for (j=0;j<i-1;j++)
if (buffer[j]=='.'&&buffer[j+1]=='.')
exit(3);
/* 當客戶端要求根目錄時讀取 index.html */
if (!strncmp(&buffer[0],"GET /\0",6)||!strncmp(&buffer[0],"get /\0",6) )
strcpy(buffer,"GET /index.html\0");
/* 檢查客戶端所要求的檔案格式 */
buflen = strlen(buffer);
fstr = (char *)0;
for(i=0;extensions[i].ext!=0;i++) {
len = strlen(extensions[i].ext);
if(!strncmp(&buffer[buflen-len], extensions[i].ext, len)) {
fstr = extensions[i].filetype;
break;
}
}
/* 檔案格式不支援 */
if(fstr == 0) {
fstr = extensions[i-1].filetype;
}
/* 開啟檔案 */
if((file_fd=open(&buffer[5],O_RDONLY))==-1)
write(newsock, "Failed to open file", 19);
/* 傳回瀏覽器成功碼 200 和內容的格式 */
sprintf(buffer,"HTTP/1.0 200 OK\r\nContent-Type: %s\r\n\r\n", fstr);
write(newsock,buffer,strlen(buffer));
/* 讀取檔案內容輸出到客戶端瀏覽器 */
while ((ret=read(file_fd, buffer, BUFSIZE))>0) {
write(newsock,buffer,ret);
}
exit(1);
}
int main(void)
{
int sock;
struct sigaction sa;
struct addrinfo hints, *res;
int reuseaddr = 1; /* True */
/* Get the address info */
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo(NULL, PORT, &hints, &res) != 0) {
perror("getaddrinfo");
return 1;
}
/* Create the socket */
sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (sock == -1) {
perror("socket");
return 1;
}
/* Enable the socket to reuse the address */
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(int)) == -1) {
perror("setsockopt");
return 1;
}
/* Bind to the address */
if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
perror("bind");
return 1;
}
/* Listen */
if (listen(sock, BACKLOG) == -1) {
perror("listen");
return 1;
}
freeaddrinfo(res);
/* Set up the signal handler */
sa.sa_handler = wait_for_child;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGCHLD, &sa, NULL) == -1) {
perror("sigaction");
return 1;
}
/* Main loop */
while (1) {
struct sockaddr_in their_addr;
socklen_t size = sizeof(struct sockaddr_in);
int newsock = accept(sock, (struct sockaddr*)&their_addr, &size);
int pid;
if (newsock == -1) {
perror("accept");
return 0;
}
printf("Got a connection from %s on port %d\n", inet_ntoa(their_addr.sin_addr),
htons(their_addr.sin_port));
pid = fork();
if (pid == 0) {
/* In child process */
close(sock);
handle(newsock);
return 0;
}
else {
/* Parent process */
if (pid == -1) {
perror("fork");
return 1;
}
else {
close(newsock);
}
}
}
close(sock);
return 0;
}