950ccccf0063e47659172d4662acb52d355d8b2d
[catagits/fcgi2.git] / examples / echo2.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
14 #ifndef lint
15 static const char rcsid[] = "$Id: echo2.c,v 1.1 1997/09/16 15:36:28 stanleyg Exp $";
16 #endif /* not lint */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include "fcgiapp.h"
21
22 #ifdef _WIN32
23 #include <process.h>
24 #endif
25
26 void PrintEnv(FCGX_Stream *out, char *label, char **envp)
27 {
28     printf("%s:<br>\n<pre>\n", label);
29     FCGX_FPrintF(out, "%s:<br>\n<pre>\n", label);
30     for(; *envp != NULL; envp++) {
31         FCGX_FPrintF(out, "%s\n", *envp);
32     }
33     FCGX_FPrintF(out, "</pre><p>\n");
34 }
35
36 #ifndef _WIN32
37 extern char **environ;
38 #endif
39
40 void main ()
41 {
42     FCGX_Stream *in, *out, *err;
43     FCGX_ParamArray envp;
44     int count = 0;
45     while (FCGX_Accept(&in, &out, &err, &envp) >= 0) {
46         char *contentLength = FCGX_GetParam("CONTENT_LENGTH", envp);
47         int len;
48         FCGX_FPrintF(out,
49                "Content-type: text/html\r\n"
50                "\r\n"
51                "<title>FastCGI echo (fcgiapp version)</title>"
52                "<h1>FastCGI echo (fcgiapp version)</h1>\n"
53                "Request number %d,  Process ID: %d<p>\n", ++count, 
54                      getpid());
55         if(contentLength != NULL) {
56             len = strtol(contentLength, NULL, 10);
57         } else {
58             len = 0;
59         }
60         if(len <= 0) {
61             FCGX_FPrintF(out, "No data from standard input.<p>\n");
62         } else {
63             int i, ch;
64             FCGX_FPrintF(out, "Standard input:<br>\n<pre>\n");
65             for(i = 0; i < len; i++) {
66                 if((ch = FCGX_GetChar(in)) < 0) {
67                     FCGX_FPrintF(out, "Error: Not enough bytes received "
68                                       "on standard input<p>\n");
69                     break;
70                 }
71                 FCGX_PutChar(ch, out);
72             }
73             FCGX_FPrintF(out, "\n</pre><p>\n");
74         }
75         PrintEnv(out, "Request environment", envp);
76         PrintEnv(out, "Initial environment", environ);
77     } /* while */
78 }