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,
28 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <netinet/tcp.h>
33 #include <arpa/inet.h>
41 #define NOT_FOUND_STR "HTTP/1.0 404 Not found\r\n\r\n" \
42 "<HEAD><TITLE>File Not Found</TITLE></HEAD>\n" \
43 "<BODY><H1>File Not Found</H1></BODY>\n"
45 #define OK_STR "HTTP/1.0 200 OK\r\n\r\n"
47 static void httpProcessInput();
48 static Bool compareAndSkip(char **ptr, const char *str);
53 int httpListenSock = -1;
56 #define BUF_SIZE 32768
58 static char buf[BUF_SIZE];
63 * httpInitSockets sets up the TCP socket to listen for HTTP connections.
66 void httpInitSockets()
68 static Bool done = FALSE;
79 httpPort = 5800 + atoi(display);
82 rfbLog("Listening for HTTP connections on TCP port %d\n", httpPort);
84 rfbLog(" URL http://%s:%d\n",rfbThisHost,httpPort);
86 if ((httpListenSock = ListenOnTCPPort(httpPort)) < 0) {
87 rfbLogPerror("ListenOnTCPPort");
91 AddEnabledDevice(httpListenSock);
96 * httpCheckFds is called from ProcessInputEvents to check for input on the
97 * HTTP socket(s). If there is input to process, httpProcessInput is called.
105 struct sockaddr_in addr;
106 unsigned int addrlen = sizeof(addr);
113 FD_SET(httpListenSock, &fds);
115 FD_SET(httpSock, &fds);
119 nfds = select(max(httpSock,httpListenSock) + 1, &fds, NULL, NULL, &tv);
124 rfbLogPerror("httpCheckFds: select");
128 if ((httpSock >= 0) && FD_ISSET(httpSock, &fds)) {
132 if (FD_ISSET(httpListenSock, &fds)) {
134 if (httpSock >= 0) close(httpSock);
136 if ((httpSock = accept(httpListenSock,
137 (struct sockaddr *)&addr, &addrlen)) < 0) {
138 rfbLogPerror("httpCheckFds: accept");
142 flags = fcntl(httpSock, F_GETFL);
144 if (flags < 0 || fcntl(httpSock, F_SETFL, flags | O_NONBLOCK) == -1) {
145 rfbLogPerror("httpCheckFds: fcntl");
151 AddEnabledDevice(httpSock);
157 static void httpCloseSock()
160 RemoveEnabledDevice(httpSock);
166 * httpProcessInput is called when input is received on the HTTP socket. We
167 * read lines from the HTTP client until we get a blank line (the end of an
168 * HTTP request). The socket is non-blocking so we return if there's no more
169 * to read and will carry on where we left off when more data is available.
172 static void httpProcessInput()
174 struct sockaddr_in addr;
175 unsigned int addrlen = sizeof(addr);
180 Bool performSubstitutions = FALSE;
182 struct passwd *user = getpwuid(getuid());
185 if (strlen(httpDir) > 200) {
186 rfbLog("-httpd directory too long\n");
190 strcpy(fullFname, httpDir);
191 fname = &fullFname[strlen(fullFname)];
192 maxFnameLen = 255 - strlen(fullFname);
195 if (bufLen >= BUF_SIZE-1) {
196 rfbLog("httpProcessInput: HTTP request is too long\n");
201 n = read(httpSock, buf + bufLen, BUF_SIZE - bufLen - 1);
205 if (errno == EAGAIN) return;
206 rfbLogPerror("httpProcessInput: read");
208 rfbLog("httpProcessInput: connection closed\n");
217 if (strstr(buf, "\r\r") || strstr(buf, "\n\n") ||
218 strstr(buf, "\r\n\r\n") || strstr(buf, "\n\r\n\r"))
222 if (strncmp(buf, "GET ", 4) != 0) {
223 rfbLog("httpProcessInput: first line wasn't a GET?\n");
228 buf[strcspn(buf, "\r\n")] = 0; /* only want first line */
230 if (strlen(buf) > maxFnameLen) {
231 rfbLog("httpProcessInput: GET line too long\n");
236 if (sscanf(buf, "GET %s HTTP", fname) != 1) {
237 rfbLog("httpProcessInput: couldn't parse GET line\n");
242 if (fname[0] != '/') {
243 rfbLog("httpProcessInput: filename didn't begin with '/'\n");
244 WriteExact(httpSock, NOT_FOUND_STR, strlen(NOT_FOUND_STR));
249 if (strchr(fname+1, '/') != NULL) {
250 rfbLog("httpProcessInput: asking for file in other directory\n");
251 WriteExact(httpSock, NOT_FOUND_STR, strlen(NOT_FOUND_STR));
256 getpeername(httpSock, (struct sockaddr *)&addr, &addrlen);
257 rfbLog("httpd: get '%s' for %s\n", fname+1,
258 inet_ntoa(addr.sin_addr));
260 /* If we were asked for '/', actually read the file index.vnc */
262 if (strcmp(fname, "/") == 0) {
263 strcpy(fname, "/index.vnc");
264 rfbLog("httpd: defaulting to '%s'\n", fname+1);
267 /* Substitutions are performed on files ending .vnc */
269 if (strlen(fname) >= 4 && strcmp(&fname[strlen(fname)-4], ".vnc") == 0) {
270 performSubstitutions = TRUE;
275 if ((fd = open(fullFname, O_RDONLY)) < 0) {
276 rfbLogPerror("httpProcessInput: open");
277 WriteExact(httpSock, NOT_FOUND_STR, strlen(NOT_FOUND_STR));
282 WriteExact(httpSock, OK_STR, strlen(OK_STR));
285 int n = read(fd, buf, BUF_SIZE-1);
287 rfbLogPerror("httpProcessInput: read");
296 if (performSubstitutions) {
298 /* Substitute $WIDTH, $HEIGHT, etc with the appropriate values.
299 This won't quite work properly if the .vnc file is longer than
300 BUF_SIZE, but it's reasonable to assume that .vnc files will
305 buf[n] = 0; /* make sure it's null-terminated */
307 while ((dollar = strchr(ptr, '$'))) {
308 WriteExact(httpSock, ptr, (dollar - ptr));
312 if (compareAndSkip(&ptr, "$WIDTH")) {
314 sprintf(str, "%d", rfbScreen.width);
315 WriteExact(httpSock, str, strlen(str));
317 } else if (compareAndSkip(&ptr, "$HEIGHT")) {
319 sprintf(str, "%d", rfbScreen.height);
320 WriteExact(httpSock, str, strlen(str));
322 } else if (compareAndSkip(&ptr, "$APPLETWIDTH")) {
324 sprintf(str, "%d", rfbScreen.width);
325 WriteExact(httpSock, str, strlen(str));
327 } else if (compareAndSkip(&ptr, "$APPLETHEIGHT")) {
329 sprintf(str, "%d", rfbScreen.height + 32);
330 WriteExact(httpSock, str, strlen(str));
332 } else if (compareAndSkip(&ptr, "$PORT")) {
334 sprintf(str, "%d", rfbPort);
335 WriteExact(httpSock, str, strlen(str));
337 } else if (compareAndSkip(&ptr, "$DESKTOP")) {
339 WriteExact(httpSock, desktopName, strlen(desktopName));
341 } else if (compareAndSkip(&ptr, "$DISPLAY")) {
343 sprintf(str, "%s:%s", rfbThisHost, display);
344 WriteExact(httpSock, str, strlen(str));
346 } else if (compareAndSkip(&ptr, "$USER")) {
349 WriteExact(httpSock, user->pw_name,
350 strlen(user->pw_name));
352 WriteExact(httpSock, "?", 1);
356 if (!compareAndSkip(&ptr, "$$"))
359 if (WriteExact(httpSock, "$", 1) < 0) {
366 if (WriteExact(httpSock, ptr, (&buf[n] - ptr)) < 0)
371 /* For files not ending .vnc, just write out the buffer */
373 if (WriteExact(httpSock, buf, n) < 0)
384 compareAndSkip(char **ptr, const char *str)
386 if (strncmp(*ptr, str, strlen(str)) == 0) {