]> git.mxchange.org Git - flightgear.git/blob - src/Network/httpd.cxx
b012e0ba1ad053bd6f2246f3c7b907e09897708a
[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         } 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
115             // parse args looking for "value="
116             bool done = false;
117             while ( ! done ) {
118                 string arg;
119                 pos = args.find("&");
120                 if ( pos != string::npos ) {
121                     arg = args.substr( 0, pos );
122                     args = args.substr( pos + 1 );
123                 } else {
124                     arg = args;
125                     done = true;
126                 }
127
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 );
136                     } 
137                 }
138             }
139         }
140
141         node = globals->get_props()->getNode(request);
142
143         string response = "";
144         response += "<HTML LANG=\"en\">";
145         response += getTerminator();
146
147         response += "<HEAD>";
148         response += getTerminator();
149
150         response += "<TITLE>FlightGear - ";
151         response += request;
152         response += "</TITLE>";
153         response += getTerminator();
154
155         response += "</HEAD>";
156         response += getTerminator();
157
158         response += "<BODY>";
159         response += getTerminator();
160
161         if (node == NULL) {
162             response += "<H3>Non-existent node requested!</H3>";
163             response += getTerminator();
164
165             response += "<B>";
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 \"";
172             response += request;
173             response += "\"</H3>";
174             response += getTerminator();
175
176             for (int i = 0; i < node->nChildren(); i++) {
177                 SGPropertyNode *child = node->getChild(i);
178                 string name = child->getName();
179                 string line = "";
180                 if ( child->nChildren() > 0 ) {
181                     line += "<B><A HREF=\"";
182                     line += request;
183                     if ( request.substr(request.length() - 1, 1) != (string)"/" ) {
184                         line += "/";
185                     }
186                     line += name;
187                     line += "\">";
188                     line += name;
189                     line += "</A></B>";
190                     line += "/<BR>";
191                 } else {
192                     string value = node->getStringValue ( name, "" );
193                     line += "<B>";
194                     line += name;
195                     line += "</B> <A HREF=\"";
196                     line += request;
197                     if ( request.substr(request.length() - 1, 1) != (string)"/" ) {
198                         line += "/";
199                     }
200                     line += name;
201                     line += "\">(";
202                     line += value;
203                     line += ")</A><BR>";
204                 }
205                 response += line;
206                 response += getTerminator();
207             }
208         } else {
209             // request for an individual data member
210             string value = node->getStringValue();
211             
212             response += "<form method=\"GET\" action=\"";
213             response += request;
214             response += "\">";
215             response += "<B>";
216             response += request;
217             response += "</B> = ";
218             response += "<input type=text name=value size=\"5\" value=\"";
219             response += value;
220             response += "\" maxlength=2047>";
221             response += "<input type=submit value=\"update\" name=\"submit\">";
222             response += "<FORM>";
223             response += "<BR>";
224         }
225         response += "</BODY>";
226         response += getTerminator();
227
228         response += "</HTML>";
229         response += getTerminator();
230
231         push( "HTTP/1.1 200 OK" );
232         push( getTerminator() );
233         
234         printf("size = %d\n", response.length());
235         char ctmp[256];
236         sprintf(ctmp, "Content-Length: %d", response.length());
237         push( ctmp );
238         push( getTerminator() );
239
240         push( "Connection: close" );
241         push( getTerminator() );
242
243         push( "Content-Type: text/html" );
244         push( getTerminator() );
245         push( getTerminator() );
246                 
247         push( response.c_str() );
248     }
249
250     buffer.remove();
251 }