-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssws_core.c
198 lines (162 loc) · 4.76 KB
/
ssws_core.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
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
/*
* Copyright (C) Cihangir Akturk 2012
*
* This file is part of chngr's simple stupid web server (ssws).
*
* ssws is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ssws is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ssws. If not, see <http://www.gnu.org/licenses/>.
*/
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h> /* former <arpa/inet.h>*/
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "http_header_parser.h"
#include "server_mem.h"
static struct server_buf app_buf;
static inline size_t get_fsize(int fd)
{
size_t len = (size_t)lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
return len;
}
static int get_server_fd(const char *port)
{
struct addrinfo hints;
struct addrinfo *result, *rp;
int fd = -1, t = 1;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if (getaddrinfo(NULL, port, &hints, &result) != 0)
return fd;
for (rp = result; rp != NULL; rp = rp->ai_next) {
fd = socket(rp->ai_family, rp->ai_socktype,
rp->ai_protocol);
if (fd == -1)
continue;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &t, sizeof(t));
if (bind(fd, rp->ai_addr, rp->ai_addrlen) == 0)
break;
close(fd);
}
if (!rp)
fd = -1;
freeaddrinfo(result);
return fd;
}
/*
* Send all chunks in one go
* On success, number of bytes sent is returned
* on error, -1 returned
*/
static size_t sendall(int fd, char *buf, size_t n)
{
size_t len, nsent = 0;
do {
len = write(fd, buf + nsent, n - nsent);
if (len == -1)
break;
nsent += len;
} while (nsent < n);
return len == -1 ? -1 : nsent;
}
static int handle_request(int sock_fd, struct http_header *hdr,
struct server_buf *mem)
{
char *out_buffer = mem->out_buf;
int fd, status = OK;
size_t len;
/* Don't allow access to parent dirs */
if (strstr(hdr->request_path, ".."))
status = FORBIDDEN;
filename(out_buffer, 32, hdr->request_path);
if (status == OK) {
fd = open(out_buffer, O_RDONLY);
const char *mim = mimestr(hdr->request_path);
if (fd != -1) {
len = get_fsize(fd);
snprintf(out_buffer, BUFSIZE, HEADER_STR,
OK, "OK", SRV_FULL_NAME, len,
mim);
sendall(sock_fd, out_buffer, strlen(out_buffer));
} else {
status = NOT_FOUND;
}
}
switch (hdr->request_type) {
case GET:
if (status == OK) {
while ((len = read(fd, out_buffer, BUFSIZE)) > 0)
sendall(sock_fd, out_buffer, len);
}
break;
case HEAD:
/* Just sent out the header */
break;
}
close(fd);
return 0;
}
int ssws_init(const char *port, const char *doc_root)
{
struct sockaddr cli_addr;
size_t size;
socklen_t cli_sock_len;
int cli_fd;
int sock_fd = get_server_fd(port);
if (sock_fd <= 0)
return sock_fd;
if (listen(sock_fd, 12) != 0) {
printf("listen: %d failed\n", sock_fd);
close(sock_fd);
return -1;
}
for (;;) {
cli_fd = accept(sock_fd, &cli_addr, &cli_sock_len);
if (cli_fd == -1) {
printf("accept error: %d\n", cli_fd);
close(sock_fd);
return -1;
}
size = read(cli_fd, app_buf.in_buf, BUFSIZE);
if (size > 0) {
app_buf.in_buf[size] = '\0';
pid_t pid = fork();
if (pid < 0)
exit(EXIT_FAILURE);
/* child process */
if (pid == 0) {
close(sock_fd);
struct http_header header;
parse_header(&header, app_buf.in_buf, BUFSIZE);
handle_request(cli_fd, &header, &app_buf);
printf("header:\n%d, %s, %s\n",
header.request_type,
header.request_path,
header.user_agent);
exit(EXIT_SUCCESS);
} else {
close(cli_fd);
}
}
}
close(sock_fd);
return sock_fd;
}