This should be replaced with Tom Poindexter's TCL lib or a
[catagits/fcgi2.git] / examples / echo.c
CommitLineData
54cf3fec 1/*
0198fd3c 2 * echo.c --
3 *
4 * Produce a page containing all FastCGI inputs
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 */
0198fd3c 13#ifndef lint
54cf3fec 14static const char rcsid[] = "$Id: echo.c,v 1.4 1999/07/27 19:01:38 roberts Exp $";
0198fd3c 15#endif /* not lint */
16
54cf3fec 17#include "fcgi_stdio.h"
18
2fd179ab 19#if defined HAVE_UNISTD_H || __linux__
20#include <unistd.h>
21#endif
22
0198fd3c 23#include <stdlib.h>
24
25#ifdef _WIN32
26#include <process.h>
27#else
28extern char **environ;
29#endif
30
2fd179ab 31static void PrintEnv(char *label, char **envp)
0198fd3c 32{
33 printf("%s:<br>\n<pre>\n", label);
3293ebdf 34 for ( ; *envp != NULL; envp++) {
0198fd3c 35 printf("%s\n", *envp);
36 }
37 printf("</pre><p>\n");
38}
39
3293ebdf 40int main ()
0198fd3c 41{
42 char **initialEnv = environ;
43 int count = 0;
3293ebdf 44
45 while (FCGI_Accept() >= 0) {
0198fd3c 46 char *contentLength = getenv("CONTENT_LENGTH");
47 int len;
3293ebdf 48
0198fd3c 49 printf("Content-type: text/html\r\n"
3293ebdf 50 "\r\n"
51 "<title>FastCGI echo</title>"
52 "<h1>FastCGI echo</h1>\n"
53 "Request number %d, Process ID: %d<p>\n", ++count, getpid());
54
55 if (contentLength != NULL) {
0198fd3c 56 len = strtol(contentLength, NULL, 10);
3293ebdf 57 }
58 else {
0198fd3c 59 len = 0;
60 }
3293ebdf 61
62 if (len <= 0) {
0198fd3c 63 printf("No data from standard input.<p>\n");
3293ebdf 64 }
65 else {
0198fd3c 66 int i, ch;
3293ebdf 67
0198fd3c 68 printf("Standard input:<br>\n<pre>\n");
3293ebdf 69 for (i = 0; i < len; i++) {
70 if ((ch = getchar()) < 0) {
71 printf("Error: Not enough bytes received on standard input<p>\n");
0198fd3c 72 break;
73 }
74 putchar(ch);
75 }
76 printf("\n</pre><p>\n");
77 }
3293ebdf 78
0198fd3c 79 PrintEnv("Request environment", environ);
80 PrintEnv("Initial environment", initialEnv);
81 } /* while */
3293ebdf 82
83 return 0;
0198fd3c 84}