e576bb | 2002-10-11 | Martin Nilsson | | |
6f36b9 | 2003-04-14 | Martin Stjernholm | | || $Id: roxen.c,v 1.33 2003/04/14 14:24:03 mast Exp $
|
e576bb | 2002-10-11 | Martin Nilsson | | */
|
33805b | 2000-08-12 | Per Hedbor | | #define NO_PIKE_SHORTHAND
#include "global.h"
#include "config.h"
#include "machine.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <limits.h>
#include "fdlib.h"
#include "stralloc.h"
#include "pike_macros.h"
#include "machine.h"
#include "object.h"
#include "constants.h"
#include "interpret.h"
#include "svalue.h"
#include "mapping.h"
#include "array.h"
#include "builtin_functions.h"
#include "module_support.h"
#include "backend.h"
#include "threads.h"
#include "operators.h"
|
ced8a3 | 2001-02-13 | Henrik Grubbström (Grubba) | |
|
33805b | 2000-08-12 | Per Hedbor | |
|
6b8788 | 2001-06-21 | Martin Stjernholm | | #define THP ((struct header_buf *)Pike_fp->current_storage)
|
33805b | 2000-08-12 | Per Hedbor | | struct header_buf
{
|
2d41d4 | 2001-10-02 | Per Hedbor | | char *headers;
|
33805b | 2000-08-12 | Per Hedbor | | char *pnt;
|
2d41d4 | 2001-10-02 | Per Hedbor | | ptrdiff_t hsize, left;
|
3c64ee | 2001-02-20 | Per Hedbor | | int slash_n, spc;
|
33805b | 2000-08-12 | Per Hedbor | | };
|
e15d14 | 2002-10-14 | Henrik Grubbström (Grubba) | | static void f_hp_init( struct object *o )
{
THP->headers = NULL;
THP->pnt = NULL;
THP->hsize = 0;
}
|
2d41d4 | 2001-10-02 | Per Hedbor | | static void f_hp_exit( struct object *o )
{
if( THP->headers )
free( THP->headers );
|
e15d14 | 2002-10-14 | Henrik Grubbström (Grubba) | | THP->headers = NULL;
THP->pnt = NULL;
THP->hsize = 0;
|
2d41d4 | 2001-10-02 | Per Hedbor | | }
static void f_hp_feed( INT32 args )
|
ced8a3 | 2001-02-13 | Henrik Grubbström (Grubba) | |
|
33805b | 2000-08-12 | Per Hedbor | | {
struct pike_string *str = Pike_sp[-1].u.string;
|
3c64ee | 2001-02-20 | Per Hedbor | | struct header_buf *hp = THP;
|
6f36b9 | 2003-04-14 | Martin Stjernholm | | int str_len;
|
4d4cc2 | 2001-09-24 | Henrik Grubbström (Grubba) | | int tot_slash_n=hp->slash_n, slash_n = 0, spc = hp->spc;
|
33805b | 2000-08-12 | Per Hedbor | | char *pp,*ep;
struct svalue *tmp;
struct mapping *headers;
|
aad584 | 2000-08-12 | Henrik Grubbström (Grubba) | | ptrdiff_t os=0, i, j, l;
|
33805b | 2000-08-12 | Per Hedbor | | unsigned char *in;
if( Pike_sp[-1].type != PIKE_T_STRING )
|
b2d3e4 | 2000-12-01 | Fredrik Hübinette (Hubbe) | | Pike_error("Wrong type of argument to feed()\n");
|
2d41d4 | 2001-10-02 | Per Hedbor | | if( str->size_shift )
Pike_error("Wide string headers not supported\n");
|
6f36b9 | 2003-04-14 | Martin Stjernholm | | str_len = str->len;
while( str_len >= hp->left )
|
2d41d4 | 2001-10-02 | Per Hedbor | | {
|
9e361c | 2002-02-14 | Henrik Grubbström (Grubba) | | char *buf;
|
2d41d4 | 2001-10-02 | Per Hedbor | | if( THP->hsize > 512 * 1024 )
Pike_error("Too many headers\n");
THP->hsize += 8192;
|
9e361c | 2002-02-14 | Henrik Grubbström (Grubba) | | buf = THP->headers;
|
2d41d4 | 2001-10-02 | Per Hedbor | | THP->headers = realloc( THP->headers, THP->hsize );
if( !THP->headers )
{
|
9e361c | 2002-02-14 | Henrik Grubbström (Grubba) | | free(buf);
|
2d41d4 | 2001-10-02 | Per Hedbor | | THP->hsize = 0;
THP->left = 0;
Pike_error("Running out of memory in header parser\n");
}
|
9e361c | 2002-02-14 | Henrik Grubbström (Grubba) | | THP->left += 8192;
|
2d41d4 | 2001-10-02 | Per Hedbor | | THP->pnt = (THP->headers + THP->hsize - THP->left);
}
|
33805b | 2000-08-12 | Per Hedbor | |
|
6f36b9 | 2003-04-14 | Martin Stjernholm | | MEMCPY( hp->pnt, str->str, str_len );
|
d5ea29 | 2001-02-01 | Per Hedbor | | pop_n_elems( args );
|
9e361c | 2002-02-14 | Henrik Grubbström (Grubba) | |
|
6f36b9 | 2003-04-14 | Martin Stjernholm | | for( ep=(hp->pnt+str_len),pp=MAXIMUM(hp->headers,hp->pnt-3);
|
d5ea29 | 2001-02-01 | Per Hedbor | | 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;
|
33805b | 2000-08-12 | Per Hedbor | |
|
3c64ee | 2001-02-20 | Per Hedbor | | hp->slash_n = tot_slash_n;
hp->spc = spc;
|
2d41d4 | 2001-10-02 | Per Hedbor | |
|
6f36b9 | 2003-04-14 | Martin Stjernholm | | hp->left -= str_len;
hp->pnt += str_len;
|
3c64ee | 2001-02-20 | Per Hedbor | | hp->pnt[0] = 0;
|
d5ea29 | 2001-02-01 | Per Hedbor | |
|
33805b | 2000-08-12 | Per Hedbor | | if( slash_n != 2 )
{
|
d5ea29 | 2001-02-01 | Per Hedbor | |
if( (spc < 2) && tot_slash_n )
{
push_text( "" );
|
3c64ee | 2001-02-20 | Per Hedbor | | push_text( hp->headers );
|
d5ea29 | 2001-02-01 | Per Hedbor | | f_aggregate_mapping( 0 );
f_aggregate( 3 );
return;
}
|
33805b | 2000-08-12 | Per Hedbor | | push_int( 0 );
return;
}
|
3c64ee | 2001-02-20 | Per Hedbor | | push_string( make_shared_binary_string( pp, hp->pnt - pp ) );
|
33805b | 2000-08-12 | Per Hedbor | | headers = allocate_mapping( 5 );
|
3c64ee | 2001-02-20 | Per Hedbor | | in = hp->headers;
l = pp - hp->headers;
|
4a8449 | 2000-08-13 | David Hedbor | |
|
33805b | 2000-08-12 | Per Hedbor | |
for( i = 0; i < l; i++ )
|
9e361c | 2002-02-14 | Henrik Grubbström (Grubba) | | if((in[i] == '\n') || (in[i] == '\r'))
|
33805b | 2000-08-12 | Per Hedbor | | break;
|
9e361c | 2002-02-14 | Henrik Grubbström (Grubba) | | push_string( make_shared_binary_string( in, i ) );
if((in[i] == '\r') && (in[i+1] == '\n'))
|
33805b | 2000-08-12 | Per Hedbor | | i++;
|
9e361c | 2002-02-14 | Henrik Grubbström (Grubba) | | i++;
|
33805b | 2000-08-12 | Per Hedbor | |
in += i; l -= i;
|
9e361c | 2002-02-14 | Henrik Grubbström (Grubba) | |
|
33805b | 2000-08-12 | Per Hedbor | | for(i = 0; i < l; i++)
{
|
9e361c | 2002-02-14 | Henrik Grubbström (Grubba) | | if(in[i] > 64 && in[i] < 91) in[i]+=32;
|
4a8449 | 2000-08-13 | David Hedbor | | else if( in[i] == ':' )
|
33805b | 2000-08-12 | Per Hedbor | | {
|
9e361c | 2002-02-14 | Henrik Grubbström (Grubba) | |
|
33805b | 2000-08-12 | Per Hedbor | |
|
9e361c | 2002-02-14 | Henrik Grubbström (Grubba) | | int val_cnt = 0;
|
33805b | 2000-08-12 | Per Hedbor | | push_string(make_shared_binary_string((char*)in+os,i-os));
|
d5ea29 | 2001-02-01 | Per Hedbor | |
|
9e361c | 2002-02-14 | Henrik Grubbström (Grubba) | |
os = i+1;
while((in[os]==' ') || (in[os]=='\t')) os++;
do {
for(j=os;j<l;j++)
if( in[j] == '\n' || in[j]=='\r')
break;
push_string(make_shared_binary_string((char*)in+os,j-os));
val_cnt++;
if((in[j] == '\r') && (in[j+1] == '\n')) j++;
os = j+1;
i = j;
} while ((os < l) && ((in[os] == ' ') || (in[os] == '\t')));
if (val_cnt > 1) {
f_add(val_cnt);
}
|
33805b | 2000-08-12 | Per Hedbor | |
if((tmp = low_mapping_lookup(headers, Pike_sp-2)))
{
|
9e361c | 2002-02-14 | Henrik Grubbström (Grubba) | | f_aggregate( 1 );
if( tmp->type == PIKE_T_ARRAY )
{
tmp->u.array->refs++;
push_array(tmp->u.array);
map_delete(headers, Pike_sp-3);
f_add(2);
} else {
tmp->u.string->refs++;
push_string(tmp->u.string);
f_aggregate(1);
map_delete(headers, Pike_sp-3);
f_add(2);
}
|
33805b | 2000-08-12 | Per Hedbor | | }
mapping_insert(headers, Pike_sp-2, Pike_sp-1);
|
9e361c | 2002-02-14 | Henrik Grubbström (Grubba) | |
|
33805b | 2000-08-12 | Per Hedbor | | pop_n_elems(2);
}
}
push_mapping( headers );
|
d5ea29 | 2001-02-01 | Per Hedbor | | f_aggregate( 3 );
|
33805b | 2000-08-12 | Per Hedbor | | }
|
2d41d4 | 2001-10-02 | Per Hedbor | | static void f_hp_create( INT32 args )
|
ced8a3 | 2001-02-13 | Henrik Grubbström (Grubba) | |
|
33805b | 2000-08-12 | Per Hedbor | | {
|
e15d14 | 2002-10-14 | Henrik Grubbström (Grubba) | | if (THP->headers) {
free(THP->headers);
THP->headers = NULL;
}
THP->headers = xalloc( 8192 );
|
33805b | 2000-08-12 | Per Hedbor | | THP->pnt = THP->headers;
|
2d41d4 | 2001-10-02 | Per Hedbor | | THP->hsize = 8192;
|
33805b | 2000-08-12 | Per Hedbor | | THP->left = 8192;
|
3c64ee | 2001-02-20 | Per Hedbor | | THP->spc = THP->slash_n = 0;
|
ced8a3 | 2001-02-13 | Henrik Grubbström (Grubba) | | pop_n_elems(args);
push_int(0);
|
33805b | 2000-08-12 | Per Hedbor | | }
|
418303 | 2001-10-03 | Martin Nilsson | |
|
2d41d4 | 2001-10-02 | Per Hedbor | | static void f_make_http_headers( INT32 args )
|
ced8a3 | 2001-02-13 | Henrik Grubbström (Grubba) | |
|
33805b | 2000-08-12 | Per Hedbor | | {
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 )
|
b2d3e4 | 2000-12-01 | Fredrik Hübinette (Hubbe) | | Pike_error("Wrong argument type to make_http_headers(mapping heads)\n");
|
33805b | 2000-08-12 | Per Hedbor | |
m = Pike_sp[-1].u.mapping;
NEW_MAPPING_LOOP( m->data )
{
if( k->ind.type != PIKE_T_STRING || k->ind.u.string->size_shift )
|
b2d3e4 | 2000-12-01 | Fredrik Hübinette (Hubbe) | | Pike_error("Wrong argument type to make_http_headers("
|
33805b | 2000-08-12 | Per Hedbor | | "mapping(string(8bit):string(8bit)|array(string(8bit))) heads)\n");
|
5fc921 | 2001-10-08 | Per Hedbor | | if( k->val.type == PIKE_T_STRING && !k->val.u.string->size_shift )
|
33805b | 2000-08-12 | Per Hedbor | | total_len += k->val.u.string->len + 2 + k->ind.u.string->len + 2;
else if( k->val.type == PIKE_T_ARRAY )
{
struct array *a = k->val.u.array;
|
aad584 | 2000-08-12 | Henrik Grubbström (Grubba) | | ptrdiff_t i, kl = k->ind.u.string->len + 2 ;
|
33805b | 2000-08-12 | Per Hedbor | | for( i = 0; i<a->size; i++ )
if( a->item[i].type != PIKE_T_STRING||a->item[i].u.string->size_shift )
|
b2d3e4 | 2000-12-01 | Fredrik Hübinette (Hubbe) | | Pike_error("Wrong argument type to make_http_headers("
|
33805b | 2000-08-12 | Per Hedbor | | "mapping(string(8bit):string(8bit)|"
"array(string(8bit))) heads)\n");
else
total_len += kl + a->item[i].u.string->len + 2;
} else
|
b2d3e4 | 2000-12-01 | Fredrik Hübinette (Hubbe) | | Pike_error("Wrong argument type to make_http_headers("
|
33805b | 2000-08-12 | Per Hedbor | | "mapping(string(8bit):string(8bit)|"
"array(string(8bit))) heads)\n");
}
total_len += 2;
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++);
NEW_MAPPING_LOOP( m->data )
{
char *s;
|
aad584 | 2000-08-12 | Henrik Grubbström (Grubba) | | ptrdiff_t l, c;
|
33805b | 2000-08-12 | Per Hedbor | | 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;
|
aad584 | 2000-08-12 | Henrik Grubbström (Grubba) | | ptrdiff_t i, kl = k->ind.u.string->len + 2;
|
33805b | 2000-08-12 | Per Hedbor | | for( i = 0; i<a->size; i++ )
{
STRADD( k->ind ); *(pnt++) = ':'; *(pnt++) = ' ';
STRADD( a->item[i] );*(pnt++) = '\r';*(pnt++) = '\n';
}
}
}
*(pnt++) = '\r';
*(pnt++) = '\n';
pop_n_elems( args );
push_string( end_shared_string( res ) );
}
|
2d41d4 | 2001-10-02 | Per Hedbor | | static void f_http_decode_string(INT32 args)
|
ced8a3 | 2001-02-13 | Henrik Grubbström (Grubba) | |
|
33805b | 2000-08-12 | Per Hedbor | | {
int proc;
char *foo,*bar,*end;
struct pike_string *newstr;
if (!args || Pike_sp[-args].type != PIKE_T_STRING)
|
b2d3e4 | 2000-12-01 | Fredrik Hübinette (Hubbe) | | Pike_error("Invalid argument to http_decode_string(STRING);\n");
|
33805b | 2000-08-12 | Per Hedbor | |
foo=bar=Pike_sp[-args].u.string->str;
end=foo+Pike_sp[-args].u.string->len;
for (proc=0; foo<end; ) if (*foo=='%') { proc++; foo+=3; } else foo++;
if (!proc) { pop_n_elems(args-1); return; }
newstr=begin_shared_string((foo-bar)-proc*2);
foo=newstr->str;
for (proc=0; bar<end; foo++)
if (*bar=='%')
{
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
*foo=0;
bar+=3;
}
else { *foo=*(bar++); }
pop_n_elems(args);
push_string(end_shared_string(newstr));
}
|
77ef81 | 2001-06-30 | Per Hedbor | | static void f_html_encode_string( INT32 args )
|
d579c8 | 2001-12-08 | Martin Nilsson | |
|
77ef81 | 2001-06-30 | Per Hedbor | | {
struct pike_string *str;
int newlen;
if( args != 1 )
Pike_error("Wrong number of arguments to html_encode_string\n" );
switch( Pike_sp[-1].type )
{
|
2a33f0 | 2002-07-02 | Per Hedbor | | void o_cast_to_string();
|
77ef81 | 2001-06-30 | Per Hedbor | |
case PIKE_T_INT:
{
char buf[21], *b = buf+19;
int neg, i, j=0;
i = Pike_sp[-1].u.integer;
pop_stack();
if( i < 0 )
{
neg = 1;
i = -i;
}
else
neg = 0;
buf[20] = 0;
|
05dc76 | 2001-07-20 | Martin Stjernholm | | while( i >= 10 )
|
77ef81 | 2001-06-30 | Per Hedbor | | {
b[ -j++ ] = '0'+(i%10);
i /= 10;
}
b[ -j++ ] = '0'+(i%10);
if( neg ) b[ -j++ ] = '-';
push_text( b-j+1 );
}
return;
case PIKE_T_FLOAT:
|
2a33f0 | 2002-07-02 | Per Hedbor | | o_cast_to_string();
|
77ef81 | 2001-06-30 | Per Hedbor | | return;
default:
|
2a33f0 | 2002-07-02 | Per Hedbor | | o_cast_to_string();
|
77ef81 | 2001-06-30 | Per Hedbor | | case PIKE_T_STRING:
break;
}
str = Pike_sp[-1].u.string;
newlen = str->len;
#define COUNT(T) { \
T *s = (T *)str->str; \
int i; \
for( i = 0; i<str->len; i++ ) \
switch( s[i] ) \
{ \
case 0: /* � */ \
case '<': /* < */ \
case '>': newlen+=3; break;/* > */ \
case '&': /* & */ \
case '"': /* " */ \
case '\'': newlen+=4;break;/* ' */ \
} \
}
#define ADD(X) if(sizeof(X)-sizeof("")==4) ADD4(X); else ADD5(X)
#define ADD4(X) ((d[0] = X[0]), (d[1] = X[1]), (d[2] = X[2]), (d[3] = X[3]),\
(d+=3))
#define ADD5(X) ((d[0] = X[0]), (d[1] = X[1]), (d[2] = X[2]), (d[3] = X[3]),\
(d[4] = X[4]), (d+=4))
#define REPLACE(T) { \
T *s = (T *)str->str; \
T *d = (T *)res->str; \
int i; \
for( i = 0; i<str->len; i++,s++,d++ ) \
switch( *s ) \
{ \
case 0: ADD("�"); break; \
case '&': ADD("&"); break; \
case '<': ADD("<"); break; \
case '>': ADD(">"); break; \
case '"': ADD("""); break; \
case '\'':ADD("'"); break; \
default: *d = *s; break; \
} \
} \
switch( str->size_shift )
{
case 0: COUNT(unsigned char); break;
case 1: COUNT(unsigned short); break;
case 2: COUNT(int); break;
}
if( newlen == str->len )
return;
{
struct pike_string *res = begin_wide_shared_string(newlen,str->size_shift);
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 ) );
}
}
|
2d41d4 | 2001-10-02 | Per Hedbor | |
|
ced8a3 | 2001-02-13 | Henrik Grubbström (Grubba) | |
|
33805b | 2000-08-12 | Per Hedbor | |
|
51ef5c | 2002-10-21 | Marcus Comstedt | | PIKE_MODULE_INIT
|
33805b | 2000-08-12 | Per Hedbor | | {
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 );
|
77ef81 | 2001-06-30 | Per Hedbor | | pike_add_function("html_encode_string", f_html_encode_string,
"function(mixed:string)", 0 );
|
2d41d4 | 2001-10-02 | Per Hedbor | |
|
33805b | 2000-08-12 | Per Hedbor | | start_new_program();
ADD_STORAGE( struct header_buf );
|
18b082 | 2002-10-14 | Henrik Grubbström (Grubba) | | set_init_callback( f_hp_init );
|
2d41d4 | 2001-10-02 | Per Hedbor | | set_exit_callback( f_hp_exit );
|
33805b | 2000-08-12 | Per Hedbor | | pike_add_function( "feed", f_hp_feed, "function(string:array(string|mapping))",0 );
|
ced8a3 | 2001-02-13 | Henrik Grubbström (Grubba) | | pike_add_function( "create", f_hp_create, "function(:void)", ID_STATIC );
|
33805b | 2000-08-12 | Per Hedbor | | end_class( "HeaderParser", 0 );
}
|
51ef5c | 2002-10-21 | Marcus Comstedt | | PIKE_MODULE_EXIT
|
33805b | 2000-08-12 | Per Hedbor | | {
}
|