Build fix. We need to escape the path or http.request would fail.
[WebKit-https.git] / Websites / perf.webkit.org / tools / js / remote.js
1 'use strict';
2
3 let assert = require('assert');
4 let http = require('http');
5 let https = require('https');
6 let querystring = require('querystring');
7
8 class RemoteAPI {
9     constructor(server)
10     {
11         this._server = null;
12         if (server)
13             this.configure(server);
14     }
15
16     url(path)
17     {
18         let scheme = this._server.scheme;
19         let port = this._server.port;
20         let portSuffix = this._server.port == this._server.defaultPort ? '' : `:${port}`;
21         if (path.charAt(0) != '/')
22             path = '/' + path;
23         return `${scheme}://${this._server.host}${portSuffix}${path}`;
24     }
25
26     configure(server)
27     {
28         assert(server.scheme === 'http' || server.scheme === 'https');
29         assert.equal(typeof(server.host), 'string', 'host should be a string');
30         assert(!server.port || typeof(server.port) == 'number', 'port should be a number');
31
32         let auth = null;
33         if (server.auth) {
34             assert.equal(typeof(server.auth), 'object', 'auth should be a dictionary with username and password as keys');
35             assert.equal(typeof(server.auth.username), 'string', 'auth should contain a string username');
36             assert.equal(typeof(server.auth.password), 'string', 'auth should contain a string password');
37             auth = {
38                 username: server.auth.username,
39                 password: server.auth.password,
40             };
41         }
42
43         const defaultPort = server.scheme == 'http' ? 80 : 443;
44         this._server = {
45             scheme: server.scheme,
46             host: server.host,
47             port: server.port || defaultPort,
48             defaultPort: defaultPort,
49             auth: auth,
50         };
51     }
52
53     getJSON(path)
54     {
55         return this.sendHttpRequest(path, 'GET', null, null).then(function (result) {
56             return JSON.parse(result.responseText);
57         });
58     }
59
60     getJSONWithStatus(path)
61     {
62         return this.getJSON(path).then(function (result) {
63             if (result['status'] != 'OK')
64                 return Promise.reject(result);
65             return result;
66         });
67     }
68
69     postJSON(path, data)
70     {
71         const contentType = 'application/json';
72         const payload = JSON.stringify(data);
73         return this.sendHttpRequest(path, 'POST', 'application/json', payload).then(function (result) {
74             return JSON.parse(result.responseText);
75         });
76     }
77
78     postFormUrlencodedData(path, data)
79     {
80         const contentType = 'application/x-www-form-urlencoded';
81         const payload = querystring.stringify(data);
82         return this.sendHttpRequest(path, 'POST', contentType, payload).then(function (result) {
83             return result.responseText;
84         });
85     }
86
87     sendHttpRequest(path, method, contentType, content)
88     {
89         let server = this._server;
90         return new Promise(function (resolve, reject) {
91             let options = {
92                 hostname: server.host,
93                 port: server.port,
94                 auth: server.auth ? server.auth.username + ':' + server.auth.password : null,
95                 method: method,
96                 path: escape(path),
97             };
98
99             let request = (server.scheme == 'http' ? http : https).request(options, function (response) {
100                 let responseText = '';
101                 response.setEncoding('utf8');
102                 response.on('data', function (chunk) { responseText += chunk; });
103                 response.on('end', function () { resolve({statusCode: response.statusCode, responseText: responseText}); });
104             });
105
106             request.on('error', reject);
107
108             if (contentType)
109                 request.setHeader('Content-Type', contentType);
110
111             if (content)
112                 request.write(content);
113
114             request.end();
115         });
116     }
117 };
118
119 if (typeof module != 'undefined')
120     module.exports.RemoteAPI = RemoteAPI;