Browse Source

Fixed parser

dynamic-accesslists
erdgeist 18 years ago
parent
commit
0de82a8925
  1. 29
      opentracker.c
  2. 12
      scan_urlencoded_query.c
  3. 11
      scan_urlencoded_query.h
  4. 3
      trackerlogic.c

29
opentracker.c

@ -111,7 +111,7 @@ void httpresponse(struct http_data* h,int64 s) @@ -111,7 +111,7 @@ void httpresponse(struct http_data* h,int64 s)
ot_torrent torrent;
ot_hash *hash = NULL;
unsigned long numwant;
int compact,x;
int compact, scanon;
size_t reply_size = 0;
array_cat0(&h->r);
@ -124,9 +124,7 @@ e400: @@ -124,9 +124,7 @@ e400:
goto bailout;
}
// expect 'GET /uri?nnbjhg HTTP/1.*'
c+=4;
for (d=c; *d!=' '&&*d!='\t'&&*d!='\n'&&*d!='\r'; ++d) ;
if (*d!=' ') goto e400;
@ -134,7 +132,7 @@ e400: @@ -134,7 +132,7 @@ e400:
if (c[0]!='/') goto e404;
while (*c=='/') ++c;
switch( x = scan_urlencoded_query( &c, data = c, SCAN_PATH ) )
switch( scan_urlencoded_query( &c, data = c, SCAN_PATH ) )
{
case 6: /* scrape ? */
if (byte_diff(data,6,"scrape"))
@ -147,9 +145,13 @@ e400: @@ -147,9 +145,13 @@ e400:
peer.port = 6881;
numwant = 50;
compact = 1;
scanon = 1;
while( 1 ) {
switch( x=scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_PARAM ) ) {
while( scanon ) {
switch( scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_PARAM ) ) {
case -2: /* terminator */
scanon = 0;
break;
case -1: /* error */
goto e404;
case 4:
@ -157,16 +159,22 @@ e400: @@ -157,16 +159,22 @@ e400:
/* scan int */ c;
else if(!byte_diff(data,4,"left"))
/* scan int */ c;
else
scan_urlencoded_query( &c, NULL, SCAN_SEARCHPATH_VALUE );
break;
case 7:
if(!byte_diff(data,7,"numwant"))
/* scan int */ c;
else if(!byte_diff(data,7,"compact"))
/* scan flag */ c;
else
scan_urlencoded_query( &c, NULL, SCAN_SEARCHPATH_VALUE );
break;
case 9:
if(byte_diff(data,9,"info_hash"))
if(byte_diff(data,9,"info_hash")) {
scan_urlencoded_query( &c, NULL, SCAN_SEARCHPATH_VALUE );
continue;
}
/* ignore this, when we have less than 20 bytes */
switch( scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE ) ) {
case -1:
@ -178,13 +186,14 @@ e400: @@ -178,13 +186,14 @@ e400:
continue;
}
default:
printf("blub %i\n",x);
scan_urlencoded_query( &c, NULL, SCAN_SEARCHPATH_VALUE );
break;
}
}
/* Scanned whole query string */
if( !hash || ( compact == 0 ) ) goto e404;
printf("ALLFINE\n");
torrent = add_peer_to_torrent( hash, &peer );
if( !torrent ) {
e500:
@ -194,18 +203,16 @@ e500: @@ -194,18 +203,16 @@ e500:
reply = malloc( numwant*6+10 );
if( reply )
reply_size = return_peers_for_torrent( torrent, numwant, reply );
if( !reply || reply_size < 0 ) {
if( !reply || ( reply_size < 0 ) ) {
if( reply ) free( reply );
goto e500;
}
break;
default: /* neither scrape nor announce */
printf("blub %i\n",x);
e404:
httperror(h,"404 Not Found","No such file or directory.");
goto bailout;
}
c=h->hdrbuf=(char*)malloc(500);
c+=fmt_str(c,"HTTP/1.1 Coming Up\r\nContent-Type: text/plain");
c+=fmt_str(c,"\r\nContent-Length: ");

12
scan_urlencoded_query.c

@ -24,21 +24,23 @@ size_t scan_urlencoded_query(char **string, char *deststring, int flags) { @@ -24,21 +24,23 @@ size_t scan_urlencoded_query(char **string, char *deststring, int flags) {
if( ( b = scan_fromhex(*s++) ) == 0xff ) return -1;
c=(c<<4)|b;
}
*d++ = c;
if(d) *d++ = c;
}
switch( c ) {
case 0: case '\r': case '\n': case ' ':
if ( ( flags & BREAK_AT_WHITESPACE ) == 0 ) return -1;
if( d == (unsigned char*)deststring ) return -2;
--s;
break;
case '?':
if ( ( flags & BREAK_AT_QUESTIONMARK ) == 0 ) return -1;
if( flags != SCAN_PATH ) return -1;
break;
case '=':
if ( ( flags & BREAK_AT_EQUALSIGN ) == 0 ) return -1;
if( flags != SCAN_SEARCHPATH_PARAM ) return -1;
break;
case '&':
if ( ( flags & BREAK_AT_AMPERSAND ) == 0 ) return -1;
if( flags == SCAN_PATH ) return -1;
if( flags == SCAN_SEARCHPATH_PARAM ) --s;
break;
default:
return -1;

11
scan_urlencoded_query.h

@ -1,14 +1,9 @@ @@ -1,14 +1,9 @@
#ifndef __SCAN_URLENCODED_QUERY_H__
#define __SCAN_URLENCODED_QUERY_H__
#define BREAK_AT_QUESTIONMARK (1<<0)
#define BREAK_AT_WHITESPACE (1<<1)
#define BREAK_AT_AMPERSAND (1<<2)
#define BREAK_AT_EQUALSIGN (1<<3)
#define SCAN_PATH ( BREAK_AT_QUESTIONMARK | BREAK_AT_WHITESPACE )
#define SCAN_SEARCHPATH_PARAM ( BREAK_AT_EQUALSIGN )
#define SCAN_SEARCHPATH_VALUE ( BREAK_AT_AMPERSAND | BREAK_AT_WHITESPACE )
#define SCAN_PATH 0
#define SCAN_SEARCHPATH_PARAM 1
#define SCAN_SEARCHPATH_VALUE 2
// string pointer to source, pointer to after terminator on return
// deststring pointer to destination

3
trackerlogic.c

@ -227,6 +227,7 @@ void *map_file( char *file_name ) { @@ -227,6 +227,7 @@ void *map_file( char *file_name ) {
char *map;
if( file_name ) {
int file_desc=open(file_name,O_RDWR|O_CREAT|O_NDELAY,0644);
printf( "%s\n", file_name );
if( file_desc < 0) return 0;
lseek( file_desc, OT_HUGE_FILESIZE, SEEK_SET );
write( file_desc, "_", 1 );
@ -263,8 +264,6 @@ int init_logic( char *directory ) { @@ -263,8 +264,6 @@ int init_logic( char *directory ) {
torrents_list = map_file( NULL );
torrents_count = 0;
printf( "%08x %08x\n", scratch_space, torrents_list );
if( !scratch_space || !torrents_list ) {
if( scratch_space || torrents_list )
unmap_file( NULL, scratch_space ? (void*)scratch_space : (void*)torrents_list, 0 );

Loading…
Cancel
Save