-
Notifications
You must be signed in to change notification settings - Fork 2
/
url.class.js
772 lines (717 loc) · 21.6 KB
/
url.class.js
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
/**
* Mock-up of the URL class from VMware Aria Automation.
* Usable object to put/get http requests.
*
* Hint: The properties are accessed using getter and setter methods.
*
* @author Stefan Schnell <[email protected]>
* @license MIT
* @version 0.3.0
*
* Hint: This mock-up works only with the Mozilla Rhino JavaScript
* engine release 1.7.14 or higher.
*
* Checked with Rhino engine version 1.7.14 and 1.7.15
*
* @function URL
* @param {object} url
* @returns {object}
*
* @example
* var urlObject = new URL("http://localhost/index.php");
* System.log(urlObject.getClassName());
*/
var URL = function(url) {
// The use of sun.net.www.protocol.http.HttpURLConnection is only
// possible from release 1.7.14.
var rhinoVersion = System.getRhinoVersion();
if (System.compareVersionNumber("1.7.13", rhinoVersion) === 1) {
throw new Error("This class can only be used with Rhino release 1.7.14");
}
this._url = null;
if (
typeof url === "undefined" ||
url === null ||
String(url).trim() === ""
) {
throw new Error("url argument can not be undefined or null");
}
try {
if (typeof url === "string") {
this._url = java.net.URI(url.toString()).toURL();
} else {
this._url = java.net.URI("http://localhost/").toURL();
}
} catch (exception) {
throw new Error(exception.message);
}
this._contentType = "application/x-www-form-urlencoded";
this._datas = null;
this._host = this._url.getHost();
this._port = this._url.getPort;
this._requestType = "GET";
this._receivedBuffer = null;
this._header = [];
this._authorization = this._url.getAuthority();
this._responseCode = 0;
this._responseMessage = null;
};
URL.prototype = {
/**
* Adds additional information to the HTTP header.<br>
* Hint: This method is not available in standard.
*
* @function addToHeader
* @param {string} key - The name of the key to add
* @param (string} value - The value of the key
*/
addToHeader : function(key, value) {
var obj = {};
obj[key] = value;
this._header.push(obj);
},
/**
* Adds a parameter (query) at the end of the URL.
*
* @function addParameter
* @param {object} key - The key of the parameter
* @param (object} value - The value of the parameter
*
* @example
* var urlObject = new URL("http://localhost/index.php");
* urlObject.addParameter("test", "test")
* System.log(urlObject.getUrl());
* // http://localhost/index.php?test=test
*/
addParameter : function(key, value) {
if (
typeof key === "undefined" ||
key === null ||
String(key).trim() === ""
) {
throw new Error("key argument can not be undefined or null");
}
if (
typeof value === "undefined" ||
value === null ||
String(value).trim() === ""
) {
throw new Error("value argument can not be undefined or null");
}
try {
var uri = this._url.toURI();
var scheme = uri.getScheme();
var userInfo = uri.getUserInfo();
var host = uri.getHost();
var port = uri.getPort();
var path = uri.getPath();
var query = uri.getQuery();
var fragment = uri.getFragment();
if (query === null) {
query = String(value) + "=" + String(key);
} else {
query += "&" + String(value) + "=" + String(key);
}
this._url = new java.net.URI(
scheme, userInfo, host, port, path, query, fragment
).toURL();
} catch (exception) {
throw new Error("Error at URL.addParameter: " + exception.message);
}
},
/**
* Adds a path to the URL.<br>
* Hint: This method behaves differently from the standard.<br> The
* path is actually added and not just appended to the end of the URL.
*
* @function addPath
* @param {object} value - The path
*
* @example
* var urlObject = new URL("http://localhost");
* urlObject.addPath("/index.php");
* System.log(urlObject.getUrl());
* // http://localhost/index.php
*/
addPath : function(value) {
if (
typeof value === "undefined" ||
value === null ||
String(value).trim() === ""
) {
throw new Error("value argument can not be undefined or null");
}
try {
var uri = this._url.toURI();
var scheme = uri.getScheme();
var userInfo = uri.getUserInfo();
var host = uri.getHost();
var port = uri.getPort();
var path = uri.getPath();
var query = uri.getQuery();
var fragment = uri.getFragment();
if (path === null) {
path = String(value);
} else {
if (value.substring(0, 1) === "/") {
path += String(value);
} else {
path += "/" + String(value);
}
}
this._url = new java.net.URI(
scheme, userInfo, host, port, path, query, fragment
).toURL();
} catch (exception) {
throw new Error("Error at URL.addPath: " + exception.message);
}
},
/**
* Clears the HTTP header.<br>
* Hint: This method is not available in standard.
*
* @function clearHeader
*/
clearHeader : function() {
this._header = [];
},
/**
* Deletes a key from the HTTP header.<br>
* Hint: This method is not available in standard.
*
* @function deleteFromHeader
* @param {string} key - The name of the key to delete
*/
deleteFromHeader : function(key) {
this._header.splice(this._header.indexOf(key), 1);
},
/**
* Escapes the host if this is needed. Usually you need to escape<br>
* IPv6 numeric addresses using brackets. If this host does not need<br>
* to be escaped then the original is returned.
*
* @function escapeHost
* @param {string} ipAddress
* @returns {string} - the host so it can be used in URL
*
* @example
* var urlObject = new URL("http://localhost/index.php");
* System.log(urlObject.escapeHost("127.0.0.1"));
* // [127.0.0.1]
*/
escapeHost : function(ipAddress) {
if (
ipAddress.substring(0, 1) !== "[" &&
ipAddress.substring(ipAddress.length - 1) !== "]"
) {
return "[" + ipAddress + "]";
}
},
/**
* Returns the class name.<br>
* Hint: This method is a standard.
*
* @function getClassName
* @returns {string}
*
* @example
* var urlObject = new URL("http://localhost/index.php");
* System.log(urlObject.getClassName());
* // URL
*/
getClassName : function() {
return "URL";
},
/**
* Returns the content of the URL
*
* @function getContent
* @returns {string}
*
* @example
* var urlObject = new URL("http://localhost/index.php");
* System.log(urlObject.getContent());
* // <!DOCTYPE html PUBLIC ...
*/
getContent : function() {
return this.postContent(null);
},
/**
* Gets the authority part of the URL.<br>
* Hint: This method is not available in standard.
*
* @function getAuthorization
*/
getAuthorization : function() {
return String(this._authorization);
},
/**
* Gets the content type.<br>
* Hint: This is a getter method to the property contentType<br>
* which is not available in standard.
*
* @function getContentType
*/
getContentType : function() {
return this._contentType;
},
/**
* Gets the datas (content).<br>
* Hint: This is a getter method to the property datas<br>
* which is not available in standard.
*
* @function getDatas
*/
getDatas : function() {
return this._datas;
},
/**
* Returns the host.<br>
* Hint: This is a getter method to the property host<br>
* which is not available in standard.
*
* @function getHost
*/
getHost : function() {
if (this._host !== null) {
return this._host;
}
},
/**
* Return the regex pattern string that matches any valid host name
* or IP address.
*
* @function getHostnameOrIPAddressPatternStr
* @returns {string} - String that is regular expression pattern that<br>
* matches valid host name or IP address.
*/
getHostnameOrIPAddressPatternStr : function() {
return "(?:(?:[\\p{L}\\d](?:[\\p{L}\\d_-]{0,61}[\\p{L}\\d])?\\.)*[\\p{L}\\d](?:[\\p{L}\\d_-]{0,61}[\\p{L}\\d])?)|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|(?i:(((?=(?>.*?::)(?!.*::)))(::)?([0-9A-F]{1,4}::?){0,5}|([0-9A-F]{1,4}:){6})(\\2([0-9A-F]{1,4}(::?|((%.+)?$))){0,2}|((25[0-5]|(2[0-4]|1[0-9]|[1-9])?[0-9])(\\.|((%.+)?$))){4}|[0-9A-F]{1,4}:[0-9A-F]{1,4})(?<![^:]:)(?<!\\.))(%.+)?";
},
/**
* Return the regex pattern string that matches any valid host name.
*
* @function getHostnamePatternStr
* @returns {string} - String that is regular expression pattern that<br>
* matches valid host name.
*/
getHostnamePatternStr : function() {
return "(?:(?:[\\p{L}\\d](?:[\\p{L}\\d_-]{0,61}[\\p{L}\\d])?\\.)*[\\p{L}\\d](?:[\\p{L}\\d_-]{0,61}[\\p{L}\\d])?)";
},
/**
* String that is regular expression pattern that matches valid<br>
* IP address.
*
* @function getIPAddressPatternStr
* @returns {string} - String that is regular expression pattern that<br>
* matches valid IP address.
*/
getIPAddressPatternStr : function() {
return "(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|(?i:(((?=(?>.*?::)(?!.*::)))(::)?([0-9A-F]{1,4}::?){0,5}|([0-9A-F]{1,4}:){6})(\\2([0-9A-F]{1,4}(::?|((%.+)?$))){0,2}|((25[0-5]|(2[0-4]|1[0-9]|[1-9])?[0-9])(\\.|((%.+)?$))){4}|[0-9A-F]{1,4}:[0-9A-F]{1,4})(?<![^:]:)(?<!\\.))(%.+)?";
},
/**
* Return the regex pattern string that matches any valid IPv4 address.
*
* @function getIPv4AddressPatternStr
* @returns {string} - String that is regular expression pattern that<br>
* matches valid IPv4 address.
*/
getIPv4AddressPatternStr : function() {
return "(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)";
},
/**
* Return the regex pattern string that matches any valid IPv6 address.
*
* @function getIPv6AddressPatternStr
* @returns {string} - String that is regular expression pattern that<br>
* matches valid IPv6 address.
*/
getIPv6AddressPatternStr : function() {
return "(?i:(((?=(?>.*?::)(?!.*::)))(::)?([0-9A-F]{1,4}::?){0,5}|([0-9A-F]{1,4}:){6})(\\2([0-9A-F]{1,4}(::?|((%.+)?$))){0,2}|((25[0-5]|(2[0-4]|1[0-9]|[1-9])?[0-9])(\\.|((%.+)?$))){4}|[0-9A-F]{1,4}:[0-9A-F]{1,4})(?<![^:]:)(?<!\\.))(%.+)?";
},
/**
* Returns the port.<br>
* Hint: This is a getter method to the property port<br>
* which is not available in standard.
*
* @function getPort
*/
getPort : function() {
if (this._port !== null) {
return this._port;
}
},
/**
* Returns the response code of the last http request.<br>
* Hint: This method is not available in standard.
*
* @function getResponseCode
*/
getResponseCode : function() {
return this._responseCode;
},
/**
* Returns the response message of the last http request.<br>
* Hint: This method is not available in standard.
*
* @function getResponseMessage
*/
getResponseMessage : function() {
return this._responseMessage;
},
/**
* Gets the request type (GET, PUT, POST or DELETE).<br>
* Hint: This is a getter method to the property requestType<br>
* which is not available in standard.
*
* @function getRequestType
*/
getRequestType : function() {
return this._requestType;
},
/**
* Returns the result.<br>
* Hint: This is a getter method to the property result<br>
* which is not available in standard.
*
* @function getResult
*/
getResult : function() {
if (this._receivedBuffer !== null) {
return String(this._receivedBuffer);
} else {
return null;
}
},
/**
* Returns the URL in the form of a String.<br>
* Hint: This is a getter method to the property url<br>
* which is not available in standard.
*
* @function getUrl
*/
getUrl : function() {
if (this._url !== null) {
return String(this._url);
}
},
/**
* Checks if the parameter is valid host name.
*
* @function isValidHostname
* @param {string} hostname
* @returns {boolean} - true if address is valid host name
*
* @example
* urlObject = new URL("http://localhost/index.php");
* System.log(urlObject.isValidHostname("www.myhost.com"));
* // true
*/
isValidHostname : function(hostname) {
if (
typeof hostname === "undefined" ||
hostname === null ||
String(hostname).trim() === ""
) {
throw new Error("hostname argument can not be undefined or null");
}
var HOSTNAME_PATTERN =
java.util.regex.Pattern.compile(this.getHostnamePatternStr(), 2);
return HOSTNAME_PATTERN.matcher(hostname).matches();
},
/**
* Checks if the parameter is valid host name or IP address<br>
* (both IPv4 and IPv6).
*
* @function isValidHostnameOrIPAddress
* @param {string} hostOrIP
* @returns {boolean} - true if address is valid host name or IP address
*/
isValidHostnameOrIPAddress : function(hostOrIP) {
if (
typeof hostOrIP === "undefined" ||
hostOrIP === null ||
String(hostOrIP).trim() === ""
) {
throw new Error("hostOrIP argument can not be undefined or null");
}
var HOSTNAME_OR_IP_PATTERN =
java.util.regex.Pattern.compile(this.getHostnameOrIPAddressPatternStr(), 2);
return HOSTNAME_OR_IP_PATTERN.matcher(hostOrIP).matches();
},
/**
* Checks if the parameter is valid IP address (both IPv4 and IPv6).
*
* @function isValidIPAddress
* @param {string} ipAddress
* @returns {boolean} - true if address is valid IP address
*
* @example
* urlObject = new URL("http://localhost/index.php");
* System.log(urlObject.isValidIPAddress("127.0.0.1"));
* // true
*/
isValidIPAddress : function(ipAddress) {
if (
typeof ipAddress === "undefined" ||
ipAddress === null ||
String(ipAddress).trim() === ""
) {
throw new Error("ipAddress argument can not be undefined or null");
}
var IP_PATTERN =
java.util.regex.Pattern.compile(this.getIPAddressPatternStr(), 2);
return IP_PATTERN.matcher(ipAddress).matches();
},
/**
* Checks if the parameter is valid IPv4 address.
*
* @function isValidIPv4Address
* @param {string} ipAddress
* @returns {boolean} - true if address is valid IPv4 address
*
* @example
* urlObject = new URL("http://localhost/index.php");
* System.log(urlObject.isValidIPv4Address("127.0.0.1"));
* // true
*/
isValidIPv4Address : function(ipAddress) {
if (
typeof ipAddress === "undefined" ||
ipAddress === null ||
String(ipAddress).trim() === ""
) {
throw new Error("ipAddress argument can not be undefined or null");
}
var IPV4_PATTERN =
java.util.regex.Pattern.compile(this.getIPv4AddressPatternStr(), 2);
return IPV4_PATTERN.matcher(ipAddress).matches();
},
/**
* Checks if the parameter is valid IPv6 address.
*
* @function isValidIPv6Address
* @param {string} ipAddress
* @returns {boolean} - true if address is valid IPv6 address
*
* @example
* urlObject = new URL("http://localhost/index.php");
* System.log(urlObject.isValidIPv6Address("::FFFF:10.2.4.1"));
* // true
*/
isValidIPv6Address : function(ipAddress) {
if (
typeof ipAddress === "undefined" ||
ipAddress === null ||
String(ipAddress).trim() === ""
) {
throw new Error("ipAddress argument can not be undefined or null");
}
var IPV6_PATTERN =
java.util.regex.Pattern.compile(this.getIPv6AddressPatternStr(), 2);
return IPV6_PATTERN.matcher(ipAddress).matches();
},
/**
* Posts the content defined in the datas property to the URL.
*
* @function post
* @returns {string}
*
* @example
* var urlObject = new URL("http://127.0.0.1:3000");
* urlObject.setRequestType("POST");
* urlObject.setContentType("application/json");
* urlObject.setDatas("{\"attributes\":{\"hostname\":\"localhost\"}}");
* var post = urlObject.post();
*/
post : function() {
return this.postContent(this._datas);
},
/**
* Posts the content to the URL.
*
* @function postContent
* @param {object} content - The content
* @returns {string}
*
* @example
* var urlObject = new URL("http://127.0.0.1:3000");
* urlObject.setRequestType("POST");
* urlObject.setContentType("application/json");
* var content = "{\"attributes\":{\"ipaddress\":\"127.0.0.1\"}}";
* var postContent = urlObject.postContent(content);
*/
postContent : function(content) {
this._receivedBuffer = new java.lang.StringBuffer();
var urlConnection;
var input;
try {
urlConnection = this._url.openConnection();
// java.net.HttpURLConnection is an abstract class, and
// sun.net.www.protocol.http.HttpURLConnection is the
// implementation in the Oracle JRE, which is used here.
urlConnection.setRequestMethod(this._requestType);
urlConnection.setDoInput(true);
urlConnection.setDoOutput((content != null));
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Content-Type", this._contentType);
// Set additional header
this._header.forEach( function(headerItem) {
for (var key in headerItem) {
urlConnection.setRequestProperty(key, headerItem[key]);
}
});
// Set authorization
if (this._authorization != null) {
var headerAuthorization = java.util.Base64.getEncoder().encodeToString(
(this._authorization.split("@")[0]).getBytes("UTF-8")
);
urlConnection.setRequestProperty(
"Authorization", "Basic " + headerAuthorization
);
}
if (content != null) {
var printout =
java.io.DataOutputStream(urlConnection.getOutputStream());
try {
printout.writeBytes(content);
printout.flush();
} finally {
printout.close();
}
}
this._responseCode = Number(urlConnection.getResponseCode());
this._responseMessage = String(urlConnection.getResponseMessage());
input = java.io.DataInputStream(urlConnection.getInputStream());
var bufferedReader = null;
try {
bufferedReader = new java.io.BufferedReader(
new java.io.InputStreamReader(input)
);
while((line = bufferedReader.readLine()) !== null) {
this._receivedBuffer.append(line + "\n");
}
} finally {
if (bufferedReader !== null) {
bufferedReader.close();
}
}
} catch (exception) {
throw new Error("Error at URL.postContent: " + exception.message);
} finally {
if (input != null) {
input.close();
}
if (urlConnection != null) {
urlConnection.disconnect();
}
}
return String(this._receivedBuffer.toString());
},
/**
* Sets the authorization part of the URL.<br>
* Hint: This method is not available in standard.
*
* @function setAuthorization
* @param {string} value
*/
setAuthorization : function(value) {
if (
typeof value === "undefined" ||
value === null ||
String(value).trim() === ""
) {
throw new Error("value argument can not be undefined or null");
}
if (value != null) {
this._authorization = java.lang.String(value);
}
},
/**
* Sets the content type(content).<br>
* Hint: This is a setter method to the property contentType<br>
* which is not available in standard.
*
* @function setContentType
* @param {string} value
*/
setContentType : function(value) {
if (
typeof value === "undefined" ||
value === null ||
String(value).trim() === ""
) {
throw new Error("value argument can not be undefined or null");
}
if (value != null) {
this._contentType = String(value);
}
},
/**
* Sets the datas (content).<br>
* Hint: This is a setter method to the property datas<br>
* which is not available in standard.
*
* @function setDatas
* @param {string} value
*/
setDatas : function(value) {
if (
typeof value === "undefined" ||
value === null ||
String(value).trim() === ""
) {
throw new Error("value argument can not be undefined or null");
}
if (value != null) {
this._datas = String(value);
} else {
this._datas = null;
}
},
/**
* Sets the request type (GET, PUT, POST or DELETE).<br>
* Hint: This is a setter method to the property requestType<br>
* which is not available in standard.
*
* @function setRequestType
* @param {string} value
*/
setRequestType : function(value) {
if (
typeof value === "undefined" ||
value === null ||
String(value).trim() === ""
) {
throw new Error("value argument can not be undefined or null");
}
if (value != null) {
this._requestType = value;
} else {
this._requestType = "GET";
}
},
/**
* Un-escapes the host if this is IPv6 address in brackets. Usually<br>
* you need to escape IPv6 numeric addresses using brackets but some<br>
* times you need the plain IPv6 address. If this is not escaped<br>
* IPv6 address the original is returned.
*
* @function unescapeHost
* @param {string} ipAddress
* @returns {string} - the host so it can be used in URL
*
* @example
* urlObject = new URL("http://localhost/index.php");
* System.log(urlObject.unescapeHost("[127.0.0.1]"));
* // 127.0.0.1
*/
unescapeHost : function(ipAddress) {
if (
ipAddress.substring(0, 1) === "[" &&
ipAddress.substring(ipAddress.length - 1) === "]"
) {
return ipAddress.substring(1, ipAddress.length - 1);
}
}
};