-
Notifications
You must be signed in to change notification settings - Fork 10
/
ev_httpd.c
149 lines (127 loc) · 3.58 KB
/
ev_httpd.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
143
144
145
146
147
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include "ev_type.h"
#include "ev_loop.h"
static int setnonblocking(int fd);
static int ev_listen(int port);
void *cb_accept(ev_loop_t *loop, int fd, EV_TYPE events);
void *cb_parse_request(ev_loop_t *loop, int fd, EV_TYPE events);
void *cb_do_response(ev_loop_t *loop, int fd, EV_TYPE events);
int main(int argc, char **argv) {
if (argc != 2) {
printf("usage: ./ev_httpd port\n");
return -1;
}
int ret;
ev_loop_t *loop;
int listen_sock;
listen_sock = ev_listen(atoi(argv[1]));
if (listen_sock == -1) {
return -1;
}
loop = ev_create_loop();
ev_io_init(loop, 1024, 0);
ret = ev_io_register(loop, listen_sock, EV_READ, cb_accept, NULL);
if (ret == -1) {
fprintf(stderr, "register listen sock error\n");
return -1;
}
printf("ev_httpd started successfully!\n");
ev_run_loop(loop);
return 0;
}
void *cb_accept(ev_loop_t *loop, int fd, EV_TYPE events) {
struct sockaddr_in client_sock;
socklen_t len = sizeof(client_sock);
int conn_fd;
while ((conn_fd = accept(fd, (struct sockaddr *)&client_sock, &len)) > 0) {
/*limit the number of connections*/
if (conn_fd >= loop->maxevent) {
fprintf(stderr, "Warn: too many connections come, \
exceeds the maximum num of the configuration!\n");
close(conn_fd);
return NULL;
}
setnonblocking(conn_fd);
int ret = ev_io_register(loop, conn_fd, EV_READ, cb_parse_request, NULL);
if (ret == -1) {
fprintf(stderr, "ev io_register err in cd_accept()\n");
close(conn_fd);
return NULL;
}
}
if (-1 == conn_fd) {
if (errno != EAGAIN) {
fprintf(stderr, "accpet err: %s\n", strerror(errno));
return NULL;
}
}
return NULL;
}
void *cb_parse_request(ev_loop_t *loop, int fd, EV_TYPE events) {
//printf("in read\n");
char tmp[1024];
while(read(fd, tmp, 1024) > 0);
int ret;
ret = ev_io_stop(loop, fd, EV_READ);
ret = ev_io_register(loop, fd, EV_WRITE, cb_do_response, NULL);
if (ret == -1) {
close(fd);
}
return NULL;
}
#define OUTPUT_STRING "hello evlib!"
void *cb_do_response(ev_loop_t *loop, int fd, EV_TYPE events) {
//printf("in write\n");
write(fd, OUTPUT_STRING, strlen(OUTPUT_STRING));
ev_io_unregister(loop, fd);
close(fd);
return NULL;
}
static
int ev_listen(int port) {
signal(SIGPIPE, SIG_IGN);
struct sockaddr_in server_addr;
int listen_sock;
listen_sock = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == listen_sock) {
fprintf(stderr, "create sock err: %s\n", strerror(errno));
return -1;
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_port = htons(port);
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
int ret;
ret = bind(listen_sock, (struct sockaddr *)&server_addr, sizeof(server_addr));
if (-1 == ret) {
fprintf(stderr, "bind err: %s\n", strerror(errno));
return -1;
}
setnonblocking(listen_sock);
int reuse = 1;
setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
if (-1 == listen(listen_sock, SOMAXCONN)) {
fprintf(stderr, "listen err: %s\n", strerror(errno));
return -1;
}
return listen_sock;
}
static
int setnonblocking(int fd) {
int flags;
if (-1 == (flags = fcntl(fd, F_GETFL, 0))) {
perror("setnonblocking error");
return -1;
}
return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
}