]> git.sesse.net Git - rdpsrv/blob - Xserver/programs/Xserver/hw/vnc/httpd.c
Import X server from vnc-3.3.7.
[rdpsrv] / Xserver / programs / Xserver / hw / vnc / httpd.c
1 /*
2  * httpd.c - a simple HTTP server
3  */
4
5 /*
6  *  Copyright (C) 2002 RealVNC Ltd.
7  *  Copyright (C) 1999 AT&T Laboratories Cambridge.  All Rights Reserved.
8  *
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.
13  *
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.
18  *
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,
22  *  USA.
23  */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/time.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <netinet/tcp.h>
33 #include <arpa/inet.h>
34 #include <netdb.h>
35 #include <fcntl.h>
36 #include <errno.h>
37 #include <pwd.h>
38
39 #include "rfb.h"
40
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"
44
45 #define OK_STR "HTTP/1.0 200 OK\r\n\r\n"
46
47 static void httpProcessInput();
48 static Bool compareAndSkip(char **ptr, const char *str);
49
50 int httpPort = 0;
51 char *httpDir = NULL;
52
53 int httpListenSock = -1;
54 int httpSock = -1;
55
56 #define BUF_SIZE 32768
57
58 static char buf[BUF_SIZE];
59 static int bufLen;
60
61
62 /*
63  * httpInitSockets sets up the TCP socket to listen for HTTP connections.
64  */
65
66 void httpInitSockets()
67 {
68     static Bool done = FALSE;
69
70     if (done)
71         return;
72
73     done = TRUE;
74
75     if (!httpDir)
76         return;
77
78     if (httpPort == 0) {
79         httpPort = 5800 + atoi(display);
80     }
81
82     rfbLog("Listening for HTTP connections on TCP port %d\n", httpPort);
83
84     rfbLog("  URL http://%s:%d\n",rfbThisHost,httpPort);
85
86     if ((httpListenSock = ListenOnTCPPort(httpPort)) < 0) {
87         rfbLogPerror("ListenOnTCPPort");
88         exit(1);
89     }
90
91     AddEnabledDevice(httpListenSock);
92 }
93
94
95 /*
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.
98  */
99
100 void httpCheckFds()
101 {
102     int nfds;
103     fd_set fds;
104     struct timeval tv;
105     struct sockaddr_in addr;
106     unsigned int addrlen = sizeof(addr);
107     int flags;
108
109     if (!httpDir)
110         return;
111
112     FD_ZERO(&fds);
113     FD_SET(httpListenSock, &fds);
114     if (httpSock >= 0) {
115         FD_SET(httpSock, &fds);
116     }
117     tv.tv_sec = 0;
118     tv.tv_usec = 0;
119     nfds = select(max(httpSock,httpListenSock) + 1, &fds, NULL, NULL, &tv);
120     if (nfds == 0) {
121         return;
122     }
123     if (nfds < 0) {
124         rfbLogPerror("httpCheckFds: select");
125         return;
126     }
127
128     if ((httpSock >= 0) && FD_ISSET(httpSock, &fds)) {
129         httpProcessInput();
130     }
131
132     if (FD_ISSET(httpListenSock, &fds)) {
133
134         if (httpSock >= 0) close(httpSock);
135
136         if ((httpSock = accept(httpListenSock,
137                                (struct sockaddr *)&addr, &addrlen)) < 0) {
138             rfbLogPerror("httpCheckFds: accept");
139             return;
140         }
141
142         flags = fcntl(httpSock, F_GETFL);
143
144         if (flags < 0 || fcntl(httpSock, F_SETFL, flags | O_NONBLOCK) == -1) {
145             rfbLogPerror("httpCheckFds: fcntl");
146             close(httpSock);
147             httpSock = -1;
148             return;
149         }
150
151         AddEnabledDevice(httpSock);
152         bufLen = 0;
153     }
154 }
155
156
157 static void httpCloseSock()
158 {
159     close(httpSock);
160     RemoveEnabledDevice(httpSock);
161     httpSock = -1;
162 }
163
164
165 /*
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.
170  */
171
172 static void httpProcessInput()
173 {
174     struct sockaddr_in addr;
175     unsigned int addrlen = sizeof(addr);
176     char fullFname[256];
177     char *fname;
178     int maxFnameLen;
179     int fd;
180     Bool performSubstitutions = FALSE;
181     char str[256];
182     struct passwd *user = getpwuid(getuid());
183     int n;
184
185     if (strlen(httpDir) > 200) {
186         rfbLog("-httpd directory too long\n");
187         httpCloseSock();
188         return;
189     }
190     strcpy(fullFname, httpDir);
191     fname = &fullFname[strlen(fullFname)];
192     maxFnameLen = 255 - strlen(fullFname);
193
194     while (1) {
195         if (bufLen >= BUF_SIZE-1) {
196             rfbLog("httpProcessInput: HTTP request is too long\n");
197             httpCloseSock();
198             return;
199         }
200
201         n = read(httpSock, buf + bufLen, BUF_SIZE - bufLen - 1);
202
203         if (n <= 0) {
204             if (n < 0) {
205                 if (errno == EAGAIN) return;
206                 rfbLogPerror("httpProcessInput: read");
207             } else {
208                 rfbLog("httpProcessInput: connection closed\n");
209             }
210             httpCloseSock();
211             return;
212         }
213
214         bufLen += n;
215         buf[bufLen] = 0;
216
217         if (strstr(buf, "\r\r") || strstr(buf, "\n\n") ||
218             strstr(buf, "\r\n\r\n") || strstr(buf, "\n\r\n\r"))
219             break;
220     }
221
222     if (strncmp(buf, "GET ", 4) != 0) {
223         rfbLog("httpProcessInput: first line wasn't a GET?\n");
224         httpCloseSock();
225         return;
226     }
227
228     buf[strcspn(buf, "\r\n")] = 0; /* only want first line */
229
230     if (strlen(buf) > maxFnameLen) {
231         rfbLog("httpProcessInput: GET line too long\n");
232         httpCloseSock();
233         return;
234     }
235
236     if (sscanf(buf, "GET %s HTTP", fname) != 1) {
237         rfbLog("httpProcessInput: couldn't parse GET line\n");
238         httpCloseSock();
239         return;
240     }
241
242     if (fname[0] != '/') {
243         rfbLog("httpProcessInput: filename didn't begin with '/'\n");
244         WriteExact(httpSock, NOT_FOUND_STR, strlen(NOT_FOUND_STR));
245         httpCloseSock();
246         return;
247     }
248
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));
252         httpCloseSock();
253         return;
254     }
255
256     getpeername(httpSock, (struct sockaddr *)&addr, &addrlen);
257     rfbLog("httpd: get '%s' for %s\n", fname+1,
258            inet_ntoa(addr.sin_addr));
259
260     /* If we were asked for '/', actually read the file index.vnc */
261
262     if (strcmp(fname, "/") == 0) {
263         strcpy(fname, "/index.vnc");
264         rfbLog("httpd: defaulting to '%s'\n", fname+1);
265     }
266
267     /* Substitutions are performed on files ending .vnc */
268
269     if (strlen(fname) >= 4 && strcmp(&fname[strlen(fname)-4], ".vnc") == 0) {
270         performSubstitutions = TRUE;
271     }
272
273     /* Open the file */
274
275     if ((fd = open(fullFname, O_RDONLY)) < 0) {
276         rfbLogPerror("httpProcessInput: open");
277         WriteExact(httpSock, NOT_FOUND_STR, strlen(NOT_FOUND_STR));
278         httpCloseSock();
279         return;
280     }
281
282     WriteExact(httpSock, OK_STR, strlen(OK_STR));
283
284     while (1) {
285         int n = read(fd, buf, BUF_SIZE-1);
286         if (n < 0) {
287             rfbLogPerror("httpProcessInput: read");
288             close(fd);
289             httpCloseSock();
290             return;
291         }
292
293         if (n == 0)
294             break;
295
296         if (performSubstitutions) {
297
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
301                always be short. */
302
303             char *ptr = buf;
304             char *dollar;
305             buf[n] = 0; /* make sure it's null-terminated */
306
307             while ((dollar = strchr(ptr, '$'))) {
308                 WriteExact(httpSock, ptr, (dollar - ptr));
309
310                 ptr = dollar;
311
312                 if (compareAndSkip(&ptr, "$WIDTH")) {
313
314                     sprintf(str, "%d", rfbScreen.width);
315                     WriteExact(httpSock, str, strlen(str));
316
317                 } else if (compareAndSkip(&ptr, "$HEIGHT")) {
318
319                     sprintf(str, "%d", rfbScreen.height);
320                     WriteExact(httpSock, str, strlen(str));
321
322                 } else if (compareAndSkip(&ptr, "$APPLETWIDTH")) {
323
324                     sprintf(str, "%d", rfbScreen.width);
325                     WriteExact(httpSock, str, strlen(str));
326
327                 } else if (compareAndSkip(&ptr, "$APPLETHEIGHT")) {
328
329                     sprintf(str, "%d", rfbScreen.height + 32);
330                     WriteExact(httpSock, str, strlen(str));
331
332                 } else if (compareAndSkip(&ptr, "$PORT")) {
333
334                     sprintf(str, "%d", rfbPort);
335                     WriteExact(httpSock, str, strlen(str));
336
337                 } else if (compareAndSkip(&ptr, "$DESKTOP")) {
338
339                     WriteExact(httpSock, desktopName, strlen(desktopName));
340
341                 } else if (compareAndSkip(&ptr, "$DISPLAY")) {
342
343                     sprintf(str, "%s:%s", rfbThisHost, display);
344                     WriteExact(httpSock, str, strlen(str));
345
346                 } else if (compareAndSkip(&ptr, "$USER")) {
347
348                     if (user) {
349                         WriteExact(httpSock, user->pw_name,
350                                    strlen(user->pw_name));
351                     } else {
352                         WriteExact(httpSock, "?", 1);
353                     }
354
355                 } else {
356                     if (!compareAndSkip(&ptr, "$$"))
357                         ptr++;
358
359                     if (WriteExact(httpSock, "$", 1) < 0) {
360                         close(fd);
361                         httpCloseSock();
362                         return;
363                     }
364                 }
365             }
366             if (WriteExact(httpSock, ptr, (&buf[n] - ptr)) < 0)
367                 break;
368
369         } else {
370
371             /* For files not ending .vnc, just write out the buffer */
372
373             if (WriteExact(httpSock, buf, n) < 0)
374                 break;
375         }
376     }
377
378     close(fd);
379     httpCloseSock();
380 }
381
382
383 static Bool
384 compareAndSkip(char **ptr, const char *str)
385 {
386     if (strncmp(*ptr, str, strlen(str)) == 0) {
387         *ptr += strlen(str);
388         return TRUE;
389     }
390
391     return FALSE;
392 }