POSTing files (server side)

Jay C. Weber (weber@eit.COM)
Thu, 4 Aug 94 12:19:09 PDT


Especially when testing my PostMosaic patches, you may be
interested in this CGI C program that reflects the POSTed
document.

It uses the CGI library I announced a couple of weeks ago,
still available as source and binaries in

ftp://ftp.eit.com/pub/wsk/<platform>/libcgi

or you could cut out the frills and compile without it.

Jay

Source follows:

#include<stdio.h>
#include"libcgi/cgi.h"

show(char *format, char *s) {
printf(format, s ? s : "");
}

cgi_main(cgi_info *ci) {
char *buf;
int nitems;

print_mimeheader("text/html");

puts("<title>CGI Echo</title>");

switch(mcode(ci)) {

case MCODE_HEAD:
return;

case MCODE_POST:
puts("<h1>POSTed document</h1>");
puts("<pre>");
nitems = fread(buf=malloc(ci->content_length), 1, ci->content_length, stdin);
fwrite(buf, 1, nitems, stdout);
free(buf);
puts("</pre>");
puts("<h1>CGI Variables</h1>");
puts("<pre>");
printf("CONTENT_LENGTH = '%d'\n", ci->content_length);
printf("CONTENT_TYPE = %s\n", ci->content_type);
puts("</pre>");

case MCODE_GET:
puts("<pre>");
show("REQUEST_METHOD = '%s'\n", ci->request_method);
show("SERVER_SOFTWARE = '%s'\n", ci->server_software);
show("SERVER_PROTOCOL = '%s'\n", ci->server_protocol);
show("SERVER_PORT = '%s'\n", ci->server_port);
show("SERVER_NAME = '%s'\n", ci->server_name);
show("GATEWAY_INTERFACE = '%s'\n", ci->gateway_interface);
show("HTTP_ACCEPT = '%s'\n", ci->http_accept);
show("PATH_INFO = '%s'\n", ci->path_info);
show("PATH_TRANSLATED = '%s'\n", ci->path_translated);
show("SCRIPT_NAME = '%s'\n", ci->script_name);
show("QUERY_STRING = '%s'\n", ci->query_string);
show("REMOTE_HOST = '%s'\n", ci->remote_host);
show("REMOTE_ADDR = '%s'\n", ci->remote_addr);
show("REMOTE_USER = '%s'\n", ci->remote_user);
show("REMOTE_IDENT = '%s'\n", ci->remote_ident);
show("AUTH_TYPE = '%s'\n", ci->auth_type);
printf("</pre>\n");
break;

default:
printf("Unrecognized method '%s'.\n", ci->request_method);
}
}