3ad2d52001-02-01Per Hedbor constant OK = 0;
3c6f142001-02-01Per Hedbor // 1 is compilation failed from pike.
3ad2d52001-02-01Per Hedbor constant BADHEADERS = 2; constant BADDATA = 3; constant NOCONN = 4;
a3d6892001-02-01Per Hedbor constant BADPROT = 5; constant BADCODE = 6; constant NODATE = 7; constant BADLENGTH = 8; constant BADMODIFIED= 9;
3c6f142001-02-01Per Hedbor // 10 is error-in-execution from pike.
3ad2d52001-02-01Per Hedbor constant HEADERS = 11; constant DATA = 12; constant CONN = 13; constant TIMEOUT = 99; constant BADARG = 100; void setup_timeout( ) { #ifndef __NT__
2a30d02008-10-21Martin Stjernholm  void timeout() { exit( TIMEOUT ); };
3ad2d52001-02-01Per Hedbor  signal( 14, timeout );
d5f6852001-10-04Marcus Wellhardh  alarm( 30 );
3ad2d52001-02-01Per Hedbor #endif } array get_host_port( string url ) { string host; int port; if( sscanf( url, "http://%s:%d/", host, port ) != 2 ) exit( BADARG ); return ({ host, port }); } Stdio.File connect( string url ) { setup_timeout( ); Stdio.File f = Stdio.File(); [string host, int port] = get_host_port( url ); if( !f->connect( (host=="*"?"127.0.0.1":host), port ) ) exit( NOCONN ); return f; }
136c032001-02-20Per Hedbor void write_fragmented( Stdio.File to, string what, int chars ) { foreach( what/((float)chars), string w ) { to->write( w ); sleep( 0.01 ); } }
65f8012001-06-23Martin Nilsson #define EXIT(X) while(1){write("\n\n\nThe offending response header:\n%O\n\n" \
7fc4a92001-06-24Martin Nilsson  "Protocol: %O (%O)\n" \ "Return code: %d (%d)\n" \ "Response mapping: %O\n", \ headers,prot,expected_prot, \ code,expected_code,hd);exit((X));}
3ad2d52001-02-01Per Hedbor 
fdc3202016-05-26Henrik Grubbström (Grubba) mapping verify_headers( string headers, int content_length, string expected_prot, int expected_code, int want_last_modified )
3ad2d52001-02-01Per Hedbor {
a3d6892001-02-01Per Hedbor  array q = headers / "\r\n"; string prot; int code; string message;
65f8012001-06-23Martin Nilsson  mapping hd = ([]);
a3d6892001-02-01Per Hedbor 
65f8012001-06-23Martin Nilsson  if( sscanf( q[0], "%s %d %s", prot, code, message ) != 3 ) EXIT( BADHEADERS ); if( prot != expected_prot ) EXIT( BADPROT ); if( code != expected_code ) EXIT( BADCODE );
a3d6892001-02-01Per Hedbor  foreach( q[1..], string header ) { string a, b; sscanf( header, "%s: %s", a, b ); hd[ lower_case( a ) ] = b; } if( !hd->date )
65f8012001-06-23Martin Nilsson  EXIT( NODATE );
a3d6892001-02-01Per Hedbor  if( !hd["content-length"] || (int)hd["content-length"] != content_length )
65f8012001-06-23Martin Nilsson  EXIT( BADLENGTH );
a3d6892001-02-01Per Hedbor  if( want_last_modified && !hd["last-modified"] )
65f8012001-06-23Martin Nilsson  EXIT( BADMODIFIED );
fdc3202016-05-26Henrik Grubbström (Grubba)  return hd;
3ad2d52001-02-01Per Hedbor }