-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhttpd.cpp
416 lines (360 loc) · 9.92 KB
/
httpd.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// Copyright 2017 Archos SA
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#define BOOST_ASIO_SEPARATE_COMPILATION
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <time.h>
#include <string.h>
#include <iostream>
#include <thread>
#include <mutex>
#include <string>
#include <condition_variable>
#include <unordered_map>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
class SocketHelper {
private:
int _fd;
uint8_t buffer[1024];
int _pos, _size;
public:
SocketHelper(int fd) : _fd(fd), _pos(0), _size(0) { };
void pop() {
if(_size == 0)
return;
_pos++;
_size--;
_pos%=sizeof(buffer);
}
int getc() {
int v = next();
pop();
return v;
}
int next() {
if(_size==0)
readMore();
if(_size==0)
return -1;
return buffer[_pos];
}
void readMore() {
if(_size)
return;
int n = sizeof(buffer)-_pos;
int got = read(_fd, &buffer[_pos], n);
_size += got;
}
std::string getLine() {
std::string res="";
while(1) {
int v = getc();
if(v == -1)
return res;
if(v == '\r' && next() == '\n') {
pop();
return res;
}
res += (char)v;
}
}
void writeString(const char *str) {
write(_fd, str, strlen(str));
}
};
static std::unordered_map<std::string, std::string> getCommand(SocketHelper& stream) {
std::unordered_map<std::string, std::string> res;
std::string line = stream.getLine();
std::vector<std::string> strs;
boost::split(strs, line, boost::is_any_of("\t "));
if(strs.size() < 3) {
res["error"] = "Invalid protocol";
return res;
}
res["method"] = strs[0];
res["file"] = strs[1];
return res;
}
static std::unordered_map<std::string, std::string> getRequest(SocketHelper& stream) {
auto res = getCommand(stream);
if(res.count("error"))
return res;
while(true) {
std::string line = stream.getLine();
if(line=="")
return res;
const char *str = line.c_str();
char *key = strdup(str);
char *sep = strchr(key, ':');
if(!sep) {
std::cerr << "Failed to parse " << line << std::endl;
res["error"] = "Failed to parse header";
return res;
}
*sep = 0;
sep++;
if(*sep)
sep++;
res[key] = sep;
free(key);
}
return res;
}
std::pair<long long,long long> parseRange(const std::string& str) {
auto res = std::make_pair(0LL, -1LL);
auto cstr = str.c_str();
if(strstr(cstr, "bytes=") != cstr)
return res;
cstr += strlen("bytes=");
std::cerr << "cstr = " << cstr << std::endl;
char *next = NULL;
res.first = strtoll(cstr, &next, 0);
if(!next || *next != '-')
return res;
next++;
char *next2 = NULL;
res.second = strtoll(next, &next2, 0);
if(next == next2)
res.second = -1;
return res;
}
static void giveContentLength(SocketHelper& fd, std::pair<long long, long long> range);
static void serveFile(std::unordered_map<std::string, std::string>& request, int fd, std::pair<long long,long long> range);
static void clientHandler(int fd) {
SocketHelper stream(fd);
auto request = getRequest(stream);
if(request.count("error")) {
stream.writeString("Protocol fail...\n\r");
close(fd);
return;
}
std::cerr << "Request =" << std::endl;
for(auto it = request.begin(); it != request.end(); ++it) {
std::cerr << "\t" << it->first << " = " << it->second << std::endl;
}
auto range = parseRange(request["Range"]);
std::cerr << "Parsed range = " << range.first << ":" << range.second << std::endl;
if(range.first)
stream.writeString("HTTP/1.0 206 Partial Content\r\n");
else
stream.writeString("HTTP/1.0 200 OK\r\n");
stream.writeString("Server: Bittorrent2Http\r\n");
stream.writeString("Connection: close\r\n");
stream.writeString("Accept-Ranges: bytes\r\n");
giveContentLength(stream, range);
stream.writeString("\r\n");
serveFile(request, fd, range);
close(fd);
}
static std::mutex fileInfos_l;
static std::condition_variable fileInfos_v;
static const char *_filePath = NULL;
//static int _maxOffset = 0;
static long long _fileSize = 0;
static std::function<long long (long long, long long)> _availableData;
static std::mutex _currentRanges_l;
static std::list<std::pair<long long, long long> > _currentRanges;
static void dumpCurrentRanges() {
std::unique_lock<std::mutex> lk(_currentRanges_l);
std::cerr << "Ranges:" << std::endl;
for(auto it = _currentRanges.begin(); it != _currentRanges.end(); ++it) {
std::cerr << "\t" << it->first << "-" << it->second << std::endl;
}
}
static void giveContentLength(SocketHelper& fd, std::pair<long long, long long> range) {
std::unique_lock<std::mutex> lk(fileInfos_l, std::defer_lock);
long long fileSize = 0;
lk.lock();
while(1) {
const char* filePath = _filePath;
//int maxOffset = _maxOffset;
fileSize = _fileSize;
if(filePath)
break;
fileInfos_v.wait(lk);
}
lk.unlock();
std::string str = "Content-Length: ";
long long size = 0;
if(range.second == -1)
size = fileSize - range.first;
else
size = range.second;
str += boost::lexical_cast<std::string>(size);
str += "\r\n";
std::cerr << "Add " << str << std::endl;
fd.writeString(str.c_str());
if(range.first) {
str = "Content-Range: bytes ";
//Start
str += boost::lexical_cast<std::string>(range.first);
str += "-";
//-End
if(range.second && range.second != -1)
str += boost::lexical_cast<std::string>(range.second);
else
str += boost::lexical_cast<std::string>(fileSize-1);
// /size
str += "/";
str += boost::lexical_cast<std::string>(fileSize);
str += "\r\n";
fd.writeString(str.c_str());
}
}
static void insertRange(std::pair<long long, long long> range) {
std::unique_lock<std::mutex> lk(_currentRanges_l);
_currentRanges.push_back(range);
}
static void deleteRange(std::pair<long long, long long> range) {
std::unique_lock<std::mutex> lk(_currentRanges_l);
for(auto it = _currentRanges.begin(); it != _currentRanges.end(); ++it) {
if(*it == range) {
_currentRanges.erase(it);
return;
}
}
std::cerr << "Couldn't find range to be deleted" << std::endl;
}
std::list<std::pair<long long, long long> > getRanges() {
std::unique_lock<std::mutex> lk(_currentRanges_l);
auto res = _currentRanges;
return res;
}
static void serveFile(std::unordered_map<std::string, std::string>& request, int fd, std::pair<long long,long long> range) {
long long currentOffset = range.first;
int file = -1;
std::unique_lock<std::mutex> lk(fileInfos_l, std::defer_lock);
insertRange(range);
dumpCurrentRanges();
lk.lock();
while(1) {
const char* filePath = _filePath;
//int maxOffset = _maxOffset;
auto fileSize = _fileSize;
auto availableData = _availableData;
lk.unlock();
std::cerr << "File path " << filePath << std::endl;
if(file == -1 && filePath) {
file = open(filePath, O_RDONLY);
perror("Opening file");
lseek(file, range.first, SEEK_SET);
perror("Seeking file");
}
int fail = 0;
//while(filePath && file != -1 && currentOffset < maxOffset && (currentOffset < range.second || range.second == -1LL)) {
while(filePath && file != -1 && (currentOffset < range.second || range.second == -1LL)) {
lseek(file, currentOffset, SEEK_SET);
char buffer[1024];
int length = availableData(currentOffset, sizeof(buffer));
if(!length)
break;
if(length > sizeof(buffer))
length = sizeof(buffer);
int res = read(file, buffer, length);
if(!res || res == -1) {
perror("read");
fail = 1;
break;
}
int res2 = write(fd, buffer, res);
if(!res2 || res2 == -1) {
perror("write");
fail = 1;
break;
}
currentOffset += res2;
}
deleteRange(range);
range.first = currentOffset;
insertRange(range);
dumpCurrentRanges();
if(fail)
break;
if(filePath && currentOffset == fileSize)
break;
std::cerr << "currentOffset = " << currentOffset
<< ", fileSize = " << fileSize
<< std::endl;
if(currentOffset >= range.second &&
range.second != -1LL)
break;
lk.lock();
//TODO: his might lead to a case where the connection is closed
//But we're still waiting here
fileInfos_v.wait(lk);
#if 1
//Check connection not dead
char c;
if(recv(fd, &c, 1, MSG_PEEK|MSG_DONTWAIT) < 0 && errno != EAGAIN) {
perror("recv");
break;
}
#endif
}
deleteRange(range);
dumpCurrentRanges();
close(file);
}
void setFileInfos(const char *filePath, long long fileSize, std::function<long long(long long, long long)> availableData) {
fileInfos_l.lock();
if(_filePath)
delete _filePath;
_filePath = strdup(filePath);
//_maxOffset = maxOffset;
_fileSize = fileSize;
_availableData = availableData;
fileInfos_l.unlock();
fileInfos_v.notify_all();
}
static void httpd() {
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd == -1) {
std::cerr << "Could not create socket" << std::endl;
return;
} else {
std::cerr << "Server started" << std::endl;
}
// Prepare the sockaddr_in structure
struct sockaddr_in s_addr;
s_addr.sin_family = AF_INET;
s_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
//s_addr.sin_port = htons(19992);
int port = 10000 + (time(NULL) % 10000);
std::cout << port << std::endl;
s_addr.sin_port = htons(port);
bind(fd, (struct sockaddr*) &s_addr, sizeof(s_addr));
perror("bind");
listen(fd, 10);
while(1) {
struct sockaddr_in c_addr;
socklen_t len = sizeof(c_addr);
int cfd = accept(fd, (struct sockaddr*)&c_addr, &len);
std::thread clientThread(clientHandler, cfd);
clientThread.detach();
}
}
void start_httpd() {
std::thread t(httpd);
t.detach();
}