1 // httpd.hxx -- 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 unsigned int pos = rest.find( " " );
101 if ( pos != string::npos ) {
102 request = rest.substr( 0, pos );
103 request = urlDecode(request);
108 SGPropertyNode *node = NULL;
109 pos = request.find( "?" );
110 if ( pos != string::npos ) {
111 // request to update property value
112 string args = request.substr( pos + 1 );
113 request = request.substr( 0, pos );
114 printf("'%s' '%s'\n", request.c_str(), args.c_str());
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 unsigned int 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, b );
142 node = globals->get_props()->getNode(request);
144 string response = "";
145 response += "<HTML LANG=\"en\">";
146 response += getTerminator();
148 response += "<HEAD>";
149 response += getTerminator();
151 response += "<TITLE>FlightGear - ";
153 response += "</TITLE>";
154 response += getTerminator();
156 response += "</HEAD>";
157 response += getTerminator();
159 response += "<BODY>";
160 response += getTerminator();
163 response += "<H3>Non-existent node requested!</H3>";
164 response += getTerminator();
167 response += request.c_str();
168 response += "</B> does not exist.";
169 response += getTerminator();
170 } else if ( node->nChildren() > 0 ) {
171 // request is a path with children
172 response += "<H3>Contents of \"";
174 response += "\"</H3>";
175 response += getTerminator();
177 for (int i = 0; i < node->nChildren(); i++) {
178 SGPropertyNode *child = node->getChild(i);
179 string name = child->getName();
180 if ( node->getChild(name, 1) ) {
182 sprintf(buf, "[%d]", child->getIndex());
186 if ( child->nChildren() > 0 ) {
187 line += "<B><A HREF=\"";
189 if ( request.substr(request.length() - 1, 1) != (string)"/" ) {
192 line += urlEncode(name);
198 string value = node->getStringValue ( name, "" );
201 line += "</B> <A HREF=\"";
203 if ( request.substr(request.length() - 1, 1) != (string)"/" ) {
206 line += urlEncode(name);
212 response += getTerminator();
215 // request for an individual data member
216 string value = node->getStringValue();
218 response += "<form method=\"GET\" action=\"";
219 response += urlEncode(request);
223 response += "</B> = ";
224 response += "<input type=text name=value size=\"15\" value=\"";
226 response += "\" maxlength=2047>";
227 response += "<input type=submit value=\"update\" name=\"submit\">";
228 response += "<FORM>";
231 response += "</BODY>";
232 response += getTerminator();
234 response += "</HTML>";
235 response += getTerminator();
237 push( "HTTP/1.1 200 OK" );
238 push( getTerminator() );
240 printf("size = %d\n", response.length());
242 sprintf(ctmp, "Content-Length: %d", response.length());
244 push( getTerminator() );
246 push( "Connection: close" );
247 push( getTerminator() );
249 push( "Content-Type: text/html" );
250 push( getTerminator() );
251 push( getTerminator() );
253 push( response.c_str() );
260 // encode everything but "a-zA-Z0-9_.-/"
261 string HttpdChannel::urlEncode(string s) {
264 for ( int i = 0; i < (int)s.length(); i++ ) {
265 if ( isalnum(s[i]) || s[i] == '_' || s[i] == '.'
266 || s[i] == '-' || s[i] == '/' ) {
270 sprintf(buf, "%%%02X", (unsigned char)s[i]);
278 string HttpdChannel::urlDecode(string s) {
280 int max = s.length();
283 for ( int i = 0; i < max; i++ ) {
286 } else if ( s[i] == '%' && i + 2 < max
287 && isxdigit(s[i + 1])
288 && isxdigit(s[i + 2]) ) {
290 a = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10;
292 b = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10;
293 r += (char)(a * 16 + b);