http.js
6.84 KB
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
var AWS = require('./core');
var inherit = AWS.util.inherit;
/**
* The endpoint that a service will talk to, for example,
* `'https://ec2.ap-southeast-1.amazonaws.com'`. If
* you need to override an endpoint for a service, you can
* set the endpoint on a service by passing the endpoint
* object with the `endpoint` option key:
*
* ```javascript
* var ep = new AWS.Endpoint('awsproxy.example.com');
* var s3 = new AWS.S3({endpoint: ep});
* s3.service.endpoint.hostname == 'awsproxy.example.com'
* ```
*
* Note that if you do not specify a protocol, the protocol will
* be selected based on your current {AWS.config} configuration.
*
* @!attribute protocol
* @return [String] the protocol (http or https) of the endpoint
* URL
* @!attribute hostname
* @return [String] the host portion of the endpoint, e.g.,
* example.com
* @!attribute host
* @return [String] the host portion of the endpoint including
* the port, e.g., example.com:80
* @!attribute port
* @return [Integer] the port of the endpoint
* @!attribute href
* @return [String] the full URL of the endpoint
*/
AWS.Endpoint = inherit({
/**
* @overload Endpoint(endpoint)
* Constructs a new endpoint given an endpoint URL. If the
* URL omits a protocol (http or https), the default protocol
* set in the global {AWS.config} will be used.
* @param endpoint [String] the URL to construct an endpoint from
*/
constructor: function Endpoint(endpoint, config) {
AWS.util.hideProperties(this, ['slashes', 'auth', 'hash', 'search', 'query']);
if (typeof endpoint === 'undefined' || endpoint === null) {
throw new Error('Invalid endpoint: ' + endpoint);
} else if (typeof endpoint !== 'string') {
return AWS.util.copy(endpoint);
}
if (!endpoint.match(/^http/)) {
var useSSL = config && config.sslEnabled !== undefined ?
config.sslEnabled : AWS.config.sslEnabled;
endpoint = (useSSL ? 'https' : 'http') + '://' + endpoint;
}
AWS.util.update(this, AWS.util.urlParse(endpoint));
// Ensure the port property is set as an integer
if (this.port) {
this.port = parseInt(this.port, 10);
} else {
this.port = this.protocol === 'https:' ? 443 : 80;
}
}
});
/**
* The low level HTTP request object, encapsulating all HTTP header
* and body data sent by a service request.
*
* @!attribute method
* @return [String] the HTTP method of the request
* @!attribute path
* @return [String] the path portion of the URI, e.g.,
* "/list/?start=5&num=10"
* @!attribute headers
* @return [map<String,String>]
* a map of header keys and their respective values
* @!attribute body
* @return [String] the request body payload
* @!attribute endpoint
* @return [AWS.Endpoint] the endpoint for the request
* @!attribute region
* @api private
* @return [String] the region, for signing purposes only.
*/
AWS.HttpRequest = inherit({
/**
* @api private
*/
constructor: function HttpRequest(endpoint, region) {
endpoint = new AWS.Endpoint(endpoint);
this.method = 'POST';
this.path = endpoint.path || '/';
this.headers = {};
this.body = '';
this.endpoint = endpoint;
this.region = region;
this._userAgent = '';
this.setUserAgent();
},
/**
* @api private
*/
setUserAgent: function setUserAgent() {
this._userAgent = this.headers[this.getUserAgentHeaderName()] = AWS.util.userAgent();
},
getUserAgentHeaderName: function getUserAgentHeaderName() {
var prefix = AWS.util.isBrowser() ? 'X-Amz-' : '';
return prefix + 'User-Agent';
},
/**
* @api private
*/
appendToUserAgent: function appendToUserAgent(agentPartial) {
if (typeof agentPartial === 'string' && agentPartial) {
this._userAgent += ' ' + agentPartial;
}
this.headers[this.getUserAgentHeaderName()] = this._userAgent;
},
/**
* @api private
*/
getUserAgent: function getUserAgent() {
return this._userAgent;
},
/**
* @return [String] the part of the {path} excluding the
* query string
*/
pathname: function pathname() {
return this.path.split('?', 1)[0];
},
/**
* @return [String] the query string portion of the {path}
*/
search: function search() {
var query = this.path.split('?', 2)[1];
if (query) {
query = AWS.util.queryStringParse(query);
return AWS.util.queryParamsToString(query);
}
return '';
},
/**
* @api private
* update httpRequest endpoint with endpoint string
*/
updateEndpoint: function updateEndpoint(endpointStr) {
var newEndpoint = new AWS.Endpoint(endpointStr);
this.endpoint = newEndpoint;
this.path = newEndpoint.path || '/';
}
});
/**
* The low level HTTP response object, encapsulating all HTTP header
* and body data returned from the request.
*
* @!attribute statusCode
* @return [Integer] the HTTP status code of the response (e.g., 200, 404)
* @!attribute headers
* @return [map<String,String>]
* a map of response header keys and their respective values
* @!attribute body
* @return [String] the response body payload
* @!attribute [r] streaming
* @return [Boolean] whether this response is being streamed at a low-level.
* Defaults to `false` (buffered reads). Do not modify this manually, use
* {createUnbufferedStream} to convert the stream to unbuffered mode
* instead.
*/
AWS.HttpResponse = inherit({
/**
* @api private
*/
constructor: function HttpResponse() {
this.statusCode = undefined;
this.headers = {};
this.body = undefined;
this.streaming = false;
this.stream = null;
},
/**
* Disables buffering on the HTTP response and returns the stream for reading.
* @return [Stream, XMLHttpRequest, null] the underlying stream object.
* Use this object to directly read data off of the stream.
* @note This object is only available after the {AWS.Request~httpHeaders}
* event has fired. This method must be called prior to
* {AWS.Request~httpData}.
* @example Taking control of a stream
* request.on('httpHeaders', function(statusCode, headers) {
* if (statusCode < 300) {
* if (headers.etag === 'xyz') {
* // pipe the stream, disabling buffering
* var stream = this.response.httpResponse.createUnbufferedStream();
* stream.pipe(process.stdout);
* } else { // abort this request and set a better error message
* this.abort();
* this.response.error = new Error('Invalid ETag');
* }
* }
* }).send(console.log);
*/
createUnbufferedStream: function createUnbufferedStream() {
this.streaming = true;
return this.stream;
}
});
AWS.HttpClient = inherit({});
/**
* @api private
*/
AWS.HttpClient.getInstance = function getInstance() {
if (this.singleton === undefined) {
this.singleton = new this();
}
return this.singleton;
};