-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimplefmap.hcpp
More file actions
621 lines (540 loc) · 18.9 KB
/
simplefmap.hcpp
File metadata and controls
621 lines (540 loc) · 18.9 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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
#include "config.h"
#include <sys/types.h> // stat opendir
#include <sys/stat.h> // stat
#include <unistd.h> // stat
#include <fcntl.h> // open
#include <string.h> // strerror
#include <errno.h> // errno
#include <limits.h> // realpath
#include <stdlib.h> // realpath
#include <dirent.h> // opendir
#include <unistd.h> // readdir_r pathconf
#include <sys/mman.h> // mmap
#include <bulkrays/bulkrays.h>
namespace simplefmap {
using namespace std;
using namespace bulkrays;
// some goodies in order to quickly output a full date : cout << os_time_t (mytime_t_var) << ....
struct ostream_time_t { time_t f_param; };
inline ostream_time_t
os_time_t(time_t param)
{
ostream_time_t funct_struct;
funct_struct.f_param = param;
return funct_struct;
}
template<typename _CharT, typename _Traits>
inline basic_ostream<_CharT,_Traits>&
operator<<(basic_ostream<_CharT,_Traits>& out, ostream_time_t f_struct)
{
time_t t = f_struct.f_param;
struct tm tt;
localtime_r (&t, &tt);
return out << setfill('0')
<< setw(4) << tt.tm_year+1900 << "/"
<< setw(2) << tt.tm_mon+1 << "/"
<< setw(2) << tt.tm_mday << " "
<< setw(2) << tt.tm_hour << ":"
<< setw(2) << tt.tm_min << ":"
<< setw(2) << tt.tm_sec;
}
struct ostream_off_t { off_t f_param; };
inline ostream_off_t
os_off_t (off_t param)
{
ostream_off_t funct_struct;
funct_struct.f_param = param;
return funct_struct;
}
// template<typename _CharT, typename _Traits>
// inline basic_ostream<_CharT,_Traits>&
// operator<<(basic_ostream<_CharT,_Traits>& out, ostream_off_t f_struct)
inline ostream& operator<< (ostream& out, ostream_off_t f_struct)
{
static const char * mult[] = {
" ", "K", "M", "G", "T", "P", "E", "Z", "Y"
};
off_t reminder = f_struct.f_param;
off_t prereminder = 0;
int pow = 0;
while ((pow<5) && (reminder > 1024)) {
prereminder = reminder % 1024;
reminder >>= 10; /* reminder /= 1024 */
pow ++;
}
if (pow == 0)
return out << reminder << " ";
else
return out << reminder << '.' << (prereminder * 10)/1024 << mult[pow];
}
// ostream& operator<< (ostream& out, const time_t &t) {
// struct tm tt;
// localtime_r (&t, &tt);
//
// return out << setfill('0')
// << setw(4) << tt.tm_year+1900 << "-"
// << setw(2) << tt.tm_mon+1 << "-"
// << setw(2) << tt.tm_mday << " "
// << setw(2) << tt.tm_hour << ":"
// << setw(2) << tt.tm_min << ":"
// << setw(2) << tt.tm_sec;
// }
class direntry {
public:
string name;
int id;
bool isvalid;
off_t size;
time_t mtime, ctime;
bool isdir;
direntry (const string &curdir, struct dirent const& d, int Id) : name (d.d_name) {
id = Id;
struct stat bstat;
string fname(curdir);
fname += "/";
fname += name;
if (stat (fname.c_str(), &bstat) != 0) {
isvalid = false;
size = 0, mtime = 0, ctime = 0;
isdir = false;
return;
}
isvalid = true;
size = bstat.st_size;
mtime = bstat.st_mtime;
ctime = bstat.st_ctime;
isdir = (d.d_type == DT_DIR);
}
};
bool direntry_cmp_ctime (direntry const &a, direntry const &b) { return a.ctime < b.ctime; }
bool direntry_cmp_mtime (direntry const &a, direntry const &b) { return a.mtime < b.mtime; }
bool direntry_cmp_size (direntry const &a, direntry const &b) { return a.size < b.size ; }
bool direntry_cmp_name (direntry const &a, direntry const &b) { return strcasecmp (a.name.c_str(), b.name.c_str()) <= 0 ; }
ostream& operator<< (ostream& out, const direntry &de) { // JDJDJDJD some css tagging is missing here
out << "<td class=\"fsize\">" << os_off_t(de.size) << "</td><td class=\"ftime\">" << os_time_t (de.mtime) << "</td>" // "<td class=\"ftime\">" << os_time_t (de.ctime) << "</td>"
<< "<td class=\"fname\"><a href=\"" << de.name;
if (de.isdir)
out << '/';
out << "\">" << xmlencode(de.name) << "</a></td>";
return out;
}
class MMapBuffer : virtual public DummyBuffer {
private:
int fd;
char * mapstart;
ssize_t maplength;
public:
// regular constructor that maps the whole file and transmit the whole file
MMapBuffer (int fd, char * start, ssize_t length) : DummyBuffer (start, length),
fd(fd),
mapstart (start),
maplength (length)
{}
// weird constructor that maps the whole file and transmit only a part of it
MMapBuffer (int fd, char * transmitstart, ssize_t transmitlength, char *mapstart, ssize_t maplength) : DummyBuffer (transmitstart, transmitlength),
fd(fd),
mapstart (mapstart),
maplength (maplength)
{}
~MMapBuffer () {
if (munmap (mapstart, maplength) != 0) {
int e = errno;
cerr << "~MMapBuffer : munmap failed : " << e << " " << strerror(e) << endl;
}
close (fd);
}
};
class FMap_TreatRequest : virtual public TreatRequest {
string rootdir;
size_t rootdirlength;
public:
FMap_TreatRequest (string proposedrootdir) : TreatRequest(), rootdir(proposedrootdir) {
if (rootdir.empty()) {
rootdir = "/";
} else if (rootdir[rootdir.size()-1] != '/') {
rootdir += '/';
}
rootdirlength = rootdir.size();
}
~FMap_TreatRequest () {}
virtual TReqResult output (ostream &cout, HTTPRequest &req) {
bool isGET = false;
bool isHEAD = false;
if (req.method == "GET") {
isGET = true;
} else if (req.method == "HEAD") {
isHEAD = true;
} else {
req.set_relative_expires (60);
return error (cout, req, 405); // JDJDJDJD The response MUST include an Allow header containing a list of valid methods for the requested resource.
}
bool isrootdir = false;
string fname (rootdir);
if (req.document_uri.empty())
req.document_uri = "/";
if (req.document_uri == "/")
isrootdir = true;
string uridecode;
percentdecode (req.document_uri, uridecode);
// req.errlog() << "req.document_uri = " << uridecode << endl;
fname += uridecode;
char * canonfname = realpath (fname.c_str(), NULL);
if (canonfname == NULL) {
int e = errno;
switch (e) {
case EACCES:
req.set_relative_expires (60);
return error (cout, req, 403, NULL, "file permission denied (001)");
case ENOENT:
req.set_relative_expires (20);
return error (cout, req, 404, NULL, "Not Found (001f)");
case ENOTDIR:
req.set_relative_expires (20);
return error (cout, req, 404, NULL, "Not Found (001d)");
case ENAMETOOLONG:
case EINVAL:
case EIO:
default:
req.errlog() << "simplefmap::output realpath gave error " << e << " " << strerror (e) << endl;
req.set_relative_expires (60);
return error (cout, req, 500);
}
}
if (!isrootdir) {
if (strncmp (canonfname, rootdir.c_str(), rootdirlength) != 0) {
stringstream err;
err << "simplefmap : FMap_TreatRequest::output : file " << canonfname << " fells outside of rootdir : " << rootdir;
req.set_relative_expires (60);
free (canonfname);
return error (cout, req, 403, NULL, "file permission denied (001b)");
}
}
struct stat statbuf;
if (lstat (canonfname, &statbuf) != 0) {
int e = errno;
req.errlog() << "simplefmap::output lstat(" << canonfname << ") gave error " << e << " " << strerror (e) << endl; // this is debug JDJDJDJD we should have an uniform way to log such things
switch (e) {
case EACCES:
req.set_relative_expires (60);
free (canonfname);
return error (cout, req, 403, NULL, "file permission denied (002)");
case ENOENT:
req.set_relative_expires (20);
free (canonfname);
return error (cout, req, 404, NULL, "Not Found (002f)");
case ENOTDIR:
req.set_relative_expires (20);
free (canonfname);
return error (cout, req, 404, NULL, "Not found (002d)");
case ENAMETOOLONG:
case EINVAL:
case EIO:
default:
req.errlog() << "simplefmap::output lstat(" << canonfname << ") gave error " << e << " " << strerror (e) << endl;
req.set_relative_expires (60);
free (canonfname);
return error (cout, req, 500);
}
}
if ( !( ((S_IFREG & statbuf.st_mode) != 0) || ((S_IFDIR & statbuf.st_mode) != 0) ) ) { // we didn't reach a file or a directory (maybe a link though, but we don't follow links)
req.set_relative_expires (60);
free (canonfname);
return error (cout, req, 403, NULL, "file permission denied (003)");
}
if ((S_IFDIR & statbuf.st_mode) != 0) { // we found a directory
if (isHEAD) { // we don't give interesting results on such things yet ...
// HTTP/1.0 200 OK
// Date: Fri, 18 Oct 2013 17:56:16 GMT
// Expires: -1
// Cache-Control: private, max-age=0
// Content-Type: text/html; charset=ISO-8859-1
// Set-Cookie: NID=67=A0gxHvH3cp0D0WjpyD7XoXn3WS5niEQ0SYij_-CT2FnFiT9LNplgH03comnilUyRb89vCdQK-l6HeedAW6mkRpv8s5Uq5xHPn54cMG5x4Muo04vGRNIl8ttgDdvDQZlt; expires=Sat, 19-Apr-2014 17:56:16 GMT; path=/; domain=.; HttpOnly
// P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
// Server: gws
// X-XSS-Protection: 1; mode=block
// X-Frame-Options: SAMEORIGIN
// Alternate-Protocol: 80:quic
// Connection: Keep-Alive
// Proxy-Connection: Keep-Alive
req.statuscode = 200;
req.outmime["Content-Type"] = "text/html;charset=UTF-8";
req.set_relative_expires (20); // JDJDJDJD this should be tunable ?
req.publish_header();
free (canonfname);
return TRCompleted;
}
DIR * fdir = opendir (canonfname);
if (fdir == NULL) {
int e = errno;
req.errlog() << "simplefmap::output opendir(" << canonfname << " ) gave error " << e << " " << strerror (e) << endl; // this is debug JDJDJDJD we should have an uniform way to log such things
switch (e) {
case EACCES:
req.set_relative_expires (60);
free (canonfname);
return error (cout, req, 403, NULL, "file permission denied (003b)");
case ENOENT:
req.set_relative_expires (20);
free (canonfname);
return error (cout, req, 404, NULL, "Not Found (003)");
default:
req.set_relative_expires (60);
free (canonfname);
return error (cout, req, 503, NULL, "weidries occured (001)");
}
}
list <direntry> thedir;
int id = 0;
while (true) {
struct dirent *presult;
if ((presult = readdir(fdir)) == NULL)
break;
id ++;
thedir.push_back (direntry(canonfname, *presult, id));
}
req.statuscode = 200;
req.outmime["Content-Type"] = "text/html;charset=UTF-8";
// silly test just to see about auth
// req.statuscode = 401;
// req.outmime["WWW-Authenticate"] = "Basic realm=\"WallyWorld\"";
stringstream s;
s <<
{{<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<title>}} << xmlencode (uridecode) << {{</title>
<link rel="stylesheet" type="text/css" href="/stylesheet.css">
}};
list<direntry>::iterator li;
s <<
{{<script>
var nextsortmtimeasc = false,
nextsortnameasc = true,
nextsortsizeasc = false;
}};
thedir.sort(direntry_cmp_size);
s << {{var bysize = new Array(}};
for (li=thedir.begin(); li!=thedir.end(); li++)
s << li->id << {{,}};
s << {{null);}} << bendl;
thedir.sort(direntry_cmp_mtime);
s << {{var bymtime = new Array(}};
for (li=thedir.begin(); li!=thedir.end(); li++)
s << li->id << {{,}};
s << {{null);}} << bendl;
thedir.sort(direntry_cmp_name);
s << {{var byname = new Array(}};
for (li=thedir.begin(); li!=thedir.end(); li++)
s << li->id << {{,}};
s << {{null);}} << bendl;
s << {{
function sortBySize() {
if (nextsortsizeasc) {
for (i in bysize)
if (bysize[i] != null) document.getElementById('thelist').appendChild(document.getElementById(bysize[i]));
} else {
for (i=bysize.length-1 ; i>=0 ; i--)
if (bysize[i] != null) document.getElementById('thelist').appendChild(document.getElementById(bysize[i]));
}
nextsortsizeasc = !nextsortsizeasc;
}
function sortByName() {
if (nextsortnameasc) {
for (i in byname)
if (byname[i] != null) document.getElementById('thelist').appendChild(document.getElementById(byname[i]));
} else {
for (i=byname.length-1 ; i>=0 ; i--)
if (byname[i] != null) document.getElementById('thelist').appendChild(document.getElementById(byname[i]));
}
nextsortnameasc = !nextsortnameasc;
}
function sortByMTime() {
if (nextsortmtimeasc) {
for (i in bymtime)
if (bymtime[i] != null) document.getElementById('thelist').appendChild(document.getElementById(bymtime[i]));
} else {
for (i=bymtime.length-1 ; i>=0 ; i--)
if (bymtime[i] != null) document.getElementById('thelist').appendChild(document.getElementById(bymtime[i]));
}
nextsortmtimeasc = !nextsortmtimeasc;
}
</script>
</head>
<body>
<h1>}} << req.document_uri << {{</h1>
}};
{ list<direntry>::iterator li;
s << {{
<table id="thelist">
<tr class="flistentry" id="legend">
<td class="listlegend"><a href="javascript:void(0)" onclick="sortBySize()">size</a></td>
<td class="listlegend"><a href="javascript:void(0)" onclick="sortByMTime()">mtime</a></td>
<td class="listlegend"><a href="javascript:void(0)" onclick="sortByName()">name</a></td>
</tr>
}};
for (li=thedir.begin() ; li!=thedir.end() ; li++) {
s << {{<tr class="flistentry" id="}} << li->id << {{">}}
<< *li << {{</tr>}} << bendl;
}
s << {{
</table>
}};
}
s << {{
</body>
</html>
}};
req.set_contentlength (s.str().size());
req.set_relative_expires (20); // JDJDJDJD this should be tunable ?
req.publish_header();
cout << s.str();
closedir (fdir);
} else { // we found a file
string mime_type;
size_t pdot = req.document_uri.rfind('.');
if (pdot == string::npos) {
mime_type = "text/plain";
} else {
string terminaison = req.document_uri.substr(pdot + 1);
const string * pmt = mimetypes.getmimefromterminaison (terminaison);
if (pmt == NULL)
mime_type = "text/plain";
else
mime_type = *pmt;
}
if (isHEAD) { // we try to give interesting results
// HTTP/1.0 200 OK
// Date: Mon, 05 May 2008 00:33:14 GMT
// Server: Apache/2.0.52 (Red Hat)
// Accept-Ranges: bytes
// Content-Length: 3980
// Content-Type: image/jpeg
req.statuscode = 200;
req.outmime["Accept-Ranges"] = "bytes";
req.outmime["Content-Type"] = mime_type;
req.set_contentlength (statbuf.st_size);
req.set_relative_expires (3660); // JDJDJDJD this should be tuneable, with mime type ?
req.outmime["Keep-Alive"] = "timeout=15, max=100";
req.publish_header();
free (canonfname);
return TRCompleted;
}
size_t rangestart, rangeend;
if (getrange (req.mime, rangestart, rangeend)) {
if (rangestart == BULKNPOS) {
cerr << "after range computation rangestart is BULKNPOS ??? ignored" << endl;
} else {
if (rangestart > (size_t)statbuf.st_size) { // we're asked something further than the eof
req.statuscode = 206;
req.outmime["Accept-Ranges"] = "bytes";
req.outmime["Content-Type"] = mime_type;
req.set_contentlength (0);
stringstream s;
s << "bytes " << statbuf.st_size << "-" << statbuf.st_size << "/" << statbuf.st_size;
req.outmime["Content-Range"] = s.str();
req.set_relative_expires (3660);
req.outmime["Keep-Alive"] = "timeout=15, max=100";
req.publish_header();
free (canonfname);
return TRCompleted;
}
if ((rangeend == 0) || (rangeend == BULKNPOS)) {
rangeend = statbuf.st_size - 1;
}
}
}
int f = open (canonfname, O_RDONLY);
if (f < 0) {
int e = errno;
req.errlog() << "simplefmap::output open(" << canonfname << ") failed : " << e << " " << strerror(e) << endl;
free (canonfname);
req.set_relative_expires (60);
return error (cout, req, 403, NULL, "file permission denied (004)");
}
char * fmap = (char *) mmap(NULL, statbuf.st_size, PROT_READ, MAP_PRIVATE, f, 0);
if (fmap == MAP_FAILED) {
int e = errno;
req.errlog() << "simplefmap::output mmap(" << canonfname << ") failed : " << e << " " << strerror(e) << endl;
close (f);
free (canonfname);
req.set_relative_expires (60);
return error (cout, req, 403, NULL, "file permission denied (005)");
}
if ((rangestart != BULKNPOS) && ((rangeend-rangestart + 1) < (size_t)statbuf.st_size)) { // we have a range shorter than the file
req.statuscode = 206;
req.outmime["Accept-Ranges"] = "bytes";
req.outmime["Content-Type"] = mime_type;
stringstream s;
s << "bytes " << rangestart << "-" << rangeend << "/" << statbuf.st_size;
req.outmime["Content-Range"] = s.str();
req.set_contentlength (rangeend-rangestart + 1);
req.set_relative_expires (3660); // JDJDJDJD this should be tuneable, with mime type ?
req.outmime["Keep-Alive"] = "timeout=15, max=100";
req.publish_header();
MMapBuffer* mmbuf = new MMapBuffer (f, fmap+rangestart, rangeend-rangestart + 1, fmap, statbuf.st_size);
if (mmbuf == NULL) {
req.errlog() << "could not allocate MMapBuffer" << endl;
munmap (fmap, statbuf.st_size);
close (f);
}
req.httppconn->pushdummybuffer (mmbuf);
} else { // either no range or range is the whole file
req.statuscode = 200;
req.outmime["Accept-Ranges"] = "bytes";
req.outmime["Content-Type"] = mime_type;
req.set_contentlength (statbuf.st_size);
req.set_relative_expires (3660); // JDJDJDJD this should be tuneable, with mime type ?
req.outmime["Keep-Alive"] = "timeout=15, max=100";
req.publish_header();
MMapBuffer* mmbuf = new MMapBuffer (f, fmap, statbuf.st_size);
if (mmbuf == NULL) {
req.errlog() << "could not allocate MMapBuffer" << endl;
munmap (fmap, statbuf.st_size);
close (f);
}
req.httppconn->pushdummybuffer (mmbuf);
}
}
free (canonfname);
return TRCompleted;
}
};
class fmap_urimapper : virtual public URIMapper {
public:
FMap_TreatRequest fmap_treatrequest;
fmap_urimapper(string rootdir) : URIMapper(), fmap_treatrequest(rootdir) {}
~fmap_urimapper() {}
virtual TreatRequest* treatrequest (HTTPRequest &req) {
return &fmap_treatrequest;
}
};
fmap_urimapper *mapper = NULL;
int simplefmap_global (BSOperation op) {
switch (op) {
case StartUp:
#if OSXFILESCHEME == 1
mapper = new fmap_urimapper("/Users/bulkrayed");
#else
mapper = new fmap_urimapper("/home/bulkrayed");
#endif
// mapper = new fmap_urimapper("/BIG/home/webs/hashttpp.zz/html");
if (mapper == NULL) {
cerr << "bulkrays::testsite_global could not allocate fmap_urimapper" << endl;
return -1;
}
// hostmapper["hashttpp.zz"] = mapper;
// hostmapper["127.0.0.1:10080"] = mapper;
hostmapper["bulkrays2.nkdn.fr"] = mapper;
hostmapper["bulkrays2.nkdn.fr:10080"] = mapper;
unset_default_host ();
return 0;
case ShutDown:
if (mapper == NULL) return -1;
hostmapper.deregisterall (mapper);
delete (mapper);
return 0;
}
return -1;
}
}