/*-------------------------------------------------------- CGI Variable Wrapper http://www.purplepixie.org/cgi * Read the Licence * dcutting [at] purplepixie.org cgi_interface.cpp 30/03/2007 2.07 --------------------------------------------------------*/ #ifndef CGI_MAIN #define CGI_MAIN #include #include #include #include "cgi_interface.h" CGI_Variable::CGI_Variable(const char *iName, const char *iValue) { strcpy(Name,iName); strcpy(Value,iValue); Next=0; } char *CGI_Variable::Encode(void) { int EncPtr=0; int InpPtr=0; int InpMax=strlen(Value); for (InpPtr=0; InpPtr\n",method); return -1; } if (strcmp("GET",method)==0) return StringLoad(getenv("QUERY_STRING")); else if (strcmp("POST",method)==0) { int count=0; char buff[1024*32]; fgets(buff,1024*32,stdin); while (!feof(stdin)) { count+=StringLoad(buff); fgets(buff,1024*32,stdin); } if (strlen(buff)>0) count+=StringLoad(buff); return count; } else { if (PrintError) printf("CGI Error: Unsupported Method %s\n
\n",method); return -1; } } int CGI_Interface::CookieLoad(void) { // New in 2005 versions (2.06+) - might still not work properly :o( char ts[256]; for (int a=0; a<256; a++) ts[a]=0; char *cookies=getenv("HTTP_COOKIE"); int loaded=0; int tptr=0; for (int cpos=0; cpos0) StringLoad(ts); return loaded; } bool CGI_Interface::Add(const char *name, const char *value) { CGI_Variable *nv=new CGI_Variable(name,value); if (First==0) { First=nv; return true; } CGI_Variable *c=First; while (c->Next!=0) c=c->Next; c->Next=nv; return true; } CGI_Interface::~CGI_Interface() { if (First==0) return; CGI_Variable *c=First; while (c->Next!=0) { CGI_Variable *d=c; c=c->Next; delete d; } delete c; } int CGI_Interface::StringLoad(const char *input) { if (input==0) return -1; if (strlen(input)<=0) return -1; int count=0; char cname[LENMAX_VAR_NAME]; char cvalue[LENMAX_VAR_VALUE]; int pos=0; bool name=true; long val=0; char d[5]; for (int a=0; a0) { Set(cname,cvalue); count++; } return count; } void CGI_Interface::PrintDump(void) { printf("CGI Interface Variable Dump:

\n\n"); CGI_Variable *c=First; int count=0; while (c!=0) { printf("%d: %s: %s
\n",count+1,c->Name,c->Value); c=c->Next; count++; } printf("\n

\nTotal of %d Variables Found\n

\n",count); } char* CGI_Interface::Fetch(const char *name, bool UseReturnValidPointer=true) { CGI_Variable *c; c=First; while (c!=0) { if ( strcmp(c->Name,name)==0) return c->Value; c=c->Next; } if (!UseReturnValidPointer) return 0; if (ReturnValidPointer) return NullString; return 0; } int CGI_Interface::Set(const char *name, const char *value) { char *c=Fetch(name,false); if (c==0) Add(name,value); else strcpy(c,value); return 1; } int CGI_Interface::HiddenDump(void) { CGI_Variable *c=First; int count=0; while (c!=0) { printf("Name); for (int a=0; aValue); a++) { if (c->Value[a]=='\"') printf("%s","%22"); else printf("%c",c->Value[a]); } printf("\">\n"); count++; c=c->Next; } return count; } int CGI_Interface::GetDump(void) { CGI_Variable *c=First; int count=0; while (c!=0) { if (count>0) printf("&"); printf("%s=%s",c->Name,c->Encode()); count++; c=c->Next; } return count; } #endif