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 );
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());
115 // parse args looking for "value="
119 pos = args.find("&");
120 if ( pos != string::npos ) {
121 arg = args.substr( 0, pos );
122 args = args.substr( pos + 1 );
128 printf(" arg = %s\n", arg.c_str() );
129 unsigned int apos = arg.find("=");
130 if ( apos != string::npos ) {
131 string a = arg.substr( 0, apos );
132 string b = arg.substr( apos + 1 );
133 printf(" a = %s b = %s\n", a.c_str(), b.c_str() );
134 if ( a == "value" ) {
135 fgSetString( request, b );
141 node = globals->get_props()->getNode(request);
143 string response = "";
144 response += "<HTML LANG=\"en\">";
145 response += getTerminator();
147 response += "<HEAD>";
148 response += getTerminator();
150 response += "<TITLE>HUD - ";
152 response += "</TITLE>";
153 response += getTerminator();
155 response += "</HEAD>";
156 response += getTerminator();
158 response += "<BODY>";
159 response += getTerminator();
162 response += "<H3>Non-existent node requested!</H3>";
163 response += getTerminator();
166 response += request.c_str();
167 response += "</B> does not exist.";
168 response += getTerminator();
169 } else if ( node->nChildren() > 0 ) {
170 // request is a path with children
171 response += "<H3>Contents of \"";
173 response += "\"</H3>";
174 response += getTerminator();
176 for (int i = 0; i < node->nChildren(); i++) {
177 SGPropertyNode *child = node->getChild(i);
178 string name = child->getName();
180 if ( child->nChildren() > 0 ) {
181 line += "<B><A HREF=\"";
183 if ( request.substr(request.length() - 1, 1) != (string)"/" ) {
192 string value = node->getStringValue ( name, "" );
195 line += "</B> <A HREF=\"";
197 if ( request.substr(request.length() - 1, 1) != (string)"/" ) {
206 response += getTerminator();
209 // request for an individual data member
210 string value = node->getStringValue();
212 response += "<form method=\"GET\" action=\"";
217 response += "</B> = ";
218 response += "<input type=text name=value size=\"5\" value=\"";
220 response += "\" maxlength=2047>";
221 response += "<input type=submit value=\"update\" name=\"submit\">";
222 response += "<FORM>";
225 response += "</BODY>";
226 response += getTerminator();
228 response += "</HTML>";
229 response += getTerminator();
231 push( "HTTP/1.1 200 OK" );
232 push( getTerminator() );
234 printf("size = %d\n", response.length());
236 sprintf(ctmp, "Content-Length: %d", response.length());
238 push( getTerminator() );
240 push( "Connection: close" );
241 push( getTerminator() );
243 push( "Content-Type: text/html" );
244 push( getTerminator() );
245 push( getTerminator() );
247 push( response.c_str() );