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 // 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., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <simgear/compiler.h>
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/io/iochannel.hxx>
33 #include <simgear/math/sg_types.hxx>
34 #include <simgear/misc/props.hxx>
36 #include <stdlib.h> // atoi() atof()
39 #include STL_STRSTREAM
41 #include <Main/fg_props.hxx>
42 #include <Main/globals.hxx>
47 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
49 SG_USING_STD(istrstream);
53 bool FGHttpd::open() {
55 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
56 << "is already in use, ignoring" );
60 server = new HttpdServer( port );
62 set_hz( 5 ); // default to processing requests @ 5Hz
69 bool FGHttpd::process() {
76 bool FGHttpd::close() {
83 // Handle http GET requests
84 void HttpdChannel::foundTerminator (void) {
85 const string s = buffer.getData();
87 if ( s.find( "GET " ) == 0 ) {
88 printf("echo: %s\n", s.c_str());
90 string rest = s.substr(4);
94 unsigned int pos = rest.find( " " );
95 if ( pos != string::npos ) {
96 request = rest.substr( 0, pos );
101 SGPropertyNode *node = NULL;
102 pos = request.find( "?" );
103 if ( pos != string::npos ) {
104 // request to update property value
105 string args = request.substr( pos + 1 );
106 request = request.substr( 0, pos );
107 printf("'%s' '%s'\n", request.c_str(), args.c_str());
109 // parse args looking for "value="
113 pos = args.find("&");
114 if ( pos != string::npos ) {
115 arg = args.substr( 0, pos );
116 args = args.substr( pos + 1 );
122 printf(" arg = %s\n", arg.c_str() );
123 unsigned int apos = arg.find("=");
124 if ( apos != string::npos ) {
125 string a = arg.substr( 0, apos );
126 string b = arg.substr( apos + 1 );
127 printf(" a = %s b = %s\n", a.c_str(), b.c_str() );
128 if ( a == "value" ) {
129 fgSetString( request, b );
135 node = globals->get_props()->getNode(request);
137 string response = "";
138 response += "<HTML LANG=\"en\">";
139 response += getTerminator();
141 response += "<HEAD>";
142 response += getTerminator();
144 response += "<TITLE>HUD - ";
146 response += "</TITLE>";
147 response += getTerminator();
149 response += "</HEAD>";
150 response += getTerminator();
152 response += "<BODY>";
153 response += getTerminator();
155 if ( node->nChildren() > 0 ) {
156 // request is a path with children
157 response += "<H3>Contents of \"";
159 response += "\"</H3>";
160 response += getTerminator();
162 for (int i = 0; i < node->nChildren(); i++) {
163 SGPropertyNode *child = node->getChild(i);
164 string name = child->getName();
166 if ( child->nChildren() > 0 ) {
167 line += "<B><A HREF=\"";
169 if ( request.substr(request.length() - 1, 1) != "/" ) {
178 string value = node->getStringValue ( name, "" );
181 line += "</B> <A HREF=\"";
183 if ( request.substr(request.length() - 1, 1) != "/" ) {
192 response += getTerminator();
195 // request for an individual data member
196 string value = node->getStringValue();
198 response += "<form method=\"GET\" action=\"";
203 response += "</B> = ";
204 response += "<input type=text name=value size=\"5\" value=\"";
206 response += "\" maxlength=2047>";
207 response += "<input type=submit value=\"update\" name=\"submit\">";
208 response += "<FORM>";
211 response += "</BODY>";
212 response += getTerminator();
214 response += "</HTML>";
215 response += getTerminator();
217 push( "HTTP/1.1 200 OK" );
218 push( getTerminator() );
220 printf("size = %d\n", response.length());
222 sprintf(ctmp, "Content-Length: %d", response.length());
224 push( getTerminator() );
226 push( "Connection: close" );
227 push( getTerminator() );
229 push( "Content-Type: text/html" );
230 push( getTerminator() );
231 push( getTerminator() );
233 push( response.c_str() );