Appendix B. The echo Program

The following listing is echo.c :

#include <stdio.h>
#include <stdlib.h>
#define MAX_ENTRIES 10000
typedef struct
    {
    char *name;
    char *val;
    } entry;
char *makeword(char *line, char stop);
char *fmakeword(FILE *f, char stop, int *len);
char x2c(char *what);
void unescape_url(char *url);
void plustospace(char *str);
int main(int argc, char *argv[])
    {
    entry entries[MAX_ENTRIES];
    register int x,m=0;
    int cl;
    char mbuf[200];
    printf("Content-type: text/html\n\n");
    if(strcmp(getenv("REQUEST_METHOD"),"POST"))
        {
        printf("This script should be referenced with a METHOD of POST.\n");
        exit(1);
        }
    if(strcmp(getenv("CONTENT_TYPE"),"application/x-www-form-urlencoded")) 
        {
        printf("This script can only be used to decode form results. \n");
        exit(1);
        }
    cl = atoi(getenv("CONTENT_LENGTH"));
// Returns the length of data to come.
    for(x=0;cl && (!feof(stdin));x++) 
        {
        m=x;
        entries[x].val = fmakeword(stdin,'&',&cl);
        plustospace(entries[x].val);
        unescape_url(entries[x].val);
        entries[x].name = makeword(entries[x].val,'=');
        }
//Reads in the data, breaking at the "&" symbols
    printf("<H1>Query Results</H1>"); 
//Sends the top of the return HTML document.
    printf("You submitted the following name/value pairs:<p>%c",10);
    printf("<ul>%c",10);
    for(x=0; x <= m; x++)
        printf("<li> <code>%s = %s</code>%c",entries[x].name,
               entries[x].val,10);
//Lists the fields in the original form with the values filled in by 
//the customer.
    printf("</ul>%c",10);
}

This listing is the helper program echo2.c

Get Apache: The Definitive Guide, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.