-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
144 lines (110 loc) · 4.32 KB
/
server.c
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
//
// Created by user on 7/18/16.
//
#include "server.h"
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <sys/epoll.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <malloc.h>
#define EPOLL_SIZE 10000
#define BUF_LEN 1024
static char Resp_200[] = "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n";
static char Resp_404[] = "HTTP/1.0 404 Not found\r\n\r\n";
int setNonblock(int fd)
{
int fdflags = fcntl(fd, F_GETFL, 0);
if (fdflags == -1)
return -1;
fdflags |= O_NONBLOCK;
if (fcntl(fd, F_SETFL, fdflags) == -1)
return -1;
return 0;
}
void *runServer(void* value)
{
int i = 0;
Settings *settings = (Settings *)value;
struct sockaddr_in sockAddr;
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons(settings->port);
int master = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (0 >= inet_pton(AF_INET, settings->ip, &sockAddr.sin_addr.s_addr))
fprintf(stderr,"ERROR: wrong IP address\n");
bind(master, (struct sockaddr *)(&sockAddr), sizeof(sockAddr));
setNonblock(master);
listen(master, SOMAXCONN);
int epoll = epoll_create1(0);
struct epoll_event epollEvent;
epollEvent.data.fd = master;
epollEvent.events = EPOLLIN;
epoll_ctl(epoll, EPOLL_CTL_ADD, master, &epollEvent);
while(1) {
struct epoll_event events[EPOLL_SIZE];
int numEvents = epoll_wait(epoll, events, EPOLL_SIZE, -1);
for (i = 0; i < numEvents; ++i) {
if (events[i].data.fd == master) {
int slave = accept (master, 0, 0);
struct epoll_event event;
event.data.fd = slave;
event.events = EPOLLIN;
setNonblock (slave);
epoll_ctl(epoll, EPOLL_CTL_ADD, slave, &event);
} else {
char query[BUF_LEN];
memset(query, 0, sizeof(query));
if (0 < recv(events[i].data.fd, query, BUF_LEN, MSG_NOSIGNAL)) {
int start = 0;
int queryLen = strlen(query);
toLog("query src: ", query);
while((query[start] != '/') && (start < queryLen))
start++;
int finish = start;
while((query[finish] != ' ')
&& (query[finish] != '?')
&& (finish < queryLen))
finish++;
char queryPath[BUF_LEN];
memset(queryPath, 0, sizeof(queryPath));
strcat(queryPath, settings->path);
int pathLen = strlen(queryPath);
strncpy(queryPath + pathLen, query+start, finish-start);
queryPath[pathLen + finish-start] = '\0';
toLog("queryPath: ", queryPath);
FILE *f = fopen(queryPath, "r");
struct stat statbuf;
stat(queryPath, &statbuf);
if((f != NULL) && !S_ISDIR(statbuf.st_mode)) {
toLog("TRY to read:", queryPath);
char *buf;
size_t len;
fseek(f, 0, SEEK_END);
len = ftell(f);
int respLen = strlen(Resp_200);
buf = (char *) malloc(len + respLen + 1);
memset(buf, 0, sizeof(len + respLen));
strcpy(buf, Resp_200);
fseek(f, 0, SEEK_SET);
fread(buf + respLen, 1, len, f);
fclose(f);
buf[len + respLen] = '\0';
toLog("buf send: ", buf);
send(events[i].data.fd, buf, strlen(buf), MSG_NOSIGNAL);
} else {
toLog("WRONG: ", Resp_404);
send(events[i].data.fd, Resp_404, strlen(Resp_404), MSG_NOSIGNAL);
}
shutdown(events[i].data.fd, SHUT_RDWR);
close(events[i].data.fd);
} else if (errno != EAGAIN) {
shutdown(events[i].data.fd, SHUT_RDWR);
close(events[i].data.fd);
}
}
}
}
}