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/props.hxx>
44 #include <Main/fg_props.hxx>
45 #include <Main/globals.hxx>
50 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
52 SG_USING_STD(istrstream);
56 bool FGHttpd::open() {
58 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
59 << "is already in use, ignoring" );
63 server = new HttpdServer( port );
65 set_hz( 5 ); // default to processing requests @ 5Hz
72 bool FGHttpd::process() {
79 bool FGHttpd::close() {
86 // Handle http GET requests
87 void HttpdChannel::foundTerminator (void) {
91 const string s = buffer.getData();
93 if ( s.find( "GET " ) == 0 ) {
94 printf("echo: %s\n", s.c_str());
96 string rest = s.substr(4);
100 string::size_type pos = rest.find( " " );
101 if ( pos != string::npos ) {
102 request = rest.substr( 0, pos );
107 SGPropertyNode *node = NULL;
108 pos = request.find( "?" );
109 if ( pos != string::npos ) {
110 // request to update property value
111 string args = request.substr( pos + 1 );
112 request = request.substr( 0, pos );
113 printf("'%s' '%s'\n", request.c_str(), args.c_str());
114 request = urlDecode(request);
116 // parse args looking for "value="
120 pos = args.find("&");
121 if ( pos != string::npos ) {
122 arg = args.substr( 0, pos );
123 args = args.substr( pos + 1 );
129 printf(" arg = %s\n", arg.c_str() );
130 string::size_type apos = arg.find("=");
131 if ( apos != string::npos ) {
132 string a = arg.substr( 0, apos );
133 string b = arg.substr( apos + 1 );
134 printf(" a = %s b = %s\n", a.c_str(), b.c_str() );
135 if ( a == "value" ) {
136 fgSetString( request.c_str(), urlDecode(b).c_str() );
141 request = urlDecode(request);
144 node = globals->get_props()->getNode(request.c_str());
146 string response = "";
147 response += "<HTML LANG=\"en\">";
148 response += getTerminator();
150 response += "<HEAD>";
151 response += getTerminator();
153 response += "<TITLE>FlightGear - ";
155 response += "</TITLE>";
156 response += getTerminator();
158 response += "</HEAD>";
159 response += getTerminator();
161 response += "<BODY>";
162 response += getTerminator();
165 response += "<H3>Non-existent node requested!</H3>";
166 response += getTerminator();
169 response += request.c_str();
170 response += "</B> does not exist.";
171 response += getTerminator();
172 } else if ( node->nChildren() > 0 ) {
173 // request is a path with children
174 response += "<H3>Contents of \"";
176 response += "\"</H3>";
177 response += getTerminator();
179 for (int i = 0; i < node->nChildren(); i++) {
180 SGPropertyNode *child = node->getChild(i);
181 string name = child->getName();
182 if ( node->getChild(name.c_str(), 1) ) {
184 sprintf(buf, "[%d]", child->getIndex());
188 if ( child->nChildren() > 0 ) {
189 line += "<B><A HREF=\"";
191 if ( request.substr(request.length() - 1, 1) != (string)"/" ) {
194 line += urlEncode(name);
200 string value = node->getStringValue ( name.c_str(), "" );
203 line += "</B> <A HREF=\"";
205 if ( request.substr(request.length() - 1, 1) != (string)"/" ) {
208 line += urlEncode(name);
214 response += getTerminator();
217 // request for an individual data member
218 string value = node->getStringValue();
220 response += "<form method=\"GET\" action=\"";
221 response += urlEncode(request);
225 response += "</B> = ";
226 response += "<input type=text name=value size=\"15\" value=\"";
228 response += "\" maxlength=2047>";
229 response += "<input type=submit value=\"update\" name=\"submit\">";
230 response += "</FORM>";
232 response += "</BODY>";
233 response += getTerminator();
235 response += "</HTML>";
236 response += getTerminator();
238 push( "HTTP/1.1 200 OK" );
239 push( getTerminator() );
241 printf("size = %d\n", response.length());
243 sprintf(ctmp, "Content-Length: %d", response.length());
245 push( getTerminator() );
247 push( "Connection: close" );
248 push( getTerminator() );
250 push( "Content-Type: text/html" );
251 push( getTerminator() );
252 push( getTerminator() );
254 push( response.c_str() );
261 // encode everything but "a-zA-Z0-9_.-/" (see RFC2396)
262 string HttpdChannel::urlEncode(string s) {
265 for ( int i = 0; i < (int)s.length(); i++ ) {
266 if ( isalnum(s[i]) || s[i] == '_' || s[i] == '.'
267 || s[i] == '-' || s[i] == '/' ) {
271 sprintf(buf, "%%%02X", (unsigned char)s[i]);
279 string HttpdChannel::urlDecode(string s) {
281 int max = s.length();
284 for ( int i = 0; i < max; i++ ) {
287 } else if ( s[i] == '%' && i + 2 < max
288 && isxdigit(s[i + 1])
289 && isxdigit(s[i + 2]) ) {
291 a = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10;
293 b = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10;
294 r += (char)(a * 16 + b);