1 | const url = require('url');
|
2 |
|
3 | const util = exports = module.exports = {};
|
4 |
|
5 |
|
6 |
|
7 | util.normalizeUrl = function(u) {
|
8 | return url.format(url.parse(u, true));
|
9 | };
|
10 |
|
11 | util.getOptions = function(req) {
|
12 |
|
13 | var requestedUrl = req.url;
|
14 |
|
15 |
|
16 | if(requestedUrl.indexOf('/render') === 0) {
|
17 |
|
18 | let optionsObj = {};
|
19 | if(req.method === 'GET') {
|
20 | optionsObj = url.parse(requestedUrl, true).query;
|
21 | } else if (req.method === 'POST') {
|
22 | optionsObj = req.body;
|
23 | }
|
24 |
|
25 | return {
|
26 | url: util.getUrl(optionsObj.url),
|
27 | renderType: optionsObj.renderType || 'html',
|
28 | userAgent: optionsObj.userAgent,
|
29 | fullpage: optionsObj.fullpage || false,
|
30 | width: optionsObj.width,
|
31 | height: optionsObj.height,
|
32 | followRedirects: optionsObj.followRedirects,
|
33 | javascript: optionsObj.javascript
|
34 | }
|
35 |
|
36 | } else {
|
37 |
|
38 | return {
|
39 | url: util.getUrl(requestedUrl),
|
40 | renderType: 'html'
|
41 | }
|
42 | }
|
43 | }
|
44 |
|
45 |
|
46 | util.getUrl = function(requestedUrl) {
|
47 | var decodedUrl, realUrl = requestedUrl,
|
48 | parts;
|
49 |
|
50 | if (!requestedUrl) {
|
51 | return '';
|
52 | }
|
53 |
|
54 | realUrl = realUrl.replace(/^\//, '');
|
55 |
|
56 | try {
|
57 | decodedUrl = decodeURIComponent(realUrl);
|
58 | } catch (e) {
|
59 | decodedUrl = realUrl;
|
60 | }
|
61 |
|
62 |
|
63 | decodedUrl = this.encodeHash(decodedUrl);
|
64 |
|
65 |
|
66 | if (decodedUrl.indexOf('?') !== decodedUrl.lastIndexOf('?')) {
|
67 | decodedUrl = decodedUrl.substr(0, decodedUrl.lastIndexOf('?')) + '&' + decodedUrl.substr(decodedUrl.lastIndexOf('?') + 1);
|
68 | }
|
69 |
|
70 | parts = url.parse(decodedUrl, true);
|
71 |
|
72 |
|
73 | if (parts.query && parts.query['_escaped_fragment_'] !== undefined) {
|
74 |
|
75 | if (parts.query['_escaped_fragment_'] && !Array.isArray(parts.query['_escaped_fragment_'])) {
|
76 | parts.hash = '#!' + parts.query['_escaped_fragment_'];
|
77 | }
|
78 |
|
79 | delete parts.query['_escaped_fragment_'];
|
80 | delete parts.search;
|
81 | }
|
82 |
|
83 |
|
84 | delete parts.query[''];
|
85 |
|
86 | var newUrl = url.format(parts);
|
87 |
|
88 |
|
89 | try {
|
90 | newUrl = decodeURIComponent(newUrl);
|
91 | } catch (e) {}
|
92 |
|
93 | newUrl = this.encodeHash(newUrl);
|
94 |
|
95 | return newUrl;
|
96 | };
|
97 |
|
98 | util.encodeHash = function(url) {
|
99 | if (url.indexOf('#!') === -1 && url.indexOf('#') >= 0) {
|
100 | url = url.replace(/#/g, '%23');
|
101 | }
|
102 |
|
103 | return url;
|
104 | }
|
105 |
|
106 | util.log = function() {
|
107 | if (process.env.DISABLE_LOGGING) {
|
108 | return;
|
109 | }
|
110 |
|
111 | console.log.apply(console.log, [new Date().toISOString()].concat(Array.prototype.slice.call(arguments, 0)));
|
112 | }; |
\ | No newline at end of file |