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