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