-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpsrv.c
More file actions
171 lines (133 loc) · 3.4 KB
/
httpsrv.c
File metadata and controls
171 lines (133 loc) · 3.4 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
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <mqueue.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#define THREADS_COUNT 10
char *host = "127.0.0.1";
char *directory = "./";
int port = 12345;
char *mqueue_name = "/mqueue.mq";
mqd_t mqueue;
void http_client(int nd)
{
char buf[1024];
int size = recv(nd, buf, sizeof(buf), MSG_NOSIGNAL);
if (size <= 0) {
printf("recv: %d : %s\n", size, strerror(errno));
return;
}
buf[size] = '\0';
printf("recv: %d\n%s\n", size, buf);
char file[128];
strcpy(file, directory);
int ret = sscanf(buf, "GET %s", file + strlen(file));
assert(ret == 1);
char *ptr = strchr(file, '?');
if (ptr != '\0') {
*ptr = '\0';
}
printf("file: %s\n\n", file);
FILE *fd = fopen(file, "r");
if (fd == NULL) {
snprintf(buf, sizeof(buf), "HTTP/1.0 404 Not Found\r\nContent-Length: 0\r\nConnection: close\r\n\r\n");
} else {
char fb[1024];
size = fread(fb, 1, sizeof(fb), fd);
assert(size >= 0);
fb[size] = '\0';
fclose(fd);
snprintf(buf, sizeof(buf), "HTTP/1.0 200 OK\r\nContent-Length: %d\r\nConnection: close\r\nContent-Type: text/html\r\n\r\n%s", strlen(fb), fb);
}
printf("send: %d\n%s\n", strlen(buf), buf);
size = send(nd, buf, strlen(buf), MSG_NOSIGNAL);
assert(size == strlen(buf));
}
void *thread_fn(void *data)
{
int ret;
int sd;
while (1) {
ret = mq_receive(mqueue, (char *)&sd, sizeof(sd), NULL);
if (ret == sizeof(sd)) {
http_client(sd);
close(sd);
}
}
}
void server_fn()
{
printf("host: %s port: %d directory: %s\n", host, port, directory);
int sd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
assert(sd != -1);
int optval = 1;
assert(setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (void *) &optval, (socklen_t) sizeof(optval)) != -1);
struct sockaddr_in sa;
sa.sin_family = AF_INET;
sa.sin_port = htons(port);
inet_aton(host, &sa.sin_addr);
assert(bind(sd, (struct sockaddr *)&sa, sizeof(sa)) != -1);
assert(listen(sd, SOMAXCONN) != -1);
while(1) {
int nd = accept(sd, NULL, 0);
assert(nd != -1);
assert(mq_send(mqueue, (const char *)&nd, sizeof(nd), 1) == 0);
}
close(sd);
}
int main(int argc, char **argv)
{
int opt;
while ((opt = getopt(argc, argv, "h:p:d:")) != -1) {
switch(opt) {
case 'h':
host = optarg;
break;
case 'p':
port = atoi(optarg);
break;
case 'd':
directory = optarg;
break;
}
}
signal(SIGCHLD, SIG_IGN);
daemon(1, 1);
char file[128];
snprintf(file, sizeof(file), "%s.access.log", argv[0]);
int fd = open(file, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
assert(fd >= 0);
close(fileno(stdin));
close(fileno(stdout));
close(fileno(stderr));
dup(fd);
dup(fd);
dup(fd);
struct mq_attr ma;
ma.mq_flags = 0; // blocking read/write
ma.mq_maxmsg = 10; // maximum number of messages allowed in queue
ma.mq_msgsize = sizeof(int);
ma.mq_curmsgs = 0; // number of messages currently in queue
mq_unlink(mqueue_name);
mqueue = mq_open(mqueue_name, O_RDWR | O_CREAT, 0660, &ma);
assert(mqueue != -1);
pthread_t id[THREADS_COUNT];
int i;
for (i = 0; i < THREADS_COUNT; ++i) {
pthread_create(&id[i], NULL, thread_fn, NULL);
}
server_fn();
mq_close(mqueue);
close(fd);
return 0;
}