-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrestix.jsx
349 lines (294 loc) · 11.7 KB
/
restix.jsx
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/****************
# Connect InDesign to the web
* HTTPS supported
* Works form CS4 to CC 2022 (ExtendScript based library)
* Based on VBScript/ServerXMLHTTP (Win) AppleScript/curl (Mac) relies on app.doScript()
## Getting started
See examples/connect.jsx
* @Version: 1.37
* @Date: 2023-10-28
* @Author: Gregor Fellenz, http://www.publishingx.de
* Acknowledgments:
** Library design pattern from Marc Autret https://forums.adobe.com/thread/1111415
*/
$.global.hasOwnProperty('restix') || (function (HOST, SELF) {
HOST[SELF] = SELF;
/****************
* PRIVATE
*/
var INNER = {};
INNER.version = "2022-03-29-1.33";
/** 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;
}
// not encoded, we need to encode;
if (decodeURI(request.fullURL) == request.fullURL) {
request.fullURL = encodeURI(request.fullURL);
}
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" || request.method == "HEAD")) 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]");
if (request.unsafe == undefined) 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 result = "";
if (INNER.isWindows()) {
// Since Win10 Update Feb 2019 msxml3 does not work anymore...
scriptCommands.push('Dim xHttp : Set xHttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")');
// 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) {
// xHttp.SetProxy 1
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.replace(/"/g, '""') + '"');
}
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.method == "PUT") && 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');
}
else {
scriptCommands.push('xHttp.Send');
}
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-----" & xHttp.getAllResponseHeaders & vbCr & "-----http-----" & xHttp.status');
}
else {
scriptCommands.push(' res = xHttp.responseText & vbCr & "-----http-----" & xHttp.getAllResponseHeaders & vbCr & "-----http-----" & 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 + " #" + e.number;
if (e.number == 104705) {
result += " Please start InDesign once with administrator rights. Close it and start it again as a normal user.";
}
}
}
else { // Mac
// -L follow redirects
var curlString = 'curl --silent --max-time 30 --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
}
if (request.method == "HEAD") {
curlString += ' -I --head ';
}
else {
curlString += ' -X ' + request.method;
}
if (request.body) {
curlString += ' -d \'' + request.body.replace(/"/g, '\\"').replace(/\n|\r/g, '') + '\'';
}
else if ((request.method == "POST" || request.method == "PUT") && request.binaryFilePath) {
curlString += ' --data-binary \'@' + request.binaryFilePath + '\'';
}
if (outFile) {
curlString += ' -w \'outFile\n-----http-----%{http_code}\'';
curlString += ' -o \'' + outFile.fsName + '\''
}
else {
curlString += ' -w \'\n-----http-----%{http_code}\'';
}
curlString += ' \'' + request.fullURL + '\'';
// log.info(curlString);
try {
result = app.doScript('do shell script "' + curlString + '"', ScriptLanguage.APPLESCRIPT_LANGUAGE);
}
catch (e) {
result = "doScriptError: " + e.message + " #" + e.number;
}
}
// Fill response
if (typeof result == 'undefined') {
throw Error("No result value. Probably System Script could not run?");
}
if (result.match(/^xHttpError|^curl: \(\d+\)|^doScriptError:/)) {
response.error = true;
response.errorMsg = result;
}
else {
if (INNER.isWindows()) {
var resArray = result.split("\r-----http-----");
if (resArray.length == 3) {
response.body = resArray[0];
response.head = resArray[1];
response.httpStatus = resArray[2] * 1;
}
else {
throw Error("Wrong result value: [" + result + "]");
}
}
else {
var resArray = result.split("\r-----http-----");
if (resArray.length == 2) {
if (request.method == "HEAD") {
response.head = resArray[0];
response.body = "";
}
else {
response.head = "";
response.body = resArray[0];
}
response.httpStatus = resArray[1] * 1;
}
else {
throw Error("Wrong result value: [" + result + "]");
}
}
var headSplit = response.head.split(/\n|\r/);
response.head = {}
for (var h = 0; h < headSplit.length; h++) {
var headProperty = headSplit[h];
if (headProperty.replace(/\s/g, '') == "") continue;
var colonIndex = headProperty.indexOf(":");
response.head[headProperty.substring(0, colonIndex).toLowerCase()] = headProperty.substring(colonIndex + 1).replace(/^ +/, "");
}
}
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.length == 0) {
outFile.remove();
}
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:"https://www.publishingx.de/",
// }
// var response = restix.fetch(request);
// $.writeln("Response HTTP Status: " + response.httpStatus);
// $.writeln("Response Body: " + response.body);