/*******************************************************************************/
/* */
/* Copyright 2005 Pascal Gloor <pascal.gloor@spale.com> */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); */
/* you may not use this file except in compliance with the License. */
/* You may obtain a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* */
/*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <libcgi.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
void dumpdata(char*, size_t);
int main()
{
CGI_CTX *cgictx; /* cgi context */
CGI_VAR *myvar; /* variable result */
struct sockaddr_in *peer; /* remote peer information */
int flags = 0; /* options for CGI_Parse */
int vars; /* number of parsed vars */
size_t max_send = 100000; /* maximum length of HTTP POST (default 1Mb) */
/* some html */
printf("Content-type: text/html\n\n<html><title>LIBCGI DEMO</title><body>\n");
if ( ( cgictx = CGI_Init() ) == NULL )
{
printf("error in CGI_Init()\n");
exit(EXIT_FAILURE);
}
printf("cgictx initialized.<br>\n");
vars = CGI_Parse(cgictx, flags, max_send);
printf("CGI_Parse found %i variables.<br>\n",vars);
/* who is the peer ? */
peer = CGI_Peer(cgictx);
printf("Remote peer is %s port %u<br>\n",inet_ntoa(peer->sin_addr),ntohs(peer->sin_port));
/* ensure we start browsing at the first variable */
CGI_FirstVar(cgictx);
while((myvar=CGI_NextVar(cgictx))!=NULL)
{
printf("<pre>VARIABLE (enclosed in ><)\n");
printf("========\n");
if ( myvar->name != NULL )
printf("name : >%s<\n",myvar->name);
if ( myvar->filename != NULL )
printf("filename : >%s<\n",myvar->filename);
if ( myvar->ctype != NULL )
printf("content-type : >%s<\n",myvar->ctype);
printf("value length : %d\n",myvar->value_len);
if ( myvar->value != NULL )
dumpdata(myvar->value,myvar->value_len);
printf("</pre><br>\n");
}
CGI_End(cgictx);
printf("</body></html>\n");
fflush(stdout);
fclose(stdout);
return 0;
}
void dumpdata(char *buf, size_t len)
{
size_t i;
printf("data hex dump:\n");
for(i=0; i<len; ++i)
{
if ( i && !(i % 16) )
printf("\n");
else if ( i && !(i % 4) )
printf(" ");
if ( !(i%16) && i+1 < len )
printf(" 0x%06x: ",i);
printf("%02x ",(unsigned char)*(buf+i));
}
printf("\n");
}