forked from grefel/restix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestix.jsx
More file actions
320 lines (262 loc) · 10.5 KB
/
restix.jsx
File metadata and controls
320 lines (262 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/****************
# Connect InDesign to the web
* HTTPS supported
* Works form CS4 to CC 2018 (ExtendScript based library)
* Based on VBScript/ServerXMLHTTP (Win) AppleScript/curl (Mac) relies on app.doScript()
## Getting started
See examples/connect.jsx
* @Version: 1.2
* @Date: 2018-11-24
* @Author: Gregor Fellenz, http://www.publishingx.de
* Acknowledgments:
** Library design pattern from Marc Aturet https://forums.adobe.com/thread/1111415
*/
$.global.hasOwnProperty('restix') || ( function (HOST, SELF) {
HOST[SELF] = SELF;
/****************
* PRIVATE
*/
var INNER = {};
INNER.version = "2018-11-24-1.2";
/** Returns if the operating system is windows
* @return {String} true | false
*/
INNER.isWindows = function () {
return ($.os.indexOf ("Windows") > -1);
}
/** Check the request information object and construct a full URL
* @param {request} Request information object
* @returns{request} Request information object or throws an error
*/
INNER.checkRequest = function (request) {
if (request.url == undefined || request.url == "") throw Error ("No property [url] found/set");
if (request.url.toString().slice(-1) == "/") request.url = request.url.toString().slice(0, -1);
if (request.command == undefined) request.command = "";
if (request.command.toString()[0] == "/") request.command = request.command.toString().substr(1);
if (request.port == undefined) request.port = "";
if ( isNaN(request.port) ) throw Error ("[port] is Not a Number");
// Add port
if (request.port != "") {
request.fullURL = request.url + ":" + request.port;
}
else {
request.fullURL = request.url;
}
// Add command
if (request.command != "") {
request.fullURL = request.fullURL + "/" + request.command;
}
if (request.method == undefined || request.method == "") request.method = "GET";
if (! ( request.method == "GET" || request.method == "POST" || request.method == "PUT" || request.method == "PATCH" || request.method == "DELETE")) throw Error ("Method " + request.method + " is not supported"); // Missing HEAD
if (request.method == "POST" && ( request.binaryFilePath == undefined || request.binaryFilePath == "" )) request.binaryFilePath = false;
if (request.headers == undefined) request.headers = [];
if ( ! ( request.headers instanceof Array ) ) throw Error ("Provide [headers] as Array of {name:'',value''} objects");
if (request.body == undefined || request.body == "") request.body = false;
if (request.body && request.binaryFilePath) throw Error ("You must not provide [body] and [binaryFilePath]");
request.unsafe = false;
if (request.proxy == undefined) request.proxy = false;
return request;
}
/** The main connection function. Need to be slashed
* @return {response} Response result object
*/
INNER.processRequest = function (request, outFile) {
var response = {
error:false,
errorMsg:"",
body:"",
httpStatus:900
};
var scriptCommands = [];
var systemCmd = "";
var result = "";
if (INNER.isWindows()) {
if (request.proxy != false) {
scriptCommands.push('Dim xHttp : Set xHttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")');
}
else {
scriptCommands.push('Dim xHttp : Set xHttp = CreateObject("MSXML2.ServerXMLHTTP")');
}
// Konstanten für ADODB.Stream
scriptCommands.push('Const adTypeBinary = 1');
scriptCommands.push('Const adSaveCreateOverWrite = 2');
scriptCommands.push('Const adModeReadWrite = 3');
scriptCommands.push('Dim res');
scriptCommands.push('On Error Resume Next');
scriptCommands.push('xHttp.Open "' + request.method + '", "'+ request.fullURL +'", False');
if (request.proxy != false) {
scriptCommands.push('xHttp.setProxy 2, "' + request.proxy + '"') ;
}
for (var i = 0; i < request.headers.length; i++) {
scriptCommands.push('xHttp.setRequestHeader "' + request.headers[i].name + '","' + request.headers[i].value + '"');
}
if (request.unsafe) {
//~ ' 2 stands for SXH_OPTION_IGNORE_SERVER_SSL_CERT_ERROR_FLAGS
//~ ' 13056 means ignore all server side cert error
scriptCommands.push('xHttp.setOption 2, 13056');
}
if (request.body) {
scriptCommands.push('xHttp.Send "' + request.body.replace(/"/g, '""').replace(/\n|\r/g, '') + '"');
}
else if (request.method == "POST" && request.binaryFilePath) {
// http://www.vbforums.com/showthread.php?418570-RESOLVED-HTTP-POST-a-zip-file
scriptCommands.push(' Dim sFile');
scriptCommands.push(' sFile = "' + request.binaryFilePath + '"');
scriptCommands.push(' Set objStream = CreateObject("ADODB.Stream")');
scriptCommands.push(' objStream.Type = adTypeBinary');
scriptCommands.push(' objStream.Mode = adModeReadWrite');
scriptCommands.push(' objStream.Open');
scriptCommands.push(' objStream.LoadFromFile(sFile)');
scriptCommands.push(' xHttp.SetRequestHeader "Content-Length", objStream.Size');
scriptCommands.push(' xHttp.Send objStream.Read(objStream.Size)');
scriptCommands.push(' Set objStream= Nothing');
}
scriptCommands.push('If err.Number = 0 Then');
if (outFile) {
scriptCommands.push(' Set objStream = CreateObject("ADODB.Stream")');
scriptCommands.push(' objStream.Type = adTypeBinary');
scriptCommands.push(' objStream.Mode = adModeReadWrite');
scriptCommands.push(' objStream.Open');
scriptCommands.push(' objStream.Write xHttp.responseBody');
scriptCommands.push(' objStream.SaveToFile "' + outFile.fsName + '" , adSaveCreateOverWrite');
scriptCommands.push(' objStream.Close');
scriptCommands.push(' Set objStream= Nothing');
/*
ADODB.Stream let's you also save text data and let's you specify charset (codepage) for text-to-binary data conversion (against of Scripting.TextStream object).
Const adTypeText = 2
Const adSaveCreateOverWrite = 2
'Create Stream object
Dim BinaryStream
Set BinaryStream = CreateObject("ADODB.Stream")
'Specify stream type - we want To save text/string data.
BinaryStream.Type = adTypeText
'Specify charset For the source text (unicode) data.
If Len(CharSet) > 0 Then
BinaryStream.CharSet = CharSet
End If
'Open the stream And write binary data To the object
BinaryStream.Open
BinaryStream.WriteText Text
'Save binary data To disk
BinaryStream.SaveToFile FileName, adSaveCreateOverWrite
End Function
*/
scriptCommands.push(' res = "outFile" & vbCr & "------http_code" & xHttp.status' );
}
else {
// ' give respones
scriptCommands.push(' res = xHttp.responseText & vbCr & "------http_code" & xHttp.status' );
}
scriptCommands.push('Else');
scriptCommands.push(' res = "xHttpError " & Err.Description & " " & Err.Number');
scriptCommands.push('End If');
scriptCommands.push('Set xHttp = Nothing');
scriptCommands.push('returnValue = res');
scriptCommands = scriptCommands.join("\r\n");
try {
result = app.doScript(scriptCommands, ScriptLanguage.VISUAL_BASIC);
}
catch (e) {
result = "doScriptError: " + e.message;
}
}
else { // Mac
// -L follow redirects
var curlString = 'curl --silent --show-error -g -L ';
for (var i = 0; i < request.headers.length; i++) {
curlString += (' -H \'' + request.headers[i].name + ': ' + request.headers[i].value + '\'');
}
if (request.unsafe) {
// Es gab einen Fall wo am Mac mit -k es nicht funktioniert hat curl: (35) Server aborted the SSL handshake
curlString += ' -k ';
}
if (request.proxy != false) {
curlString += ' --proxy ' + request.proxy
}
curlString += ' -X ' + request.method;
if (request.body) {
curlString += ' -d \'' + request.body.replace(/"/g, '\\"').replace(/\n|\r/g, '') + '\'';
}
else if (request.method == "POST" && request.binaryFilePath) {
curlString += ' --data-binary \'@' + request.binaryFilePath + '\'';
}
if (outFile) {
curlString += ' -w \'outFile\n------http_code%{http_code}\'';
curlString += ' -o \'' + outFile.fsName+ '\''
}
else {
curlString += ' -w \'\n------http_code%{http_code}\'';
}
curlString += ' \'' + request.fullURL + '\'';
//~ $.writeln(curlString);
try {
result = app.doScript('do shell script "' + curlString + '"', ScriptLanguage.APPLESCRIPT_LANGUAGE);
}
catch (e) {
result = "doScriptError: " + e.message;
}
}
// Fill response
if (result.match (/^xHttpError|^curl: \(\d+\)|^doScriptError:/)) {
response.error = true;
response.errorMsg = result;
}
else {
var resArray = result.split("\r------http_code");
if (resArray.length == 2) {
response.httpStatus = resArray[1] * 1;
response.body = resArray[0];
}
else {
throw Error ("Wrong result value: " + result);
}
}
return response;
}
/****************
* API
*/
/** Process an HTTP Request
* @param {request} Request object with connection Information
* @return {response} Response object {error:error, errorMsg:errorMsg, body:body, httpStatus:httpStatus}
*/
SELF.fetch = function (request) {
request = INNER.checkRequest(request);
return INNER.processRequest(request, false);
}
/** Process an HTTP Request and writes the result to a give File
* @param {request} Request Object with connection Information
* @param {outFile} File to write to
* @return {response} Response object {error:error, errorMsg:errorMsg, body:body, httpStatus:httpStatus}
*/
SELF.fetchFile = function (request, outFile) {
if (outFile == undefined) throw Error ("No file provided");
if (outFile instanceof String) outFile = File (outFile);
request = INNER.checkRequest(request);
var response = INNER.processRequest(request, outFile);
if (!outFile.exists) {
response.error = true;
response.errorMsg = "File was not created\n" + response.errorMsg;
}
return response;
}
}) ( $.global, { toString : function() {return 'restix';} } );
// Example Request
//~ var request = {
//~ url:"String",
//~ command:"String", // defaults to ""
//~ port:443, // defaults to ""
//~ method:"GET|POST", // defaults to GET
//~ headers:[{name:"String", value:"String"}], // defaults to []
//~ body:"" // defaults to ""
//~ }
//~ var request = {
//~ url:"https://www.publishingx.de"
//~ }
//~ var response = restix.fetch(request);
//~ if (response.error) {
//~ $.writeln("Response Error: " + response.error);
//~ $.writeln("Response errorMsg: " + response.errorMsg);
//~ }
//~ $.writeln("Response HTTP Status: " + response.httpStatus);
//~ $.writeln("Response Body: " + response.body);