FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Cashew CGI

 
Post new topic   Reply to topic     Forum Index -> Cashew
View previous topic :: View next topic  
Author Message
mmcdermo



Joined: 09 Nov 2006
Posts: 18

PostPosted: Tue Nov 28, 2006 2:02 pm    Post subject: Cashew CGI Reply with quote

I'm currently working on a web-based project using D, and while exploring the CGI route I've come up with some CGI functions. Error handling isn't particularly elegant, and there's no real Object-Oriented-goodness inside this code, but I hope all the same that it will save someone some time, and maybe get transformed into something suitable for Cashew.

I've made my own URL Encoding & Unencoding functions, but since I believe that's already been developed for Cashew, I won't repost my own.

Note about my code:
Any // comment after an end bracket } indicates which block of code that bracket ends. For instance,
Code:
 
   if(foo){
     bar();
   }//if foo



Code:
   
//Imports
   import std.stdio; //writefln
   import std.string;
   import std.conv; //toInt

   import std.c.stdio; //fgets

//Aliases
    alias char[] string; //ease of use

//C
    extern (C) char* getenv(char*);

//Receiving Data   
      char[] getPOSTData(uint maxlen = 2048){
            //Buffer info
            char dataBuffer[512];
            long bufferSize = dataBuffer.length;
            
            //Define some pointers
            char *lenstr;
            char *pblank;
            char[] error;
            
            //Variable to hold lenstr in int form
            long len;
            
            lenstr = getenv("CONTENT_LENGTH");
            
            //Test for null length
            if(lenstr == pblank){
               //throw error
               error = "No POST data";
               return error;
            }
            else {
               //copy lenstr into len with format width=long, type = decimal integer
               sscanf(lenstr,"?ld",&len);
            }
            if( len > maxlen ){
               error = "POST data has exceeded size limits";
               return error;
            }
            else {
               char *ptmpData;
               char [] tempString;
               char [] dataStorage;
               
               //While stream pointer is at a valid location, [stdin has more data to give us]
               while(fgets(dataBuffer, bufferSize, stdin)){
                  //Set pointer to buffer
                  ptmpData = dataBuffer;         
                  
                  //Get the contents of the buffer into tempString
                  tempString = toString(ptmpData);
                  
                  //Append tempString to dataStorage
                  dataStorage ~= tempString;
               }   
               if(dataStorage.length >= len){
                  dataStorage.length = len - 1;
               }
               return dataStorage;
              }   
         }

   string[string] fetchPOST(){
      char *pdataLength= std.c.stdlib.getenv("CONTENT_LENGTH");
      char *pblank;
      string[string] postData; //associative array
      if(pdataLength == pblank){
         postData["error"] = "No content.";
         return postData;
      }
      string sdataLength = toString(pdataLength);
      int dataLength = std.conv.toInt(sdataLength);

      //Get data
         string dataString = getPOSTData();
         
      //split data into pairs and then into values
         string[] tmpPOSTDataArr = split(dataString, "&"); //pairs
         
         if(tmpPOSTDataArr.length == 0) { //just one value being passed
            string[] pair = split(dataString, "="); //values
            if(pair.length < 2) {
               //throw error
               postData["error"] = "Malformed POST content";
            }
            else {
               //insert data into postData array as postData[key] = value
               postData[pair[0]] = pair[1];
            }
         }//if there is only one value being passed

         else{ //multiple values being passed
         foreach(tmpPOSTDataPair; tmpPOSTDataArr) {
            string[] pair = split(tmpPOSTDataPair, "="); //values
            if(pair.length < 2) {
               //throw error
               postData["error"] = "Malformed POST content";
            }
            else {
               //insert data into postData array as postData[key] = value
               postData[pair[0]] = pair[1];
            }
         }//foreach
         }//else
      return postData;
   }//fetchPOST()

   string[string] fetchGET(){
      string[string] GETData;
      char *data = std.c.stdlib.getenv("QUERY_STRING");
      char *pblank;
      if(data == pblank){
         GETData["error"] = "No GET Data.";
         return GETData;
      }
      else{
      string sdata;
      sdata = toString(data);
      
      //split sdata by &'s, then into key-value pairs and assign to GETVars;
         string[] tmpGETDataArr = split(sdata, "&"); //pairs
         
         if(tmpGETDataArr.length == 0) { //just one value being passed
            string[] pair = split(sdata, "="); //values
            if(pair.length < 2) {
               //throw error
               GETData["error"] = "Malformed GET content.";
               return GETData;
            }
            else {//insert data into GETData array as GETData[key] = value
               GETData[pair[0]] = pair[1];
            }
         }//if there is only one value being passed
         
         else{ //multiple values being passed
         foreach(tmpGETDataPair; tmpGETDataArr) {
            string[] pair = split(tmpGETDataPair, "="); //values
            if(pair.length < 2) {
               //throw error
               GETData["error"] = "Malformed GET content";
               return GETData;
            }
            else {
               //insert data into GETData array as GETData[key] = value
               GETData[pair[0]] = pair[1];
            }
         }//foreach
         }//else
      return GETData;
      }//else -- there IS GET data
   }//fetchGet()
Back to top
View user's profile Send private message Send e-mail
csauls



Joined: 27 Mar 2004
Posts: 278

PostPosted: Tue Nov 28, 2006 7:53 pm    Post subject: Reply with quote

Thanks! I'll give it a look-over and see what can be done with it. There is URL encoding/decoding but you may as well compare it with yours if you like, just to see if I've done anything silly. (It happens.)
_________________
Chris Nicholson-Sauls
Back to top
View user's profile Send private message AIM Address Yahoo Messenger
mmcdermo



Joined: 09 Nov 2006
Posts: 18

PostPosted: Wed Nov 29, 2006 1:48 am    Post subject: Reply with quote

Always happy to help out Smile.. Just let me know if I can assist you further in any way.
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Cashew All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group