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()
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>
44 #include <Main/fg_props.hxx>
45 #include <Main/globals.hxx>
53 bool FGHttpd::open() {
55 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
56 << "is already in use, ignoring" );
60 server = new HttpdServer( port );
62 set_hz( 15 ); // default to processing requests @ 15Hz
69 bool FGHttpd::process() {
76 bool FGHttpd::close() {
83 // Handle http GET requests
84 void HttpdChannel::foundTerminator (void) {
88 const string s = buffer.getData();
90 if ( s.find( "GET " ) == 0 ) {
91 printf("echo: %s\n", s.c_str());
93 string rest = s.substr(4);
97 string::size_type pos = rest.find( " " );
98 if ( pos != string::npos ) {
99 request = rest.substr( 0, pos );
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);
113 // parse args looking for "value="
117 pos = args.find("&");
118 if ( pos != string::npos ) {
119 arg = args.substr( 0, pos );
120 args = args.substr( pos + 1 );
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" ) {
134 if ( a == "value" ) {
136 if ( !globals->get_commands()
137 ->execute(urlDecode(b).c_str(), &args) )
139 SG_LOG( SG_GENERAL, SG_ALERT,
140 "Command " << urlDecode(b)
146 if ( a == "value" ) {
147 // update a property value
148 fgSetString( request.c_str(),
149 urlDecode(b).c_str() );
155 request = urlDecode(request);
158 node = globals->get_props()->getNode(request.c_str());
160 string response = "";
161 response += "<HTML LANG=\"en\">";
162 response += getTerminator();
164 response += "<HEAD>";
165 response += getTerminator();
167 response += "<TITLE>FlightGear - ";
169 response += "</TITLE>";
170 response += getTerminator();
172 response += "</HEAD>";
173 response += getTerminator();
175 response += "<BODY>";
176 response += getTerminator();
179 response += "<H3>Non-existent node requested!</H3>";
180 response += getTerminator();
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 \"";
190 response += "\"</H3>";
191 response += getTerminator();
193 for (int i = 0; i < node->nChildren(); i++) {
194 SGPropertyNode *child = node->getChild(i);
195 string name = child->getDisplayName(true);
197 if ( child->nChildren() > 0 ) {
198 line += "<B><A HREF=\"";
200 if ( request.substr(request.length() - 1, 1) != "/" ) {
203 line += urlEncode(name);
209 string value = node->getStringValue ( name.c_str(), "" );
212 line += "</B> <A HREF=\"";
214 if ( request.substr(request.length() - 1, 1) != "/" ) {
217 line += urlEncode(name);
223 response += getTerminator();
226 // request for an individual data member
227 string value = node->getStringValue();
229 response += "<form method=\"GET\" action=\"";
230 response += urlEncode(request);
234 response += "</B> = ";
235 response += "<input type=text name=value size=\"15\" value=\"";
237 response += "\" maxlength=2047>";
238 response += "<input type=submit value=\"update\" name=\"submit\">";
239 response += "</FORM>";
241 response += "</BODY>";
242 response += getTerminator();
244 response += "</HTML>";
245 response += getTerminator();
247 push( "HTTP/1.1 200 OK" );
248 push( getTerminator() );
250 printf("size = %d\n", response.length());
252 sprintf(ctmp, "Content-Length: %d", response.length());
254 push( getTerminator() );
256 push( "Connection: close" );
257 push( getTerminator() );
259 push( "Content-Type: text/html" );
260 push( getTerminator() );
261 push( getTerminator() );
263 push( response.c_str() );
270 // encode everything but "a-zA-Z0-9_.-/" (see RFC2396)
271 string HttpdChannel::urlEncode(string s) {
274 for ( int i = 0; i < (int)s.length(); i++ ) {
275 if ( isalnum(s[i]) || s[i] == '_' || s[i] == '.'
276 || s[i] == '-' || s[i] == '/' ) {
280 sprintf(buf, "%%%02X", (unsigned char)s[i]);
288 string HttpdChannel::urlDecode(string s) {
290 int max = s.length();
293 for ( int i = 0; i < max; i++ ) {
296 } else if ( s[i] == '%' && i + 2 < max
297 && isxdigit(s[i + 1])
298 && isxdigit(s[i + 2]) ) {
300 a = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10;
302 b = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10;
303 r += (char)(a * 16 + b);