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->getDisplayName(true);
183 if ( child->nChildren() > 0 ) {
184 line += "<B><A HREF=\"";
186 if ( request.substr(request.length() - 1, 1) != (string)"/" ) {
189 line += urlEncode(name);
195 string value = node->getStringValue ( name.c_str(), "" );
198 line += "</B> <A HREF=\"";
200 if ( request.substr(request.length() - 1, 1) != (string)"/" ) {
203 line += urlEncode(name);
209 response += getTerminator();
212 // request for an individual data member
213 string value = node->getStringValue();
215 response += "<form method=\"GET\" action=\"";
216 response += urlEncode(request);
220 response += "</B> = ";
221 response += "<input type=text name=value size=\"15\" value=\"";
223 response += "\" maxlength=2047>";
224 response += "<input type=submit value=\"update\" name=\"submit\">";
225 response += "</FORM>";
227 response += "</BODY>";
228 response += getTerminator();
230 response += "</HTML>";
231 response += getTerminator();
233 push( "HTTP/1.1 200 OK" );
234 push( getTerminator() );
236 printf("size = %d\n", response.length());
238 sprintf(ctmp, "Content-Length: %d", response.length());
240 push( getTerminator() );
242 push( "Connection: close" );
243 push( getTerminator() );
245 push( "Content-Type: text/html" );
246 push( getTerminator() );
247 push( getTerminator() );
249 push( response.c_str() );
256 // encode everything but "a-zA-Z0-9_.-/" (see RFC2396)
257 string HttpdChannel::urlEncode(string s) {
260 for ( int i = 0; i < (int)s.length(); i++ ) {
261 if ( isalnum(s[i]) || s[i] == '_' || s[i] == '.'
262 || s[i] == '-' || s[i] == '/' ) {
266 sprintf(buf, "%%%02X", (unsigned char)s[i]);
274 string HttpdChannel::urlDecode(string s) {
276 int max = s.length();
279 for ( int i = 0; i < max; i++ ) {
282 } else if ( s[i] == '%' && i + 2 < max
283 && isxdigit(s[i + 1])
284 && isxdigit(s[i + 2]) ) {
286 a = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10;
288 b = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10;
289 r += (char)(a * 16 + b);