]> git.mxchange.org Git - flightgear.git/blob - src/Network/httpd.cxx
Work on nav2_obs tuner.
[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/commands.hxx>
43 #include <simgear/misc/props.hxx>
44
45 #include <Main/fg_props.hxx>
46 #include <Main/globals.hxx>
47
48 #include "httpd.hxx"
49
50 SG_USING_STD(string);
51 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
52 SG_USING_STD(cout);
53 SG_USING_STD(istrstream);
54 #endif
55
56
57 bool FGHttpd::open() {
58     if ( is_enabled() ) {
59         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
60                 << "is already in use, ignoring" );
61         return false;
62     }
63
64     server = new HttpdServer( port );
65     
66     set_hz( 15 );                // default to processing requests @ 15Hz
67     set_enabled( true );
68
69     return true;
70 }
71
72
73 bool FGHttpd::process() {
74     netChannel::poll();
75
76     return true;
77 }
78
79
80 bool FGHttpd::close() {
81     delete server;
82
83     return true;
84 }
85
86
87 // Handle http GET requests
88 void HttpdChannel::foundTerminator (void) {
89     
90     closeWhenDone ();
91
92     const string s = buffer.getData();
93
94     if ( s.find( "GET " ) == 0 ) {
95         printf("echo: %s\n", s.c_str());
96
97         string rest = s.substr(4);
98         string request;
99         string tmp;
100
101         string::size_type pos = rest.find( " " );
102         if ( pos != string::npos ) {
103             request = rest.substr( 0, pos );
104         } else {
105             request = "/";
106         }
107
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());
115             request = urlDecode(request);
116
117             // parse args looking for "value="
118             bool done = false;
119             while ( ! done ) {
120                 string arg;
121                 pos = args.find("&");
122                 if ( pos != string::npos ) {
123                     arg = args.substr( 0, pos );
124                     args = args.substr( pos + 1 );
125                 } else {
126                     arg = args;
127                     done = true;
128                 }
129
130                 printf("  arg = %s\n", arg.c_str() );
131                 string::size_type apos = arg.find("=");
132                 if ( apos != string::npos ) {
133                     string a = arg.substr( 0, apos );
134                     string b = arg.substr( apos + 1 );
135                     printf("    a = %s  b = %s\n", a.c_str(), b.c_str() );
136                     if ( request == "/run.cgi" ) {
137                         // execute a command
138                         if ( a == "value" ) {
139                             SGPropertyNode args;
140                             if ( !globals->get_commands()
141                                  ->execute(urlDecode(b).c_str(), &args) )
142                             {
143                                 SG_LOG( SG_GENERAL, SG_ALERT,
144                                         "Command " << urlDecode(b)
145                                         << " failed.");
146                             }
147
148                         }
149                     } else {
150                         if ( a == "value" ) {
151                             // update a property value
152                             fgSetString( request.c_str(),
153                                          urlDecode(b).c_str() );
154                         }
155                     }
156                 }
157             }
158         } else {
159             request = urlDecode(request);
160         }
161
162         node = globals->get_props()->getNode(request.c_str());
163
164         string response = "";
165         response += "<HTML LANG=\"en\">";
166         response += getTerminator();
167
168         response += "<HEAD>";
169         response += getTerminator();
170
171         response += "<TITLE>FlightGear - ";
172         response += request;
173         response += "</TITLE>";
174         response += getTerminator();
175
176         response += "</HEAD>";
177         response += getTerminator();
178
179         response += "<BODY>";
180         response += getTerminator();
181
182         if (node == NULL) {
183             response += "<H3>Non-existent node requested!</H3>";
184             response += getTerminator();
185
186             response += "<B>";
187             response += request.c_str();
188             response += "</B> does not exist.";
189             response += getTerminator();
190         } else if ( node->nChildren() > 0 ) {
191             // request is a path with children
192             response += "<H3>Contents of \"";
193             response += request;
194             response += "\"</H3>";
195             response += getTerminator();
196
197             for (int i = 0; i < node->nChildren(); i++) {
198                 SGPropertyNode *child = node->getChild(i);
199                 string name = child->getDisplayName(true);
200                 string line = "";
201                 if ( child->nChildren() > 0 ) {
202                     line += "<B><A HREF=\"";
203                     line += request;
204                     if ( request.substr(request.length() - 1, 1) != (string)"/" ) {
205                         line += "/";
206                     }
207                     line += urlEncode(name);
208                     line += "\">";
209                     line += name;
210                     line += "</A></B>";
211                     line += "/<BR>";
212                 } else {
213                     string value = node->getStringValue ( name.c_str(), "" );
214                     line += "<B>";
215                     line += name;
216                     line += "</B> <A HREF=\"";
217                     line += request;
218                     if ( request.substr(request.length() - 1, 1) != (string)"/" ) {
219                         line += "/";
220                     }
221                     line += urlEncode(name);
222                     line += "\">(";
223                     line += value;
224                     line += ")</A><BR>";
225                 }
226                 response += line;
227                 response += getTerminator();
228             }
229         } else {
230             // request for an individual data member
231             string value = node->getStringValue();
232             
233             response += "<form method=\"GET\" action=\"";
234             response += urlEncode(request);
235             response += "\">";
236             response += "<B>";
237             response += request;
238             response += "</B> = ";
239             response += "<input type=text name=value size=\"15\" value=\"";
240             response += value;
241             response += "\" maxlength=2047>";
242             response += "<input type=submit value=\"update\" name=\"submit\">";
243             response += "</FORM>";
244         }
245         response += "</BODY>";
246         response += getTerminator();
247
248         response += "</HTML>";
249         response += getTerminator();
250
251         push( "HTTP/1.1 200 OK" );
252         push( getTerminator() );
253         
254         printf("size = %d\n", response.length());
255         char ctmp[256];
256         sprintf(ctmp, "Content-Length: %d", response.length());
257         push( ctmp );
258         push( getTerminator() );
259
260         push( "Connection: close" );
261         push( getTerminator() );
262
263         push( "Content-Type: text/html" );
264         push( getTerminator() );
265         push( getTerminator() );
266                 
267         push( response.c_str() );
268     }
269
270     buffer.remove();
271 }
272
273
274 // encode everything but "a-zA-Z0-9_.-/" (see RFC2396)
275 string HttpdChannel::urlEncode(string s) {
276     string r = "";
277     
278     for ( int i = 0; i < (int)s.length(); i++ ) {
279         if ( isalnum(s[i]) || s[i] == '_' || s[i] == '.'
280                 || s[i] == '-' || s[i] == '/' ) {
281             r += s[i];
282         } else {
283             char buf[16];
284             sprintf(buf, "%%%02X", (unsigned char)s[i]);
285             r += buf;
286         }
287     }
288     return r;
289 }
290
291
292 string HttpdChannel::urlDecode(string s) {
293     string r = "";
294     int max = s.length();
295     int a, b;
296
297     for ( int i = 0; i < max; i++ ) {
298         if ( s[i] == '+' ) {
299             r += ' ';
300         } else if ( s[i] == '%' && i + 2 < max
301                 && isxdigit(s[i + 1])
302                 && isxdigit(s[i + 2]) ) {
303             i++;
304             a = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10;
305             i++;
306             b = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10;
307             r += (char)(a * 16 + b);
308         } else {
309             r += s[i];
310         }
311     }
312     return r;
313 }