Build fix. We need to escape the path or http.request would fail.
[WebKit.git] / Websites / perf.webkit.org / tools / js / remote.js
index 64e381d..d251019 100644 (file)
@@ -3,56 +3,97 @@
 let assert = require('assert');
 let http = require('http');
 let https = require('https');
 let assert = require('assert');
 let http = require('http');
 let https = require('https');
+let querystring = require('querystring');
 
 
-let RemoteAPI = new (class RemoteAPI {
-    constructor()
+class RemoteAPI {
+    constructor(server)
     {
     {
-        this._server = {
-            scheme: 'http',
-            host: 'localhost',
-        }
+        this._server = null;
+        if (server)
+            this.configure(server);
+    }
+
+    url(path)
+    {
+        let scheme = this._server.scheme;
+        let port = this._server.port;
+        let portSuffix = this._server.port == this._server.defaultPort ? '' : `:${port}`;
+        if (path.charAt(0) != '/')
+            path = '/' + path;
+        return `${scheme}://${this._server.host}${portSuffix}${path}`;
     }
 
     configure(server)
     {
         assert(server.scheme === 'http' || server.scheme === 'https');
     }
 
     configure(server)
     {
         assert(server.scheme === 'http' || server.scheme === 'https');
-        assert.equal(typeof(server.host), 'string');
-        assert(!server.port || typeof(server.port) == 'number');
-        assert(!server.auth || typeof(server.auth) == 'object');
-        this._server = server;
+        assert.equal(typeof(server.host), 'string', 'host should be a string');
+        assert(!server.port || typeof(server.port) == 'number', 'port should be a number');
+
+        let auth = null;
+        if (server.auth) {
+            assert.equal(typeof(server.auth), 'object', 'auth should be a dictionary with username and password as keys');
+            assert.equal(typeof(server.auth.username), 'string', 'auth should contain a string username');
+            assert.equal(typeof(server.auth.password), 'string', 'auth should contain a string password');
+            auth = {
+                username: server.auth.username,
+                password: server.auth.password,
+            };
+        }
+
+        const defaultPort = server.scheme == 'http' ? 80 : 443;
+        this._server = {
+            scheme: server.scheme,
+            host: server.host,
+            port: server.port || defaultPort,
+            defaultPort: defaultPort,
+            auth: auth,
+        };
     }
 
     }
 
-    fetchJSON(path, data)
+    getJSON(path)
     {
     {
-        let contentType = null;
-        if (data) {
-            contentType = 'application/json';
-            data = JSON.stringify(data);
-        }
-        return this.sendHttpRequest(path, 'GET', contentType, data).then(function (result) {
+        return this.sendHttpRequest(path, 'GET', null, null).then(function (result) {
             return JSON.parse(result.responseText);
         });
     }
 
             return JSON.parse(result.responseText);
         });
     }
 
-    fetchJSONWithStatus(path, data)
+    getJSONWithStatus(path)
     {
     {
-        return this.fetchJSON(path, data).then(function (result) {
+        return this.getJSON(path).then(function (result) {
             if (result['status'] != 'OK')
                 return Promise.reject(result);
             return result;
         });
     }
 
             if (result['status'] != 'OK')
                 return Promise.reject(result);
             return result;
         });
     }
 
+    postJSON(path, data)
+    {
+        const contentType = 'application/json';
+        const payload = JSON.stringify(data);
+        return this.sendHttpRequest(path, 'POST', 'application/json', payload).then(function (result) {
+            return JSON.parse(result.responseText);
+        });
+    }
+
+    postFormUrlencodedData(path, data)
+    {
+        const contentType = 'application/x-www-form-urlencoded';
+        const payload = querystring.stringify(data);
+        return this.sendHttpRequest(path, 'POST', contentType, payload).then(function (result) {
+            return result.responseText;
+        });
+    }
+
     sendHttpRequest(path, method, contentType, content)
     {
         let server = this._server;
         return new Promise(function (resolve, reject) {
             let options = {
                 hostname: server.host,
     sendHttpRequest(path, method, contentType, content)
     {
         let server = this._server;
         return new Promise(function (resolve, reject) {
             let options = {
                 hostname: server.host,
-                port: server.port || 80,
-                auth: server.auth,
+                port: server.port,
+                auth: server.auth ? server.auth.username + ':' + server.auth.password : null,
                 method: method,
                 method: method,
-                path: path,
+                path: escape(path),
             };
 
             let request = (server.scheme == 'http' ? http : https).request(options, function (response) {
             };
 
             let request = (server.scheme == 'http' ? http : https).request(options, function (response) {
@@ -73,7 +114,7 @@ let RemoteAPI = new (class RemoteAPI {
             request.end();
         });
     }
             request.end();
         });
     }
-})
+};
 
 if (typeof module != 'undefined')
     module.exports.RemoteAPI = RemoteAPI;
 
 if (typeof module != 'undefined')
     module.exports.RemoteAPI = RemoteAPI;