http/index.js

1// Licensed to the Software Freedom Conservancy (SFC) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The SFC licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18/**
19 * @fileoverview Defines the {@code webdriver.http.Client} for use with
20 * NodeJS.
21 */
22
23var http = require('http'),
24 url = require('url');
25
26var base = require('../_base'),
27 HttpResponse = base.require('webdriver.http.Response');
28
29
30/**
31 * A {@link webdriver.http.Client} implementation using Node's built-in http
32 * module.
33 * @param {string} serverUrl URL for the WebDriver server to send commands to.
34 * @param {http.Agent=} opt_agent The agent to use for each request.
35 * Defaults to {@code http.globalAgent}.
36 * @param {string=} opt_proxy The proxy to use for the connection to the server.
37 * Default is to use no proxy.
38 * @constructor
39 * @implements {webdriver.http.Client}
40 */
41var HttpClient = function(serverUrl, opt_agent, opt_proxy) {
42 var parsedUrl = url.parse(serverUrl);
43 if (!parsedUrl.hostname) {
44 throw new Error('Invalid server URL: ' + serverUrl);
45 }
46
47 /** @private {http.Agent} */
48 this.agent_ = opt_agent;
49
50 /** @private {string} */
51 this.proxy_ = opt_proxy;
52
53 /**
54 * Base options for each request.
55 * @private {!Object}
56 */
57 this.options_ = {
58 host: parsedUrl.hostname,
59 path: parsedUrl.pathname,
60 port: parsedUrl.port
61 };
62};
63
64
65/** @override */
66HttpClient.prototype.send = function(httpRequest, callback) {
67 var data;
68 httpRequest.headers['Content-Length'] = 0;
69 if (httpRequest.method == 'POST' || httpRequest.method == 'PUT') {
70 data = JSON.stringify(httpRequest.data);
71 httpRequest.headers['Content-Length'] = Buffer.byteLength(data, 'utf8');
72 httpRequest.headers['Content-Type'] = 'application/json;charset=UTF-8';
73 }
74
75 var path = this.options_.path;
76 if (path[path.length - 1] === '/' && httpRequest.path[0] === '/') {
77 path += httpRequest.path.substring(1);
78 } else {
79 path += httpRequest.path;
80 }
81
82 var options = {
83 method: httpRequest.method,
84 host: this.options_.host,
85 port: this.options_.port,
86 path: path,
87 headers: httpRequest.headers
88 };
89
90 if (this.agent_) {
91 options.agent = this.agent_;
92 }
93
94 sendRequest(options, callback, data, this.proxy_);
95};
96
97
98/**
99 * Sends a single HTTP request.
100 * @param {!Object} options The request options.
101 * @param {function(Error, !webdriver.http.Response=)} callback The function to
102 * invoke with the server's response.
103 * @param {string=} opt_data The data to send with the request.
104 * @param {string=} opt_proxy The proxy server to use for the request.
105 */
106var sendRequest = function(options, callback, opt_data, opt_proxy) {
107 var host = options.host;
108 var port = options.port;
109
110 if (opt_proxy) {
111 var proxy = url.parse(opt_proxy);
112
113 options.headers['Host'] = options.host;
114 options.host = proxy.hostname;
115 options.port = proxy.port;
116
117 if (proxy.auth) {
118 options.headers['Proxy-Authorization'] =
119 'Basic ' + new Buffer(proxy.auth).toString('base64');
120 }
121 }
122
123 var request = http.request(options, function(response) {
124 if (response.statusCode == 302 || response.statusCode == 303) {
125 try {
126 var location = url.parse(response.headers['location']);
127 } catch (ex) {
128 callback(Error(
129 'Failed to parse "Location" header for server redirect: ' +
130 ex.message + '\nResponse was: \n' +
131 new HttpResponse(response.statusCode, response.headers, '')));
132 return;
133 }
134
135 if (!location.hostname) {
136 location.hostname = host;
137 location.port = port;
138 }
139
140 request.abort();
141 sendRequest({
142 method: 'GET',
143 host: location.hostname,
144 path: location.pathname + (location.search || ''),
145 port: location.port,
146 headers: {
147 'Accept': 'application/json; charset=utf-8'
148 }
149 }, callback, undefined, opt_proxy);
150 return;
151 }
152
153 var body = [];
154 response.on('data', body.push.bind(body));
155 response.on('end', function() {
156 var resp = new HttpResponse(response.statusCode,
157 response.headers, body.join('').replace(/\0/g, ''));
158 callback(null, resp);
159 });
160 });
161
162 request.on('error', function(e) {
163 if (e.code === 'ECONNRESET') {
164 setTimeout(function() {
165 sendRequest(options, callback, opt_data, opt_proxy);
166 }, 15);
167 } else {
168 var message = e.message;
169 if (e.code) {
170 message = e.code + ' ' + message;
171 }
172 callback(new Error(message));
173 }
174 });
175
176 if (opt_data) {
177 request.write(opt_data);
178 }
179
180 request.end();
181};
182
183
184// PUBLIC API
185
186/** @type {webdriver.http.Executor.} */
187exports.Executor = base.require('webdriver.http.Executor');
188
189/** @type {webdriver.http.Request.} */
190exports.Request = base.require('webdriver.http.Request');
191
192/** @type {webdriver.http.Response.} */
193exports.Response = base.require('webdriver.http.Response');
194
195exports.HttpClient = HttpClient;