Changeset 362
- Timestamp:
- 06/21/08 18:44:59 (5 months ago)
- Files:
-
- trunk/tools/tools/downloader.d (modified) (9 diffs)
- trunk/tools/tools/log.d (modified) (2 diffs)
- trunk/tools/tools/threadpool.d (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/tools/tools/downloader.d
r361 r362 11 11 } 12 12 13 void filterLink(ref string addr ) {13 void filterLink(ref string addr, ref string prepend) { 14 14 while (addr.length && addr[$-1]==' ') addr=addr[0..$-1]; 15 string post, refer ;15 string post, refer, cookie; 16 16 bool old_html=false; 17 17 if (addr.startsWith("POST=")) { post=addr[0..addr.find(" ")+1]; addr=addr[addr.find(" ")+1..$]; } 18 18 if (addr.startsWith("REFER=")) { refer=addr[0..addr.find(" ")+1]; addr=addr[addr.find(" ")+1..$]; } 19 if (addr.startsWith("COOKIE=")) { cookie=addr[0..addr.find(" ")+1]; addr=addr[addr.find(" ")+1..$]; } 19 20 if (addr.startsWith("1.0")) { old_html=true; addr=addr[addr.find(" ")+1..$]; } 20 21 21 22 addr=addr.replace("&", "&").replace(" ", "%20"); 22 23 23 addr=post~refer~(old_html?"1.0 ":"")~addr; 24 } 24 prepend = post~refer~cookie~(old_html?"1.0 ":""); 25 } 26 27 bool quiet = false; 25 28 26 29 char[] followLink(char[] oldAddr, char[] newAddr) { 27 newAddr.filterLink(); 28 if (oldAddr.length>7 && oldAddr[0..7]=="http://") oldAddr=oldAddr[7..$]; 29 if (newAddr.beginsWith("data://")) return newAddr; 30 string new_prepend; newAddr.filterLink(new_prepend); 31 string old_prepend; oldAddr.filterLink(old_prepend); 32 string[string] tags; 33 foreach (entry; old_prepend.split(" ")~new_prepend.split(" ")) { 34 if (!entry.length) continue; 35 auto sp = entry.split("="); 36 tags[sp[0]] = (sp.length>1)?(sp[1..$].join("=")):""; 37 } 30 38 string prepend; 31 auto spaceloc=newAddr.rfind(" "); 32 if (spaceloc!=-1) { prepend=newAddr[0..spaceloc+1]; newAddr=newAddr[spaceloc+1..$]; } 33 if (newAddr.length>7 && newAddr[0..7]=="http://") return prepend~newAddr[7..$]; 39 foreach (key, value; tags) prepend ~= key~"="~value~" "; 40 41 if (auto rest = oldAddr.startsWith("http://")) oldAddr = rest; 42 if (newAddr.startsWith("data://")) return new_prepend~newAddr; 43 if (auto rest = newAddr.startsWith("http://")) return prepend~rest; 34 44 if (!newAddr.length) throw new Exception("Empty newAddr found! Aborting followLink."); 35 45 if (newAddr[0]=='/') return prepend~oldAddr.split("/")[0]~newAddr; … … 96 106 if (url=="") throw new Exception("URL empty"); 97 107 if (url.beginsWith("data://")) return "<data>"~url[7 .. $]; 98 string post ="";108 string post; 99 109 if (auto rest=url.startsWith("POST=")) { 100 110 url=rest; 101 111 post=url[0..url.find(" ")]; 102 112 url=url[url.find(" ")+1..$]; 103 logln("Posting ", post);113 if (!quiet) logln("Posting ", post); 104 114 } 105 115 string refer; … … 108 118 url=rest[rest.find(" ")+1..$]; 109 119 if (refer.length<7 || refer[0..7]!="http://") refer="http://"~refer; // referrer needs that 110 logln("Refer ", refer); 120 if (!quiet) logln("Refer ", refer); 121 } 122 string cookie; 123 if (auto rest=url.startsWith("COOKIE=")) { 124 cookie=rest[0..rest.find(" ")]; 125 url=rest[rest.find(" ")+1..$]; 126 if (!quiet) logln("Cookie ", cookie); 111 127 } 112 128 bool http_1_0=false; 113 129 if (auto rest=url.startsWith("1.0 ")) { 114 130 url=rest; 115 logln("Fallback to HTTP 1.0");131 if (!quiet) logln("Fallback to HTTP 1.0"); 116 132 http_1_0=true; 117 133 } … … 177 193 if (!retries) throw new Exception("Can't download page."); 178 194 retries--; 179 //logln("Downloading ", folder);180 195 if (post.length) sock=openSocket("POST "~folder~" HTTP/"~(http_1_0?"1.0":"1.1")~"\r\n"); 181 196 else sock=openSocket("GET "~folder~" HTTP/"~(http_1_0?"1.0":"1.1")~"\r\n"); … … 189 204 if (refer.length) 190 205 sock.send("Referer: "~refer~"\r\n"); 206 if (cookie.length) 207 sock.send("Cookie: "~cookie~"\r\n"); 191 208 sock.send("\r\n"~post); 192 209 string received; … … 202 219 if (ifind(header, "\r\ncontent-length:")==-1) { 203 220 if (ifind(header, "\r\ntransfer-encoding: chunked")!=-1) { 204 logln("Recognize chunked mode");221 if (!quiet) logln("Recognize chunked mode"); 205 222 chunked=true; 206 223 } … … 216 233 if (auto rest = line.startsWith("Location: ")) { 217 234 string loc = param.followLink(rest); 218 logln("Following redirect to ", loc);235 if (!quiet) logln("Following redirect to ", loc); 219 236 if (redirect) { *redirect=loc; res=null; return true; } 220 237 void delegate(float) dg=(float f) { download_callbacks.doCbs(param, f); }; … … 254 271 } 255 272 auto chars=hex2ulong(line); 256 //logln("line ", line, ", chars ", chars, ", received now ", received.length, ", line ends at ", line_end);257 273 if (chars) { 258 274 if (received.length < line_end+2) return true; … … 285 301 if (chunked) if (receiveChunked) continue; else break; 286 302 string _res; if (checkProgress(chunked, _res)) return _res; 287 if (length && (received.length !< length)) { logln("Received all data"); break; }303 if (length && (received.length !< length)) { if (!quiet) logln("Received all data"); break; } 288 304 } 289 305 if (!header.length) { 290 logln("Connection closed, but no body found. Presuming invalid persistant connection; retrying ... ");306 if (!quiet) logln("Connection closed, but no body found. Presuming invalid persistant connection; retrying ... "); 291 307 goto retry; 292 308 } trunk/tools/tools/log.d
r361 r362 7 7 int getThread() { foreach (int idx, thr; Thread.getAll) if (thr.isSelf) return idx; throw new Exception("Not in a known thread"); } 8 8 9 bool log_threads = true; 10 9 11 // This _needs_ to be outside the function because it's a template func 10 12 Object log_sync=null; 11 13 FILE * logfile=null; 12 14 static ~this() { 13 if (l astThread) log("\\", repeat("-", Format("+--[ ", lastThread, " ]------").length-1), "\n");15 if (log_threads && lastThread) log("\\", repeat("-", Format("+--[ ", lastThread, " ]------").length-1), "\n"); 14 16 if (logfile) fclose(logfile); 15 17 } … … 79 81 if (!log_sync) log_sync = new Object; 80 82 synchronized (log_sync) { 81 if (l astThread !is thr)83 if (log_threads && (lastThread !is thr)) 82 84 _log(prefix, "--[ ", threadname, " ]------", "\n"); 83 _log("| ", t, "\n"); 85 if (log_threads) _log("| ", t, "\n"); 86 else _log(t, "\n"); 84 87 } 85 88 lastThread=thr; trunk/tools/tools/threadpool.d
r359 r362 22 22 bool finish=false; 23 23 MessageMultiChannel!(task, false, false) tasks; 24 int getThreadId() { foreach (id, thread; threads) if (thread.isSelf) return id; throw new Exception("Not a thread of this pool!"); } 24 25 task getTask() { if (finish) return task("ShutdownThreadsBusy", &shutdown_me); else return tasks.get(); } 25 26 void addTask(string name, proc c) { tasks.put(task(name, c)); }
