2 * A simple FastCGI application example in C++.
4 * $Id: echo-cpp.cpp,v 1.4 2001/11/21 20:18:46 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
85 do cin
.ignore(1024); while (! cin
.eof());
98 FCGX_InitRequest(&request
, 0, 0);
100 while (FCGX_Accept_r(&request
) == 0)
102 // Note that the default bufsize (0) will cause the use of iostream
103 // methods that require positioning (such as peek(), seek(),
104 // unget() and putback()) to fail (in favour of more efficient IO).
105 fcgi_streambuf
fin(request
.in
);
106 fcgi_streambuf
fout(request
.out
);
107 fcgi_streambuf
ferr(request
.err
);
119 // Although FastCGI supports writing before reading,
120 // many http clients (browsers) don't support it (so
121 // the connection deadlocks until a timeout expires!).
123 unsigned long clen
= gstdin(&request
, &content
);
125 cout
<< "Content-type: text/html\r\n"
127 "<TITLE>echo-cpp</TITLE>\n"
128 "<H1>echo-cpp</H1>\n"
129 "<H4>PID: " << pid
<< "</H4>\n"
130 "<H4>Request Number: " << ++count
<< "</H4>\n";
132 cout
<< "<H4>Request Environment</H4>\n";
135 cout
<< "<H4>Process/Initial Environment</H4>\n";
138 cout
<< "<H4>Standard Input - " << clen
;
139 if (clen
== STDIN_MAX
) cout
<< " (STDIN_MAX)";
140 cout
<< " bytes</H4>\n";
141 if (clen
) cout
.write(content
, clen
);
143 if (content
) delete []content
;
145 // If the output streambufs had non-zero bufsizes and
146 // were constructed outside of the accept loop (i.e.
147 // their destructor won't be called here), they would
148 // have to be flushed here.