-
Notifications
You must be signed in to change notification settings - Fork 13
/
Response.cpp
394 lines (344 loc) · 11 KB
/
Response.cpp
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
/*
Copyright (c) 2011, Chris Pearce
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- Neither the name of Chris Pearce nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ORGANISATION OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <sstream>
#include "PathEnumerator.h"
#include "Response.h"
#include "Utils.h"
#ifdef _WIN32
#include <Windows.h>
#define S_ISDIR(m) ((m & _S_IFDIR) == _S_IFDIR)
#define fseek64 _fseeki64
#define ftell64 _ftelli64
static int gmtime_r(const time_t *timep, struct tm *result) {
return gmtime_s(result, timep) == 0;
}
#define DIR_LIST_CHARSET "text/html; charset=windows"
#else
#define __stat64 stat64
#define _stat64 stat64
#include <unistd.h>
#define fseek64 fseeko64
#define ftell64 ftello64
void Sleep(int ms) {
usleep(ms * 1000);
}
static int fopen_s(FILE** file, const char* path, const char* mode) {
FILE *f = fopen(path, mode);
if (!f) {
return 1;
}
*file = f;
return 0;
}
#define DIR_LIST_CHARSET "text/html; charset=utf-8"
#endif
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
static const char* gContentTypes[][2] = {
{"ogv", "video/ogg"},
{"ogg", "video/ogg"},
{"oga", "audio/ogg"},
{"webm", "video/webm"},
{"wav", "audio/x-wav"},
{"html", "text/html; charset=utf-8"},
{"txt", "text/plain; charset=utf-8"},
{"js", "text/javascript; charset=utf-8"},
{"js", "text/css; charset=utf-8"},
{"jpg", "image/jpeg"},
{"jpeg", "image/jpeg"},
{"png", "image/png"},
{"gif", "image/gif"}
};
Response::Response(RequestParser p)
: parser(p),
mode(INTERNAL_ERROR),
fileLength(-1),
file(0),
rangeStart(0),
rangeEnd(0),
offset(0),
bytesRemaining(0)
{
string target = parser.GetTarget();
if (target == "") {
mode = DIR_LIST;
path = ".";
} else if (target.find("..") != string::npos) {
mode = ERROR_FILE_NOT_EXIST;
} else {
// Determine if the file exists, and if it is a directory.
struct __stat64 buf;
int result;
result = _stat64(target.c_str(), &buf );
if (result == -1) {
cerr << "File not found" << std::endl;
mode = ERROR_FILE_NOT_EXIST;
} else if (result != 0) {
mode = INTERNAL_ERROR;
} else if (S_ISDIR(buf.st_mode)) {
mode = DIR_LIST;
path = parser.GetTarget();
} else if (parser.IsRangeRequest() && !parser.IsLive()) {
mode = GET_FILE_RANGE;
path = parser.GetTarget();
parser.GetRange(rangeStart, rangeEnd);
if (rangeEnd == -1) {
rangeEnd = buf.st_size;
}
fileLength = buf.st_size;
} else {
mode = GET_ENTIRE_FILE;
path = parser.GetTarget();
fileLength = buf.st_size;
}
}
}
bool Response::SendHeaders(Socket* aSocket) {
string headers;
headers.append("HTTP/1.1 ");
headers.append(StatusCode(mode));
headers.append("\r\n");
headers.append("Connection: close\r\n");
headers.append(GetDate());
headers.append("\r\n");
headers.append("Server: HttpMediaServer/0.1\r\n");
if (ContainsKey(parser.GetParams(), "delay")) {
const map<string,string> params = parser.GetParams();
string delayStr = params.find("delay")->second;
double delay = atof(delayStr.c_str());
if (delay > 0.0) {
Sleep((unsigned)(delay+0.5));
}
}
if (!parser.IsLive()) {
if (mode == GET_ENTIRE_FILE) {
headers.append("Accept-Ranges: bytes\r\n");
headers.append("Content-Length: ");
headers.append(ToString(fileLength));
headers.append("\r\n");
} else if (mode == GET_FILE_RANGE) {
headers.append("Accept-Ranges: bytes\r\n");
headers.append("Content-Length: ");
headers.append(ToString(rangeEnd - rangeStart));
headers.append("\r\n");
headers.append("Content-Range: bytes ");
headers.append(ToString(rangeStart));
headers.append("-");
headers.append(ToString(rangeEnd));
headers.append("/");
headers.append(ToString(fileLength));
headers.append("\r\n");
}
}
//headers.append("Last-Modified: Wed, 10 Nov 2009 04:58:08 GMT\r\n");
headers.append("Content-Type: ");
if (parser.HasSpecifiedMimeType()) {
headers.append(parser.GetSpecifiedMimeType());
} else {
headers.append(ExtractContentType(path, mode));
}
headers.append("\r\n\r\n");
cout << "Sending Headers " << parser.id << std::endl << headers;
return aSocket->Send(headers.c_str(), (int)headers.size()) != -1;
}
// Returns true if we need to call again.
bool Response::SendBody(Socket *aSocket) {
if (mode == ERROR_FILE_NOT_EXIST) {
cout << "Sent (empty) body (" << parser.id << ")" << std::endl;
return false;
}
int len = 1024;
unsigned wait = 0;
string rateStr;
if (ContainsKey(parser.GetParams(), "rate")) {
const map<string,string> params = parser.GetParams();
rateStr = params.find("rate")->second;
double rate = atof(rateStr.c_str());
const double period = 0.1;
if (rate <= 0.0) {
len = 1024;
wait = 0;
} else {
len = (unsigned)(rate * 1024 * period);
wait = (unsigned)(period * 1000.0); // ms
}
}
if (mode == GET_ENTIRE_FILE) {
if (!file) {
if (fopen_s(&file, path.c_str(), "rb")) {
file = 0;
return false;
}
}
if (feof(file)) {
// Transmitted entire file!
fclose(file);
file = 0;
return false;
}
int64_t tell = ftell64(file);
// Transmit the next segment.
char* buf = new char[len];
int x = (int)fread(buf, 1, len, file);
int r = aSocket->Send(buf, x);
delete buf;
if (r < 0) {
// Some kind of error.
return false;
}
if (wait > 0) {
Sleep(wait);
}
// Else we tranmitted that segment, we're ok.
return true;
} else if (mode == GET_FILE_RANGE) {
if (!file) {
if (fopen_s(&file, path.c_str(), "rb")) {
file = 0;
return false;
}
fseek64(file, rangeStart, SEEK_SET);
offset = rangeStart;
bytesRemaining = rangeEnd - rangeStart;
}
if (feof(file) || bytesRemaining == 0) {
// Transmitted entire range.
fclose(file);
file = 0;
return false;
}
// Transmit the next segment.
char* buf = new char[len];
len = (unsigned)MIN(bytesRemaining, len);
size_t bytesSent = fread(buf, 1, len, file);
bytesRemaining -= bytesSent;
int r = aSocket->Send(buf, (int)bytesSent);
delete buf;
if (r < 0) {
// Some kind of error.
return false;
}
offset += bytesSent;
assert(ftell64(file) == offset);
if (wait > 0) {
Sleep(wait);
}
// Else we tranmitted that segment, we're ok.
return true;
}
else if (mode == DIR_LIST) {
std::stringstream response;
PathEnumerator *enumerator = PathEnumerator::getEnumerator(path);
if (enumerator) {
response << "<!DOCTYPE html>\n<ul>";
string href;
while (enumerator->next(href)) {
if (href == "." || path == "." && href == "..") {
continue;
}
response << "<li><a href=\"" << path + "/" + href;
if (!rateStr.empty()) {
response << "?rate=" + rateStr;
}
response << "\">" << href << "</a></li>";
}
response << "</ul>";
delete enumerator;
}
string _r = response.str();
aSocket->Send(_r.c_str(), (int)_r.size());
return false;
}
return false;
}
#ifdef _DEBUG
void Response::Test() {
assert(ExtractContentType("dir1/dir2/file.ogv", GET_ENTIRE_FILE) == string("video/ogg"));
assert(ExtractContentType("dir1/dir2/file.ogg", GET_ENTIRE_FILE) == string("video/ogg"));
assert(ExtractContentType("dir1/dir2/file.oga", GET_ENTIRE_FILE) == string("audio/ogg"));
assert(ExtractContentType("dir1/dir2/file.wav", GET_ENTIRE_FILE) == string("audio/x-wav"));
assert(ExtractContentType("dir1/dir2/file.webm", GET_ENTIRE_FILE) == string("video/webm"));
assert(ExtractContentType("dir1/dir2/file.txt", GET_ENTIRE_FILE) == string("text/plain; charset=utf-8"));
assert(ExtractContentType("dir1/dir2/file.html", GET_ENTIRE_FILE) == string("text/html; charset=utf-8"));
assert(ExtractContentType("", DIR_LIST) == string(DIR_LIST_CHARSET));
}
#endif
string Response::StatusCode(eMode mode) {
switch (mode) {
case GET_ENTIRE_FILE: return string("200 OK");
case GET_FILE_RANGE: return string("206 OK");
case DIR_LIST: return string("200 OK");
case ERROR_FILE_NOT_EXIST: return string("404 File Not Found");
case INTERNAL_ERROR:
default:
return string("500 Internal Server Error");
};
}
string Response::ExtractContentType(const string& file, eMode mode) {
if (mode == ERROR_FILE_NOT_EXIST || mode == INTERNAL_ERROR)
return "text/html; charset=utf-8";
if (mode == DIR_LIST)
return DIR_LIST_CHARSET;
size_t dot = file.rfind(".");
if (dot == string::npos) {
// File has no extension. Just send it as binary.
return "application/octet-stream";
}
string extension(file, dot+1, file.size() - dot - 1);
StrToLower(extension);
for (unsigned i=0; i<ARRAY_LENGTH(gContentTypes); i++) {
if (extension == gContentTypes[i][0]) {
return string(gContentTypes[i][1]);
}
}
return "application/octet-stream";
}
static char const month[12][4] = {
"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
};
static char const day[7][4] = {
"Sun","Mon","Tue","Wed","Thu","Fri","Sat"
};
string Response::GetDate() {
time_t rawtime;
time(&rawtime);
struct tm t;
if (!gmtime_r(&rawtime, &t)) {
return "Date: Thu Jan 01 1970 00:00:00 GMT";
}
char buf[128];
unsigned len = snprintf(buf, ARRAY_LENGTH(buf),
"Date: %s, %d %s %d %.2d:%.2d:%.2d GMT",
day[t.tm_wday], t.tm_mday, month[t.tm_mon],
1900 + t.tm_year, t.tm_hour, t.tm_min, t.tm_sec);
return string(buf, len);
}