OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 function Download(url, path, verbose) { | 5 function Download(url, path, verbose) { |
6 if (verbose) { | 6 if (verbose) { |
7 WScript.StdOut.Write(" * GET " + url + "..."); | 7 WScript.StdOut.Write(" * GET " + url + "..."); |
8 } | 8 } |
9 try { | 9 try { |
10 xml_http = new ActiveXObject("MSXML2.ServerXMLHTTP"); | 10 xml_http = new ActiveXObject("MSXML2.ServerXMLHTTP"); |
11 } catch (e) { | 11 } catch (e) { |
12 WScript.StdOut.WriteLine("[-] XMLHTTP " + new Number(e.number).toHex() + | 12 WScript.StdOut.WriteLine("[-] XMLHTTP " + new Number(e.number).toHex() + |
13 ": Cannot create Active-X object (" + e.description) + ")."; | 13 ": Cannot create Active-X object (" + e.description) + ")."; |
14 WScript.Quit(1); | 14 WScript.Quit(1); |
15 } | 15 } |
16 try { | 16 try { |
17 xml_http.open("GET", url, false); | 17 xml_http.open("GET", url, false); |
18 } catch (e) { | 18 } catch (e) { |
19 WScript.StdOut.WriteLine("[-] XMLHTTP " + new Number(e.number).toHex() + | 19 WScript.StdOut.WriteLine("[-] XMLHTTP " + new Number(e.number).toHex() + |
20 ": invalid URL."); | 20 ": invalid URL."); |
21 WScript.Quit(1); | 21 WScript.Quit(1); |
22 } | 22 } |
23 | 23 |
24 var response_body = null; | 24 var response_body = null; |
25 var size_description = "?"; | 25 var size_description = "?"; |
26 var file_size; | 26 var file_size; |
27 try { | 27 try { |
28 xml_http.send(null); | 28 xml_http.send(null); |
29 if (xml_http.status != 200) { | 29 if (xml_http.status != 200) { |
30 WScript.StdOut.WriteLine("[-] HTTP " + xml_http.status + " " + | 30 WScript.StdOut.WriteLine("[-] HTTP " + xml_http.status + " " + |
31 xml_http.statusText); | 31 xml_http.statusText); |
32 WScript.Quit(1); | 32 WScript.Quit(1); |
33 } | 33 } |
34 response_body = xml_http.responseBody; | 34 response_body = xml_http.responseBody; |
35 size_description = xml_http.getResponseHeader("Content-Length"); | 35 size_description = xml_http.getResponseHeader("Content-Length"); |
36 if (size_description != "") { | 36 if (size_description != "") { |
37 file_size = parseInt(size_description) | 37 file_size = parseInt(size_description) |
38 size_description = file_size.toBytes(); | 38 size_description = file_size.toBytes(); |
39 } else { | 39 } else { |
40 try { | 40 try { |
41 file_size = new Number(xml_http.responseText.length) | 41 file_size = new Number(xml_http.responseText.length) |
42 size_description = file_size.toBytes(); | 42 size_description = file_size.toBytes(); |
43 } catch(e) { | 43 } catch(e) { |
44 size_description = "unknown size"; | 44 size_description = "unknown size"; |
45 } | 45 } |
46 } | 46 } |
47 } catch (e) { | 47 } catch (e) { |
48 WScript.StdOut.WriteLine("[-] XMLHTTP " + new Number(e.number).toHex() + | 48 WScript.StdOut.WriteLine("[-] XMLHTTP " + new Number(e.number).toHex() + |
49 ": Cannot make HTTP request (" + e.description) + ")"; | 49 ": Cannot make HTTP request (" + e.description) + ")"; |
50 WScript.Quit(1); | 50 WScript.Quit(1); |
51 } | 51 } |
52 | 52 |
53 if (verbose) { | 53 if (verbose) { |
54 WScript.StdOut.WriteLine("ok (" + size_description + ")."); | 54 WScript.StdOut.WriteLine("ok (" + size_description + ")."); |
55 WScript.StdOut.Write(" * Save " + path + "..."); | 55 WScript.StdOut.Write(" * Save " + path + "..."); |
56 } | 56 } |
57 | 57 |
58 try { | 58 try { |
59 var adodb_stream = new ActiveXObject("ADODB.Stream"); | 59 var adodb_stream = new ActiveXObject("ADODB.Stream"); |
60 adodb_stream.Mode = 3; // ReadWrite | 60 adodb_stream.Mode = 3; // ReadWrite |
61 adodb_stream.Type = 1; // 1= Binary | 61 adodb_stream.Type = 1; // 1= Binary |
62 adodb_stream.Open(); // Open the stream | 62 adodb_stream.Open(); // Open the stream |
63 adodb_stream.Write(response_body); // Write the data | 63 adodb_stream.Write(response_body); // Write the data |
64 adodb_stream.SaveToFile(path, 2); // Save to our destination | 64 adodb_stream.SaveToFile(path, 2); // Save to our destination |
65 adodb_stream.Close(); | 65 adodb_stream.Close(); |
66 } catch(e) { | 66 } catch(e) { |
67 WScript.StdOut.WriteLine("[-] ADODB.Stream " + new Number( | 67 WScript.StdOut.WriteLine( |
68 e.number).toHex() + ": Cannot save file (" + e.description + ")"); | 68 "[-] ADODB.Stream " + new Number(e.number).toHex() + |
| 69 ": Cannot save file to " + path + ": " + e.description); |
69 WScript.Quit(1); | 70 WScript.Quit(1); |
70 } | 71 } |
71 if (typeof(file_size) != undefined) { | 72 if (typeof(file_size) != undefined) { |
72 var file_system_object = WScript.CreateObject("Scripting.FileSystemObject") | 73 var file_system_object = WScript.CreateObject("Scripting.FileSystemObject") |
73 var file = file_system_object.GetFile(path) | 74 var file = file_system_object.GetFile(path) |
74 if (file.Size < file_size) { | 75 if (file.Size < file_size) { |
75 WScript.StdOut.WriteLine("[-] File only partially downloaded."); | 76 WScript.StdOut.WriteLine("[-] File only partially downloaded."); |
76 WScript.Quit(1); | 77 WScript.Quit(1); |
77 } | 78 } |
78 } | 79 } |
79 if (verbose) { | 80 if (verbose) { |
80 WScript.StdOut.WriteLine("ok."); | 81 WScript.StdOut.WriteLine("ok."); |
81 } | 82 } |
82 } | 83 } |
83 | 84 |
84 // Utilities | 85 // Utilities |
85 Number.prototype.isInt = function NumberIsInt() { | 86 Number.prototype.isInt = function NumberIsInt() { |
86 return this % 1 == 0; | 87 return this % 1 == 0; |
87 }; | 88 }; |
88 Number.prototype.toBytes = function NumberToBytes() { | 89 Number.prototype.toBytes = function NumberToBytes() { |
89 // Returns a "pretty" string representation of a number of bytes: | 90 // Returns a "pretty" string representation of a number of bytes: |
90 var units = ["KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; | 91 var units = ["KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; |
91 var unit = "bytes"; | 92 var unit = "bytes"; |
92 var limit = 1; | 93 var limit = 1; |
93 while(this > limit * 1100 && units.length > 0) { | 94 while(this > limit * 1100 && units.length > 0) { |
94 limit *= 1024; | 95 limit *= 1024; |
95 unit = units.shift(); | 96 unit = units.shift(); |
96 } | 97 } |
97 return (Math.round(this * 100 / limit) / 100).toString() + " " + unit; | 98 return (Math.round(this * 100 / limit) / 100).toString() + " " + unit; |
98 }; | 99 }; |
99 Number.prototype.toHex = function NumberToHex(length) { | 100 Number.prototype.toHex = function NumberToHex(length) { |
100 if (arguments.length == 0) length = 1; | 101 if (arguments.length == 0) length = 1; |
101 if (typeof(length) != "number" && !(length instanceof Number)) { | 102 if (typeof(length) != "number" && !(length instanceof Number)) { |
102 throw Exception("Length must be a positive integer larger than 0.", | 103 throw Exception("Length must be a positive integer larger than 0.", |
103 TypeError, 0); | 104 TypeError, 0); |
104 } | 105 } |
105 if (length < 1 || !length.isInt()) { | 106 if (length < 1 || !length.isInt()) { |
106 throw Exception("Length must be a positive integer larger than 0.", | 107 throw Exception("Length must be a positive integer larger than 0.", |
107 "RangeError", 0); | 108 "RangeError", 0); |
108 } | 109 } |
109 var result = (this + (this < 0 ? 0x100000000 : 0)).toString(16); | 110 var result = (this + (this < 0 ? 0x100000000 : 0)).toString(16); |
110 while (result.length < length) result = "0" + result; | 111 while (result.length < length) result = "0" + result; |
111 return result; | 112 return result; |
112 }; | 113 }; |
113 | 114 |
114 if (WScript.Arguments.length != 2) { | 115 if (WScript.Arguments.length != 2) { |
115 WScript.StdOut.Write("Incorrect arguments to get_file.js") | 116 WScript.StdOut.Write("Incorrect arguments to get_file.js") |
116 } else { | 117 } else { |
117 Download(WScript.Arguments(0), WScript.Arguments(1), false); | 118 Download(WScript.Arguments(0), WScript.Arguments(1), false); |
118 } | 119 } |
OLD | NEW |