diff options
Diffstat (limited to 'opentracker.c')
| -rw-r--r-- | opentracker.c | 61 |
1 files changed, 41 insertions, 20 deletions
diff --git a/opentracker.c b/opentracker.c index 8313d90..d46858d 100644 --- a/opentracker.c +++ b/opentracker.c | |||
| @@ -97,15 +97,20 @@ const char* http_header(struct http_data* r,const char* h) | |||
| 97 | if (*c==' ' || *c=='\t') ++c; | 97 | if (*c==' ' || *c=='\t') ++c; |
| 98 | return c; | 98 | return c; |
| 99 | } | 99 | } |
| 100 | return 0; | 100 | return 0; |
| 101 | } | 101 | } |
| 102 | return 0; | ||
| 102 | } | 103 | } |
| 103 | 104 | ||
| 104 | void httpresponse(struct http_data* h,int64 s) | 105 | void httpresponse(struct http_data* h,int64 s) |
| 105 | { | 106 | { |
| 106 | char *c, *d, *data; | 107 | char *c, *d, *data, *reply = NULL; |
| 107 | struct ot_peer peer; | 108 | struct ot_peer peer; |
| 109 | ot_torrent torrent; | ||
| 108 | ot_hash *hash = NULL; | 110 | ot_hash *hash = NULL; |
| 111 | unsigned long numwant; | ||
| 112 | int compact; | ||
| 113 | size_t reply_size = 0; | ||
| 109 | 114 | ||
| 110 | array_cat0(&h->r); | 115 | array_cat0(&h->r); |
| 111 | 116 | ||
| @@ -128,10 +133,6 @@ e400: | |||
| 128 | while (c[1]=='/') ++c; | 133 | while (c[1]=='/') ++c; |
| 129 | 134 | ||
| 130 | switch( scan_urlencoded_query( &c, data = c, SCAN_PATH ) ) { | 135 | switch( scan_urlencoded_query( &c, data = c, SCAN_PATH ) ) { |
| 131 | case 0: | ||
| 132 | e404: | ||
| 133 | httperror(h,"404 Not Found","No such file or directory."); | ||
| 134 | goto bailout; | ||
| 135 | case 6: /* scrape ? */ | 136 | case 6: /* scrape ? */ |
| 136 | if (!byte_diff(c,6,"scrape")) | 137 | if (!byte_diff(c,6,"scrape")) |
| 137 | goto e404; | 138 | goto e404; |
| @@ -142,6 +143,8 @@ e404: | |||
| 142 | 143 | ||
| 143 | byte_copy( peer.ip, 4, h->ip ); | 144 | byte_copy( peer.ip, 4, h->ip ); |
| 144 | peer.port = 6881; | 145 | peer.port = 6881; |
| 146 | numwant = 50; | ||
| 147 | compact = 1; | ||
| 145 | 148 | ||
| 146 | while( 1 ) { | 149 | while( 1 ) { |
| 147 | switch( scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_PARAM ) ) { | 150 | switch( scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_PARAM ) ) { |
| @@ -158,24 +161,42 @@ e404: | |||
| 158 | /* scan int */ c; | 161 | /* scan int */ c; |
| 159 | else if(!byte_diff(c,7,"compact")) | 162 | else if(!byte_diff(c,7,"compact")) |
| 160 | /* scan flag */ c; | 163 | /* scan flag */ c; |
| 161 | break; | 164 | break; |
| 162 | case 9: /* info_hash= */ | 165 | case 9: |
| 163 | if(!byte_diff(c,9,"info_hash")) { | 166 | if(byte_diff(c,9,"info_hash")) |
| 164 | /* ignore this, when we have less than 20 bytes */ | 167 | continue; |
| 165 | switch( scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE ) ) | 168 | /* ignore this, when we have less than 20 bytes */ |
| 166 | case -1: | 169 | switch( scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE ) ) { |
| 167 | httperror(h,"404 Not Found","No such file or directory."); | 170 | case -1: |
| 168 | goto bailout; | 171 | goto e404; |
| 169 | case 20: | 172 | case 20: |
| 170 | hash = (ot_hash*)data; /* Fall through intended */ | 173 | hash = (ot_hash*)data; /* Fall through intended */ |
| 171 | default: | 174 | default: |
| 172 | continue; | 175 | continue; |
| 173 | } | 176 | } |
| 174 | break; | 177 | default: |
| 178 | continue; | ||
| 175 | } | 179 | } |
| 176 | } | 180 | } |
| 181 | |||
| 182 | /* Scanned whole query string */ | ||
| 183 | if( !hash || ( compact == 0 ) ) goto e404; | ||
| 184 | torrent = add_peer_to_torrent( hash, &peer ); | ||
| 185 | if( !torrent ) { | ||
| 186 | e500: | ||
| 187 | httperror(h,"500 Internal Server Error","A server error has occured. Please retry later."); | ||
| 188 | goto bailout; | ||
| 189 | } | ||
| 190 | reply = malloc( numwant*6+10 ); | ||
| 191 | if( reply ) | ||
| 192 | reply_size = return_peers_for_torrent( torrent, numwant, reply ); | ||
| 193 | if( !reply || reply_size < 0 ) { | ||
| 194 | if( reply ) free( reply ); | ||
| 195 | goto e500; | ||
| 196 | } | ||
| 177 | break; | 197 | break; |
| 178 | default: /* neither scrape nor announce */ | 198 | default: /* neither scrape nor announce */ |
| 199 | e404: | ||
| 179 | httperror(h,"404 Not Found","No such file or directory."); | 200 | httperror(h,"404 Not Found","No such file or directory."); |
| 180 | goto bailout; | 201 | goto bailout; |
| 181 | } | 202 | } |
| @@ -190,7 +211,7 @@ e404: | |||
| 190 | c+=fmt_httpdate(c,s.st_mtime); */ | 211 | c+=fmt_httpdate(c,s.st_mtime); */ |
| 191 | c+=fmt_str(c,"\r\nConnection: close\r\n\r\n"); | 212 | c+=fmt_str(c,"\r\nConnection: close\r\n\r\n"); |
| 192 | iob_addbuf(&h->iob,h->hdrbuf,c - h->hdrbuf); | 213 | iob_addbuf(&h->iob,h->hdrbuf,c - h->hdrbuf); |
| 193 | iob_addbuf(&h->iob,tracker_answer, tracker_answer_size); | 214 | if( reply && reply_size ) iob_addbuf(&h->iob,reply, reply_size ); |
| 194 | 215 | ||
| 195 | bailout: | 216 | bailout: |
| 196 | io_dontwantread(s); | 217 | io_dontwantread(s); |
