]> git.mxchange.org Git - flightgear.git/blob - src/Network/httpd.cxx
Updated adf property names.
[flightgear.git] / src / Network / httpd.cxx
1 // httpd.cxx -- FGFS http property manager interface / external script
2 //              and control class
3 //
4 // Written by Curtis Olson, started June 2001.
5 //
6 // Copyright (C) 2001  Curtis L. Olson - curt@flightgear.org
7 //
8 // Jpeg Image Support added August 2001
9 //  by Norman Vine - nhv@cape.com
10 //
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.
15 //
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.
20 //
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.
24 //
25 // $Id$
26
27
28 #ifdef HAVE_CONFIG_H
29 #  include <config.h>
30 #endif
31
32 #include <simgear/compiler.h>
33
34 #include <stdlib.h>             // atoi() atof()
35
36 #include STL_STRING
37 #include STL_STRSTREAM
38
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>
43
44 #include <Main/fg_props.hxx>
45 #include <Main/globals.hxx>
46
47 #include "httpd.hxx"
48
49 SG_USING_STD(string);
50 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
51 SG_USING_STD(cout);
52 SG_USING_STD(istrstream);
53 #endif
54
55
56 bool FGHttpd::open() {
57     if ( is_enabled() ) {
58         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
59                 << "is already in use, ignoring" );
60         return false;
61     }
62
63     server = new HttpdServer( port );
64     
65     set_hz( 5 );                // default to processing requests @ 5Hz
66     set_enabled( true );
67
68     return true;
69 }
70
71
72 bool FGHttpd::process() {
73     netChannel::poll();
74
75     return true;
76 }
77
78
79 bool FGHttpd::close() {
80     delete server;
81
82     return true;
83 }
84
85
86 // Handle http GET requests
87 void HttpdChannel::foundTerminator (void) {
88     
89     closeWhenDone ();
90
91     const string s = buffer.getData();
92
93     if ( s.find( "GET " ) == 0 ) {
94         printf("echo: %s\n", s.c_str());
95
96         string rest = s.substr(4);
97         string request;
98         string tmp;
99
100         string::size_type pos = rest.find( " " );
101         if ( pos != string::npos ) {
102             request = rest.substr( 0, pos );
103         } else {
104             request = "/";
105         }
106
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);
115
116             // parse args looking for "value="
117             bool done = false;
118             while ( ! done ) {
119                 string arg;
120                 pos = args.find("&");
121                 if ( pos != string::npos ) {
122                     arg = args.substr( 0, pos );
123                     args = args.substr( pos + 1 );
124                 } else {
125                     arg = args;
126                     done = true;
127                 }
128
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() );
137                     } 
138                 }
139             }
140         } else {
141             request = urlDecode(request);
142         }
143
144         node = globals->get_props()->getNode(request.c_str());
145
146         string response = "";
147         response += "<HTML LANG=\"en\">";
148         response += getTerminator();
149
150         response += "<HEAD>";
151         response += getTerminator();
152
153         response += "<TITLE>FlightGear - ";
154         response += request;
155         response += "</TITLE>";
156         response += getTerminator();
157
158         response += "</HEAD>";
159         response += getTerminator();
160
161         response += "<BODY>";
162         response += getTerminator();
163
164         if (node == NULL) {
165             response += "<H3>Non-existent node requested!</H3>";
166             response += getTerminator();
167
168             response += "<B>";
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 \"";
175             response += request;
176             response += "\"</H3>";
177             response += getTerminator();
178
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) ) {
183                     char buf[16];
184                     sprintf(buf, "[%d]", child->getIndex());
185                     name += buf;
186                 }
187                 string line = "";
188                 if ( child->nChildren() > 0 ) {
189                     line += "<B><A HREF=\"";
190                     line += request;
191                     if ( request.substr(request.length() - 1, 1) != (string)"/" ) {
192                         line += "/";
193                     }
194                     line += urlEncode(name);
195                     line += "\">";
196                     line += name;
197                     line += "</A></B>";
198                     line += "/<BR>";
199                 } else {
200                     string value = node->getStringValue ( name.c_str(), "" );
201                     line += "<B>";
202                     line += name;
203                     line += "</B> <A HREF=\"";
204                     line += request;
205                     if ( request.substr(request.length() - 1, 1) != (string)"/" ) {
206                         line += "/";
207                     }
208                     line += urlEncode(name);
209                     line += "\">(";
210                     line += value;
211                     line += ")</A><BR>";
212                 }
213                 response += line;
214                 response += getTerminator();
215             }
216         } else {
217             // request for an individual data member
218             string value = node->getStringValue();
219             
220             response += "<form method=\"GET\" action=\"";
221             response += urlEncode(request);
222             response += "\">";
223             response += "<B>";
224             response += request;
225             response += "</B> = ";
226             response += "<input type=text name=value size=\"15\" value=\"";
227             response += value;
228             response += "\" maxlength=2047>";
229             response += "<input type=submit value=\"update\" name=\"submit\">";
230             response += "</FORM>";
231         }
232         response += "</BODY>";
233         response += getTerminator();
234
235         response += "</HTML>";
236         response += getTerminator();
237
238         push( "HTTP/1.1 200 OK" );
239         push( getTerminator() );
240         
241         printf("size = %d\n", response.length());
242         char ctmp[256];
243         sprintf(ctmp, "Content-Length: %d", response.length());
244         push( ctmp );
245         push( getTerminator() );
246
247         push( "Connection: close" );
248         push( getTerminator() );
249
250         push( "Content-Type: text/html" );
251         push( getTerminator() );
252         push( getTerminator() );
253                 
254         push( response.c_str() );
255     }
256
257     buffer.remove();
258 }
259
260
261 // encode everything but "a-zA-Z0-9_.-/" (see RFC2396)
262 string HttpdChannel::urlEncode(string s) {
263     string r = "";
264     
265     for ( int i = 0; i < (int)s.length(); i++ ) {
266         if ( isalnum(s[i]) || s[i] == '_' || s[i] == '.'
267                 || s[i] == '-' || s[i] == '/' ) {
268             r += s[i];
269         } else {
270             char buf[16];
271             sprintf(buf, "%%%02X", (unsigned char)s[i]);
272             r += buf;
273         }
274     }
275     return r;
276 }
277
278
279 string HttpdChannel::urlDecode(string s) {
280     string r = "";
281     int max = s.length();
282     int a, b;
283
284     for ( int i = 0; i < max; i++ ) {
285         if ( s[i] == '+' ) {
286             r += ' ';
287         } else if ( s[i] == '%' && i + 2 < max
288                 && isxdigit(s[i + 1])
289                 && isxdigit(s[i + 2]) ) {
290             i++;
291             a = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10;
292             i++;
293             b = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10;
294             r += (char)(a * 16 + b);
295         } else {
296             r += s[i];
297         }
298     }
299     return r;
300 }