summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--opentracker.c97
-rw-r--r--trackerlogic.c5
-rw-r--r--trackerlogic.h11
3 files changed, 22 insertions, 91 deletions
diff --git a/opentracker.c b/opentracker.c
index 1c0d558..16f2f1e 100644
--- a/opentracker.c
+++ b/opentracker.c
@@ -31,7 +31,6 @@ struct http_data {
31 io_batch iob; 31 io_batch iob;
32 char* hdrbuf; 32 char* hdrbuf;
33 int hlen; 33 int hlen;
34 int keepalive;
35}; 34};
36 35
37int header_complete(struct http_data* r) { 36int header_complete(struct http_data* r) {
@@ -58,9 +57,7 @@ void httperror(struct http_data* r,const char* title,const char* message) {
58 } else { 57 } else {
59 c+=fmt_str(c,"HTTP/1.0 "); 58 c+=fmt_str(c,"HTTP/1.0 ");
60 c+=fmt_str(c,title); 59 c+=fmt_str(c,title);
61 c+=fmt_str(c,"\r\nContent-Type: text/html\r\nConnection: "); 60 c+=fmt_str(c,"\r\nContent-Type: text/html\r\nConnection: close\r\nContent-Length: ");
62 c+=fmt_str(c,r->keepalive?"keep-alive":"close");
63 c+=fmt_str(c,"\r\nContent-Length: ");
64 c+=fmt_ulong(c,strlen(message)+strlen(title)+16-4); 61 c+=fmt_ulong(c,strlen(message)+strlen(title)+16-4);
65 c+=fmt_str(c,"\r\n\r\n<title>"); 62 c+=fmt_str(c,"\r\n\r\n<title>");
66 c+=fmt_str(c,title+4); 63 c+=fmt_str(c,title+4);
@@ -70,48 +67,6 @@ void httperror(struct http_data* r,const char* title,const char* message) {
70 iob_addbuf(&r->iob,r->hdrbuf,r->hlen); 67 iob_addbuf(&r->iob,r->hdrbuf,r->hlen);
71} 68}
72 69
73static struct mimeentry { const char* name, *type; } mimetab[] = {
74 { "html", "text/html" },
75 { "css", "text/css" },
76 { "dvi", "application/x-dvi" },
77 { "ps", "application/postscript" },
78 { "pdf", "application/pdf" },
79 { "gif", "image/gif" },
80 { "png", "image/png" },
81 { "jpeg", "image/jpeg" },
82 { "jpg", "image/jpeg" },
83 { "mpeg", "video/mpeg" },
84 { "mpg", "video/mpeg" },
85 { "avi", "video/x-msvideo" },
86 { "mov", "video/quicktime" },
87 { "qt", "video/quicktime" },
88 { "mp3", "audio/mpeg" },
89 { "ogg", "audio/x-oggvorbis" },
90 { "wav", "audio/x-wav" },
91 { "pac", "application/x-ns-proxy-autoconfig" },
92 { "sig", "application/pgp-signature" },
93 { "torrent", "application/x-bittorrent" },
94 { "class", "application/octet-stream" },
95 { "js", "application/x-javascript" },
96 { "tar", "application/x-tar" },
97 { "zip", "application/zip" },
98 { "dtd", "text/xml" },
99 { "xml", "text/xml" },
100 { "xbm", "image/x-xbitmap" },
101 { "xpm", "image/x-xpixmap" },
102 { "xwd", "image/x-xwindowdump" },
103 { 0,0 } };
104
105const char* mimetype(const char* filename) {
106 int i,e=str_rchr(filename,'.');
107 if (filename[e]==0) return "text/plain";
108 ++e;
109 for (i=0; mimetab[i].name; ++i)
110 if (str_equal(mimetab[i].name,filename+e))
111 return mimetab[i].type;
112 return "application/octet-stream";
113}
114
115const char* http_header(struct http_data* r,const char* h) { 70const char* http_header(struct http_data* r,const char* h) {
116 long i; 71 long i;
117 long l=array_bytes(&r->r); 72 long l=array_bytes(&r->r);
@@ -128,7 +83,6 @@ const char* http_header(struct http_data* r,const char* h) {
128 83
129void httpresponse(struct http_data* h,int64 s) { 84void httpresponse(struct http_data* h,int64 s) {
130 char* c; 85 char* c;
131 const char* m;
132 array_cat0(&h->r); 86 array_cat0(&h->r);
133 c=array_start(&h->r); 87 c=array_start(&h->r);
134 if (byte_diff(c,4,"GET ")) { 88 if (byte_diff(c,4,"GET ")) {
@@ -152,28 +106,15 @@ e404:
152 io_close(fd); 106 io_close(fd);
153 goto e404; 107 goto e404;
154 } 108 }
155 if ((m=http_header(h,"Connection"))) {
156 if (str_equal(m,"keep-alive"))
157 h->keepalive=1;
158 else
159 h->keepalive=0;
160 } else {
161 if (byte_equal(d+1,8,"HTTP/1.0"))
162 h->keepalive=0;
163 else
164 h->keepalive=1;
165 }
166 m=mimetype(c);
167 c=h->hdrbuf=(char*)malloc(500); 109 c=h->hdrbuf=(char*)malloc(500);
168 c+=fmt_str(c,"HTTP/1.1 Coming Up\r\nContent-Type: "); 110 c+=fmt_str(c,"HTTP/1.1 Coming Up\r\nContent-Type: text/plain");
169 c+=fmt_str(c,m);
170 c+=fmt_str(c,"\r\nContent-Length: "); 111 c+=fmt_str(c,"\r\nContent-Length: ");
112/* ANSWER SIZE*/
171 c+=fmt_ulonglong(c,s.st_size); 113 c+=fmt_ulonglong(c,s.st_size);
172 c+=fmt_str(c,"\r\nLast-Modified: "); 114 c+=fmt_str(c,"\r\nLast-Modified: ");
115/* MODIFY DATE */
173 c+=fmt_httpdate(c,s.st_mtime); 116 c+=fmt_httpdate(c,s.st_mtime);
174 c+=fmt_str(c,"\r\nConnection: "); 117 c+=fmt_str(c,"\r\nConnection: close\r\n\r\n");
175 c+=fmt_str(c,h->keepalive?"keep-alive":"close");
176 c+=fmt_str(c,"\r\n\r\n");
177 iob_addbuf(&h->iob,h->hdrbuf,c - h->hdrbuf); 118 iob_addbuf(&h->iob,h->hdrbuf,c - h->hdrbuf);
178 iob_addfile(&h->iob,fd,0,s.st_size); 119 iob_addfile(&h->iob,fd,0,s.st_size);
179 } 120 }
@@ -201,14 +142,6 @@ int main() {
201 if (i==s) { 142 if (i==s) {
202 int n; 143 int n;
203 while ((n=socket_accept6(s,ip,&port,&scope_id))!=-1) { 144 while ((n=socket_accept6(s,ip,&port,&scope_id))!=-1) {
204 char buf[IP6_FMT];
205 buffer_puts(buffer_2,"accepted new connection from ");
206 buffer_put(buffer_2,buf,fmt_ip6(buf,ip));
207 buffer_puts(buffer_2,":");
208 buffer_putulong(buffer_2,port);
209 buffer_puts(buffer_2," (fd ");
210 buffer_putulong(buffer_2,n);
211 buffer_puts(buffer_2,")");
212 if (io_fd(n)) { 145 if (io_fd(n)) {
213 struct http_data* h=(struct http_data*)malloc(sizeof(struct http_data)); 146 struct http_data* h=(struct http_data*)malloc(sizeof(struct http_data));
214 io_wantread(n); 147 io_wantread(n);
@@ -217,10 +150,8 @@ int main() {
217 io_setcookie(n,h); 150 io_setcookie(n,h);
218 } else 151 } else
219 io_close(n); 152 io_close(n);
220 } else { 153 } else
221 buffer_puts(buffer_2,", but io_fd failed.");
222 io_close(n); 154 io_close(n);
223 }
224 buffer_putnlflush(buffer_2); 155 buffer_putnlflush(buffer_2);
225 } 156 }
226 if (errno==EAGAIN) 157 if (errno==EAGAIN)
@@ -237,11 +168,6 @@ int main() {
237 iob_reset(&h->iob); 168 iob_reset(&h->iob);
238 free(h->hdrbuf); h->hdrbuf=0; 169 free(h->hdrbuf); h->hdrbuf=0;
239 } 170 }
240 buffer_puts(buffer_2,"io_tryread(");
241 buffer_putulong(buffer_2,i);
242 buffer_puts(buffer_2,"): ");
243 buffer_puterror(buffer_2);
244 buffer_putnlflush(buffer_2);
245 io_close(i); 171 io_close(i);
246 } else if (l==0) { 172 } else if (l==0) {
247 if (h) { 173 if (h) {
@@ -249,12 +175,8 @@ int main() {
249 iob_reset(&h->iob); 175 iob_reset(&h->iob);
250 free(h->hdrbuf); h->hdrbuf=0; 176 free(h->hdrbuf); h->hdrbuf=0;
251 } 177 }
252 buffer_puts(buffer_2,"eof on fd #");
253 buffer_putulong(buffer_2,i);
254 buffer_putnlflush(buffer_2);
255 io_close(i); 178 io_close(i);
256 } else if (l>0) { 179 } else if (l>0) {
257buffer_puts(buffer_2,"Garr");
258 array_catb(&h->r,buf,l); 180 array_catb(&h->r,buf,l);
259 if (array_failed(&h->r)) { 181 if (array_failed(&h->r)) {
260 httperror(h,"500 Server Error","request too long."); 182 httperror(h,"500 Server Error","request too long.");
@@ -272,17 +194,12 @@ emerge:
272 while ((i=io_canwrite())!=-1) { 194 while ((i=io_canwrite())!=-1) {
273 struct http_data* h=io_getcookie(i); 195 struct http_data* h=io_getcookie(i);
274 int64 r=iob_send(i,&h->iob); 196 int64 r=iob_send(i,&h->iob);
275/* printf("iob_send returned %lld\n",r); */
276 if (r==-1) io_eagain(i); else 197 if (r==-1) io_eagain(i); else
277 if (r<=0) { 198 if (r<=0) {
278 array_trunc(&h->r); 199 array_trunc(&h->r);
279 iob_reset(&h->iob); 200 iob_reset(&h->iob);
280 free(h->hdrbuf); h->hdrbuf=0; 201 free(h->hdrbuf); h->hdrbuf=0;
281 if (h->keepalive) { 202 io_close(i);
282 io_dontwantwrite(i);
283 io_wantread(i);
284 } else
285 io_close(i);
286 } 203 }
287 } 204 }
288 } 205 }
diff --git a/trackerlogic.c b/trackerlogic.c
index a19f9d4..735041e 100644
--- a/trackerlogic.c
+++ b/trackerlogic.c
@@ -250,10 +250,13 @@ void count_peers_and_seeds( ot_peer peer_list, unsigned long *peers, unsigned lo
250 } while( compare_ip_port( peer_list + *peers, peer_list + *peers - 1 ) < 0 ); 250 } while( compare_ip_port( peer_list + *peers, peer_list + *peers - 1 ) < 0 );
251} 251}
252 252
253int init_logic( ) { 253int init_logic( char *directory ) {
254 glob_t globber; 254 glob_t globber;
255 int i; 255 int i;
256 256
257 if( directory )
258 chdir( directory );
259
257 scratch_space = map_file( "" ); 260 scratch_space = map_file( "" );
258 torrents_list = map_file( "" ); 261 torrents_list = map_file( "" );
259 torrents_count = 0; 262 torrents_count = 0;
diff --git a/trackerlogic.h b/trackerlogic.h
index 46efb57..03ed577 100644
--- a/trackerlogic.h
+++ b/trackerlogic.h
@@ -57,4 +57,15 @@ void *binary_search( const void *key, const void *base,
57 int (*compar) (const void *, const void *), 57 int (*compar) (const void *, const void *),
58 int *exactmatch ); 58 int *exactmatch );
59 59
60//
61// Exported functions
62//
63
64int init_logic( char *chdir_directory );
65void deinit_logic( );
66
67ot_torrent add_peer_to_torrent( ot_hash hash, ot_peer peer );
68void return_peers_for_torrent( ot_torrent torrent, unsigned long amount, char *reply );
69void heal_torrent( ot_torrent torrent );
70
60#endif 71#endif