1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
+
+
+
+
|
#include <sys/sendfile.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
/* Default values */
#define MAX_FAILURE_COUNT 30
#define PORT 8080
#define THREAD_COUNT 10
#define BIND_ADDR "::"
|
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
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
|
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
}
static int filed_logging_thread_init(void) {
/* XXX:TODO: Unimplemented */
return(0);
}
struct filed_fileinfo {
int fd;
size_t len;
};
static void filed_handle_client(int fd) {
static struct filed_fileinfo *filed_open_file(const char *path, struct filed_fileinfo *buffer) {
/* XXX:TODO: Cache file descriptors */
off_t len;
int fd;
fd = open(path, O_RDONLY);
if (fd < 0) {
return(NULL);
}
len = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
buffer->fd = fd;
buffer->len = len;
return(buffer);
}
static char *filed_get_http_request(FILE *fp, char *buffer, size_t buffer_len) {
/* XXX:TODO: Unimplemented */
setlinebuf(fp);
fd = fd;
fp = fp;
buffer = buffer;
buffer_len = buffer_len;
fflush(fp);
return("./hello_world.txt");
}
static void filed_handle_client(int fd) {
struct filed_fileinfo *fileinfo, fileinfo_b;
char *path, path_b[1010];
FILE *fp;
fp = fdopen(fd, "w+b");
if (fp == NULL) {
close(fd);
return;
}
path = filed_get_http_request(fp, path_b, sizeof(path_b));
if (path == NULL) {
fclose(fp);
return;
}
fileinfo = filed_open_file(path, &fileinfo_b);
if (fileinfo == NULL) {
/* XXX: TODO: Return error page */
} else {
/* XXX: TODO: Send HTTP response header */
sendfile(fd, fileinfo->fd, NULL, fileinfo->len);
close(fileinfo->fd);
}
fclose(fp);
return;
}
static void *filed_worker_thread(void *arg_v) {
struct filed_worker_thread_args *arg;
struct sockaddr_in6 addr;
socklen_t addrlen;
|
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
-
-
-
|
}
/* Reset failure count*/
failure_count = 0;
/* Handle socket */
filed_handle_client(fd);
/* Cleanup */
close(fd);
}
/* XXX:TODO: Report error */
return(NULL);
}
static int filed_worker_threads_init(int fd, int thread_count) {
|
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
+
+
|
/* Ignore */
argc = argc;
argv = argv;
/* Create listening socket */
fd = filed_listen(bind_addr, port);
if (fd < 0) {
perror("filed_listen");
return(1);
}
/* Become a daemon */
/* XXX:TODO: Become a daemon */
/* Initialize */
|