]> git.mxchange.org Git - flightgear.git/blob - src/Network/httpd.cxx
7a567a61a53d3884e1b5a6dfd20e005a53e636e6
[flightgear.git] / src / Network / httpd.cxx
1 // httpd.hxx -- 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         unsigned int pos = rest.find( " " );
101         if ( pos != string::npos ) {
102             request = rest.substr( 0, pos );
103             request = urlDecode(request);
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
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                 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 );
137                     } 
138                 }
139             }
140         }
141
142         node = globals->get_props()->getNode(request);
143
144         string response = "";
145         response += "<HTML LANG=\"en\">";
146         response += getTerminator();
147
148         response += "<HEAD>";
149         response += getTerminator();
150
151         response += "<TITLE>FlightGear - ";
152         response += request;
153         response += "</TITLE>";
154         response += getTerminator();
155
156         response += "</HEAD>";
157         response += getTerminator();
158
159         response += "<BODY>";
160         response += getTerminator();
161
162         if (node == NULL) {
163             response += "<H3>Non-existent node requested!</H3>";
164             response += getTerminator();
165
166             response += "<B>";
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 \"";
173             response += request;
174             response += "\"</H3>";
175             response += getTerminator();
176
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) ) {
181                     char buf[16];
182                     sprintf(buf, "[%d]", child->getIndex());
183                     name += buf;
184                 }
185                 string line = "";
186                 if ( child->nChildren() > 0 ) {
187                     line += "<B><A HREF=\"";
188                     line += request;
189                     if ( request.substr(request.length() - 1, 1) != (string)"/" ) {
190                         line += "/";
191                     }
192                     line += urlEncode(name);
193                     line += "\">";
194                     line += name;
195                     line += "</A></B>";
196                     line += "/<BR>";
197                 } else {
198                     string value = node->getStringValue ( name, "" );
199                     line += "<B>";
200                     line += name;
201                     line += "</B> <A HREF=\"";
202                     line += request;
203                     if ( request.substr(request.length() - 1, 1) != (string)"/" ) {
204                         line += "/";
205                     }
206                     line += urlEncode(name);
207                     line += "\">(";
208                     line += value;
209                     line += ")</A><BR>";
210                 }
211                 response += line;
212                 response += getTerminator();
213             }
214         } else {
215             // request for an individual data member
216             string value = node->getStringValue();
217             
218             response += "<form method=\"GET\" action=\"";
219             response += urlEncode(request);
220             response += "\">";
221             response += "<B>";
222             response += request;
223             response += "</B> = ";
224             response += "<input type=text name=value size=\"15\" value=\"";
225             response += value;
226             response += "\" maxlength=2047>";
227             response += "<input type=submit value=\"update\" name=\"submit\">";
228             response += "<FORM>";
229             response += "<BR>";
230         }
231         response += "</BODY>";
232         response += getTerminator();
233
234         response += "</HTML>";
235         response += getTerminator();
236
237         push( "HTTP/1.1 200 OK" );
238         push( getTerminator() );
239         
240         printf("size = %d\n", response.length());
241         char ctmp[256];
242         sprintf(ctmp, "Content-Length: %d", response.length());
243         push( ctmp );
244         push( getTerminator() );
245
246         push( "Connection: close" );
247         push( getTerminator() );
248
249         push( "Content-Type: text/html" );
250         push( getTerminator() );
251         push( getTerminator() );
252                 
253         push( response.c_str() );
254     }
255
256     buffer.remove();
257 }
258
259
260 // encode everything but "a-zA-Z0-9_.-/"
261 string HttpdChannel::urlEncode(string s) {
262     string r = "";
263     
264     for ( int i = 0; i < (int)s.length(); i++ ) {
265         if ( isalnum(s[i]) || s[i] == '_' || s[i] == '.'
266                 || s[i] == '-' || s[i] == '/' ) {
267             r += s[i];
268         } else {
269             char buf[16];
270             sprintf(buf, "%%%02X", (unsigned char)s[i]);
271             r += buf;
272         }
273     }
274     return r;
275 }
276
277
278 string HttpdChannel::urlDecode(string s) {
279     string r = "";
280     int max = s.length();
281     int a, b;
282
283     for ( int i = 0; i < max; i++ ) {
284         if ( s[i] == '+' ) {
285             r += ' ';
286         } else if ( s[i] == '%' && i + 2 < max
287                 && isxdigit(s[i + 1])
288                 && isxdigit(s[i + 2]) ) {
289             i++;
290             a = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10;
291             i++;
292             b = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10;
293             r += (char)(a * 16 + b);
294         } else {
295             r += s[i];
296         }
297     }
298     return r;
299 }