-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathConnection.js
235 lines (203 loc) · 7.24 KB
/
Connection.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
/* jshint -W058 */
var d = require('describe-property');
var isBinary = require('bodec').isBinary;
var decodeBase64 = require('./utils/decodeBase64');
var encodeBase64 = require('./utils/encodeBase64');
var stringifyQuery = require('./utils/stringifyQuery');
var Promise = require('./utils/Promise');
var Location = require('./Location');
var Message = require('./Message');
function locationPropertyAlias(name) {
return d.gs(function () {
return this.location[name];
}, function (value) {
this.location[name] = value;
});
}
function defaultErrorHandler(error) {
if (typeof console !== 'undefined' && console.error) {
console.error((error && error.stack) || error);
} else {
throw error; // Don't silently swallow errors!
}
}
function defaultCloseHandler() {}
function defaultApp(conn) {
conn.status = 404;
conn.response.contentType = 'text/plain';
conn.response.content = 'Not found: ' + conn.method + ' ' + conn.path;
}
/**
* An HTTP connection that acts as the asynchronous primitive for
* the duration of the request/response cycle.
*
* Important features are:
*
* - request A Message representing the request being made. In
* a server environment, this is an "incoming" message
* that was probably generated by a web browser or some
* other consumer. In a client environment, this is an
* "outgoing" message that we send to a remote server.
* - response A Message representing the response to the request.
* In a server environment, this is an "outgoing" message
* that will be sent back to the client. In a client
* environment, this is the response that was received
* from the remote server.
* - method The HTTP method that the request uses
* - location The URL of the request. In a server environment, this
* is derived from the URL path used in the request as
* well as a combination of the Host, X-Forwarded-* and
* other relevant headers.
* - version The version of HTTP used in the request
* - status The HTTP status code of the response
* - statusText The HTTP status text that corresponds to the status
* - responseText This is a special property that contains the entire
* content of the response. It is present by default when
* making client requests for convenience, but may also be
* disabled when you need to stream the response.
*
* Options may be any of the following:
*
* - content The request content, defaults to ""
* - headers The request headers, defaults to {}
* - method The request HTTP method, defaults to "GET"
* - location/url The request Location or URL
* - params The request params
* - onError A function that is called when there is an error
* - onClose A function that is called when the request closes
*
* The options may also be a URL string to specify the URL.
*/
function Connection(options) {
options = options || {};
var location;
if (typeof options === 'string') {
location = options; // options may be a URL string.
} else if (options.location || options.url) {
location = options.location || options.url;
} else if (typeof window === 'object') {
location = window.location.href;
}
this.location = location;
this.version = options.version || '1.1';
this.method = options.method;
this.onError = (options.onError || defaultErrorHandler).bind(this);
this.onClose = (options.onClose || defaultCloseHandler).bind(this);
this.request = new Message(options.content, options.headers);
this.response = new Message;
// Params may be given as an object.
if (options.params) {
if (this.method === 'GET' || this.method === 'HEAD') {
this.query = options.params;
} else {
this.request.contentType = 'application/x-www-form-urlencoded';
this.request.content = stringifyQuery(options.params);
}
}
this.withCredentials = options.withCredentials || false;
this.remoteHost = options.remoteHost || null;
this.remoteUser = options.remoteUser || null;
this.basename = '';
this.responseText = null;
this.status = 200;
}
Object.defineProperties(Connection.prototype, {
/**
* The method used in the request.
*/
method: d.gs(function () {
return this._method;
}, function (value) {
this._method = typeof value === 'string' ? value.toUpperCase() : 'GET';
}),
/**
* The Location of the request.
*/
location: d.gs(function () {
return this._location;
}, function (value) {
this._location = (value instanceof Location) ? value : new Location(value);
}),
href: locationPropertyAlias('href'),
protocol: locationPropertyAlias('protocol'),
host: locationPropertyAlias('host'),
hostname: locationPropertyAlias('hostname'),
port: locationPropertyAlias('port'),
search: locationPropertyAlias('search'),
queryString: locationPropertyAlias('queryString'),
query: locationPropertyAlias('query'),
/**
* True if the request uses SSL, false otherwise.
*/
isSSL: d.gs(function () {
return this.protocol === 'https:';
}),
/**
* The username:password used in the request, an empty string
* if no auth was provided.
*/
auth: d.gs(function () {
var header = this.request.headers['Authorization'];
if (header) {
var parts = header.split(' ', 2);
var scheme = parts[0];
if (scheme.toLowerCase() === 'basic')
return decodeBase64(parts[1]);
return header;
}
return this.location.auth;
}, function (value) {
var headers = this.request.headers;
if (value && typeof value === 'string') {
headers['Authorization'] = 'Basic ' + encodeBase64(value);
} else {
delete headers['Authorization'];
}
}),
/**
* The portion of the original URL path that is still relevant
* for request processing.
*/
pathname: d.gs(function () {
return this.location.pathname.replace(this.basename, '') || '/';
}, function (value) {
this.location.pathname = this.basename + value;
}),
/**
* The URL path with query string.
*/
path: d.gs(function () {
return this.pathname + this.search;
}, function (value) {
this.location.path = this.basename + value;
}),
/**
* Calls the given `app` with this connection as the only argument.
* as the first argument and returns a promise for a Response.
*/
call: d(function (app) {
app = app || defaultApp;
var conn = this;
try {
return Promise.resolve(app(conn)).then(function (value) {
if (value == null)
return;
if (typeof value === 'number') {
conn.status = value;
} else if (typeof value === 'string' || isBinary(value) || typeof value.pipe === 'function') {
conn.response.content = value;
} else {
if (value.headers != null)
conn.response.headers = value.headers;
if (value.content != null)
conn.response.content = value.content;
if (value.status != null)
conn.status = value.status;
}
});
} catch (error) {
return Promise.reject(error);
}
})
});
module.exports = Connection;