Branch: Tag:

2001-10-02

2001-10-02 13:21:31 by Per Hedbor <ph@opera.com>

The header parser now allocates memory dynamically. Slightly slower, but way more flexible

Rev: src/modules/_Roxen/roxen.c:1.22

42:   #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;
63:       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 );
161:    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;
248:    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;
408:    push_string( low_end_shared_string( res ) );    }   } +    /*! @endmodule    */   
424:       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 );