Exemple #1
0
static const char *
parse_req(char *reqpath)
{
    static char buf[8192];        /* static variables are not on the stack */

    if (fgets_trim(&buf[0], sizeof(buf), cur_f) < 0)
        return "Socket IO error";

    /* Parse request like "GET /foo.html HTTP/1.0" */
    char *sp1 = strchr(&buf[0], ' ');
    if (!sp1)
        return "Cannot parse HTTP request (1)";
    *sp1 = '\0';
    sp1++;

    char *sp2 = strchr(sp1, ' ');
    if (!sp2)
        return "Cannot parse HTTP request (2)";
    *sp2 = '\0';
    sp2++;

    /* We only support GET requests */
    if (strcmp(&buf[0], "GET"))
        return "Non-GET request";

    /* Decode URL escape sequences in the requested path into reqpath */
    url_decode(sp1, reqpath);

    /* Parse out query string, e.g. "foo.py?user=bob" */
    char *qp = strchr(reqpath, '?');
    if (qp) {
        *qp = '\0';
        setenv("QUERY_STRING", qp+1, 1);
    }

    /* Now parse HTTP headers */
    for (;;) {
        if (fgets_trim(&buf[0], sizeof(buf), cur_f) < 0)
            return "Socket IO error";

        if (buf[0] == '\0')        /* end of headers */
            break;

        /* Parse things like "Cookie: foo bar" */
        char *sp = strchr(&buf[0], ' ');
        if (!sp)
            return "Header parse error (1)";
        *sp = '\0';
        sp++;

        /* Strip off the colon, making sure it's there */
        if (strlen(buf) == 0)
            return "Header parse error (2)";

        char *colon = &buf[strlen(buf) - 1];
        if (*colon != ':')
            return "Header parse error (3)";
        *colon = '\0';

        /* Set the header name to uppercase */
        for (int i = 0; i < strlen(buf); i++)
            buf[i] = toupper(buf[i]);

        /* Decode URL escape sequences in the value */
        char value[256];
        url_decode(sp, &value[0]);

        /* Store header in env. variable for application code */
        char envvar[256];
        sprintf(&envvar[0], "HTTP_%s", buf);
        setenv(envvar, value, 1);
    }

    return NULL;
}
Exemple #2
0
/*
 *  Stack test routine
 */
void stk_test( void )
{
#define CSIZE 80
 char opt[CSIZE];
 char* item;
 int n;
 Stack stack= NULL;
 strcpy( opt, " " );
 while( *opt != 'q' ) {
  printf( "stk: " );
  fscanf( stdin, "%s", opt );
  if ( !strcmp( opt, " " ) ) {
   ;
  } else if ( !strcmp( opt, "build" ) ) {
   fgets_trim( opt, CSIZE, stdin );
   stack = stk_build( opt );
   printf( " \n" );
  } else if ( !strcmp( opt, "close" ) ) {
   stk_close( stack );
   stack = NULL;
   printf( "Close\n" );
  } else if ( !strcmp( opt, "echo" ) ) {
   fgets_trim( opt, CSIZE, stdin );
   printf( "%s\n", opt );
  } else if ( !strcmp( opt, "append" ) ) {
   fgets_trim( opt, CSIZE, stdin );
   stk_append( stack, opt );
   printf( "Rewind\n" );
  } else if ( !strcmp( opt, "disp" ) ) {
   stk_disp( stack );
  } else if ( !strcmp( opt, "rewind" ) ) {
   stk_rewind( stack );
   printf( "Rewind\n" );
  } else if ( !strcmp( opt, "read" ) ) {
   fgets_trim( opt, CSIZE, stdin );
   n = atoi( opt );
   item = stk_read_num( stack, n );   
   printf( "Item %4d = %s\n", n, item );
   free( item );
  } else if ( !strcmp( opt, "next" ) ) {
   item = stk_read_next( stack );
   n = stk_current( stack );
   printf( "Item %4d = %s\n", n, item );
   free( item );
  } else if ( !strcmp( opt, "delete" ) ) {
    fgets_trim( opt, CSIZE, stdin );
    n = atoi( opt );
    if ( n >= 1 )
      stk_delete_num( stack, n );
    else
      stk_delete_current(stack );
  } else if ( !strcmp( opt, "set" ) ) {
    fgets_trim( opt, CSIZE, stdin );
    n = atoi( opt );
    stk_set_current( stack, n );
  } else if ( !strcmp( opt, "change" )) {
    fgets_trim( opt, CSIZE, stdin );
    stk_change_current( stack, opt );
  } else if ( strcmp( opt, "q" ) ) {
    printf( "Unknown opt\n" );
  } 

 }
 printf( "\n" );
}