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 <algorithm> // sort()
35 #include <cstdlib> // atoi() atof()
39 #include <simgear/debug/logstream.hxx>
40 #include <simgear/io/iochannel.hxx>
41 #include <simgear/math/sg_types.hxx>
42 #include <simgear/structure/commands.hxx>
43 #include <simgear/props/props.hxx>
45 #include <Main/fg_props.hxx>
46 #include <Main/globals.hxx>
52 bool FGHttpd::open() {
54 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
55 << "is already in use, ignoring" );
59 server = new HttpdServer( port );
61 set_hz( 15 ); // default to processing requests @ 15Hz
68 bool FGHttpd::process() {
69 simgear::NetChannel::poll();
75 bool FGHttpd::close() {
76 SG_LOG( SG_IO, SG_INFO, "closing FGHttpd" );
87 bool operator() (const SGPropertyNode *a, const SGPropertyNode *b) const {
88 int r = strcmp(a->getName(), b->getName());
89 return r ? r < 0 : a->getIndex() < b->getIndex();
94 // Handle http GET requests
95 void HttpdChannel::foundTerminator (void) {
99 const string s = buffer.getData();
101 if ( s.find( "GET " ) == 0 ) {
102 SG_LOG( SG_IO, SG_INFO, "echo: " << s );
104 string rest = s.substr(4);
108 string::size_type pos = rest.find( " " );
109 if ( pos != string::npos ) {
110 request = rest.substr( 0, pos );
115 SGPropertyNode *node = NULL;
116 pos = request.find( "?" );
117 if ( pos != string::npos ) {
118 // request to update property value
119 string args = request.substr( pos + 1 );
120 request = request.substr( 0, pos );
121 SG_LOG( SG_IO, SG_INFO, "'" << request << "' '" << args << "'" );
122 request = urlDecode(request);
124 // parse args looking for "value="
128 pos = args.find("&");
129 if ( pos != string::npos ) {
130 arg = args.substr( 0, pos );
131 args = args.substr( pos + 1 );
137 SG_LOG( SG_IO, SG_INFO, " arg = " << arg );
138 string::size_type apos = arg.find("=");
139 if ( apos != string::npos ) {
140 string a = arg.substr( 0, apos );
141 string b = arg.substr( apos + 1 );
142 SG_LOG( SG_IO, SG_INFO, " a = " << a << " b = " << b );
143 if ( request == "/run.cgi" ) {
145 if ( a == "value" ) {
147 if ( !globals->get_commands()
148 ->execute(urlDecode(b).c_str(), &args) )
150 SG_LOG( SG_GENERAL, SG_ALERT,
151 "Command " << urlDecode(b)
157 if ( a == "value" ) {
158 // update a property value
159 fgSetString( request.c_str(),
160 urlDecode(b).c_str() );
166 request = urlDecode(request);
169 node = globals->get_props()->getNode(request.c_str());
171 string response = "";
172 response += "<HTML LANG=\"en\">";
173 response += getTerminator();
175 response += "<HEAD>";
176 response += getTerminator();
178 response += "<TITLE>FlightGear - ";
180 response += "</TITLE>";
181 response += getTerminator();
183 response += "</HEAD>";
184 response += getTerminator();
186 response += "<BODY>";
187 response += getTerminator();
190 response += "<H3>Non-existent node requested!</H3>";
191 response += getTerminator();
194 response += request.c_str();
195 response += "</B> does not exist.";
196 response += getTerminator();
197 } else if ( node->nChildren() > 0 ) {
198 // request is a path with children
199 response += "<H3>Contents of \"";
201 response += "\"</H3>";
202 response += getTerminator();
205 vector<SGPropertyNode *> children;
206 for (int i = 0; i < node->nChildren(); i++)
207 children.push_back(node->getChild(i));
208 std::sort(children.begin(), children.end(), CompareNodes());
210 vector<SGPropertyNode *>::iterator it, end = children.end();
211 for (it = children.begin(); it != end; ++it) {
212 SGPropertyNode *child = *it;
213 string name = child->getDisplayName(true);
215 if ( child->nChildren() > 0 ) {
216 line += "<B><A HREF=\"";
218 if ( request.substr(request.length() - 1, 1) != "/" ) {
221 line += urlEncode(name);
227 string value = node->getStringValue ( name.c_str(), "" );
230 line += "</B> <A HREF=\"";
232 if ( request.substr(request.length() - 1, 1) != "/" ) {
235 line += urlEncode(name);
241 response += getTerminator();
244 // request for an individual data member
245 string value = node->getStringValue();
247 response += "<form method=\"GET\" action=\"";
248 response += urlEncode(request);
252 response += "</B> = ";
253 response += "<input type=text name=value size=\"15\" value=\"";
255 response += "\" maxlength=2047>";
256 response += "<input type=submit value=\"update\" name=\"submit\">";
257 response += "</FORM>";
259 response += "</BODY>";
260 response += getTerminator();
262 response += "</HTML>";
263 response += getTerminator();
265 push( "HTTP/1.1 200 OK" );
266 push( getTerminator() );
268 SG_LOG( SG_IO, SG_INFO, "size = " << response.length() );
270 sprintf(ctmp, "Content-Length: %u", (unsigned)response.length());
272 push( getTerminator() );
274 push( "Connection: close" );
275 push( getTerminator() );
277 push( "Content-Type: text/html" );
278 push( getTerminator() );
279 push( getTerminator() );
281 push( response.c_str() );
288 // encode everything but "a-zA-Z0-9_.-/" (see RFC2396)
289 string HttpdChannel::urlEncode(string s) {
292 for ( int i = 0; i < (int)s.length(); i++ ) {
293 if ( isalnum(s[i]) || s[i] == '_' || s[i] == '.'
294 || s[i] == '-' || s[i] == '/' ) {
298 sprintf(buf, "%%%02X", (unsigned char)s[i]);
306 string HttpdChannel::urlDecode(string s) {
308 int max = s.length();
311 for ( int i = 0; i < max; i++ ) {
314 } else if ( s[i] == '%' && i + 2 < max
315 && isxdigit(s[i + 1])
316 && isxdigit(s[i + 2]) ) {
318 a = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10;
320 b = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10;
321 r += (char)(a * 16 + b);