Skip to content

Commit 72f55bb

Browse files
authored
fix: delete unless code and delete invalid Content-Type (#805)
1 parent c7ceedd commit 72f55bb

File tree

3 files changed

+94
-161
lines changed

3 files changed

+94
-161
lines changed

lib/browser/client.js

Lines changed: 2 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
const debug = require('debug')('ali-oss');
2-
const crypto = require('crypto');
3-
const path = require('path');
42
const copy = require('copy-to');
5-
const mime = require('mime');
63
const xml = require('xml2js');
74
const AgentKeepalive = require('agentkeepalive');
85
const merge = require('merge-descriptors');
@@ -12,18 +9,14 @@ const platform = require('platform');
129
const utility = require('utility');
1310
const urllib = require('urllib');
1411
const pkg = require('./version');
15-
const dateFormat = require('dateformat');
1612
const bowser = require('bowser');
1713
const signUtils = require('../common/signUtils');
1814
const _isIP = require('../common/utils/isIP');
1915
const _initOptions = require('../common/client/initOptions');
16+
const createRequest = require('../common/utils/createRequest');
2017

2118
const globalHttpAgent = new AgentKeepalive();
2219

23-
function getHeader(headers, name) {
24-
return headers[name] || headers[name.toLowerCase()];
25-
}
26-
2720
function _unSupportBrowserTip() {
2821
const { name, version } = platform;
2922
if (name && name.toLowerCase && name.toLowerCase() === 'ie' && version.split('.')[0] < 10) {
@@ -167,74 +160,6 @@ proto.authorization = function authorization(method, resource, subres, headers)
167160
return signUtils.authorization(this.options.accessKeyId, this.options.accessKeySecret, stringToSign);
168161
};
169162

170-
/**
171-
* create request params
172-
* See `request`
173-
* @api private
174-
*/
175-
176-
proto.createRequest = function createRequest(params) {
177-
const headers = {
178-
'x-oss-date': dateFormat(+new Date() + this.options.amendTimeSkewed, 'UTC:ddd, dd mmm yyyy HH:MM:ss \'GMT\''),
179-
'x-oss-user-agent': this.userAgent
180-
};
181-
182-
if (this.options.isRequestPay) {
183-
Object.assign(headers, { 'x-oss-request-payer': 'requester' });
184-
}
185-
186-
if (this.options.stsToken) {
187-
headers['x-oss-security-token'] = this.options.stsToken;
188-
}
189-
190-
copy(params.headers).to(headers);
191-
192-
if (!getHeader(headers, 'Content-Type')) {
193-
if (params.mime === mime.default_type) {
194-
params.mime = '';
195-
}
196-
197-
if (params.mime && params.mime.indexOf('/') > 0) {
198-
headers['Content-Type'] = params.mime;
199-
} else {
200-
headers['Content-Type'] = mime.getType(params.mime || path.extname(params.object || '')) || 'application/octet-stream';
201-
}
202-
}
203-
204-
if (params.content) {
205-
headers['Content-Md5'] = crypto
206-
.createHash('md5')
207-
.update(Buffer.from(params.content, 'utf8'))
208-
.digest('base64');
209-
if (!headers['Content-Length']) {
210-
headers['Content-Length'] = params.content.length;
211-
}
212-
}
213-
214-
const authResource = this._getResource(params);
215-
headers.authorization = this.authorization(params.method, authResource, params.subres, headers);
216-
217-
const url = this._getReqUrl(params);
218-
this.debug('request %s %s, with headers %j, !!stream: %s', params.method, url, headers, !!params.stream, 'info');
219-
const timeout = params.timeout || this.options.timeout;
220-
const reqParams = {
221-
agent: this.agent,
222-
method: params.method,
223-
content: params.content,
224-
stream: params.stream,
225-
headers,
226-
timeout,
227-
writeStream: params.writeStream,
228-
customResponse: params.customResponse,
229-
ctx: params.ctx || this.ctx
230-
};
231-
232-
return {
233-
url,
234-
params: reqParams
235-
};
236-
};
237-
238163
/**
239164
* request oss server
240165
* @param {Object} params
@@ -255,7 +180,7 @@ proto.createRequest = function createRequest(params) {
255180
*/
256181

257182
proto.request = async function request(params) {
258-
const reqParams = this.createRequest(params);
183+
const reqParams = createRequest.call(this, params);
259184

260185
if (!this.options.useFetch) {
261186
reqParams.params.mode = 'disable-fetch';

lib/client.js

Lines changed: 2 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11

22
const debug = require('debug')('ali-oss');
33
const sendToWormhole = require('stream-wormhole');
4-
const crypto = require('crypto');
5-
const path = require('path');
64
const copy = require('copy-to');
7-
const mime = require('mime');
85
const xml = require('xml2js');
96
const AgentKeepalive = require('agentkeepalive');
107
const HttpsAgentKeepalive = require('agentkeepalive').HttpsAgent;
@@ -15,20 +12,15 @@ const platform = require('platform');
1512
const utility = require('utility');
1613
const urllib = require('urllib');
1714
const pkg = require('../package.json');
18-
const dateFormat = require('dateformat');
1915
const bowser = require('bowser');
2016
const signUtils = require('./common/signUtils');
2117
const _isIP = require('./common/utils/isIP');
2218
const _initOptions = require('./common/client/initOptions');
19+
const createRequest = require('./common/utils/createRequest');
2320

2421
const globalHttpAgent = new AgentKeepalive();
2522
const globalHttpsAgent = new HttpsAgentKeepalive();
2623

27-
function getHeader(headers, name) {
28-
return headers[name] || headers[name.toLowerCase()];
29-
}
30-
31-
3224
function Client(options, ctx) {
3325
if (!(this instanceof Client)) {
3426
return new Client(options, ctx);
@@ -153,80 +145,6 @@ proto.authorization = function authorization(method, resource, subres, headers)
153145
return signUtils.authorization(this.options.accessKeyId, this.options.accessKeySecret, stringToSign);
154146
};
155147

156-
/**
157-
* create request params
158-
* See `request`
159-
* @api private
160-
*/
161-
162-
proto.createRequest = function createRequest(params) {
163-
const headers = {
164-
'x-oss-date': dateFormat(new Date(), 'UTC:ddd, dd mmm yyyy HH:MM:ss \'GMT\''),
165-
'x-oss-user-agent': this.userAgent,
166-
'User-Agent': this.userAgent
167-
};
168-
169-
if (this.options.isRequestPay) {
170-
Object.assign(headers, { 'x-oss-request-payer': 'requester' });
171-
}
172-
173-
if (this.options.stsToken) {
174-
headers['x-oss-security-token'] = this.options.stsToken;
175-
}
176-
177-
copy(params.headers).to(headers);
178-
179-
if (!getHeader(headers, 'Content-Type')) {
180-
if (params.mime === mime.default_type) {
181-
params.mime = '';
182-
}
183-
184-
if (params.mime && params.mime.indexOf('/') > 0) {
185-
headers['Content-Type'] = params.mime;
186-
} else {
187-
headers['Content-Type'] = mime.getType(params.mime || path.extname(params.object || ''));
188-
}
189-
}
190-
191-
if (params.content) {
192-
headers['Content-Md5'] = crypto
193-
.createHash('md5')
194-
.update(Buffer.from(params.content, 'utf8'))
195-
.digest('base64');
196-
if (!headers['Content-Length']) {
197-
headers['Content-Length'] = params.content.length;
198-
}
199-
}
200-
201-
const authResource = this._getResource(params);
202-
headers.authorization = this.authorization(params.method, authResource, params.subres, headers);
203-
204-
const url = this._getReqUrl(params);
205-
debug('request %s %s, with headers %j, !!stream: %s', params.method, url, headers, !!params.stream);
206-
const timeout = params.timeout || this.options.timeout;
207-
const reqParams = {
208-
method: params.method,
209-
content: params.content,
210-
stream: params.stream,
211-
headers,
212-
timeout,
213-
writeStream: params.writeStream,
214-
customResponse: params.customResponse,
215-
ctx: params.ctx || this.ctx
216-
};
217-
if (this.agent) {
218-
reqParams.agent = this.agent;
219-
}
220-
if (this.httpsAgent) {
221-
reqParams.httpsAgent = this.httpsAgent;
222-
}
223-
224-
return {
225-
url,
226-
params: reqParams
227-
};
228-
};
229-
230148
/**
231149
* request oss server
232150
* @param {Object} params
@@ -247,7 +165,7 @@ proto.createRequest = function createRequest(params) {
247165
*/
248166

249167
proto.request = async function request(params) {
250-
const reqParams = this.createRequest(params);
168+
const reqParams = createRequest.call(this, params);
251169
let result;
252170
let reqErr;
253171
try {

lib/common/utils/createRequest.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
const crypto = require('crypto');
2+
const debug = require('debug')('ali-oss');
3+
const mime = require('mime');
4+
const dateFormat = require('dateformat');
5+
const copy = require('copy-to');
6+
const path = require('path');
7+
8+
function getHeader(headers, name) {
9+
return headers[name] || headers[name.toLowerCase()];
10+
}
11+
12+
function delHeader(headers, name) {
13+
delete headers[name];
14+
delete headers[name.toLowerCase()];
15+
}
16+
17+
module.exports = function createRequest(params) {
18+
let date = new Date();
19+
if (this.options.amendTimeSkewed) {
20+
date = +new Date() + this.options.amendTimeSkewed;
21+
}
22+
const headers = {
23+
'x-oss-date': dateFormat(date, 'UTC:ddd, dd mmm yyyy HH:MM:ss \'GMT\''),
24+
'x-oss-user-agent': this.userAgent
25+
};
26+
27+
if (this.userAgent.includes('nodejs')) {
28+
headers['User-Agent'] = this.userAgent;
29+
}
30+
31+
if (this.options.isRequestPay) {
32+
Object.assign(headers, { 'x-oss-request-payer': 'requester' });
33+
}
34+
35+
if (this.options.stsToken) {
36+
headers['x-oss-security-token'] = this.options.stsToken;
37+
}
38+
39+
copy(params.headers).to(headers);
40+
41+
if (!getHeader(headers, 'Content-Type')) {
42+
if (params.mime && params.mime.indexOf('/') > 0) {
43+
headers['Content-Type'] = params.mime;
44+
} else {
45+
headers['Content-Type'] = mime.getType(params.mime || path.extname(params.object || ''));
46+
}
47+
}
48+
49+
if (!getHeader(headers, 'Content-Type')) {
50+
delHeader(headers, 'Content-Type');
51+
}
52+
53+
if (params.content) {
54+
headers['Content-Md5'] = crypto
55+
.createHash('md5')
56+
.update(Buffer.from(params.content, 'utf8'))
57+
.digest('base64');
58+
if (!headers['Content-Length']) {
59+
headers['Content-Length'] = params.content.length;
60+
}
61+
}
62+
63+
const authResource = this._getResource(params);
64+
headers.authorization = this.authorization(params.method, authResource, params.subres, headers);
65+
66+
const url = this._getReqUrl(params);
67+
debug('request %s %s, with headers %j, !!stream: %s', params.method, url, headers, !!params.stream);
68+
const timeout = params.timeout || this.options.timeout;
69+
const reqParams = {
70+
method: params.method,
71+
content: params.content,
72+
stream: params.stream,
73+
headers,
74+
timeout,
75+
writeStream: params.writeStream,
76+
customResponse: params.customResponse,
77+
ctx: params.ctx || this.ctx
78+
};
79+
if (this.agent) {
80+
reqParams.agent = this.agent;
81+
}
82+
if (this.httpsAgent) {
83+
reqParams.httpsAgent = this.httpsAgent;
84+
}
85+
86+
return {
87+
url,
88+
params: reqParams
89+
};
90+
};

0 commit comments

Comments
 (0)