107e56f69cb56624c813ed8c4809229705f26ba2
[catagits/fcgi2.git] / examples / echo-x.c
1 /*
2  * echo2.c --
3  *
4  *      Produce a page containing all the inputs (fcgiapp version)
5  *
6  *
7  * Copyright (c) 1996 Open Market, Inc.
8  *
9  * See the file "LICENSE.TERMS" for information on usage and redistribution
10  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11  *
12  */
13 #ifndef lint
14 static const char rcsid[] = "$Id: echo-x.c,v 1.1 2001/06/19 15:06:17 robs Exp $";
15 #endif /* not lint */
16
17 #include "fcgi_config.h"
18
19 #include <stdlib.h>
20
21 #ifdef HAVE_UNISTD_H
22 #include <unistd.h>
23 #endif
24
25 #ifdef _WIN32
26 #include <process.h>
27 #else
28 extern char **environ;
29 #endif
30
31 #include "fcgiapp.h"
32
33 static void PrintEnv(FCGX_Stream *out, char *label, char **envp)
34 {
35     FCGX_FPrintF(out, "%s:<br>\n<pre>\n", label);
36     for( ; *envp != NULL; envp++) {
37         FCGX_FPrintF(out, "%s\n", *envp);
38     }
39     FCGX_FPrintF(out, "</pre><p>\n");
40 }
41
42 int main ()
43 {
44     FCGX_Stream *in, *out, *err;
45     FCGX_ParamArray envp;
46     int count = 0;
47
48     while (FCGX_Accept(&in, &out, &err, &envp) >= 0) {
49         char *contentLength = FCGX_GetParam("CONTENT_LENGTH", envp);
50         int len = 0;
51
52         FCGX_FPrintF(out,
53            "Content-type: text/html\r\n"
54            "\r\n"
55            "<title>FastCGI echo (fcgiapp version)</title>"
56            "<h1>FastCGI echo (fcgiapp version)</h1>\n"
57            "Request number %d,  Process ID: %d<p>\n", ++count, getpid());
58
59         if (contentLength != NULL)
60             len = strtol(contentLength, NULL, 10);
61
62         if (len <= 0) {
63             FCGX_FPrintF(out, "No data from standard input.<p>\n");
64         }
65         else {
66             int i, ch;
67
68             FCGX_FPrintF(out, "Standard input:<br>\n<pre>\n");
69             for (i = 0; i < len; i++) {
70                 if ((ch = FCGX_GetChar(in)) < 0) {
71                     FCGX_FPrintF(out,
72                         "Error: Not enough bytes received on standard input<p>\n");
73                     break;
74                 }
75                 FCGX_PutChar(ch, out);
76             }
77             FCGX_FPrintF(out, "\n</pre><p>\n");
78         }
79
80         PrintEnv(out, "Request environment", envp);
81         PrintEnv(out, "Initial environment", environ);
82     } /* while */
83
84     return 0;
85 }