Thread: decode the querystring

  1. #1
    Unregistered
    Guest

    decode the querystring

    Can anyone help me with this?
    I can't get the correct result, and don't know what is wrong.

    After the program runs, the myName variable is supposed to contain the value I pass to the cgi program

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

    // Helper macro to convert two-character hex strings to character value
    #define ToHex(Y) (Y>='0'&&Y<='9'?Y-'0':Y-'A'+10)

    char InputData[4096];

    void getAllParams() {
    // Determing if it is a POST or GET method
    if( getenv( "REQUEST_METHOD" ) == 0 ) {
    printf("No REQUEST_METHOD, must be running in DOS mode");
    return;
    } else if (strcmp( getenv("REQUEST_METHOD"), "POST") == 0) {
    // If POST
    char *endptr; // quite useless, but required
    char *len1 = getenv("CONTENT_LENGTH");
    int contentlength = strtol(len1, &endptr, 10);
    fread(InputData , contentlength, 1, stdin);
    } else {
    // If GET
    strcpy(InputData, getenv("QUERY_STRING"));
    }
    }

    void getParam(const char *Name, char *Dest, char *Default) {
    char *pArg = strstr(InputData, Name);

    if (pArg) {
    pArg += strlen(Name);

    if (*pArg == '=') { // Make sure there is an '=' where we expect it
    pArg++;

    while (*pArg && *pArg != '&') {
    if (*pArg == '%') { // Convert it to a single ASCII character and store at our destination
    Dest += (char)ToHex(pArg[1]) * 16 + ToHex(pArg[2]);
    pArg += 3;
    } else if( *pArg=='+' ) { // If it's a '+', store a space at our destination
    Dest += ' ';
    pArg++;
    } else {
    Dest += *pArg++; // Otherwise, just store the character at our destination
    }
    }

    return;
    }

    }
    Dest = Default; // If param not found, then use default parameter
    return;
    }

    int main() {

    char *myName = "";

    printf("Content-Type:text/html \n\n");
    getAllParams();
    getParam("Name", myName, "No Name");
    printf("QueryString: %s", InputData);
    printf("<br>");
    printf("Name: %s", myName);

    return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this.
    Code:
    void getParam(const char *Name, char *Dest, char *Default) { 
    char *pArg = strstr(InputData, Name); 
    
    if (pArg) { 
    pArg += strlen(Name); 
    
    if (*pArg == '=') { // Make sure there is an '=' where we expect it 
    pArg++; 
    
    while (*pArg && *pArg != '&') { 
    if (*pArg == '%') { // Convert it to a single ASCII character and store at our destination 
    *Dest++ = (char)ToHex(pArg[1]) * 16 + ToHex(pArg[2]); 
    pArg += 3; 
    } else if( *pArg=='+' ) { // If it's a '+', store a space at our destination 
    *Dest++ = ' '; 
    pArg++; 
    } else { 
    *Dest++ = *pArg++; // Otherwise, just store the character at our destination 
    } 
    } 
    
    *Dest++ = '\0';
    return; 
    } 
    
    } 
    Dest = Default; // If param not found, then use default parameter 
    return; 
    }
    Also in main()
    Code:
    int main() { 
    
    >char *myName = ""; 
    //Change this to:
    char myName[80] = "";

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encode and Decode algorith for 2 to 7 bits
    By kingofpop in forum C Programming
    Replies: 2
    Last Post: 11-15-2008, 07:08 AM
  2. File I/O Encode & Decode with XOR
    By DarrenY in forum C# Programming
    Replies: 2
    Last Post: 04-12-2007, 04:31 AM
  3. decode data and screen refresh
    By jérèm in forum C Programming
    Replies: 3
    Last Post: 12-16-2005, 02:43 AM
  4. Request QueryString Value for CGI style app
    By Tarran in forum C Programming
    Replies: 17
    Last Post: 01-03-2005, 01:56 PM
  5. suggestions : encode / decode header + documentation
    By gamer in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2004, 05:17 AM