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 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 #include <simgear/compiler.h>
31 #include <algorithm> // sort()
32 #include <cstdlib> // atoi() atof()
37 #include <simgear/debug/logstream.hxx>
38 #include <simgear/io/sg_netChat.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>
51 /* simple httpd server that makes an hasty stab at following the http
54 //////////////////////////////////////////////////////////////
56 //////////////////////////////////////////////////////////////
58 class HttpdChannel : public simgear::NetChat
60 simgear::NetBuffer buffer;
62 string urlEncode(string);
63 string urlDecode(string);
67 HttpdChannel() : buffer(512) { setTerminator("\r\n"); }
69 virtual void collectIncomingData (const char* s, int n) {
73 // Handle the actual http request
74 virtual void foundTerminator(void);
78 //////////////////////////////////////////////////////////////
80 //////////////////////////////////////////////////////////////
82 class HttpdServer : private simgear::NetChannel
84 virtual bool writable (void) { return false; }
86 virtual void handleAccept (void) {
87 simgear::IPAddress addr;
88 int handle = accept ( &addr );
89 SG_LOG( SG_IO, SG_INFO, "Client " << addr.getHost() << ":" << addr.getPort() << " connected" );
91 HttpdChannel *hc = new HttpdChannel;
92 hc->setHandle ( handle );
97 HttpdServer ( int port );
100 HttpdServer::HttpdServer(int port)
104 SG_LOG( SG_IO, SG_ALERT, "Failed to open HTTP port.");
108 if (0 != bind( "", port ))
110 SG_LOG( SG_IO, SG_ALERT, "Failed to bind HTTP port.");
114 if (0 != listen( 5 ))
116 SG_LOG( SG_IO, SG_ALERT, "Failed to listen on HTTP port.");
120 SG_LOG(SG_IO, SG_ALERT, "Httpd server started on port " << port);
123 //////////////////////////////////////////////////////////////
125 //////////////////////////////////////////////////////////////
127 FGHttpd::FGHttpd(int p) :
138 bool FGHttpd::open() {
139 if ( is_enabled() ) {
140 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
141 << "is already in use, ignoring" );
145 server = new HttpdServer( port );
147 set_hz( 15 ); // default to processing requests @ 15Hz
154 bool FGHttpd::process() {
155 simgear::NetChannel::poll();
161 bool FGHttpd::close() {
165 SG_LOG( SG_IO, SG_INFO, "closing FGHttpd" );
169 set_enabled( false );
177 bool operator() (const SGPropertyNode *a, const SGPropertyNode *b) const {
178 int r = strcmp(a->getName(), b->getName());
179 return r ? r < 0 : a->getIndex() < b->getIndex();
184 // Handle http GET requests
185 void HttpdChannel::foundTerminator (void) {
189 const string s = buffer.getData();
191 if ( s.find( "GET " ) == 0 ) {
192 SG_LOG( SG_IO, SG_INFO, "echo: " << s );
194 string rest = s.substr(4);
198 string::size_type pos = rest.find( " " );
199 if ( pos != string::npos ) {
200 request = rest.substr( 0, pos );
205 SGPropertyNode *node = NULL;
206 pos = request.find( "?" );
207 if ( pos != string::npos ) {
208 // request to update property value
209 string args = request.substr( pos + 1 );
210 request = request.substr( 0, pos );
211 SG_LOG( SG_IO, SG_INFO, "'" << request << "' '" << args << "'" );
212 request = urlDecode(request);
214 // parse args looking for "value="
218 pos = args.find("&");
219 if ( pos != string::npos ) {
220 arg = args.substr( 0, pos );
221 args = args.substr( pos + 1 );
227 SG_LOG( SG_IO, SG_INFO, " arg = " << arg );
228 string::size_type apos = arg.find("=");
229 if ( apos != string::npos ) {
230 string a = arg.substr( 0, apos );
231 string b = arg.substr( apos + 1 );
232 SG_LOG( SG_IO, SG_INFO, " a = " << a << " b = " << b );
233 if ( request == "/run.cgi" ) {
235 if ( a == "value" ) {
237 if ( !globals->get_commands()
238 ->execute(urlDecode(b).c_str(), &args) )
240 SG_LOG( SG_NETWORK, SG_ALERT,
241 "Command " << urlDecode(b)
247 if ( a == "value" ) {
248 // update a property value
249 fgSetString( request.c_str(),
250 urlDecode(b).c_str() );
256 request = urlDecode(request);
259 node = globals->get_props()->getNode(request.c_str());
261 string response = "";
262 response += "<HTML LANG=\"en\">";
263 response += getTerminator();
265 response += "<HEAD>";
266 response += getTerminator();
268 response += "<TITLE>FlightGear - ";
270 response += "</TITLE>";
271 response += getTerminator();
273 response += "</HEAD>";
274 response += getTerminator();
276 response += "<BODY>";
277 response += getTerminator();
280 response += "<H3>Non-existent node requested!</H3>";
281 response += getTerminator();
284 response += request.c_str();
285 response += "</B> does not exist.";
286 response += getTerminator();
287 } else if ( node->nChildren() > 0 ) {
288 // request is a path with children
289 response += "<H3>Contents of \"";
291 response += "\"</H3>";
292 response += getTerminator();
295 vector<SGPropertyNode *> children;
296 for (int i = 0; i < node->nChildren(); i++)
297 children.push_back(node->getChild(i));
298 std::sort(children.begin(), children.end(), CompareNodes());
300 vector<SGPropertyNode *>::iterator it, end = children.end();
301 for (it = children.begin(); it != end; ++it) {
302 SGPropertyNode *child = *it;
303 string name = child->getDisplayName(true);
305 if ( child->nChildren() > 0 ) {
306 line += "<B><A HREF=\"";
308 if ( request.substr(request.length() - 1, 1) != "/" ) {
311 line += urlEncode(name);
317 string value = node->getStringValue ( name.c_str(), "" );
320 line += "</B> <A HREF=\"";
322 if ( request.substr(request.length() - 1, 1) != "/" ) {
325 line += urlEncode(name);
331 response += getTerminator();
334 // request for an individual data member
335 string value = node->getStringValue();
337 response += "<form method=\"GET\" action=\"";
338 response += urlEncode(request);
342 response += "</B> = ";
343 response += "<input type=text name=value size=\"15\" value=\"";
345 response += "\" maxlength=2047>";
346 response += "<input type=submit value=\"update\" name=\"submit\">";
347 response += "</FORM>";
349 response += "</BODY>";
350 response += getTerminator();
352 response += "</HTML>";
353 response += getTerminator();
355 push( "HTTP/1.1 200 OK" );
356 push( getTerminator() );
358 SG_LOG( SG_IO, SG_INFO, "size = " << response.length() );
360 sprintf(ctmp, "Content-Length: %u", (unsigned)response.length());
362 push( getTerminator() );
364 push( "Connection: close" );
365 push( getTerminator() );
367 push( "Content-Type: text/html" );
368 push( getTerminator() );
369 push( getTerminator() );
371 push( response.c_str() );
378 // encode everything but "a-zA-Z0-9_.-/" (see RFC2396)
379 string HttpdChannel::urlEncode(string s) {
382 for ( int i = 0; i < (int)s.length(); i++ ) {
383 if ( isalnum(s[i]) || s[i] == '_' || s[i] == '.'
384 || s[i] == '-' || s[i] == '/' ) {
388 sprintf(buf, "%%%02X", (unsigned char)s[i]);
396 string HttpdChannel::urlDecode(string s) {
398 int max = s.length();
401 for ( int i = 0; i < max; i++ ) {
404 } else if ( s[i] == '%' && i + 2 < max
405 && isxdigit(s[i + 1])
406 && isxdigit(s[i + 2]) ) {
408 a = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10;
410 b = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10;
411 r += (char)(a * 16 + b);