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) | |
|
488385 | 2009-07-17 | Henrik Grubbström (Grubba) | | *!
*! Class for parsing HTTP-requests.
|
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
{
|
fc0352 | 2005-04-08 | Henrik Grubbström (Grubba) | | unsigned char *headers;
unsigned char *pnt;
|
2d41d4 | 2001-10-02 | Per Hedbor | | ptrdiff_t hsize, left;
|
117d2c | 2005-11-28 | Per Hedbor | | int slash_n, tslash_n, spc;
|
b67515 | 2009-07-29 | Martin Nilsson | | int mode;
|
33805b | 2000-08-12 | Per Hedbor | | };
|
74dfe8 | 2012-12-30 | Jonas Walldén | | static void f_hp_init( struct object *UNUSED(o) )
|
e15d14 | 2002-10-14 | Henrik Grubbström (Grubba) | | {
THP->headers = NULL;
THP->pnt = NULL;
THP->hsize = 0;
|
8f7719 | 2004-01-27 | Henrik Grubbström (Grubba) | | THP->left = 0;
|
117d2c | 2005-11-28 | Per Hedbor | | THP->spc = THP->slash_n = THP->tslash_n = 0;
|
b67515 | 2009-07-29 | Martin Nilsson | | THP->mode = 0;
|
e15d14 | 2002-10-14 | Henrik Grubbström (Grubba) | | }
|
74dfe8 | 2012-12-30 | Jonas Walldén | | static void f_hp_exit( struct object *UNUSED(o) )
|
2d41d4 | 2001-10-02 | Per Hedbor | | {
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) | | |
488385 | 2009-07-17 | Henrik Grubbström (Grubba) | | *!
*! @returns
*! @array
*! @elem string 0
*! Trailing data.
*! @elem string 1
*! First line of request.
*! @elem mapping(string:string|array(string)) 2
*! Headers.
*! @endarray
|
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;
|
21dc26 | 2003-04-14 | Martin Stjernholm | | int str_len;
|
117d2c | 2005-11-28 | Per Hedbor | | int tot_slash_n=hp->slash_n, slash_n = hp->tslash_n, spc = hp->spc;
|
fc0352 | 2005-04-08 | Henrik Grubbström (Grubba) | | unsigned char *pp,*ep;
|
33805b | 2000-08-12 | Per Hedbor | | 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;
|
488385 | 2009-07-17 | Henrik Grubbström (Grubba) | | if (args != 1)
Pike_error("Bad number of arguments to feed().\n");
|
017b57 | 2011-10-28 | Henrik Grubbström (Grubba) | | if( TYPEOF(Pike_sp[-1]) != 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");
|
21dc26 | 2003-04-14 | Martin Stjernholm | | str_len = str->len;
while( str_len >= hp->left )
|
2d41d4 | 2001-10-02 | Per Hedbor | | {
|
fc0352 | 2005-04-08 | Henrik Grubbström (Grubba) | | unsigned char *buf;
|
117d2c | 2005-11-28 | Per Hedbor | | if( hp->hsize > 512 * 1024 )
|
2d41d4 | 2001-10-02 | Per Hedbor | | Pike_error("Too many headers\n");
|
117d2c | 2005-11-28 | Per Hedbor | | hp->hsize += 8192;
buf = hp->headers;
hp->headers = realloc( hp->headers, hp->hsize );
if( !hp->headers )
|
2d41d4 | 2001-10-02 | Per Hedbor | | {
|
9e361c | 2002-02-14 | Henrik Grubbström (Grubba) | | free(buf);
|
117d2c | 2005-11-28 | Per Hedbor | | hp->hsize = 0;
hp->left = 0;
hp->spc = hp->slash_n = 0;
hp->pnt = NULL;
|
2d41d4 | 2001-10-02 | Per Hedbor | | Pike_error("Running out of memory in header parser\n");
}
|
117d2c | 2005-11-28 | Per Hedbor | | hp->left += 8192;
hp->pnt = (hp->headers + hp->hsize - hp->left);
|
2d41d4 | 2001-10-02 | Per Hedbor | | }
|
33805b | 2000-08-12 | Per Hedbor | |
|
59fc9e | 2014-09-03 | Martin Nilsson | | 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) | |
|
21dc26 | 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++ )
|
117d2c | 2005-11-28 | Per Hedbor | | if( *pp == ' ' )
{
spc++;
slash_n = 0;
}
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;
|
117d2c | 2005-11-28 | Per Hedbor | | hp->tslash_n = slash_n;
|
21dc26 | 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 )
{
|
7863d6 | 2005-05-06 | Martin Nilsson | | push_empty_string();
|
d5ea29 | 2001-02-01 | Per Hedbor | |
|
571b7c | 2005-04-09 | Henrik Grubbström (Grubba) | | push_text((char *)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;
}
|
117d2c | 2005-11-28 | Per Hedbor | |
|
571b7c | 2005-04-09 | Henrik Grubbström (Grubba) | |
push_string(make_shared_binary_string((char *)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++ )
|
117d2c | 2005-11-28 | Per Hedbor | | if( (in[i] == '\n') || (in[i] == '\r') )
|
33805b | 2000-08-12 | Per Hedbor | | break;
|
571b7c | 2005-04-09 | Henrik Grubbström (Grubba) | | push_string(make_shared_binary_string((char *)in, i));
|
9e361c | 2002-02-14 | Henrik Grubbström (Grubba) | |
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++)
{
|
b67515 | 2009-07-29 | Martin Nilsson | | 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)))
{
|
017b57 | 2011-10-28 | Henrik Grubbström (Grubba) | | if( TYPEOF(*tmp) == PIKE_T_ARRAY )
|
9e361c | 2002-02-14 | Henrik Grubbström (Grubba) | | {
|
11b8af | 2006-01-02 | Martin Nilsson | | f_aggregate( 1 );
|
50ea68 | 2003-03-14 | Henrik Grubbström (Grubba) | | ref_push_array(tmp->u.array);
|
f540ce | 2006-11-10 | Per Hedbor | | stack_swap();
|
9e361c | 2002-02-14 | Henrik Grubbström (Grubba) | | map_delete(headers, Pike_sp-3);
f_add(2);
} else {
|
50ea68 | 2003-03-14 | Henrik Grubbström (Grubba) | | ref_push_string(tmp->u.string);
|
f540ce | 2006-11-10 | Per Hedbor | | stack_swap();
|
9e361c | 2002-02-14 | Henrik Grubbström (Grubba) | | map_delete(headers, Pike_sp-3);
|
1b25eb | 2006-01-02 | Martin Nilsson | | f_aggregate(2);
|
9e361c | 2002-02-14 | Henrik Grubbström (Grubba) | | }
|
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);
}
|
b67515 | 2009-07-29 | Martin Nilsson | | else if( in[i]=='\r' || in[i]=='\n' )
{
if( THP->mode == 1 )
{
Pike_error("Malformed HTTP header.\n");
}
else
os = i+1;
}
|
33805b | 2000-08-12 | Per Hedbor | | }
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 )
|
b67515 | 2009-07-29 | Martin Nilsson | | |
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;
}
|
b67515 | 2009-07-29 | Martin Nilsson | |
|
ddc1a3 | 2010-07-27 | Martin Stjernholm | | THP->mode = 0;
|
b67515 | 2009-07-29 | Martin Nilsson | | get_all_args("create",args,".%i",&THP->mode);
|
e15d14 | 2002-10-14 | Henrik Grubbström (Grubba) | | 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) | | |
d7a534 | 2005-10-27 | Henrik Grubbström (Grubba) | | *! make_http_headers(mapping(string:string|array(string)) headers, @
*! int(0..1)|void no_terminator)
|
ced8a3 | 2001-02-13 | Henrik Grubbström (Grubba) | | */
|
33805b | 2000-08-12 | Per Hedbor | | {
int total_len = 0, e;
|
fc0352 | 2005-04-08 | Henrik Grubbström (Grubba) | | unsigned char *pnt;
|
33805b | 2000-08-12 | Per Hedbor | | struct mapping *m;
struct keypair *k;
struct pike_string *res;
|
d7a534 | 2005-10-27 | Henrik Grubbström (Grubba) | | int terminator = 2;
|
017b57 | 2011-10-28 | Henrik Grubbström (Grubba) | | if( TYPEOF(Pike_sp[-args]) != PIKE_T_MAPPING )
|
b2d3e4 | 2000-12-01 | Fredrik Hübinette (Hubbe) | | Pike_error("Wrong argument type to make_http_headers(mapping heads)\n");
|
d7a534 | 2005-10-27 | Henrik Grubbström (Grubba) | | m = Pike_sp[-args].u.mapping;
if (args > 1) {
|
017b57 | 2011-10-28 | Henrik Grubbström (Grubba) | | if (TYPEOF(Pike_sp[1-args]) != PIKE_T_INT)
|
d7a534 | 2005-10-27 | Henrik Grubbström (Grubba) | | Pike_error("Bad argument 2 to make_http_headers(). Expected int.\n");
if (Pike_sp[1-args].u.integer)
terminator = 0;
}
|
33805b | 2000-08-12 | Per Hedbor | |
NEW_MAPPING_LOOP( m->data )
{
|
017b57 | 2011-10-28 | Henrik Grubbström (Grubba) | | if( TYPEOF(k->ind) != 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");
|
017b57 | 2011-10-28 | Henrik Grubbström (Grubba) | | if( TYPEOF(k->val) == 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;
|
017b57 | 2011-10-28 | Henrik Grubbström (Grubba) | | else if( TYPEOF(k->val) == PIKE_T_ARRAY )
|
33805b | 2000-08-12 | Per Hedbor | | {
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++ )
|
017b57 | 2011-10-28 | Henrik Grubbström (Grubba) | | if( TYPEOF(a->item[i]) != 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");
}
|
d7a534 | 2005-10-27 | Henrik Grubbström (Grubba) | | total_len += terminator;
|
33805b | 2000-08-12 | Per Hedbor | |
res = begin_shared_string( total_len );
|
fc0352 | 2005-04-08 | Henrik Grubbström (Grubba) | | pnt = STR0(res);
|
571b7c | 2005-04-09 | Henrik Grubbström (Grubba) | | #define STRADD(X) \
for( l=(X).u.string->len, s=STR0((X).u.string), c=0; c<l; c++ ) \
*(pnt++)=*(s++)
|
33805b | 2000-08-12 | Per Hedbor | |
NEW_MAPPING_LOOP( m->data )
{
|
fc0352 | 2005-04-08 | Henrik Grubbström (Grubba) | | unsigned char *s;
|
aad584 | 2000-08-12 | Henrik Grubbström (Grubba) | | ptrdiff_t l, c;
|
017b57 | 2011-10-28 | Henrik Grubbström (Grubba) | | if( TYPEOF(k->val) == PIKE_T_STRING )
|
33805b | 2000-08-12 | Per Hedbor | | {
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';
}
}
}
|
d7a534 | 2005-10-27 | Henrik Grubbström (Grubba) | | if (terminator) {
*(pnt++) = '\r';
*(pnt++) = '\n';
}
|
33805b | 2000-08-12 | Per Hedbor | |
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) | | |
4a823c | 2016-03-02 | Henrik Grubbström (Grubba) | | *! Decodes an http transport-encoded string. Knows about @tt{%XX@} and
*! @tt{%uXXXX@} syntax. Treats @tt{%UXXXX@} as @tt{%uXXXX@}. It will
*! treat '+' as '+' and not ' ', so form decoding needs to replace that
*! in a second step.
*!
*! It also knows about UTF-16 surrogate pairs when decoding @tt{%UXXXX@}
*! sequences.
|
c6345a | 2014-10-17 | Henrik Grubbström (Grubba) | | *!
*! @note
*! Performs a best-effort decoding. Invalid and truncated escapes
*! will still be decoded.
|
ced8a3 | 2001-02-13 | Henrik Grubbström (Grubba) | | */
|
33805b | 2000-08-12 | Per Hedbor | | {
|
d3bcc9 | 2014-10-08 | Henrik Grubbström (Grubba) | | int proc = 0;
|
c6345a | 2014-10-17 | Henrik Grubbström (Grubba) | | int trunc = 0;
|
d3bcc9 | 2014-10-08 | Henrik Grubbström (Grubba) | | int size_shift;
|
4a823c | 2016-03-02 | Henrik Grubbström (Grubba) | | int got_surrogates = 0;
|
d3bcc9 | 2014-10-08 | Henrik Grubbström (Grubba) | | PCHARP foo, end;
struct string_builder newstr;
|
0d9737 | 2014-10-08 | Henrik Grubbström (Grubba) | | if (!args || TYPEOF(Pike_sp[-args]) != PIKE_T_STRING)
|
d3bcc9 | 2014-10-08 | Henrik Grubbström (Grubba) | | Pike_error("Invalid argument to http_decode_string(string).\n");
foo = MKPCHARP_STR(Pike_sp[-args].u.string);
end = ADD_PCHARP(foo, Pike_sp[-args].u.string->len);
size_shift = Pike_sp[-args].u.string->size_shift;
|
c6345a | 2014-10-17 | Henrik Grubbström (Grubba) | | * trunc counts the number of characters that are to be removed.
|
d3bcc9 | 2014-10-08 | Henrik Grubbström (Grubba) | | */
|
c6345a | 2014-10-17 | Henrik Grubbström (Grubba) | | for (; COMPARE_PCHARP(foo, <, end);) {
p_wchar2 c = EXTRACT_PCHARP(foo);
INC_PCHARP(foo, 1);
if (c != '%') continue;
proc++;
if (SUBTRACT_PCHARP(end, foo) < 2) {
trunc += SUBTRACT_PCHARP(end, foo);
break;
}
c = EXTRACT_PCHARP(foo);
if (c == 'u' || c == 'U') {
if (SUBTRACT_PCHARP(end, foo) < 5) {
trunc += SUBTRACT_PCHARP(end, foo);
break;
}
if (EXTRACT_PCHARP(foo) != '0' || INDEX_PCHARP(foo, 1) != '0') {
if (!size_shift) size_shift = 1;
|
8f7719 | 2004-01-27 | Henrik Grubbström (Grubba) | | }
|
c6345a | 2014-10-17 | Henrik Grubbström (Grubba) | | trunc += 5;
INC_PCHARP(foo, 5);
} else {
trunc += 2;
INC_PCHARP(foo, 2);
|
8f7719 | 2004-01-27 | Henrik Grubbström (Grubba) | | }
}
|
33805b | 2000-08-12 | Per Hedbor | |
if (!proc) { pop_n_elems(args-1); return; }
|
c6345a | 2014-10-17 | Henrik Grubbström (Grubba) | | init_string_builder_alloc(&newstr, Pike_sp[-args].u.string->len - trunc,
|
d3bcc9 | 2014-10-08 | Henrik Grubbström (Grubba) | | size_shift);
foo = MKPCHARP_STR(Pike_sp[-args].u.string);
for (; COMPARE_PCHARP(foo, <, end); INC_PCHARP(foo, 1)) {
|
c6345a | 2014-10-17 | Henrik Grubbström (Grubba) | | p_wchar2 c = EXTRACT_PCHARP(foo);
|
d3bcc9 | 2014-10-08 | Henrik Grubbström (Grubba) | | if (c == '%') {
c = INDEX_PCHARP(foo, 1);
if (c == 'u' || c == 'U') {
c = 0;
if (SUBTRACT_PCHARP(end, foo) > 5) {
p_wchar2 hex = INDEX_PCHARP(foo, 2);
c = (((hex<'A')?hex:(hex + 9)) & 15)<<12;
hex = INDEX_PCHARP(foo, 3);
c |= (((hex<'A')?hex:(hex + 9)) & 15)<<8;
hex = INDEX_PCHARP(foo, 4);
c |= (((hex<'A')?hex:(hex + 9)) & 15)<<4;
hex = INDEX_PCHARP(foo, 5);
c |= ((hex<'A')?hex:(hex + 9)) & 15;
|
8f7719 | 2004-01-27 | Henrik Grubbström (Grubba) | | }
|
d3bcc9 | 2014-10-08 | Henrik Grubbström (Grubba) | | INC_PCHARP(foo, 5);
|
4a823c | 2016-03-02 | Henrik Grubbström (Grubba) | | if ((c & 0xf800) == 0xd800) {
got_surrogates = 1;
}
|
8f7719 | 2004-01-27 | Henrik Grubbström (Grubba) | | } else {
|
d3bcc9 | 2014-10-08 | Henrik Grubbström (Grubba) | | c = 0;
if (SUBTRACT_PCHARP(end, foo) > 2) {
p_wchar2 hex = INDEX_PCHARP(foo, 1);
c = (((hex<'A')?hex:(hex + 9)) & 15)<<4;
hex = INDEX_PCHARP(foo, 2);
c |= ((hex<'A')?hex:(hex + 9)) & 15;
|
8f7719 | 2004-01-27 | Henrik Grubbström (Grubba) | | }
|
d3bcc9 | 2014-10-08 | Henrik Grubbström (Grubba) | | INC_PCHARP(foo, 2);
|
8f7719 | 2004-01-27 | Henrik Grubbström (Grubba) | | }
|
d3bcc9 | 2014-10-08 | Henrik Grubbström (Grubba) | | }
string_builder_putchar(&newstr, c);
|
8f7719 | 2004-01-27 | Henrik Grubbström (Grubba) | | }
|
d3bcc9 | 2014-10-08 | Henrik Grubbström (Grubba) | |
|
33805b | 2000-08-12 | Per Hedbor | | pop_n_elems(args);
|
4a823c | 2016-03-02 | Henrik Grubbström (Grubba) | |
if (got_surrogates) {
newstr.s->size_shift = 0;
newstr.known_shift = 0;
newstr.s->len <<= 1;
push_string(finish_string_builder(&newstr));
push_int(2);
f_unicode_to_string(2);
} else {
push_string(finish_string_builder(&newstr));
}
|
33805b | 2000-08-12 | Per Hedbor | | }
|
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" );
|
017b57 | 2011-10-28 | Henrik Grubbström (Grubba) | | switch( TYPEOF(Pike_sp[-1]) )
|
77ef81 | 2001-06-30 | Per Hedbor | | {
|
2a33f0 | 2002-07-02 | Per Hedbor | | void o_cast_to_string();
|
77ef81 | 2001-06-30 | Per Hedbor | |
case PIKE_T_INT:
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] ) \
{ \
|
8f7719 | 2004-01-27 | Henrik Grubbström (Grubba) | | case 0: /* � */ \
case '<': /* < */ \
|
77ef81 | 2001-06-30 | Per Hedbor | | case '>': newlen+=3; break;/* > */ \
|
8f7719 | 2004-01-27 | Henrik Grubbström (Grubba) | | case '&': /* & */ \
case '"': /* " */ \
|
77ef81 | 2001-06-30 | Per Hedbor | | 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 )
{
|
8f7719 | 2004-01-27 | Henrik Grubbström (Grubba) | | case 0: COUNT(p_wchar0); break;
case 1: COUNT(p_wchar1); break;
case 2: COUNT(p_wchar2); break;
|
77ef81 | 2001-06-30 | Per Hedbor | | }
if( newlen == str->len )
return;
{
struct pike_string *res = begin_wide_shared_string(newlen,str->size_shift);
switch( str->size_shift )
{
|
8f7719 | 2004-01-27 | Henrik Grubbström (Grubba) | | case 0: REPLACE(p_wchar0); break;
case 1: REPLACE(p_wchar1); break;
case 2: REPLACE(p_wchar2); break;
|
77ef81 | 2001-06-30 | Per Hedbor | | }
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 | | {
|
09959a | 2005-01-20 | Martin Nilsson | | ADD_FUNCTION("make_http_headers", f_make_http_headers,
|
d7a534 | 2005-10-27 | Henrik Grubbström (Grubba) | | tFunc(tMap(tStr,tOr(tStr,tArr(tStr))) tOr(tInt01,tVoid), tStr),
0);
|
33805b | 2000-08-12 | Per Hedbor | |
|
09959a | 2005-01-20 | Martin Nilsson | | ADD_FUNCTION("http_decode_string", f_http_decode_string,
tFunc(tStr,tStr), 0 );
|
33805b | 2000-08-12 | Per Hedbor | |
|
09959a | 2005-01-20 | Martin Nilsson | | ADD_FUNCTION("html_encode_string", f_html_encode_string,
tFunc(tMix,tStr), 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 );
|
09959a | 2005-01-20 | Martin Nilsson | | ADD_FUNCTION( "feed", f_hp_feed, tFunc(tStr,tArr(tOr(tStr,tMapping))), 0 );
|
b67515 | 2009-07-29 | Martin Nilsson | | ADD_FUNCTION( "create", f_hp_create, tFunc(tOr(tInt,tVoid),tVoid), ID_PROTECTED );
|
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 | | {
}
|