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