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 - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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() {
77 SG_LOG( SG_IO, SG_INFO, "closing FGHttpd" );
79 // the following delete causes a seg fault, gdb is not helpful.
88 // Handle http GET requests
89 void HttpdChannel::foundTerminator (void) {
93 const string s = buffer.getData();
95 if ( s.find( "GET " ) == 0 ) {
96 printf("echo: %s\n", s.c_str());
98 string rest = s.substr(4);
102 string::size_type pos = rest.find( " " );
103 if ( pos != string::npos ) {
104 request = rest.substr( 0, pos );
109 SGPropertyNode *node = NULL;
110 pos = request.find( "?" );
111 if ( pos != string::npos ) {
112 // request to update property value
113 string args = request.substr( pos + 1 );
114 request = request.substr( 0, pos );
115 printf("'%s' '%s'\n", request.c_str(), args.c_str());
116 request = urlDecode(request);
118 // parse args looking for "value="
122 pos = args.find("&");
123 if ( pos != string::npos ) {
124 arg = args.substr( 0, pos );
125 args = args.substr( pos + 1 );
131 printf(" arg = %s\n", arg.c_str() );
132 string::size_type apos = arg.find("=");
133 if ( apos != string::npos ) {
134 string a = arg.substr( 0, apos );
135 string b = arg.substr( apos + 1 );
136 printf(" a = %s b = %s\n", a.c_str(), b.c_str() );
137 if ( request == "/run.cgi" ) {
139 if ( a == "value" ) {
141 if ( !globals->get_commands()
142 ->execute(urlDecode(b).c_str(), &args) )
144 SG_LOG( SG_GENERAL, SG_ALERT,
145 "Command " << urlDecode(b)
151 if ( a == "value" ) {
152 // update a property value
153 fgSetString( request.c_str(),
154 urlDecode(b).c_str() );
160 request = urlDecode(request);
163 node = globals->get_props()->getNode(request.c_str());
165 string response = "";
166 response += "<HTML LANG=\"en\">";
167 response += getTerminator();
169 response += "<HEAD>";
170 response += getTerminator();
172 response += "<TITLE>FlightGear - ";
174 response += "</TITLE>";
175 response += getTerminator();
177 response += "</HEAD>";
178 response += getTerminator();
180 response += "<BODY>";
181 response += getTerminator();
184 response += "<H3>Non-existent node requested!</H3>";
185 response += getTerminator();
188 response += request.c_str();
189 response += "</B> does not exist.";
190 response += getTerminator();
191 } else if ( node->nChildren() > 0 ) {
192 // request is a path with children
193 response += "<H3>Contents of \"";
195 response += "\"</H3>";
196 response += getTerminator();
198 for (int i = 0; i < node->nChildren(); i++) {
199 SGPropertyNode *child = node->getChild(i);
200 string name = child->getDisplayName(true);
202 if ( child->nChildren() > 0 ) {
203 line += "<B><A HREF=\"";
205 if ( request.substr(request.length() - 1, 1) != "/" ) {
208 line += urlEncode(name);
214 string value = node->getStringValue ( name.c_str(), "" );
217 line += "</B> <A HREF=\"";
219 if ( request.substr(request.length() - 1, 1) != "/" ) {
222 line += urlEncode(name);
228 response += getTerminator();
231 // request for an individual data member
232 string value = node->getStringValue();
234 response += "<form method=\"GET\" action=\"";
235 response += urlEncode(request);
239 response += "</B> = ";
240 response += "<input type=text name=value size=\"15\" value=\"";
242 response += "\" maxlength=2047>";
243 response += "<input type=submit value=\"update\" name=\"submit\">";
244 response += "</FORM>";
246 response += "</BODY>";
247 response += getTerminator();
249 response += "</HTML>";
250 response += getTerminator();
252 push( "HTTP/1.1 200 OK" );
253 push( getTerminator() );
255 printf("size = %d\n", response.length());
257 sprintf(ctmp, "Content-Length: %d", response.length());
259 push( getTerminator() );
261 push( "Connection: close" );
262 push( getTerminator() );
264 push( "Content-Type: text/html" );
265 push( getTerminator() );
266 push( getTerminator() );
268 push( response.c_str() );
275 // encode everything but "a-zA-Z0-9_.-/" (see RFC2396)
276 string HttpdChannel::urlEncode(string s) {
279 for ( int i = 0; i < (int)s.length(); i++ ) {
280 if ( isalnum(s[i]) || s[i] == '_' || s[i] == '.'
281 || s[i] == '-' || s[i] == '/' ) {
285 sprintf(buf, "%%%02X", (unsigned char)s[i]);
293 string HttpdChannel::urlDecode(string s) {
295 int max = s.length();
298 for ( int i = 0; i < max; i++ ) {
301 } else if ( s[i] == '%' && i + 2 < max
302 && isxdigit(s[i + 1])
303 && isxdigit(s[i + 2]) ) {
305 a = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10;
307 b = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10;
308 r += (char)(a * 16 + b);