Logo ROOT  
Reference Guide
TFastCgi.cxx
Go to the documentation of this file.
1// $Id$
2// Author: Sergey Linev 28/12/2013
3
4/*************************************************************************
5 * Copyright (C) 1995-2013, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#include "TFastCgi.h"
13
14#include "TThread.h"
15#include "TUrl.h"
16#include "THttpServer.h"
17
18#include <string.h>
19
20#ifdef WIN32
21#include <io.h>
22#else
23#include <unistd.h>
24#endif
25
26#ifndef HTTP_WITHOUT_FASTCGI
27
28#include "fcgiapp.h"
29
30#include <stdlib.h>
31
32void FCGX_ROOT_send_file(FCGX_Request *request, const char *fname)
33{
34 std::string buf = THttpServer::ReadFileContent(fname);
35
36 if (buf.empty()) {
37 FCGX_FPrintF(request->out,
38 "Status: 404 Not Found\r\n"
39 "Content-Length: 0\r\n" // Always set Content-Length
40 "Connection: close\r\n\r\n");
41 } else {
42
43 FCGX_FPrintF(request->out,
44 "Status: 200 OK\r\n"
45 "Content-Type: %s\r\n"
46 "Content-Length: %d\r\n" // Always set Content-Length
47 "\r\n",
48 THttpServer::GetMimeType(fname), (int) buf.length());
49
50 FCGX_PutStr(buf.c_str(), buf.length(), request->out);
51 }
52}
53
54#endif
55
56//////////////////////////////////////////////////////////////////////////
57// //
58// TFastCgi //
59// //
60// http engine implementation, based on fastcgi package //
61// Allows to redirect http requests from normal web server like //
62// Apache or lighttpd //
63// //
64// Configuration example for lighttpd //
65// //
66// server.modules += ( "mod_fastcgi" ) //
67// fastcgi.server = ( //
68// "/remote_scripts/" => //
69// (( "host" => "192.168.1.11", //
70// "port" => 9000, //
71// "check-local" => "disable", //
72// "docroot" => "/" //
73// )) //
74// ) //
75// //
76// When creating THttpServer, one should specify: //
77// //
78// THttpServer* serv = new THttpServer("fastcgi:9000"); //
79// //
80// In this case, requests to lighttpd server will be //
81// redirected to ROOT session. Like: //
82// http://lighttpdhost/remote_scripts/root.cgi/ //
83// //
84// Following additional options can be specified //
85// top=foldername - name of top folder, seen in the browser //
86// debug=1 - run fastcgi server in debug mode //
87// Example: //
88// serv->CreateEngine("fastcgi:9000?top=fastcgiserver"); //
89// //
90// //
91//////////////////////////////////////////////////////////////////////////
92
93////////////////////////////////////////////////////////////////////////////////
94/// normal constructor
95
97 : THttpEngine("fastcgi", "fastcgi interface to webserver"), fSocket(0), fDebugMode(kFALSE), fTopName(),
98 fThrd(nullptr), fTerminating(kFALSE)
99{
100}
101
102////////////////////////////////////////////////////////////////////////////////
103/// destructor
104
106{
108
109 if (fThrd) {
110 // running thread will be killed
111 fThrd->Kill();
112 delete fThrd;
113 fThrd = nullptr;
114 }
115
116 if (fSocket > 0) {
117 // close opened socket
118 close(fSocket);
119 fSocket = 0;
120 }
121}
122
123////////////////////////////////////////////////////////////////////////////////
124/// initializes fastcgi variables and start thread,
125/// which will process incoming http requests
126
127Bool_t TFastCgi::Create(const char *args)
128{
129#ifndef HTTP_WITHOUT_FASTCGI
130 FCGX_Init();
131
132 TString sport = ":9000";
133
134 if ((args != 0) && (strlen(args) > 0)) {
135
136 // first extract port number
137 sport = ":";
138 while ((*args != 0) && (*args >= '0') && (*args <= '9'))
139 sport.Append(*args++);
140
141 // than search for extra parameters
142 while ((*args != 0) && (*args != '?'))
143 args++;
144
145 if (*args == '?') {
146 TUrl url(TString::Format("http://localhost/folder%s", args));
147
148 if (url.IsValid()) {
149
150 url.ParseOptions();
151
152 if (url.GetValueFromOptions("debug") != 0)
154
155 const char *top = url.GetValueFromOptions("top");
156 if (top != 0)
157 fTopName = top;
158 }
159 }
160 }
161
162 Info("Create", "Starting FastCGI server on port %s", sport.Data() + 1);
163
164 fSocket = FCGX_OpenSocket(sport.Data(), 10);
165 fThrd = new TThread("FastCgiThrd", TFastCgi::run_func, this);
166 fThrd->Run();
167
168 return kTRUE;
169#else
170 (void)args;
171 Error("Create", "ROOT compiled without fastcgi support");
172 return kFALSE;
173#endif
174}
175
176////////////////////////////////////////////////////////////////////////////////
177
178void *TFastCgi::run_func(void *args)
179{
180#ifndef HTTP_WITHOUT_FASTCGI
181
182 TFastCgi *engine = (TFastCgi *)args;
183
184 FCGX_Request request;
185
186 FCGX_InitRequest(&request, engine->GetSocket(), 0);
187
188 int count = 0;
189
190 while (!engine->fTerminating) {
191
192 int rc = FCGX_Accept_r(&request);
193
194 if (rc != 0)
195 continue;
196
197 count++;
198
199 const char *inp_path = FCGX_GetParam("PATH_INFO", request.envp);
200 if (!inp_path) inp_path = FCGX_GetParam("SCRIPT_FILENAME", request.envp);
201 const char *inp_query = FCGX_GetParam("QUERY_STRING", request.envp);
202 const char *inp_method = FCGX_GetParam("REQUEST_METHOD", request.envp);
203 const char *inp_length = FCGX_GetParam("CONTENT_LENGTH", request.envp);
204
205 auto arg = std::make_shared<THttpCallArg>();
206 if (inp_path)
207 arg->SetPathAndFileName(inp_path);
208 if (inp_query)
209 arg->SetQuery(inp_query);
210 if (inp_method)
211 arg->SetMethod(inp_method);
212 if (engine->fTopName.Length() > 0)
213 arg->SetTopName(engine->fTopName.Data());
214 int len = 0;
215 if (inp_length)
216 len = strtol(inp_length, NULL, 10);
217 if (len > 0) {
218 std::string buf;
219 buf.resize(len);
220 int nread = FCGX_GetStr((char *)buf.data(), len, request.in);
221 if (nread == len)
222 arg->SetPostData(std::move(buf));
223 }
224
225 TString header;
226 for (char **envp = request.envp; *envp != NULL; envp++) {
227 TString entry = *envp;
228 for (Int_t n = 0; n < entry.Length(); n++)
229 if (entry[n] == '=') {
230 entry[n] = ':';
231 break;
232 }
233 header.Append(entry);
234 header.Append("\r\n");
235 }
236 arg->SetRequestHeader(header);
237
238 TString username = arg->GetRequestHeader("REMOTE_USER");
239 if ((username.Length() > 0) && (arg->GetRequestHeader("AUTH_TYPE").Length() > 0))
240 arg->SetUserName(username);
241
242 if (engine->fDebugMode) {
243 FCGX_FPrintF(request.out, "Status: 200 OK\r\n"
244 "Content-type: text/html\r\n"
245 "\r\n"
246 "<title>FastCGI echo</title>"
247 "<h1>FastCGI echo</h1>\n");
248
249 FCGX_FPrintF(request.out, "Request %d:<br/>\n<pre>\n", count);
250 FCGX_FPrintF(request.out, " Method : %s\n", arg->GetMethod());
251 FCGX_FPrintF(request.out, " PathName : %s\n", arg->GetPathName());
252 FCGX_FPrintF(request.out, " FileName : %s\n", arg->GetFileName());
253 FCGX_FPrintF(request.out, " Query : %s\n", arg->GetQuery());
254 FCGX_FPrintF(request.out, " PostData : %ld\n", arg->GetPostDataLength());
255 FCGX_FPrintF(request.out, "</pre><p>\n");
256
257 FCGX_FPrintF(request.out, "Environment:<br/>\n<pre>\n");
258 for (char **envp = request.envp; *envp != NULL; envp++) {
259 FCGX_FPrintF(request.out, " %s\n", *envp);
260 }
261 FCGX_FPrintF(request.out, "</pre><p>\n");
262
263 FCGX_Finish_r(&request);
264 continue;
265 }
266
267 TString fname;
268
269 if (engine->GetServer()->IsFileRequested(inp_path, fname)) {
270 FCGX_ROOT_send_file(&request, fname.Data());
271 FCGX_Finish_r(&request);
272 continue;
273 }
274
275 if (!engine->GetServer()->ExecuteHttp(arg) || arg->Is404()) {
276 std::string hdr = arg->FillHttpHeader("Status:");
277 FCGX_FPrintF(request.out, hdr.c_str());
278 } else if (arg->IsFile()) {
279 FCGX_ROOT_send_file(&request, (const char *)arg->GetContent());
280 } else {
281
282 // TODO: check in request header that gzip encoding is supported
283 if (arg->GetZipping() != THttpCallArg::kNoZip)
284 arg->CompressWithGzip();
285
286 std::string hdr = arg->FillHttpHeader("Status:");
287 FCGX_FPrintF(request.out, hdr.c_str());
288
289 FCGX_PutStr((const char *)arg->GetContent(), (int)arg->GetContentLength(), request.out);
290 }
291
292 FCGX_Finish_r(&request);
293
294 } /* while */
295
296 return nullptr;
297
298#else
299 return args;
300#endif
301}
const Bool_t kFALSE
Definition: RtypesCore.h:90
const Bool_t kTRUE
Definition: RtypesCore.h:89
void FCGX_ROOT_send_file(FCGX_Request *request, const char *fname)
Definition: TFastCgi.cxx:32
typedef void((*Func_t)())
TFastCgi()
normal constructor
Definition: TFastCgi.cxx:96
TString fTopName
! name of top item
Definition: TFastCgi.h:23
virtual Bool_t Create(const char *args)
initializes fastcgi variables and start thread, which will process incoming http requests
Definition: TFastCgi.cxx:127
Bool_t fDebugMode
! debug mode, may required for fastcgi debugging in other servers
Definition: TFastCgi.h:22
static void * run_func(void *)
Definition: TFastCgi.cxx:178
Int_t GetSocket() const
Definition: TFastCgi.h:33
virtual ~TFastCgi()
destructor
Definition: TFastCgi.cxx:105
Int_t fSocket
! socket used by fastcgi
Definition: TFastCgi.h:21
Bool_t fTerminating
! set when http server wants to terminate all engines
Definition: TFastCgi.h:25
TThread * fThrd
! thread which takes requests, can be many later
Definition: TFastCgi.h:24
THttpServer * GetServer() const
Returns pointer to THttpServer associated with engine.
Definition: THttpEngine.h:40
Bool_t IsFileRequested(const char *uri, TString &res) const
Check if file is requested, thread safe.
Bool_t ExecuteHttp(std::shared_ptr< THttpCallArg > arg)
Execute HTTP request.
static char * ReadFileContent(const char *filename, Int_t &len)
Reads content of file from the disk.
static const char * GetMimeType(const char *path)
Guess mime type base on file extension.
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:891
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition: TObject.cxx:865
Basic string class.
Definition: TString.h:131
Ssiz_t Length() const
Definition: TString.h:405
const char * Data() const
Definition: TString.h:364
TString & Append(const char *cs)
Definition: TString.h:559
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition: TString.cxx:2311
Int_t Kill()
Kill this thread.
Definition: TThread.cxx:586
Int_t Run(void *arg=0)
Start the thread.
Definition: TThread.cxx:562
This class represents a WWW compatible URL.
Definition: TUrl.h:35
const char * GetValueFromOptions(const char *key) const
Return a value for a given key from the URL options.
Definition: TUrl.cxx:651
Bool_t IsValid() const
Definition: TUrl.h:81
void ParseOptions() const
Parse URL options into a key/value map.
Definition: TUrl.cxx:617
const Int_t n
Definition: legend1.C:16