2
* httpd.c - a simple HTTP server
6
* Copyright (C) 2002 RealVNC Ltd.
7
* Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
9
* This is free software; you can redistribute it and/or modify
10
* it under the terms of the GNU General Public License as published by
11
* the Free Software Foundation; either version 2 of the License, or
12
* (at your option) any later version.
14
* This software is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU General Public License for more details.
19
* You should have received a copy of the GNU General Public License
20
* along with this software; if not, write to the Free Software
21
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
31
#ifdef HAVE_SYS_TYPES_H
32
#include <sys/types.h>
41
#define close closesocket
43
#ifdef HAVE_SYS_TIME_H
46
#ifdef HAVE_SYS_SOCKET_H
47
#include <sys/socket.h>
49
#ifdef HAVE_NETINET_IN_H
50
#include <netinet/in.h>
51
#include <netinet/tcp.h>
53
#include <arpa/inet.h>
62
#define NOT_FOUND_STR "HTTP/1.0 404 Not found\r\n\r\n" \
63
"<HEAD><TITLE>File Not Found</TITLE></HEAD>\n" \
64
"<BODY><H1>File Not Found</H1></BODY>\n"
66
#define INVALID_REQUEST_STR "HTTP/1.0 400 Invalid Request\r\n\r\n" \
67
"<HEAD><TITLE>Invalid Request</TITLE></HEAD>\n" \
68
"<BODY><H1>Invalid request</H1></BODY>\n"
70
#define OK_STR "HTTP/1.0 200 OK\nContent-Type: text/html\r\n\r\n"
72
static void httpProcessInput();
73
static rfbBool compareAndSkip(char **ptr, const char *str);
74
static rfbBool parseParams(const char *request, char *result, int max_bytes);
75
static rfbBool validateString(char *str);
77
#define BUF_SIZE 32768
79
static char buf[BUF_SIZE];
80
static size_t buf_filled=0;
83
* httpInitSockets sets up the TCP socket to listen for HTTP connections.
87
httpInitSockets(rfbScreenInfoPtr rfbScreen)
89
if (rfbScreen->httpInitDone)
92
rfbScreen->httpInitDone = TRUE;
94
if (!rfbScreen->httpDir)
97
if (rfbScreen->httpPort == 0) {
98
rfbScreen->httpPort = rfbScreen->rfbPort-100;
101
rfbLog("Listening for HTTP connections on TCP port %d\n", rfbScreen->httpPort);
103
rfbLog(" URL http://%s:%d\n",rfbScreen->rfbThisHost,rfbScreen->httpPort);
105
if ((rfbScreen->httpListenSock = ListenOnTCPPort(rfbScreen->httpPort)) < 0) {
106
rfbLogPerror("ListenOnTCPPort");
110
/*AddEnabledDevice(httpListenSock);*/
115
* httpCheckFds is called from ProcessInputEvents to check for input on the
116
* HTTP socket(s). If there is input to process, httpProcessInput is called.
120
httpCheckFds(rfbScreenInfoPtr rfbScreen)
125
struct sockaddr_in addr;
126
size_t addrlen = sizeof(addr);
128
if (!rfbScreen->httpDir)
132
FD_SET(rfbScreen->httpListenSock, &fds);
133
if (rfbScreen->httpSock >= 0) {
134
FD_SET(rfbScreen->httpSock, &fds);
138
nfds = select(max(rfbScreen->httpSock,rfbScreen->httpListenSock) + 1, &fds, NULL, NULL, &tv);
144
errno = WSAGetLastError();
147
rfbLogPerror("httpCheckFds: select");
151
if ((rfbScreen->httpSock >= 0) && FD_ISSET(rfbScreen->httpSock, &fds)) {
152
httpProcessInput(rfbScreen);
155
if (FD_ISSET(rfbScreen->httpListenSock, &fds)) {
157
if (rfbScreen->httpSock >= 0) close(rfbScreen->httpSock);
159
if ((rfbScreen->httpSock = accept(rfbScreen->httpListenSock,
160
(struct sockaddr *)&addr, &addrlen)) < 0) {
161
rfbLogPerror("httpCheckFds: accept");
165
if(!hosts_ctl("vnc",STRING_UNKNOWN,inet_ntoa(addr.sin_addr),
167
rfbLog("Rejected HTTP connection from client %s\n",
168
inet_ntoa(addr.sin_addr));
170
flags = fcntl(rfbScreen->httpSock, F_GETFL);
172
if (flags < 0 || fcntl(rfbScreen->httpSock, F_SETFL, flags | O_NONBLOCK) == -1) {
173
rfbLogPerror("httpCheckFds: fcntl");
175
close(rfbScreen->httpSock);
176
rfbScreen->httpSock = -1;
179
flags=fcntl(rfbScreen->httpSock,F_GETFL);
181
fcntl(rfbScreen->httpSock,F_SETFL,flags|O_NONBLOCK)==-1) {
182
rfbLogPerror("httpCheckFds: fcntl");
183
close(rfbScreen->httpSock);
184
rfbScreen->httpSock=-1;
188
/*AddEnabledDevice(httpSock);*/
194
httpCloseSock(rfbScreenInfoPtr rfbScreen)
196
close(rfbScreen->httpSock);
197
rfbScreen->httpSock = -1;
201
static rfbClientRec cl;
204
* httpProcessInput is called when input is received on the HTTP socket.
208
httpProcessInput(rfbScreenInfoPtr rfbScreen)
210
struct sockaddr_in addr;
211
size_t addrlen = sizeof(addr);
216
unsigned int maxFnameLen;
218
rfbBool performSubstitutions = FALSE;
221
char* user=getenv("USER");
224
cl.sock=rfbScreen->httpSock;
226
if (strlen(rfbScreen->httpDir) > 255) {
227
rfbErr("-httpd directory too long\n");
228
httpCloseSock(rfbScreen);
231
strcpy(fullFname, rfbScreen->httpDir);
232
fname = &fullFname[strlen(fullFname)];
233
maxFnameLen = 511 - strlen(fullFname);
237
/* Read data from the HTTP client until we get a complete request. */
241
if (buf_filled > sizeof (buf)) {
242
rfbErr("httpProcessInput: HTTP request is too long\n");
243
httpCloseSock(rfbScreen);
247
got = read (rfbScreen->httpSock, buf + buf_filled,
248
sizeof (buf) - buf_filled - 1);
252
rfbErr("httpd: premature connection close\n");
254
if (errno == EAGAIN) {
257
rfbLogPerror("httpProcessInput: read");
259
httpCloseSock(rfbScreen);
264
buf[buf_filled] = '\0';
266
/* Is it complete yet (is there a blank line)? */
267
if (strstr (buf, "\r\r") || strstr (buf, "\n\n") ||
268
strstr (buf, "\r\n\r\n") || strstr (buf, "\n\r\n\r"))
273
/* Process the request. */
274
if(rfbScreen->httpEnableProxyConnect) {
275
const static char* PROXY_OK_STR = "HTTP/1.0 200 OK\r\nContent-Type: octet-stream\r\nPragma: no-cache\r\n\r\n";
276
if(!strncmp(buf, "CONNECT ", 8)) {
277
if(atoi(strchr(buf, ':')+1)!=rfbScreen->rfbPort) {
278
rfbErr("httpd: CONNECT format invalid.\n");
279
WriteExact(&cl,INVALID_REQUEST_STR, strlen(INVALID_REQUEST_STR));
280
httpCloseSock(rfbScreen);
283
/* proxy connection */
284
rfbLog("httpd: client asked for CONNECT\n");
285
WriteExact(&cl,PROXY_OK_STR,strlen(PROXY_OK_STR));
286
rfbNewClientConnection(rfbScreen,rfbScreen->httpSock);
287
rfbScreen->httpSock = -1;
290
if (!strncmp(buf, "GET ",4) && !strncmp(strchr(buf,'/'),"/proxied.connection HTTP/1.", 27)) {
291
/* proxy connection */
292
rfbLog("httpd: client asked for /proxied.connection\n");
293
WriteExact(&cl,PROXY_OK_STR,strlen(PROXY_OK_STR));
294
rfbNewClientConnection(rfbScreen,rfbScreen->httpSock);
295
rfbScreen->httpSock = -1;
300
if (strncmp(buf, "GET ", 4)) {
301
rfbErr("httpd: no GET line\n");
302
httpCloseSock(rfbScreen);
305
/* Only use the first line. */
306
buf[strcspn(buf, "\n\r")] = '\0';
309
if (strlen(buf) > maxFnameLen) {
310
rfbErr("httpd: GET line too long\n");
311
httpCloseSock(rfbScreen);
315
if (sscanf(buf, "GET %s HTTP/1.0", fname) != 1) {
316
rfbErr("httpd: couldn't parse GET line\n");
317
httpCloseSock(rfbScreen);
321
if (fname[0] != '/') {
322
rfbErr("httpd: filename didn't begin with '/'\n");
323
WriteExact(&cl, NOT_FOUND_STR, strlen(NOT_FOUND_STR));
324
httpCloseSock(rfbScreen);
328
if (strchr(fname+1, '/') != NULL) {
329
rfbErr("httpd: asking for file in other directory\n");
330
WriteExact(&cl, NOT_FOUND_STR, strlen(NOT_FOUND_STR));
331
httpCloseSock(rfbScreen);
335
getpeername(rfbScreen->httpSock, (struct sockaddr *)&addr, &addrlen);
336
rfbLog("httpd: get '%s' for %s\n", fname+1,
337
inet_ntoa(addr.sin_addr));
339
/* Extract parameters from the URL string if necessary */
342
ptr = strchr(fname, '?');
345
if (!parseParams(&ptr[1], params, 1024)) {
347
rfbErr("httpd: bad parameters in the URL\n");
352
/* If we were asked for '/', actually read the file index.vnc */
354
if (strcmp(fname, "/") == 0) {
355
strcpy(fname, "/index.vnc");
356
rfbLog("httpd: defaulting to '%s'\n", fname+1);
359
/* Substitutions are performed on files ending .vnc */
361
if (strlen(fname) >= 4 && strcmp(&fname[strlen(fname)-4], ".vnc") == 0) {
362
performSubstitutions = TRUE;
367
if ((fd = fopen(fullFname, "r")) == 0) {
368
rfbLogPerror("httpProcessInput: open");
369
WriteExact(&cl, NOT_FOUND_STR, strlen(NOT_FOUND_STR));
370
httpCloseSock(rfbScreen);
374
WriteExact(&cl, OK_STR, strlen(OK_STR));
377
int n = fread(buf, 1, BUF_SIZE-1, fd);
379
rfbLogPerror("httpProcessInput: read");
381
httpCloseSock(rfbScreen);
388
if (performSubstitutions) {
390
/* Substitute $WIDTH, $HEIGHT, etc with the appropriate values.
391
This won't quite work properly if the .vnc file is longer than
392
BUF_SIZE, but it's reasonable to assume that .vnc files will
397
buf[n] = 0; /* make sure it's null-terminated */
399
while ((dollar = strchr(ptr, '$'))!=NULL) {
400
WriteExact(&cl, ptr, (dollar - ptr));
404
if (compareAndSkip(&ptr, "$WIDTH")) {
406
sprintf(str, "%d", rfbScreen->width);
407
WriteExact(&cl, str, strlen(str));
409
} else if (compareAndSkip(&ptr, "$HEIGHT")) {
411
sprintf(str, "%d", rfbScreen->height);
412
WriteExact(&cl, str, strlen(str));
414
} else if (compareAndSkip(&ptr, "$APPLETWIDTH")) {
416
sprintf(str, "%d", rfbScreen->width);
417
WriteExact(&cl, str, strlen(str));
419
} else if (compareAndSkip(&ptr, "$APPLETHEIGHT")) {
421
sprintf(str, "%d", rfbScreen->height + 32);
422
WriteExact(&cl, str, strlen(str));
424
} else if (compareAndSkip(&ptr, "$PORT")) {
426
sprintf(str, "%d", rfbScreen->rfbPort);
427
WriteExact(&cl, str, strlen(str));
429
} else if (compareAndSkip(&ptr, "$DESKTOP")) {
431
WriteExact(&cl, rfbScreen->desktopName, strlen(rfbScreen->desktopName));
433
} else if (compareAndSkip(&ptr, "$DISPLAY")) {
435
sprintf(str, "%s:%d", rfbScreen->rfbThisHost, rfbScreen->rfbPort-5900);
436
WriteExact(&cl, str, strlen(str));
438
} else if (compareAndSkip(&ptr, "$USER")) {
441
WriteExact(&cl, user,
445
WriteExact(&cl, "?", 1);
446
} else if (compareAndSkip(&ptr, "$PARAMS")) {
447
if (params[0] != '\0')
448
WriteExact(&cl, params, strlen(params));
450
if (!compareAndSkip(&ptr, "$$"))
453
if (WriteExact(&cl, "$", 1) < 0) {
455
httpCloseSock(rfbScreen);
460
if (WriteExact(&cl, ptr, (&buf[n] - ptr)) < 0)
465
/* For files not ending .vnc, just write out the buffer */
467
if (WriteExact(&cl, buf, n) < 0)
473
httpCloseSock(rfbScreen);
478
compareAndSkip(char **ptr, const char *str)
480
if (strncmp(*ptr, str, strlen(str)) == 0) {
489
* Parse the request tail after the '?' character, and format a sequence
490
* of <param> tags for inclusion into an HTML page with embedded applet.
494
parseParams(const char *request, char *result, int max_bytes)
496
char param_request[128];
497
char param_formatted[196];
508
/* Copy individual "name=value" string into a buffer */
509
delim_ptr = strchr((char *)tail, '&');
510
if (delim_ptr == NULL) {
511
if (strlen(tail) >= sizeof(param_request)) {
514
strcpy(param_request, tail);
516
len = delim_ptr - tail;
517
if (len >= sizeof(param_request)) {
520
memcpy(param_request, tail, len);
521
param_request[len] = '\0';
524
/* Split the request into parameter name and value */
525
value_str = strchr(¶m_request[1], '=');
526
if (value_str == NULL) {
530
if (strlen(value_str) == 0) {
534
/* Validate both parameter name and value */
535
if (!validateString(param_request) || !validateString(value_str)) {
539
/* Prepare HTML-formatted representation of the name=value pair */
540
len = sprintf(param_formatted,
541
"<PARAM NAME=\"%s\" VALUE=\"%s\">\n",
542
param_request, value_str);
543
if (cur_bytes + len + 1 > max_bytes) {
546
strcat(result, param_formatted);
549
/* Go to the next parameter */
550
if (delim_ptr == NULL) {
553
tail = delim_ptr + 1;
559
* Check if the string consists only of alphanumeric characters, '+'
560
* signs, underscores, and dots. Replace all '+' signs with spaces.
564
validateString(char *str)
568
for (ptr = str; *ptr != '\0'; ptr++) {
569
if (!isalnum(*ptr) && *ptr != '_' && *ptr != '.') {