2 * A simple FastCGI application example in C++.
4 * $Id: echo-cpp.cpp,v 1.6 2001/11/26 18:08:25 robs Exp $
6 * Copyright (c) 2001 Rob Saccoccio and Chelsea Networks
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 extern char ** environ
;
42 // Maximum number of bytes allowed to be read from stdin
43 static const unsigned long STDIN_MAX
= 1000000;
45 static void penv(const char * const * envp
)
48 for ( ; *envp
; ++envp
)
50 cout
<< *envp
<< "\n";
55 static long gstdin(FCGX_Request
* request
, char ** content
)
57 char * clenstr
= FCGX_GetParam("CONTENT_LENGTH", request
->envp
);
58 unsigned long clen
= STDIN_MAX
;
62 clen
= strtol(clenstr
, &clenstr
, 10);
65 cerr
<< "can't parse \"CONTENT_LENGTH="
66 << FCGX_GetParam("CONTENT_LENGTH", request
->envp
)
72 // Note that *you* should not read stdin when CONTENT_LENGTH
73 // is missing or unparsable (this is a demo/test program).
75 // *always* put a cap on the amount of data that will be read
76 if (clen
> STDIN_MAX
) clen
= STDIN_MAX
;
78 *content
= new char[clen
];
80 cin
.read(*content
, clen
);
83 // Chew up any remaining stdin - this shouldn't be necessary
84 // but is because mod_fastcgi doesn't handle it correctly.
86 // ignore() doesn't set the eof bit in some versions of glibc++
87 // so use gcount() instead of eof()...
88 do cin
.ignore(1024); while (cin
.gcount() == 1024);
101 FCGX_InitRequest(&request
, 0, 0);
103 while (FCGX_Accept_r(&request
) == 0)
105 // Note that the default bufsize (0) will cause the use of iostream
106 // methods that require positioning (such as peek(), seek(),
107 // unget() and putback()) to fail (in favour of more efficient IO).
108 fcgi_streambuf
fin(request
.in
);
109 fcgi_streambuf
fout(request
.out
);
110 fcgi_streambuf
ferr(request
.err
);
122 // Although FastCGI supports writing before reading,
123 // many http clients (browsers) don't support it (so
124 // the connection deadlocks until a timeout expires!).
126 unsigned long clen
= gstdin(&request
, &content
);
128 cout
<< "Content-type: text/html\r\n"
130 "<TITLE>echo-cpp</TITLE>\n"
131 "<H1>echo-cpp</H1>\n"
132 "<H4>PID: " << pid
<< "</H4>\n"
133 "<H4>Request Number: " << ++count
<< "</H4>\n";
135 cout
<< "<H4>Request Environment</H4>\n";
138 cout
<< "<H4>Process/Initial Environment</H4>\n";
141 cout
<< "<H4>Standard Input - " << clen
;
142 if (clen
== STDIN_MAX
) cout
<< " (STDIN_MAX)";
143 cout
<< " bytes</H4>\n";
144 if (clen
) cout
.write(content
, clen
);
146 if (content
) delete []content
;
148 // If the output streambufs had non-zero bufsizes and
149 // were constructed outside of the accept loop (i.e.
150 // their destructor won't be called here), they would
151 // have to be flushed here.