1 // httpd.cxx -- FGFS http property manager interface / external script
4 // Written by Curtis Olson, started June 2001.
6 // Copyright (C) 2001 Curtis L. Olson - curt@flightgear.org
8 // Jpeg Image Support added August 2001
9 // by Norman Vine - nhv@cape.com
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.
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.
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.
32 #include <simgear/compiler.h>
34 #include <stdlib.h> // atoi() atof()
37 #include STL_STRSTREAM
39 #include <simgear/debug/logstream.hxx>
40 #include <simgear/io/iochannel.hxx>
41 #include <simgear/math/sg_types.hxx>
42 #include <simgear/misc/commands.hxx>
43 #include <simgear/misc/props.hxx>
45 #include <Main/fg_props.hxx>
46 #include <Main/globals.hxx>
51 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
53 SG_USING_STD(istrstream);
57 bool FGHttpd::open() {
59 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
60 << "is already in use, ignoring" );
64 server = new HttpdServer( port );
66 set_hz( 15 ); // default to processing requests @ 15Hz
73 bool FGHttpd::process() {
80 bool FGHttpd::close() {
87 // Handle http GET requests
88 void HttpdChannel::foundTerminator (void) {
92 const string s = buffer.getData();
94 if ( s.find( "GET " ) == 0 ) {
95 printf("echo: %s\n", s.c_str());
97 string rest = s.substr(4);
101 string::size_type pos = rest.find( " " );
102 if ( pos != string::npos ) {
103 request = rest.substr( 0, pos );
108 SGPropertyNode *node = NULL;
109 pos = request.find( "?" );
110 if ( pos != string::npos ) {
111 // request to update property value
112 string args = request.substr( pos + 1 );
113 request = request.substr( 0, pos );
114 printf("'%s' '%s'\n", request.c_str(), args.c_str());
115 request = urlDecode(request);
117 // parse args looking for "value="
121 pos = args.find("&");
122 if ( pos != string::npos ) {
123 arg = args.substr( 0, pos );
124 args = args.substr( pos + 1 );
130 printf(" arg = %s\n", arg.c_str() );
131 string::size_type apos = arg.find("=");
132 if ( apos != string::npos ) {
133 string a = arg.substr( 0, apos );
134 string b = arg.substr( apos + 1 );
135 printf(" a = %s b = %s\n", a.c_str(), b.c_str() );
136 if ( request == "/run.cgi" ) {
138 if ( a == "value" ) {
140 if ( !globals->get_commands()
141 ->execute(urlDecode(b).c_str(), &args) )
143 SG_LOG( SG_GENERAL, SG_ALERT,
144 "Command " << urlDecode(b)
150 if ( a == "value" ) {
151 // update a property value
152 fgSetString( request.c_str(),
153 urlDecode(b).c_str() );
159 request = urlDecode(request);
162 node = globals->get_props()->getNode(request.c_str());
164 string response = "";
165 response += "<HTML LANG=\"en\">";
166 response += getTerminator();
168 response += "<HEAD>";
169 response += getTerminator();
171 response += "<TITLE>FlightGear - ";
173 response += "</TITLE>";
174 response += getTerminator();
176 response += "</HEAD>";
177 response += getTerminator();
179 response += "<BODY>";
180 response += getTerminator();
183 response += "<H3>Non-existent node requested!</H3>";
184 response += getTerminator();
187 response += request.c_str();
188 response += "</B> does not exist.";
189 response += getTerminator();
190 } else if ( node->nChildren() > 0 ) {
191 // request is a path with children
192 response += "<H3>Contents of \"";
194 response += "\"</H3>";
195 response += getTerminator();
197 for (int i = 0; i < node->nChildren(); i++) {
198 SGPropertyNode *child = node->getChild(i);
199 string name = child->getDisplayName(true);
201 if ( child->nChildren() > 0 ) {
202 line += "<B><A HREF=\"";
204 if ( request.substr(request.length() - 1, 1) != (string)"/" ) {
207 line += urlEncode(name);
213 string value = node->getStringValue ( name.c_str(), "" );
216 line += "</B> <A HREF=\"";
218 if ( request.substr(request.length() - 1, 1) != (string)"/" ) {
221 line += urlEncode(name);
227 response += getTerminator();
230 // request for an individual data member
231 string value = node->getStringValue();
233 response += "<form method=\"GET\" action=\"";
234 response += urlEncode(request);
238 response += "</B> = ";
239 response += "<input type=text name=value size=\"15\" value=\"";
241 response += "\" maxlength=2047>";
242 response += "<input type=submit value=\"update\" name=\"submit\">";
243 response += "</FORM>";
245 response += "</BODY>";
246 response += getTerminator();
248 response += "</HTML>";
249 response += getTerminator();
251 push( "HTTP/1.1 200 OK" );
252 push( getTerminator() );
254 printf("size = %d\n", response.length());
256 sprintf(ctmp, "Content-Length: %d", response.length());
258 push( getTerminator() );
260 push( "Connection: close" );
261 push( getTerminator() );
263 push( "Content-Type: text/html" );
264 push( getTerminator() );
265 push( getTerminator() );
267 push( response.c_str() );
274 // encode everything but "a-zA-Z0-9_.-/" (see RFC2396)
275 string HttpdChannel::urlEncode(string s) {
278 for ( int i = 0; i < (int)s.length(); i++ ) {
279 if ( isalnum(s[i]) || s[i] == '_' || s[i] == '.'
280 || s[i] == '-' || s[i] == '/' ) {
284 sprintf(buf, "%%%02X", (unsigned char)s[i]);
292 string HttpdChannel::urlDecode(string s) {
294 int max = s.length();
297 for ( int i = 0; i < max; i++ ) {
300 } else if ( s[i] == '%' && i + 2 < max
301 && isxdigit(s[i + 1])
302 && isxdigit(s[i + 2]) ) {
304 a = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10;
306 b = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10;
307 r += (char)(a * 16 + b);