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

version» Context lines:

pike.git/src/modules/_Roxen/roxen.c:1:   /*   || This file is part of Pike. For copyright information see COPYRIGHT.   || Pike is distributed under GPL, LGPL and MPL. See the file COPYING   || for more information. - || $Id: roxen.c,v 1.41 2005/10/27 16:54:10 grubba Exp $ + || $Id: roxen.c,v 1.42 2005/11/28 16:55:46 per Exp $   */      #define NO_PIKE_SHORTHAND      #include "global.h"   #include "config.h"         #include "machine.h"   
pike.git/src/modules/_Roxen/roxen.c:40:      /*! @module _Roxen    */      /*! @class HeaderParser    */      #define THP ((struct header_buf *)Pike_fp->current_storage)   struct header_buf   { -  char *headers; -  char *pnt; +  unsigned char *headers; +  unsigned char *pnt;    ptrdiff_t hsize, left; -  int slash_n, spc; +  int slash_n, tslash_n, spc;   };      static void f_hp_init( struct object *o )   {    THP->headers = NULL;    THP->pnt = NULL;    THP->hsize = 0;    THP->left = 0; -  THP->spc = THP->slash_n = 0; +  THP->spc = THP->slash_n = THP->tslash_n = 0;   }      static void f_hp_exit( struct object *o )   {    if( THP->headers )    free( THP->headers );    THP->headers = NULL;    THP->pnt = NULL;    THP->hsize = 0;   }      static void f_hp_feed( INT32 args )   /*! @decl array(string|mapping) feed(string data)    */   {    struct pike_string *str = Pike_sp[-1].u.string;    struct header_buf *hp = THP;    int str_len; -  int tot_slash_n=hp->slash_n, slash_n = 0, spc = hp->spc; -  char *pp,*ep; +  int tot_slash_n=hp->slash_n, slash_n = hp->tslash_n, spc = hp->spc; +  unsigned 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->size_shift )    Pike_error("Wide string headers not supported\n");    str_len = str->len;    while( str_len >= hp->left )    { -  char *buf; -  if( THP->hsize > 512 * 1024 ) +  unsigned char *buf; +  if( hp->hsize > 512 * 1024 )    Pike_error("Too many headers\n"); -  THP->hsize += 8192; -  buf = THP->headers; -  THP->headers = realloc( THP->headers, THP->hsize ); -  if( !THP->headers ) +  hp->hsize += 8192; +  buf = hp->headers; +  hp->headers = realloc( hp->headers, hp->hsize ); +  if( !hp->headers )    {    free(buf); -  THP->hsize = 0; -  THP->left = 0; -  THP->spc = THP->slash_n = 0; -  THP->pnt = NULL; +  hp->hsize = 0; +  hp->left = 0; +  hp->spc = hp->slash_n = 0; +  hp->pnt = NULL;    Pike_error("Running out of memory in header parser\n");    } -  THP->left += 8192; -  THP->pnt = (THP->headers + THP->hsize - THP->left); +  hp->left += 8192; +  hp->pnt = (hp->headers + hp->hsize - hp->left);    }       MEMCPY( hp->pnt, str->str, str_len );    pop_n_elems( args );       /* FIXME: The below does not support lines terminated with just \r. */    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; +  if( *pp == ' ' ) +  { +  spc++; +  slash_n = 0; +  } +  else if( *pp == '\n' ) +  { +  slash_n++; +  tot_slash_n++; +  } +  else if( *pp != '\r' ) +  { +  slash_n=0; +  }       hp->slash_n = tot_slash_n;    hp->spc = spc; -  +  hp->tslash_n = slash_n;    hp->left -= str_len;    hp->pnt += str_len;    hp->pnt[0] = 0;       if( slash_n != 2 )    {    /* one newline, but less than 2 space,    * --> HTTP/0.9 or broken request    */    if( (spc < 2) && tot_slash_n )    { -  push_constant_text( "" ); +  push_empty_string();    /* This includes (all eventual) \r\n etc. */ -  push_text( hp->headers ); +  push_text((char *)hp->headers);    f_aggregate_mapping( 0 );    f_aggregate( 3 );    return;    }    push_int( 0 );    return;    }    -  push_string( make_shared_binary_string( pp, hp->pnt - pp ) ); /*leftovers*/ +  /*leftovers*/ +  push_string(make_shared_binary_string((char *)pp, hp->pnt - pp));    headers = allocate_mapping( 5 );    in = hp->headers;    l = pp - hp->headers;       /* find first line here */    for( i = 0; i < l; i++ ) -  if((in[i] == '\n') || (in[i] == '\r')) +  if( (in[i] == '\n') || (in[i] == '\r') )    break;    -  push_string( make_shared_binary_string( in, i ) ); +  push_string(make_shared_binary_string((char *)in, i));       if((in[i] == '\r') && (in[i+1] == '\n'))    i++;    i++;       in += i; l -= i;       /* Parse headers. */    for(i = 0; i < l; i++)    {
pike.git/src/modules/_Roxen/roxen.c:245:   /*! @endclass    */      static void f_make_http_headers( INT32 args )   /*! @decl string @    *! make_http_headers(mapping(string:string|array(string)) headers, @    *! int(0..1)|void no_terminator)    */   {    int total_len = 0, e; -  char *pnt; +  unsigned char *pnt;    struct mapping *m;    struct keypair *k;    struct pike_string *res;    int terminator = 2;       if( Pike_sp[-args].type != PIKE_T_MAPPING )    Pike_error("Wrong argument type to make_http_headers(mapping heads)\n");    m = Pike_sp[-args].u.mapping;       if (args > 1) {
pike.git/src/modules/_Roxen/roxen.c:289:    else    total_len += kl + a->item[i].u.string->len + 2;    } else    Pike_error("Wrong argument type to make_http_headers("    "mapping(string(8bit):string(8bit)|"    "array(string(8bit))) heads)\n");    }    total_len += terminator;       res = begin_shared_string( total_len ); -  pnt = (char *)res->str; - #define STRADD(X)\ -  for( l=X.u.string->len,s=X.u.string->str,c=0; c<l; c++ )\ -  *(pnt++)=*(s++); +  pnt = STR0(res); + #define STRADD(X) \ +  for( l=(X).u.string->len, s=STR0((X).u.string), c=0; c<l; c++ ) \ +  *(pnt++)=*(s++)       NEW_MAPPING_LOOP( m->data )    { -  char *s; +  unsigned char *s;    ptrdiff_t l, c;    if( k->val.type == PIKE_T_STRING )    {    STRADD( k->ind ); *(pnt++) = ':'; *(pnt++) = ' ';    STRADD( k->val ); *(pnt++) = '\r'; *(pnt++) = '\n';    }    else    {    struct array *a = k->val.u.array;    ptrdiff_t i, kl = k->ind.u.string->len + 2;
pike.git/src/modules/_Roxen/roxen.c:398:    *dest=(((bar[1]<'A')?(bar[1]&15):((bar[1]+9)&15))<<4)|    ((bar[2]<'A')?(bar[2]&15):((bar[2]+9)&15));    else    *dest=0;    bar+=3;    }    } else {    *dest=*(bar++);    }    } else { -  foo = newstr->str; +  foo = STR0(newstr);    for (proc=0; bar<end; foo++)    if (*bar=='%') {    if (bar[1] == 'u' || bar[1] == 'U') {    /* We know that the following two characters are zeros. */    bar+=3;    }    if (bar<end-2)    *foo=(((bar[1]<'A')?(bar[1]&15):((bar[1]+9)&15))<<4)|    ((bar[2]<'A')?(bar[2]&15):((bar[2]+9)&15));    else
pike.git/src/modules/_Roxen/roxen.c:547:    pop_stack();    push_string( low_end_shared_string( res ) );    }   }      /*! @endmodule    */      PIKE_MODULE_INIT   { -  pike_add_function("make_http_headers", f_make_http_headers, -  "function(mapping(string:string|array(string)), int|void:string)", 0 ); +  ADD_FUNCTION("make_http_headers", f_make_http_headers, +  tFunc(tMap(tStr,tOr(tStr,tArr(tStr))) tOr(tInt01,tVoid), tStr), +  0);    -  pike_add_function("http_decode_string", f_http_decode_string, -  "function(string:string)", 0 ); +  ADD_FUNCTION("http_decode_string", f_http_decode_string, +  tFunc(tStr,tStr), 0 );    -  pike_add_function("html_encode_string", f_html_encode_string, -  "function(mixed:string)", 0 ); +  ADD_FUNCTION("html_encode_string", f_html_encode_string, +  tFunc(tMix,tStr), 0 );       start_new_program();    ADD_STORAGE( struct header_buf );    set_init_callback( f_hp_init );    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 ); +  ADD_FUNCTION( "feed", f_hp_feed, tFunc(tStr,tArr(tOr(tStr,tMapping))), 0 ); +  ADD_FUNCTION( "create", f_hp_create, tFunc(tNone,tVoid), ID_STATIC );    end_class( "HeaderParser", 0 );   }      PIKE_MODULE_EXIT   {   }