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>
52 SG_USING_STD(istrstream);
55 bool FGHttpd::open() {
57 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
58 << "is already in use, ignoring" );
62 server = new HttpdServer( port );
64 set_hz( 15 ); // default to processing requests @ 15Hz
71 bool FGHttpd::process() {
78 bool FGHttpd::close() {
85 // Handle http GET requests
86 void HttpdChannel::foundTerminator (void) {
90 const string s = buffer.getData();
92 if ( s.find( "GET " ) == 0 ) {
93 printf("echo: %s\n", s.c_str());
95 string rest = s.substr(4);
99 string::size_type pos = rest.find( " " );
100 if ( pos != string::npos ) {
101 request = rest.substr( 0, pos );
106 SGPropertyNode *node = NULL;
107 pos = request.find( "?" );
108 if ( pos != string::npos ) {
109 // request to update property value
110 string args = request.substr( pos + 1 );
111 request = request.substr( 0, pos );
112 printf("'%s' '%s'\n", request.c_str(), args.c_str());
113 request = urlDecode(request);
115 // parse args looking for "value="
119 pos = args.find("&");
120 if ( pos != string::npos ) {
121 arg = args.substr( 0, pos );
122 args = args.substr( pos + 1 );
128 printf(" arg = %s\n", arg.c_str() );
129 string::size_type apos = arg.find("=");
130 if ( apos != string::npos ) {
131 string a = arg.substr( 0, apos );
132 string b = arg.substr( apos + 1 );
133 printf(" a = %s b = %s\n", a.c_str(), b.c_str() );
134 if ( request == "/run.cgi" ) {
136 if ( a == "value" ) {
138 if ( !globals->get_commands()
139 ->execute(urlDecode(b).c_str(), &args) )
141 SG_LOG( SG_GENERAL, SG_ALERT,
142 "Command " << urlDecode(b)
148 if ( a == "value" ) {
149 // update a property value
150 fgSetString( request.c_str(),
151 urlDecode(b).c_str() );
157 request = urlDecode(request);
160 node = globals->get_props()->getNode(request.c_str());
162 string response = "";
163 response += "<HTML LANG=\"en\">";
164 response += getTerminator();
166 response += "<HEAD>";
167 response += getTerminator();
169 response += "<TITLE>FlightGear - ";
171 response += "</TITLE>";
172 response += getTerminator();
174 response += "</HEAD>";
175 response += getTerminator();
177 response += "<BODY>";
178 response += getTerminator();
181 response += "<H3>Non-existent node requested!</H3>";
182 response += getTerminator();
185 response += request.c_str();
186 response += "</B> does not exist.";
187 response += getTerminator();
188 } else if ( node->nChildren() > 0 ) {
189 // request is a path with children
190 response += "<H3>Contents of \"";
192 response += "\"</H3>";
193 response += getTerminator();
195 for (int i = 0; i < node->nChildren(); i++) {
196 SGPropertyNode *child = node->getChild(i);
197 string name = child->getDisplayName(true);
199 if ( child->nChildren() > 0 ) {
200 line += "<B><A HREF=\"";
202 if ( request.substr(request.length() - 1, 1) != (string)"/" ) {
205 line += urlEncode(name);
211 string value = node->getStringValue ( name.c_str(), "" );
214 line += "</B> <A HREF=\"";
216 if ( request.substr(request.length() - 1, 1) != (string)"/" ) {
219 line += urlEncode(name);
225 response += getTerminator();
228 // request for an individual data member
229 string value = node->getStringValue();
231 response += "<form method=\"GET\" action=\"";
232 response += urlEncode(request);
236 response += "</B> = ";
237 response += "<input type=text name=value size=\"15\" value=\"";
239 response += "\" maxlength=2047>";
240 response += "<input type=submit value=\"update\" name=\"submit\">";
241 response += "</FORM>";
243 response += "</BODY>";
244 response += getTerminator();
246 response += "</HTML>";
247 response += getTerminator();
249 push( "HTTP/1.1 200 OK" );
250 push( getTerminator() );
252 printf("size = %d\n", response.length());
254 sprintf(ctmp, "Content-Length: %d", response.length());
256 push( getTerminator() );
258 push( "Connection: close" );
259 push( getTerminator() );
261 push( "Content-Type: text/html" );
262 push( getTerminator() );
263 push( getTerminator() );
265 push( response.c_str() );
272 // encode everything but "a-zA-Z0-9_.-/" (see RFC2396)
273 string HttpdChannel::urlEncode(string s) {
276 for ( int i = 0; i < (int)s.length(); i++ ) {
277 if ( isalnum(s[i]) || s[i] == '_' || s[i] == '.'
278 || s[i] == '-' || s[i] == '/' ) {
282 sprintf(buf, "%%%02X", (unsigned char)s[i]);
290 string HttpdChannel::urlDecode(string s) {
292 int max = s.length();
295 for ( int i = 0; i < max; i++ ) {
298 } else if ( s[i] == '%' && i + 2 < max
299 && isxdigit(s[i + 1])
300 && isxdigit(s[i + 2]) ) {
302 a = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10;
304 b = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10;
305 r += (char)(a * 16 + b);