Skip to content

Commit 0777979

Browse files
author
tleydxdy
committed
somehow, 1208 bytes
1 parent 8eb816c commit 0777979

File tree

3 files changed

+17
-22
lines changed

3 files changed

+17
-22
lines changed

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ gcc \
1616
-Wl,--build-id=none \
1717
start.S httpd.c \
1818
-o httpd &&
19-
strip -R .comment -R .bss httpd
19+
strip -R .comment httpd

httpd.c

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ int setsockopt(int socket, int level, int option_name, const void *option_value,
3535
int fork();
3636
void exit(int status);
3737

38-
/* this is fine because forked processes are copy on write */
39-
/* so there shouldn't be any data races */
40-
static char http_buf[8192];
41-
4238
static size_t strlen(const char *s) {
4339
const char *p = s;
4440
while (*p)
@@ -78,22 +74,20 @@ static uint16_t swap_uint16(uint16_t x) {
7874
#define perror(s)
7975
#endif
8076

81-
static int tcp_listen(int port) {
82-
static int yes = 1;
83-
static sockaddr_in_t addr = {AF_INET};
84-
addr.sin_port = swap_uint16(port);
77+
int tcp_listen(const sockaddr_in_t *addr, const void *option_value,
78+
socklen_t option_len) {
8579
int sock;
8680
if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0 ||
87-
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) ||
88-
bind(sock, &addr, sizeof(addr)) || listen(sock, 10)) {
81+
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, option_value, option_len) ||
82+
bind(sock, addr, sizeof(sockaddr_in_t)) || listen(sock, 10)) {
8983
die("listen");
9084
}
9185
return sock;
9286
}
9387

94-
static void http_consume(int clientfd) {
88+
static void http_consume(int clientfd, char *http_buf, size_t buf_len) {
9589
int n;
96-
while ((n = read(clientfd, http_buf, sizeof(http_buf))) > 0) {
90+
while ((n = read(clientfd, http_buf, buf_len)) > 0) {
9791
printn(http_buf, n);
9892
const char *p = http_buf + (n - 3);
9993
if (n < 3 || (*p == '\n' && *(p + 1) == '\r' && *(p + 2) == '\n')) {
@@ -125,16 +119,17 @@ static void http_drop(int clientfd) {
125119

126120
#define http_code(fd, x) fprintl(fd, "HTTP/1.1 " x "\r\n\r\n" x);
127121

128-
static int http_serve(int clientfd, const char *file_path) {
122+
static int http_serve(int clientfd, const char *file_path, char *http_buf,
123+
size_t buf_len) {
129124
int f, n;
130-
http_consume(clientfd);
125+
http_consume(clientfd, http_buf, buf_len);
131126
if ((f = open(file_path, O_RDONLY)) < 0) {
132127
perror("open");
133128
http_code(clientfd, "404 Not Found");
134129
return 1;
135130
}
136131
fprintl(clientfd, "HTTP/1.1 200 OK\r\n\r\n");
137-
while ((n = read(f, http_buf, sizeof(http_buf))) > 0) {
132+
while ((n = read(f, http_buf, buf_len)) > 0) {
138133
if (write(clientfd, http_buf, n) < 0) {
139134
perror("write");
140135
return 1;
@@ -155,7 +150,7 @@ static uint16_t string2port(const char *s) {
155150
}
156151
res = res * 10 + *s - '0';
157152
}
158-
return res;
153+
return swap_uint16(res);
159154
}
160155

161156
static void usage(const char *self) {
@@ -168,18 +163,21 @@ static void usage(const char *self) {
168163
int main(int argc, char *argv[]) {
169164
int sock;
170165
uint16_t port;
166+
char http_buf[8192];
171167
if (argc != 3 || (port = string2port(argv[1])) == 0) {
172168
usage(argv[0]);
173169
}
174-
sock = tcp_listen(port);
170+
const int yes = 1;
171+
const sockaddr_in_t addr = {AF_INET, port, 0};
172+
sock = tcp_listen(&addr, &yes, sizeof(yes));
175173
while (1) {
176174
int pid, clientfd;
177175
if ((clientfd = accept(sock, 0, 0)) < 0) {
178176
perror("accept");
179177
} else if ((pid = fork()) < 0) {
180178
perror("fork");
181179
} else if (pid == 0) {
182-
return http_serve(clientfd, argv[2]);
180+
return http_serve(clientfd, argv[2], http_buf, sizeof(http_buf));
183181
}
184182
}
185183
return 0;

start.S

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ c(open, 1) /* 02 */
2323
c(write, 1) /* 01 */
2424
.global read /* 00 */
2525
read:
26-
27-
.global _syscall
28-
_syscall:
2926
mov r10,rcx
3027
mov rax,r9
3128
xor r9,r9

0 commit comments

Comments
 (0)