pike.git / src / modules / _Roxen / roxen.c

version» Context lines:

pike.git/src/modules/_Roxen/roxen.c:35:      /*! @module _Roxen    */      /*! @class HeaderParser    */      #define THP ((struct header_buf *)Pike_fp->current_storage)   struct header_buf   { -  char headers[8192]; +  char *headers;    char *pnt; -  ptrdiff_t left; +  ptrdiff_t hsize, left;    int slash_n, spc;   };    -  + static void f_hp_exit( struct object *o ) + { +  if( THP->headers ) +  free( THP->headers ); + } +  + static void f_hp_feed( INT32 args )   /*! @decl array(string|mapping) feed(string data)    */ - static void f_hp_feed( INT32 args ) +    {    struct pike_string *str = Pike_sp[-1].u.string;    struct header_buf *hp = THP;    int tot_slash_n=hp->slash_n, slash_n = 0, spc = hp->spc;    char *pp,*ep;    struct svalue *tmp;    struct mapping *headers;    ptrdiff_t os=0, i, j, l;    unsigned char *in;       if( Pike_sp[-1].type != PIKE_T_STRING )    Pike_error("Wrong type of argument to feed()\n"); -  -  if( str->len >= hp->left ) +  if( str->size_shift ) +  Pike_error("Wide string headers not supported\n"); +  while( str->len >= hp->left ) +  { +  if( THP->hsize > 512 * 1024 )    Pike_error("Too many headers\n"); -  +  THP->hsize += 8192; +  THP->headers = realloc( THP->headers, THP->hsize ); +  if( !THP->headers ) +  { +  THP->hsize = 0; +  THP->left = 0; +  Pike_error("Running out of memory in header parser\n"); +  } +  THP->left += 8192; +  THP->pnt = (THP->headers + THP->hsize - THP->left); +  }       MEMCPY( hp->pnt, str->str, str->len );    pop_n_elems( args );       for( ep=(hp->pnt+str->len),pp=MAXIMUM(hp->headers,hp->pnt-3);    pp<ep && slash_n<2; pp++ )    if( *pp == ' ' ) spc++;    else if( *pp == '\n' ) slash_n++, tot_slash_n++;    else if( *pp != '\r' ) slash_n=0;   
pike.git/src/modules/_Roxen/roxen.c:154:    pop_n_elems(2);    if( in[j+1] == '\n' ) j++;    os = j+1;    i = j;    }    }    push_mapping( headers );    f_aggregate( 3 ); /* data, firstline, headers */   }    + static void f_hp_create( INT32 args )   /*! @decl void create(void)    */ - static void f_hp_create( INT32 args ) +    { -  +  THP->headers = malloc( 8192 );    THP->pnt = THP->headers; -  +  THP->hsize = 8192;    THP->left = 8192;    THP->spc = THP->slash_n = 0;    pop_n_elems(args);    push_int(0);   }    - /*! @endclass -  */ -  + static void f_make_http_headers( INT32 args )   /*! @decl string @    *! make_http_headers(mapping(string:string|array(string)) headers)    */ - static void f_make_http_headers( INT32 args ) +    {    int total_len = 0, e;    char *pnt;    struct mapping *m;    struct keypair *k;    struct pike_string *res;    if( Pike_sp[-1].type != PIKE_T_MAPPING )    Pike_error("Wrong argument type to make_http_headers(mapping heads)\n");       m = Pike_sp[-1].u.mapping;
pike.git/src/modules/_Roxen/roxen.c:241:    }    }    }    *(pnt++) = '\r';    *(pnt++) = '\n';       pop_n_elems( args );    push_string( end_shared_string( res ) );   }    + static void f_http_decode_string(INT32 args)   /*! @decl string http_decode_string(string encoded)    *!    *! Decodes an http transport-encoded string.    */ - static void f_http_decode_string(INT32 args) +    {    int proc;    char *foo,*bar,*end;    struct pike_string *newstr;       if (!args || Pike_sp[-args].type != PIKE_T_STRING)    Pike_error("Invalid argument to http_decode_string(STRING);\n");       foo=bar=Pike_sp[-args].u.string->str;    end=foo+Pike_sp[-args].u.string->len;
pike.git/src/modules/_Roxen/roxen.c:401:    switch( str->size_shift )    {    case 0: REPLACE(unsigned char); break;    case 1: REPLACE(unsigned short); break;    case 2: REPLACE(int); break;    }    pop_stack();    push_string( low_end_shared_string( res ) );    }   } +    /*! @endmodule    */      void pike_module_init()   {    pike_add_function("make_http_headers", f_make_http_headers,    "function(mapping(string:string|array(string)):string)", 0 );       pike_add_function("http_decode_string", f_http_decode_string,    "function(string:string)", 0 );       pike_add_function("html_encode_string", f_html_encode_string,    "function(mixed:string)", 0 );       start_new_program();    ADD_STORAGE( struct header_buf ); -  +  set_exit_callback( f_hp_exit );    pike_add_function( "feed", f_hp_feed, "function(string:array(string|mapping))",0 );    pike_add_function( "create", f_hp_create, "function(:void)", ID_STATIC );    end_class( "HeaderParser", 0 );   }      void pike_module_exit()   {   }