-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.c
275 lines (228 loc) · 6.98 KB
/
response.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#define _DEFAULT_SOURCE
#include <dirent.h>
#include <openssl/ssl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "config.h"
#include "file.h"
#include "mime.h"
#include "response.h"
#include "url.h"
extern struct gem_config cfg;
/* helper */
static int write_ssl(SSL *ssl, const char *str) {
return SSL_write(ssl, str, (int)strlen(str));
}
/* iterate (alphanumerically) all of the files but "." and ".." */
/* write them as a gemtext document, where each file is written */
/* as a link, making distinction between other dir's and regular files. */
static void iterate_dir(const char *path, SSL *ssl) {
char buffer[4096];
char escaped[2048];
char header[256] = {0};
char meta[32] = {0};
struct dirent **files;
struct pfs_data pfs;
int qty, i;
size_t size;
if (!path || !ssl) {
return;
}
/* get a LL of all files in path, sorted alphabetically */
qty = scandir(path, &files, NULL, alphasort);
if (qty == -1) {
printf("scandir() error: %s\n", path);
return;
}
/* write gemini response for gemtext document */
sprintf(header, "20 text/gemini");
if (ENABLE_CHARSET_LOOKUP) {
memset(meta, 0, 32);
if (!file_read_dir_meta(path, ".charset", meta, 32)) {
strcat(header, "; charset=");
strcat(header, meta);
} else if (GEM_USE_DEFAULT_META) {
/* file read failed but can use default */
strncpy(meta, GEM_DEFAULT_CHARSET, 32);
strcat(header, "; charset=");
strcat(header, meta);
}
}
if (ENABLE_LANG_LOOKUP) {
memset(meta, 0, 32);
if (!file_read_dir_meta(path, ".lang", meta, 32)) {
strcat(header, "; lang=");
strcat(header, meta);
} else if (GEM_USE_DEFAULT_META) {
/* file read failed but can use default */
strncpy(meta, GEM_DEFAULT_LANG, 32);
strcat(header, "; lang=");
strcat(header, meta);
}
}
strcat(header, "\r\nIndex\n");
if (write_ssl(ssl, header) <= 0) {
goto EXIT;
}
for(i=0; i<qty; i++) {
if (!ENUMERATE_DOT_FILES) {
if (files[i]->d_name[0] == '.') {
continue;
}
} else {
/* skip rel files . and .. */
if (!strcmp(files[i]->d_name, ".") || !strcmp(files[i]->d_name, "..")) {
continue;
}
}
sprintf(buffer, "%s%s", path, files[i]->d_name);
if ((!url_encode(buffer, escaped, 2048)) != 0) {
fprintf(stderr, "unable to url_encode \"%s\"\n", buffer);
goto EXIT;
}
if (files[i]->d_type == DT_DIR) {
sprintf(buffer, "=> %s/ %s/\n", escaped, files[i]->d_name);
} else {
size = filesize(buffer);
pfs = pretty_filesize(size);
sprintf(buffer,
pfs.type ? "=> %s %s <%.2f %s>\n" : "=> %s %s <%.0f %s>\n",
escaped, files[i]->d_name, (double)pfs.value,
pfs.type ? pfs.type : "B");
}
if (write_ssl(ssl, buffer) <= 0) {
goto EXIT;
}
}
EXIT:
while (qty--) {
free(files[qty]);
}
free(files);
}
/* attempt to transfer the file over ssl in chunks */
/* non-zero return means error */
static int file_transfer(const char *path, SSL *ssl) {
char header[256] = {0};
char meta[32] = {0};
const mime_t *mime;
char *buf;
int err;
FILE *f;
size_t n;
err = 1; /* default err */
if (!path || !ssl) {
return 1;
}
if (!(mime = mime_type(path))) {
return 1;
}
if (!(f = fopen(path, "rb"))) {
return 1;
}
if (!(buf = malloc(GEM_XFER_CHUNK_SIZ))) {
goto EXIT;
}
sprintf(header, "20 %s", mime->mime);
if (ENABLE_CHARSET_LOOKUP && mime->type & MIME_TYPE_TEXT) {
memset(meta, 0, 32);
if (!file_read_dir_meta(path, ".charset", meta, 32)) {
strcat(header, "; charset=");
strcat(header, meta);
} else if (GEM_USE_DEFAULT_META) {
/* file read failed but can use default */
strncpy(meta, GEM_DEFAULT_CHARSET, 32);
strcat(header, "; charset=");
strcat(header, meta);
}
}
if (ENABLE_LANG_LOOKUP && mime->type & MIME_TYPE_TEXT) {
memset(meta, 0, 32);
if (!file_read_dir_meta(path, ".lang", meta, 32)) {
strcat(header, "; lang=");
strcat(header, meta);
} else if (GEM_USE_DEFAULT_META) {
/* file read failed but can use default */
strncpy(meta, GEM_DEFAULT_LANG, 32);
strcat(header, "; lang=");
strcat(header, meta);
}
}
strcat(header, "\r\n");
if (write_ssl(ssl, header) <= 0) {
goto EXIT;
}
while(!feof(f)) {
n = fread(buf, 1, GEM_XFER_CHUNK_SIZ, f);
if (n < GEM_XFER_CHUNK_SIZ && ferror(f)) {
perror("fread()");
goto EXIT;
}
if (SSL_write(ssl, buf, (int)n) <= 0) {
perror("SSL_write()");
goto EXIT;
}
}
err = 0;
EXIT:
free(buf);
fclose(f);
return err;
}
/* attempts to locate a file in the docroot and serve it */
/* non-zero return means an error has occurred */
int resp_serve_file(struct gem_uri *u, SSL *ssl) {
int index;
char buf[2048];
if (!u || !ssl) {
return 3;
}
/* copy user-provided path to buf */
sprintf(buf, "%s", u->path);
if (!file_exists(buf)) {
return RESP_FILE_NOT_FOUND;
}
if (file_is_dir(buf)) {
/* ensure there's enough space to append '/' */
if (strlen(buf) < sizeof(buf) - 1) {
if (buf[strlen(buf) - 1] != '/') {
strcat(buf, "/");
}
} else {
return RESP_FILE_PATH_TOO_LONG;
}
/* check if directory contains an index file */
index = dir_has_index(buf);
/* no index file present, and dir enumeration is enabled */
if (!index && cfg.enumerate) {
iterate_dir(buf, ssl);
return 0;
}
/* index file is present so don't enum dir regardless */
if (index) {
/* ensure there's enough space to append index file */
if (strlen(buf) + strlen(cfg.index) < sizeof(buf)) {
strcat(buf, cfg.index);
} else {
return RESP_FILE_PATH_TOO_LONG;
}
} else {
/* no directory enumeration and no index file, send error */
resp_error(RESP_STATUS_CERT_NOT_AUTH, ssl);
return 0;
}
}
/* if file transfer failed */
if (file_transfer(buf, ssl)) {
return RESP_FILE_TRANSFER;
}
return 0;
}
/* write a gemini error code to client */
int resp_error(const char *code, SSL *ssl) {
char buffer[16];
sprintf(buffer, "%s\r\n", code);
return write_ssl(ssl, buffer);
}