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
|
/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
#include <rfb/HTTPServer.h>
#include <rfb/LogWriter.h>
#include <rfb/util.h>
#include <rdr/MemOutStream.h>
using namespace rfb;
using namespace rdr;
static LogWriter vlog("HTTPServer");
const int clientWaitTimeMillis = 20000;
const int idleTimeoutSecs = 5 * 60;
//
// -=- LineReader
// Helper class which is repeatedly called until a line has been read
// (lines end in \n or \r\n).
// Returns true when line complete, and resets internal state so that
// next read() call will start reading a new line.
// Only one buffer is kept - process line before reading next line!
//
class LineReader : public CharArray {
public:
LineReader(InStream& is_, int l)
: CharArray(l), is(is_), pos(0), len(l), bufferOverrun(false) {}
// Returns true if line complete, false otherwise
bool read() {
while (is.checkNoWait(1)) {
char c = is.readU8();
if (c == '\n') {
if (pos && (buf[pos-1] == '\r'))
pos--;
bufferOverrun = false;
buf[pos++] = 0;
pos = 0;
return true;
}
if (pos == (len-1)) {
bufferOverrun = true;
buf[pos] = 0;
return true;
}
buf[pos++] = c;
}
return false;
}
bool didBufferOverrun() const {return bufferOverrun;}
protected:
InStream& is;
int pos, len;
bool bufferOverrun;
};
//
// -=- HTTPServer::Session
// Manages the internal state for an HTTP session.
// processHTTP returns true when request has completed,
// indicating that socket & session data can be deleted.
//
class rfb::HTTPServer::Session {
public:
Session(network::Socket& s, rfb::HTTPServer& srv)
: contentType(0), contentLength(-1), lastModified(-1),
line(s.inStream(), 256), sock(s),
server(srv), state(ReadRequestLine), lastActive(time(0)) {
}
~Session() {
}
void writeResponse(int result, const char* text);
bool writeResponse(int code);
bool processHTTP();
network::Socket* getSock() const {return &sock;}
int checkIdleTimeout();
protected:
CharArray uri;
const char* contentType;
int contentLength;
time_t lastModified;
LineReader line;
network::Socket& sock;
rfb::HTTPServer& server;
enum {ReadRequestLine, ReadHeaders, WriteResponse} state;
enum {GetRequest, HeadRequest} request;
time_t lastActive;
};
// - Internal helper routines
void
copyStream(InStream& is, OutStream& os) {
try {
while (1) {
os.writeU8(is.readU8());
}
} catch (rdr::EndOfStream) {
}
}
void writeLine(OutStream& os, const char* text) {
os.writeBytes(text, strlen(text));
os.writeBytes("\r\n", 2);
}
// - Write an HTTP-compliant response to the client
void
HTTPServer::Session::writeResponse(int result, const char* text) {
char buffer[1024];
if (strlen(text) > 512)
throw new rdr::Exception("Internal error - HTTP response text too big");
sprintf(buffer, "%s %d %s", "HTTP/1.1", result, text);
OutStream& os=sock.outStream();
writeLine(os, buffer);
writeLine(os, "Server: TigerVNC/4.0");
time_t now = time(0);
struct tm* tm = gmtime(&now);
strftime(buffer, 1024, "Date: %a, %d %b %Y %H:%M:%S GMT", tm);
writeLine(os, buffer);
if (lastModified == (time_t)-1 || lastModified == 0)
lastModified = now;
tm = gmtime(&lastModified);
strftime(buffer, 1024, "Last-Modified: %a, %d %b %Y %H:%M:%S GMT", tm);
writeLine(os, buffer);
if (contentLength != -1) {
sprintf(buffer,"Content-Length: %d",contentLength);
writeLine(os, buffer);
}
writeLine(os, "Connection: close");
os.writeBytes("Content-Type: ", 14);
if (result == 200) {
if (!contentType)
contentType = guessContentType(uri.buf, "text/html");
os.writeBytes(contentType, strlen(contentType));
} else {
os.writeBytes("text/html", 9);
}
os.writeBytes("\r\n", 2);
writeLine(os, "");
if (result != 200) {
writeLine(os, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">");
writeLine(os, "<HTML><HEAD>");
sprintf(buffer, "<TITLE>%d %s</TITLE>", result, text);
writeLine(os, buffer);
writeLine(os, "</HEAD><BODY><H1>");
writeLine(os, text);
writeLine(os, "</H1></BODY></HTML>");
sock.outStream().flush();
}
}
bool
HTTPServer::Session::writeResponse(int code) {
switch (code) {
case 200: writeResponse(code, "OK"); break;
case 400: writeResponse(code, "Bad Request"); break;
case 404: writeResponse(code, "Not Found"); break;
case 501: writeResponse(code, "Not Implemented"); break;
default: writeResponse(500, "Unknown Error"); break;
};
// This return code is passed straight out of processHTTP().
// true indicates that the request has been completely processed.
return true;
}
// - Main HTTP request processing routine
bool
HTTPServer::Session::processHTTP() {
lastActive = time(0);
while (sock.inStream().checkNoWait(1)) {
switch (state) {
// Reading the Request-Line
case ReadRequestLine:
// Either read a line, or run out of incoming data
if (!line.read())
return false;
// We have read a line! Skip it if it's blank
if (strlen(line.buf) == 0)
continue;
// The line contains a request to process.
{
char method[16], path[128], version[16];
int matched = sscanf(line.buf, "%15s%127s%15s",
method, path, version);
if (matched != 3)
return writeResponse(400);
// Store the required "method"
if (strcmp(method, "GET") == 0)
request = GetRequest;
else if (strcmp(method, "HEAD") == 0)
request = HeadRequest;
else
return writeResponse(501);
// Store the URI to the "document"
uri.buf = strDup(path);
}
// Move on to reading the request headers
state = ReadHeaders;
break;
// Reading the request headers
case ReadHeaders:
// Try to read a line
if (!line.read())
return false;
// Skip headers until we hit a blank line
if (strlen(line.buf) != 0)
continue;
// Headers ended - write the response!
{
CharArray address(sock.getPeerAddress());
vlog.info("getting %s for %s", uri.buf, address.buf);
contentLength = -1;
lastModified = -1;
InStream* data = server.getFile(uri.buf, &contentType, &contentLength,
&lastModified);
if (!data)
return writeResponse(404);
try {
writeResponse(200);
if (request == GetRequest)
copyStream(*data, sock.outStream());
sock.outStream().flush();
} catch (rdr::Exception& e) {
vlog.error("error writing HTTP document:%s", e.str());
}
delete data;
}
// The operation is complete!
return true;
default:
throw rdr::Exception("invalid HTTPSession state!");
};
}
// Indicate that we're still processing the HTTP request.
return false;
}
int HTTPServer::Session::checkIdleTimeout() {
time_t now = time(0);
int timeout = (lastActive + idleTimeoutSecs) - now;
if (timeout > 0)
return secsToMillis(timeout);
sock.shutdown();
return 0;
}
// -=- Constructor / destructor
HTTPServer::HTTPServer() {
}
HTTPServer::~HTTPServer() {
std::list<Session*>::iterator i;
for (i=sessions.begin(); i!=sessions.end(); i++)
delete *i;
}
// -=- SocketServer interface implementation
void
HTTPServer::addSocket(network::Socket* sock, bool) {
Session* s = new Session(*sock, *this);
if (!s) {
sock->shutdown();
} else {
sock->inStream().setTimeout(clientWaitTimeMillis);
sock->outStream().setTimeout(clientWaitTimeMillis);
sessions.push_front(s);
}
}
void
HTTPServer::removeSocket(network::Socket* sock) {
std::list<Session*>::iterator i;
for (i=sessions.begin(); i!=sessions.end(); i++) {
if ((*i)->getSock() == sock) {
delete *i;
sessions.erase(i);
return;
}
}
}
void
HTTPServer::processSocketEvent(network::Socket* sock) {
std::list<Session*>::iterator i;
for (i=sessions.begin(); i!=sessions.end(); i++) {
if ((*i)->getSock() == sock) {
try {
if ((*i)->processHTTP()) {
vlog.info("completed HTTP request");
sock->shutdown();
}
} catch (rdr::Exception& e) {
vlog.error("untrapped: %s", e.str());
sock->shutdown();
}
return;
}
}
throw rdr::Exception("invalid Socket in HTTPServer");
}
void HTTPServer::getSockets(std::list<network::Socket*>* sockets)
{
sockets->clear();
std::list<Session*>::iterator ci;
for (ci = sessions.begin(); ci != sessions.end(); ci++) {
sockets->push_back((*ci)->getSock());
}
}
int HTTPServer::checkTimeouts() {
std::list<Session*>::iterator ci;
int timeout = 0;
for (ci = sessions.begin(); ci != sessions.end(); ci++) {
soonestTimeout(&timeout, (*ci)->checkIdleTimeout());
}
return timeout;
}
// -=- Default getFile implementation
InStream*
HTTPServer::getFile(const char* name, const char** contentType,
int* contentLength, time_t* lastModified)
{
return 0;
}
const char*
HTTPServer::guessContentType(const char* name, const char* defType) {
CharArray file, ext;
if (!strSplit(name, '.', &file.buf, &ext.buf))
return defType;
if (strcasecmp(ext.buf, "html") == 0 ||
strcasecmp(ext.buf, "htm") == 0) {
return "text/html";
} else if (strcasecmp(ext.buf, "txt") == 0) {
return "text/plain";
} else if (strcasecmp(ext.buf, "gif") == 0) {
return "image/gif";
} else if (strcasecmp(ext.buf, "jpg") == 0) {
return "image/jpeg";
} else if (strcasecmp(ext.buf, "jar") == 0) {
return "application/java-archive";
} else if (strcasecmp(ext.buf, "exe") == 0) {
return "application/octet-stream";
}
return defType;
}
|