]> git.mxchange.org Git - flightgear.git/blob - src/Network/httpd.cxx
plib-1.8.0 no longer defines the cchar type (which we used because we were
[flightgear.git] / src / Network / httpd.cxx
1 // httpd.cxx -- FGFS http property manager interface / external script
2 //              and control class
3 //
4 // Written by Curtis Olson, started June 2001.
5 //
6 // Copyright (C) 2001  Curtis L. Olson - curt@flightgear.org
7 //
8 // Jpeg Image Support added August 2001
9 //  by Norman Vine - nhv@cape.com
10 //
11 // This program is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU General Public License as
13 // published by the Free Software Foundation; either version 2 of the
14 // License, or (at your option) any later version.
15 //
16 // This program is distributed in the hope that it will be useful, but
17 // WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 // General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 //
25 // $Id$
26
27
28 #ifdef HAVE_CONFIG_H
29 #  include <config.h>
30 #endif
31
32 #include <simgear/compiler.h>
33
34 #include <stdlib.h>             // atoi() atof()
35
36 #include STL_STRING
37
38 #include <simgear/debug/logstream.hxx>
39 #include <simgear/io/iochannel.hxx>
40 #include <simgear/math/sg_types.hxx>
41 #include <simgear/structure/commands.hxx>
42 #include <simgear/props/props.hxx>
43
44 #include <Main/fg_props.hxx>
45 #include <Main/globals.hxx>
46
47 #include "httpd.hxx"
48
49 SG_USING_STD(string);
50 SG_USING_STD(cout);
51
52
53 bool FGHttpd::open() {
54     if ( is_enabled() ) {
55         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
56                 << "is already in use, ignoring" );
57         return false;
58     }
59
60     server = new HttpdServer( port );
61     
62     set_hz( 15 );                // default to processing requests @ 15Hz
63     set_enabled( true );
64
65     return true;
66 }
67
68
69 bool FGHttpd::process() {
70     netChannel::poll();
71
72     return true;
73 }
74
75
76 bool FGHttpd::close() {
77     delete server;
78
79     return true;
80 }
81
82
83 // Handle http GET requests
84 void HttpdChannel::foundTerminator (void) {
85     
86     closeWhenDone ();
87
88     const string s = buffer.getData();
89
90     if ( s.find( "GET " ) == 0 ) {
91         printf("echo: %s\n", s.c_str());
92
93         string rest = s.substr(4);
94         string request;
95         string tmp;
96
97         string::size_type pos = rest.find( " " );
98         if ( pos != string::npos ) {
99             request = rest.substr( 0, pos );
100         } else {
101             request = "/";
102         }
103
104         SGPropertyNode *node = NULL;
105         pos = request.find( "?" );
106         if ( pos != string::npos ) {
107             // request to update property value
108             string args = request.substr( pos + 1 );
109             request = request.substr( 0, pos );
110             printf("'%s' '%s'\n", request.c_str(), args.c_str());
111             request = urlDecode(request);
112
113             // parse args looking for "value="
114             bool done = false;
115             while ( ! done ) {
116                 string arg;
117                 pos = args.find("&");
118                 if ( pos != string::npos ) {
119                     arg = args.substr( 0, pos );
120                     args = args.substr( pos + 1 );
121                 } else {
122                     arg = args;
123                     done = true;
124                 }
125
126                 printf("  arg = %s\n", arg.c_str() );
127                 string::size_type apos = arg.find("=");
128                 if ( apos != string::npos ) {
129                     string a = arg.substr( 0, apos );
130                     string b = arg.substr( apos + 1 );
131                     printf("    a = %s  b = %s\n", a.c_str(), b.c_str() );
132                     if ( request == "/run.cgi" ) {
133                         // execute a command
134                         if ( a == "value" ) {
135                             SGPropertyNode args;
136                             if ( !globals->get_commands()
137                                  ->execute(urlDecode(b).c_str(), &args) )
138                             {
139                                 SG_LOG( SG_GENERAL, SG_ALERT,
140                                         "Command " << urlDecode(b)
141                                         << " failed.");
142                             }
143
144                         }
145                     } else {
146                         if ( a == "value" ) {
147                             // update a property value
148                             fgSetString( request.c_str(),
149                                          urlDecode(b).c_str() );
150                         }
151                     }
152                 }
153             }
154         } else {
155             request = urlDecode(request);
156         }
157
158         node = globals->get_props()->getNode(request.c_str());
159
160         string response = "";
161         response += "<HTML LANG=\"en\">";
162         response += getTerminator();
163
164         response += "<HEAD>";
165         response += getTerminator();
166
167         response += "<TITLE>FlightGear - ";
168         response += request;
169         response += "</TITLE>";
170         response += getTerminator();
171
172         response += "</HEAD>";
173         response += getTerminator();
174
175         response += "<BODY>";
176         response += getTerminator();
177
178         if (node == NULL) {
179             response += "<H3>Non-existent node requested!</H3>";
180             response += getTerminator();
181
182             response += "<B>";
183             response += request.c_str();
184             response += "</B> does not exist.";
185             response += getTerminator();
186         } else if ( node->nChildren() > 0 ) {
187             // request is a path with children
188             response += "<H3>Contents of \"";
189             response += request;
190             response += "\"</H3>";
191             response += getTerminator();
192
193             for (int i = 0; i < node->nChildren(); i++) {
194                 SGPropertyNode *child = node->getChild(i);
195                 string name = child->getDisplayName(true);
196                 string line = "";
197                 if ( child->nChildren() > 0 ) {
198                     line += "<B><A HREF=\"";
199                     line += request;
200                     if ( request.substr(request.length() - 1, 1) != "/" ) {
201                         line += "/";
202                     }
203                     line += urlEncode(name);
204                     line += "\">";
205                     line += name;
206                     line += "</A></B>";
207                     line += "/<BR>";
208                 } else {
209                     string value = node->getStringValue ( name.c_str(), "" );
210                     line += "<B>";
211                     line += name;
212                     line += "</B> <A HREF=\"";
213                     line += request;
214                     if ( request.substr(request.length() - 1, 1) != "/" ) {
215                         line += "/";
216                     }
217                     line += urlEncode(name);
218                     line += "\">(";
219                     line += value;
220                     line += ")</A><BR>";
221                 }
222                 response += line;
223                 response += getTerminator();
224             }
225         } else {
226             // request for an individual data member
227             string value = node->getStringValue();
228             
229             response += "<form method=\"GET\" action=\"";
230             response += urlEncode(request);
231             response += "\">";
232             response += "<B>";
233             response += request;
234             response += "</B> = ";
235             response += "<input type=text name=value size=\"15\" value=\"";
236             response += value;
237             response += "\" maxlength=2047>";
238             response += "<input type=submit value=\"update\" name=\"submit\">";
239             response += "</FORM>";
240         }
241         response += "</BODY>";
242         response += getTerminator();
243
244         response += "</HTML>";
245         response += getTerminator();
246
247         push( "HTTP/1.1 200 OK" );
248         push( getTerminator() );
249         
250         printf("size = %d\n", response.length());
251         char ctmp[256];
252         sprintf(ctmp, "Content-Length: %d", response.length());
253         push( ctmp );
254         push( getTerminator() );
255
256         push( "Connection: close" );
257         push( getTerminator() );
258
259         push( "Content-Type: text/html" );
260         push( getTerminator() );
261         push( getTerminator() );
262                 
263         push( response.c_str() );
264     }
265
266     buffer.remove();
267 }
268
269
270 // encode everything but "a-zA-Z0-9_.-/" (see RFC2396)
271 string HttpdChannel::urlEncode(string s) {
272     string r = "";
273     
274     for ( int i = 0; i < (int)s.length(); i++ ) {
275         if ( isalnum(s[i]) || s[i] == '_' || s[i] == '.'
276                 || s[i] == '-' || s[i] == '/' ) {
277             r += s[i];
278         } else {
279             char buf[16];
280             sprintf(buf, "%%%02X", (unsigned char)s[i]);
281             r += buf;
282         }
283     }
284     return r;
285 }
286
287
288 string HttpdChannel::urlDecode(string s) {
289     string r = "";
290     int max = s.length();
291     int a, b;
292
293     for ( int i = 0; i < max; i++ ) {
294         if ( s[i] == '+' ) {
295             r += ' ';
296         } else if ( s[i] == '%' && i + 2 < max
297                 && isxdigit(s[i + 1])
298                 && isxdigit(s[i + 2]) ) {
299             i++;
300             a = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10;
301             i++;
302             b = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10;
303             r += (char)(a * 16 + b);
304         } else {
305             r += s[i];
306         }
307     }
308     return r;
309 }