Some more about functions and codes

comment No Comments Written by Anders on April 2, 2008 – 2:53 am

The get function is simpler; the data should be available as the value of the QUERY_STRING environment variable:

void CGI_Helper::HandleGet()
   {
   // Easy, get the string with all input parameters
   // from an environment string
   char *query = getenv(”QUERY_STRING”);
   if(query == NULL) return;
   Process(query);
   }

The post function must allocate a buffer to hold the data (and should free the buffer before the program terminates); when the inputs are form data in the x-www-form-urlencoded representation, the environment variable CONTENT_LENGTH should define the buffer size:

void CGI_Helper::HandlePost()
   {
   char *length = getenv(”CONTENT_LENGTH”);
   if(length == NULL) return;
   int len = atoi(length);
   if(len == 0) return;
   char *buffer = new char[len+1];
   int i =0;
   for(;i    char ch;
   cin.get(ch);
   if(cin.fail()) break;
   buffer[i] = ch;
   }
   buffer[i] = ‘\0′;
   Process(buffer);
   delete [ ] buffer;
   }

The Process function works through virtual functions that allow customization in subclasses:

void CGI_Helper::Process(char *data)
   {
   StartTokens();
   while(*data != ‘\0′) {
   Token *tknext = GetToken(data);
   if(tknext == NULL) break;
   ProcessToken(tknext);
   delete tknext;
   }
   EndTokens();
   }

The GetToken method is a non-virtual function. Its code is defined in the CGI_Helper class; it selects the next name=value& sequence from the string that encodes the form data, and creates a new Token object from the data in this substring. The StartTokens, ProcessToken, and EndTokens functions are all virtual, with empty default definitions.
These are the functions that need to be provided in effective subclasses of CGI_Helper.

Bookmark or Share:
  • E-mail this story to a friend!
  • Technorati
  • StumbleUpon
  • Facebook
  • Google
  • del.icio.us
  • Digg
  • Slashdot
If you enjoyed the article, why not subscribe?

Browse Timeline

Post a Comment

About The Author: Anders

Anders is a freelance graphic designer. He specializes in CSS/XHTML web design and design of print materials including business cards, brochures and flyer’s. You can view his portfolio at andershaig.com.

Want to subscribe?

SEO blog and web design related issues. Subscribe in a reader Or, subscribe via email:
Enter your email address: