File:  [ELWIX - Embedded LightWeight unIX -] / libaitwww / example / test.c
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Sat Mar 10 00:26:49 2012 UTC (12 years, 8 months ago) by misho
Branches: MAIN
CVS tags: www1_3, www1_2, www1_1, WWW1_2, WWW1_1, WWW1_0, HEAD
version 1.0

#include <stdio.h>
#include <stdlib.h>
#include <aitwww.h>


static int
showPair(struct tagCGI *c, void *arg)
{
	if (arg)
		printf("ARG=%s ... ", arg);

	printf("next=%p ... ", c->cgi_node.sle_next);
	printf("name=%s value=%s\n", c->cgi_name, c->cgi_value);
	return 0;
}


int
main(int argc, char **argv)
{
	int ret;
	cgi_t *cgi, *cgi1;

	if (argc < 2) {
		/* GET test */
		putenv("QUERY_STRING=test1=abra+kadabra&testche=piuk%20mdaa");
		putenv("CONENT_LENGTH=390");
		putenv("REQUEST_METHOD=GET");
		cgi = www_initCGI();
		if (!cgi) {
			printf("Error:: #%d - %s\n", www_GetErrno(), www_GetError());
			return 1;
		}

		printf("%s\n%s\n", www_getValue(cgi, "testche"), www_getValue(cgi, "test1"));

		cgi1 = www_parseQuery("blahA=this is a string&blahB=this is another string");
		if (!cgi1) {
			printf("Error:: #%d - %s\n", www_GetErrno(), www_GetError());
			www_closeCGI(&cgi);
			return 2;
		}
		printf("\n%s\n%s\n", www_getValue(cgi1, "blahA"), www_getValue(cgi1, "blahB"));

		printf("cgi num pairs=%d\n", www_listPairs(cgi, showPair, ".oOo."));

		www_closeCGI(&cgi);

		www_addValue(cgi1, "oho", "boho");
		printf("\n%s\n", www_getValue(cgi1, "oho"));
		www_delPair(cgi1, "oho");
		printf("\n%s\n", www_getValue(cgi1, "oho"));
		www_delPair(cgi1, "oho");

		printf("cgi1 num pairs=%d\n", www_listPairs(cgi1, showPair, NULL));

		www_closeCGI(&cgi1);
	} else {
		/* POST test */
		putenv("REQUEST_METHOD=POST");
		putenv("CONTENT_LENGTH=1000");
		if (!strcmp(argv[1], "multi"))
			putenv("CONTENT_TYPE=multipart/form-data; boundary=012345678909876543210");
		else
			putenv("CONTENT_TYPE=application/x-www-form-urlencoded");

		cgi = www_initCGI();
		if (!cgi) {
			printf("Error:: #%d - %s\n", www_GetErrno(), www_GetError());
			return 1;
		}

		printf("blah=%s\n", www_getValue(cgi, "blah"));

		printf("num pairs=%d\n", www_listPairs(cgi, showPair, argv[1]));

		www_closeCGI(&cgi);
	}
	return 0;
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>