Ricardo Peres wrote:

> I have some questions for you gurus regarding CGI programming and
> PostgreSQL databases:
> 
> 1-What is the simplest way to convert an "urlencoded" string
> (resultant from a HTTP POST or GET) into an ascii string? I have written a
> couple of functions do to that, but I'm have certain doubts as to their
> simplicity...

You need to first split the string into its components according to
the presence of & and = characters. Then, replace all + characters by
spaces and all hex sequences (%XX) with the characters which they
represent.

It is possible to perform all of these steps simultaneously. The main
thing to bear in mind is that only literal & and = characters split
the string into its components. The URL-encoded forms (%26 and %3D
respectively) correspond to the presence of literal & or = characters
in the variable or the value.

The attached lex program will parse a URL-encoded string (from stdin)
and emit a sequence of shell commands which define environment
variables.

-- 
Glynn Clements <[EMAIL PROTECTED]>


%pointer

%option 8bit

%s NAME
%s VALUE

HEX     [0-9A-Fa-f]

%{

static int yywrap(void)
{
        return 1;
}

static int digitval(int c)
{
        switch (c)
        {
        case '0': case '1': case '2': case '3': case '4':
        case '5': case '6': case '7': case '8': case '9':
                return (c - '0');

        case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
                return (c - 'A' + 10);

        case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
                return (c - 'a' + 10);

        default:
                fprintf(stderr, "Invalid hex digit '%c'\n", c);
                return 0;
        }
}

static int hexcode(const char *p)
{
        return digitval(p[0]) * 0x10 + digitval(p[1]);
}

#define OUTCHR(c)       fputc((c), yyout)
#define OUTSTR(s)       fputs(s, yyout);

%}

%%

<INITIAL>.|\n           { yyless(0); OUTSTR("export CGI_"); BEGIN(NAME); }

<NAME>"%"{HEX}{HEX}"%"  OUTCHR(hexcode(yytext + 1));
<NAME>"%"{HEX}{HEX}     return -1;
<NAME>"%"{HEX}          return -1;
<NAME>"%"               return -1;
<NAME>"+"               OUTCHR(' ');
<NAME>"&"               { OUTSTR(";\n"); BEGIN(INITIAL); }
<NAME>"="               { OUTSTR("='"); BEGIN(VALUE); }
<NAME>.|\n              OUTCHR(yytext[0]);
<NAME><<EOF>>           OUTSTR(";\n");

<VALUE>"%"{HEX}{HEX}"%" OUTCHR(hexcode(yytext + 1));
<VALUE>"%"{HEX}{HEX}    return -1;
<VALUE>"%"{HEX}         return -1;
<VALUE>"%"              return -1;
<VALUE>"+"              OUTCHR(' ');
<VALUE>"&"              { OUTSTR("';\n"); BEGIN(INITIAL); }
<VALUE>.|\n             OUTCHR(yytext[0]);
<VALUE><<EOF>>          { OUTSTR("';\n"); return 0; }

%%

int main(int argc, char **argv)
{
        return yylex();
}

Reply via email to