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) :
137 bool FGHttpd::open() {
138 if ( is_enabled() ) {
139 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
140 << "is already in use, ignoring" );
144 server = new HttpdServer( port );
146 set_hz( 15 ); // default to processing requests @ 15Hz
153 bool FGHttpd::process() {
154 simgear::NetChannel::poll();
160 bool FGHttpd::close() {
164 SG_LOG( SG_IO, SG_INFO, "closing FGHttpd" );
168 set_enabled( false );
176 bool operator() (const SGPropertyNode *a, const SGPropertyNode *b) const {
177 int r = strcmp(a->getName(), b->getName());
178 return r ? r < 0 : a->getIndex() < b->getIndex();
183 // Handle http GET requests
184 void HttpdChannel::foundTerminator (void) {
188 const string s = buffer.getData();
190 if ( s.find( "GET " ) == 0 ) {
191 SG_LOG( SG_IO, SG_INFO, "echo: " << s );
193 string rest = s.substr(4);
197 string::size_type pos = rest.find( " " );
198 if ( pos != string::npos ) {
199 request = rest.substr( 0, pos );
204 SGPropertyNode *node = NULL;
205 pos = request.find( "?" );
206 if ( pos != string::npos ) {
207 // request to update property value
208 string args = request.substr( pos + 1 );
209 request = request.substr( 0, pos );
210 SG_LOG( SG_IO, SG_INFO, "'" << request << "' '" << args << "'" );
211 request = urlDecode(request);
213 // parse args looking for "value="
217 pos = args.find("&");
218 if ( pos != string::npos ) {
219 arg = args.substr( 0, pos );
220 args = args.substr( pos + 1 );
226 SG_LOG( SG_IO, SG_INFO, " arg = " << arg );
227 string::size_type apos = arg.find("=");
228 if ( apos != string::npos ) {
229 string a = arg.substr( 0, apos );
230 string b = arg.substr( apos + 1 );
231 SG_LOG( SG_IO, SG_INFO, " a = " << a << " b = " << b );
232 if ( request == "/run.cgi" ) {
234 if ( a == "value" ) {
236 if ( !globals->get_commands()
237 ->execute(urlDecode(b).c_str(), &args) )
239 SG_LOG( SG_NETWORK, SG_ALERT,
240 "Command " << urlDecode(b)
246 if ( a == "value" ) {
247 // update a property value
248 fgSetString( request.c_str(),
249 urlDecode(b).c_str() );
255 request = urlDecode(request);
258 node = globals->get_props()->getNode(request.c_str());
260 string response = "";
261 response += "<HTML LANG=\"en\">";
262 response += getTerminator();
264 response += "<HEAD>";
265 response += getTerminator();
267 response += "<TITLE>FlightGear - ";
269 response += "</TITLE>";
270 response += getTerminator();
272 response += "</HEAD>";
273 response += getTerminator();
275 response += "<BODY>";
276 response += getTerminator();
279 response += "<H3>Non-existent node requested!</H3>";
280 response += getTerminator();
283 response += request.c_str();
284 response += "</B> does not exist.";
285 response += getTerminator();
286 } else if ( node->nChildren() > 0 ) {
287 // request is a path with children
288 response += "<H3>Contents of \"";
290 response += "\"</H3>";
291 response += getTerminator();
294 vector<SGPropertyNode *> children;
295 for (int i = 0; i < node->nChildren(); i++)
296 children.push_back(node->getChild(i));
297 std::sort(children.begin(), children.end(), CompareNodes());
299 vector<SGPropertyNode *>::iterator it, end = children.end();
300 for (it = children.begin(); it != end; ++it) {
301 SGPropertyNode *child = *it;
302 string name = child->getDisplayName(true);
304 if ( child->nChildren() > 0 ) {
305 line += "<B><A HREF=\"";
307 if ( request.substr(request.length() - 1, 1) != "/" ) {
310 line += urlEncode(name);
316 string value = node->getStringValue ( name.c_str(), "" );
319 line += "</B> <A HREF=\"";
321 if ( request.substr(request.length() - 1, 1) != "/" ) {
324 line += urlEncode(name);
330 response += getTerminator();
333 // request for an individual data member
334 string value = node->getStringValue();
336 response += "<form method=\"GET\" action=\"";
337 response += urlEncode(request);
341 response += "</B> = ";
342 response += "<input type=text name=value size=\"15\" value=\"";
344 response += "\" maxlength=2047>";
345 response += "<input type=submit value=\"update\" name=\"submit\">";
346 response += "</FORM>";
348 response += "</BODY>";
349 response += getTerminator();
351 response += "</HTML>";
352 response += getTerminator();
354 push( "HTTP/1.1 200 OK" );
355 push( getTerminator() );
357 SG_LOG( SG_IO, SG_INFO, "size = " << response.length() );
359 sprintf(ctmp, "Content-Length: %u", (unsigned)response.length());
361 push( getTerminator() );
363 push( "Connection: close" );
364 push( getTerminator() );
366 push( "Content-Type: text/html" );
367 push( getTerminator() );
368 push( getTerminator() );
370 push( response.c_str() );
377 // encode everything but "a-zA-Z0-9_.-/" (see RFC2396)
378 string HttpdChannel::urlEncode(string s) {
381 for ( int i = 0; i < (int)s.length(); i++ ) {
382 if ( isalnum(s[i]) || s[i] == '_' || s[i] == '.'
383 || s[i] == '-' || s[i] == '/' ) {
387 sprintf(buf, "%%%02X", (unsigned char)s[i]);
395 string HttpdChannel::urlDecode(string s) {
397 int max = s.length();
400 for ( int i = 0; i < max; i++ ) {
403 } else if ( s[i] == '%' && i + 2 < max
404 && isxdigit(s[i + 1])
405 && isxdigit(s[i + 2]) ) {
407 a = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10;
409 b = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10;
410 r += (char)(a * 16 + b);