More cleanup

dynamic-accesslists
erdgeist 18 years ago
parent 898206ac7a
commit b9743bf516

@ -103,89 +103,101 @@ const char* http_header(struct http_data* r,const char* h)
void httpresponse(struct http_data* h,int64 s) void httpresponse(struct http_data* h,int64 s)
{ {
char* c; char *c, *d, *data;
array_cat0(&h->r); array_cat0(&h->r);
c = array_start(&h->r); c = array_start(&h->r);
if (byte_diff(c,4,"GET ")) if (byte_diff(c,4,"GET ")) {
{
e400: e400:
httperror(h,"400 Invalid Request","This server only understands GET."); httperror(h,"400 Invalid Request","This server only understands GET.");
goto bailout;
} }
else
{
char *d, *data;
// expect 'GET /uri?nnbjhg HTTP/1.*' // expect 'GET /uri?nnbjhg HTTP/1.*'
c+=4; c+=4;
for (d=c; *d!=' '&&*d!='\t'&&*d!='\n'&&*d!='\r'; ++d) ; for (d=c; *d!=' '&&*d!='\t'&&*d!='\n'&&*d!='\r'; ++d) ;
if (*d!=' ') goto e400; if (*d!=' ') goto e400;
*d=0; *d=0;
if (c[0]!='/') goto e404; if (c[0]!='/') goto e404;
while (c[1]=='/') ++c; while (c[1]=='/') ++c;
data = c; data = c;
switch( scan_urlencoded_query( &c, data, SCAN_PATH ) ) { switch( scan_urlencoded_query( &c, data, SCAN_PATH ) ) {
case 6: /* scrape ? */ case 0:
if (!byte_diff(c,6,"scrape")) e404:
goto e404; httperror(h,"404 Not Found","No such file or directory.");
break; goto bailout;
case 9: case 6: /* scrape ? */
if( !byte_diff(c,8,"announce")) if (!byte_diff(c,6,"scrape"))
goto e404;
break;
case 9:
if( !byte_diff(c,8,"announce"))
goto e404;
else {
// info_hash, left, port, numwant, compact
struct ot_peer peer;
ot_hash *hash = NULL;
byte_copy( peer.ip, 4, h->ip );
peer.port = 6881;
while( 1 ) {
data = c;
switch( scan_urlencoded_query( &c, data, SCAN_SEARCHPATH_PARAM ) ) {
case -1: /* error */
goto e404; goto e404;
else { case 4:
// info_hash, left, port, numwant, compact if(!byte_diff(c,4,"port"))
struct ot_peer peer; /* scan int */ c;
byte_copy( peer.ip, 4, h->ip ); else if(!byte_diff(c,4,"left"))
peer.port = 6881; /* scan int */ c;
break;
while( 1 ) { case 7:
if(!byte_diff(c,7,"numwant"))
/* scan int */ c;
else if(!byte_diff(c,7,"compact"))
/* scan flag */ c;
break;
case 9: /* info_hash= */
if(!byte_diff(c,9,"info_hash")) {
data = c; data = c;
switch( scan_urlencoded_query( &c, data, SCAN_SEARCHPATH_PARAM ) ) { /* ignore this, when we have less than 20 bytes */
case -1: /* error */ switch( scan_urlencoded_query( &c, data, SCAN_SEARCHPATH_VALUE ) )
case -1:
httperror(h,"404 Not Found","No such file or directory."); httperror(h,"404 Not Found","No such file or directory.");
goto e404; goto bailout;
case 4: case 20:
if(!byte_diff(c,4,"port")) hash = (ot_hash*)data;
/* scan int */ c;
else if(!byte_diff(c,4,"left"))
/* scan int */ c;
break;
case 7:
if(!byte_diff(c,7,"numwant"))
/* scan int */ c;
else if(!byte_diff(c,7,"compact"))
/* scan flag */ c;
break;
case 9: /* info_hash */
if(!byte_diff(c,9,"info_hash")) c;
/* scan 20 bytes */
break; break;
} default:
continue;
} }
break;
} }
break;
default: /* neither scrape nor announce */
httperror(h,"404 Not Found","No such file or directory.");
goto e404;
} }
}
c=h->hdrbuf=(char*)malloc(500); break;
c+=fmt_str(c,"HTTP/1.1 Coming Up\r\nContent-Type: text/plain"); default: /* neither scrape nor announce */
c+=fmt_str(c,"\r\nContent-Length: "); httperror(h,"404 Not Found","No such file or directory.");
/* ANSWER SIZE*/ goto bailout;
c+=fmt_ulonglong(c, 100 );
c+=fmt_str(c,"\r\nLast-Modified: ");
/* MODIFY DATE
c+=fmt_httpdate(c,s.st_mtime); */
c+=fmt_str(c,"\r\nConnection: close\r\n\r\n");
iob_addbuf(&h->iob,h->hdrbuf,c - h->hdrbuf);
iob_addbuf(&h->iob,tracker_answer, tracker_answer_size);
} }
e404:
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: ");
/* ANSWER SIZE*/
c+=fmt_ulonglong(c, 100 );
c+=fmt_str(c,"\r\nLast-Modified: ");
/* MODIFY DATE
c+=fmt_httpdate(c,s.st_mtime); */
c+=fmt_str(c,"\r\nConnection: close\r\n\r\n");
iob_addbuf(&h->iob,h->hdrbuf,c - h->hdrbuf);
iob_addbuf(&h->iob,tracker_answer, tracker_answer_size);
bailout:
io_dontwantread(s); io_dontwantread(s);
io_wantwrite(s); io_wantwrite(s);
} }

Loading…
Cancel
Save