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()
36 #include <simgear/debug/logstream.hxx>
37 #include <simgear/io/sg_netChat.hxx>
38 #include <simgear/io/iochannel.hxx>
39 #include <simgear/math/sg_types.hxx>
40 #include <simgear/structure/commands.hxx>
41 #include <simgear/props/props.hxx>
43 #include <Main/fg_props.hxx>
44 #include <Main/globals.hxx>
50 /* simple httpd server that makes an hasty stab at following the http
53 //////////////////////////////////////////////////////////////
55 //////////////////////////////////////////////////////////////
57 class HttpdChannel : public simgear::NetChat
59 simgear::NetBuffer buffer;
61 string urlEncode(string);
62 string urlDecode(string);
66 HttpdChannel() : buffer(512) { setTerminator("\r\n"); }
68 virtual void collectIncomingData (const char* s, int n) {
72 // Handle the actual http request
73 virtual void foundTerminator(void);
77 //////////////////////////////////////////////////////////////
79 //////////////////////////////////////////////////////////////
81 class HttpdServer : private simgear::NetChannel
83 virtual bool writable (void) { return false; }
85 virtual void handleAccept (void) {
86 simgear::IPAddress addr;
87 int handle = accept ( &addr );
88 SG_LOG( SG_IO, SG_INFO, "Client " << addr.getHost() << ":" << addr.getPort() << " connected" );
90 HttpdChannel *hc = new HttpdChannel;
91 hc->setHandle ( handle );
96 HttpdServer ( int port );
99 HttpdServer::HttpdServer(int port)
103 SG_LOG( SG_IO, SG_ALERT, "Failed to open HTTP port.");
107 if (0 != bind( "", port ))
109 SG_LOG( SG_IO, SG_ALERT, "Failed to bind HTTP port.");
113 if (0 != listen( 5 ))
115 SG_LOG( SG_IO, SG_ALERT, "Failed to listen on HTTP port.");
119 SG_LOG(SG_IO, SG_ALERT, "Httpd server started on port " << port);
122 //////////////////////////////////////////////////////////////
124 //////////////////////////////////////////////////////////////
126 FGHttpd::FGHttpd(int p) :
136 bool FGHttpd::open() {
137 if ( is_enabled() ) {
138 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
139 << "is already in use, ignoring" );
143 server = new HttpdServer( port );
145 set_hz( 15 ); // default to processing requests @ 15Hz
152 bool FGHttpd::process() {
153 simgear::NetChannel::poll();
159 bool FGHttpd::close() {
163 SG_LOG( SG_IO, SG_INFO, "closing FGHttpd" );
167 set_enabled( false );
175 bool operator() (const SGPropertyNode *a, const SGPropertyNode *b) const {
176 int r = strcmp(a->getName(), b->getName());
177 return r ? r < 0 : a->getIndex() < b->getIndex();
182 // Handle http GET requests
183 void HttpdChannel::foundTerminator (void) {
187 const string s = buffer.getData();
189 if ( s.find( "GET " ) == 0 ) {
190 SG_LOG( SG_IO, SG_INFO, "echo: " << s );
192 string rest = s.substr(4);
196 string::size_type pos = rest.find( " " );
197 if ( pos != string::npos ) {
198 request = rest.substr( 0, pos );
203 SGPropertyNode *node = NULL;
204 pos = request.find( "?" );
205 if ( pos != string::npos ) {
206 // request to update property value
207 string args = request.substr( pos + 1 );
208 request = request.substr( 0, pos );
209 SG_LOG( SG_IO, SG_INFO, "'" << request << "' '" << args << "'" );
210 request = urlDecode(request);
212 // parse args looking for "value="
216 pos = args.find("&");
217 if ( pos != string::npos ) {
218 arg = args.substr( 0, pos );
219 args = args.substr( pos + 1 );
225 SG_LOG( SG_IO, SG_INFO, " arg = " << arg );
226 string::size_type apos = arg.find("=");
227 if ( apos != string::npos ) {
228 string a = arg.substr( 0, apos );
229 string b = arg.substr( apos + 1 );
230 SG_LOG( SG_IO, SG_INFO, " a = " << a << " b = " << b );
231 if ( request == "/run.cgi" ) {
233 if ( a == "value" ) {
235 if ( !globals->get_commands()
236 ->execute(urlDecode(b).c_str(), &args) )
238 SG_LOG( SG_NETWORK, SG_ALERT,
239 "Command " << urlDecode(b)
245 if ( a == "value" ) {
246 // update a property value
247 fgSetString( request.c_str(),
248 urlDecode(b).c_str() );
254 request = urlDecode(request);
257 node = globals->get_props()->getNode(request.c_str());
259 string response = "";
260 response += "<HTML LANG=\"en\">";
261 response += getTerminator();
263 response += "<HEAD>";
264 response += getTerminator();
266 response += "<TITLE>FlightGear - ";
268 response += "</TITLE>";
269 response += getTerminator();
271 response += "</HEAD>";
272 response += getTerminator();
274 response += "<BODY>";
275 response += getTerminator();
278 response += "<H3>Non-existent node requested!</H3>";
279 response += getTerminator();
282 response += request.c_str();
283 response += "</B> does not exist.";
284 response += getTerminator();
285 } else if ( node->nChildren() > 0 ) {
286 // request is a path with children
287 response += "<H3>Contents of \"";
289 response += "\"</H3>";
290 response += getTerminator();
293 vector<SGPropertyNode *> children;
294 for (int i = 0; i < node->nChildren(); i++)
295 children.push_back(node->getChild(i));
296 std::sort(children.begin(), children.end(), CompareNodes());
298 vector<SGPropertyNode *>::iterator it, end = children.end();
299 for (it = children.begin(); it != end; ++it) {
300 SGPropertyNode *child = *it;
301 string name = child->getDisplayName(true);
303 if ( child->nChildren() > 0 ) {
304 line += "<B><A HREF=\"";
306 if ( request.substr(request.length() - 1, 1) != "/" ) {
309 line += urlEncode(name);
315 string value = node->getStringValue ( name.c_str(), "" );
318 line += "</B> <A HREF=\"";
320 if ( request.substr(request.length() - 1, 1) != "/" ) {
323 line += urlEncode(name);
329 response += getTerminator();
332 // request for an individual data member
333 string value = node->getStringValue();
335 response += "<form method=\"GET\" action=\"";
336 response += urlEncode(request);
340 response += "</B> = ";
341 response += "<input type=text name=value size=\"15\" value=\"";
343 response += "\" maxlength=2047>";
344 response += "<input type=submit value=\"update\" name=\"submit\">";
345 response += "</FORM>";
347 response += "</BODY>";
348 response += getTerminator();
350 response += "</HTML>";
351 response += getTerminator();
353 push( "HTTP/1.1 200 OK" );
354 push( getTerminator() );
356 SG_LOG( SG_IO, SG_INFO, "size = " << response.length() );
358 sprintf(ctmp, "Content-Length: %u", (unsigned)response.length());
360 push( getTerminator() );
362 push( "Connection: close" );
363 push( getTerminator() );
365 push( "Content-Type: text/html" );
366 push( getTerminator() );
367 push( getTerminator() );
369 push( response.c_str() );
376 // encode everything but "a-zA-Z0-9_.-/" (see RFC2396)
377 string HttpdChannel::urlEncode(string s) {
380 for ( int i = 0; i < (int)s.length(); i++ ) {
381 if ( isalnum(s[i]) || s[i] == '_' || s[i] == '.'
382 || s[i] == '-' || s[i] == '/' ) {
386 sprintf(buf, "%%%02X", (unsigned char)s[i]);
394 string HttpdChannel::urlDecode(string s) {
396 int max = s.length();
399 for ( int i = 0; i < max; i++ ) {
402 } else if ( s[i] == '%' && i + 2 < max
403 && isxdigit(s[i + 1])
404 && isxdigit(s[i + 2]) ) {
406 a = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10;
408 b = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10;
409 r += (char)(a * 16 + b);