Skip to content

Commit 61858a3

Browse files
committed
request超时时间重置
1 parent 176e191 commit 61858a3

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

qiniu/rpc.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
var urllib = require('urllib');
2+
var conf = require('./conf');
3+
4+
exports.post = post;
5+
exports.put = put;
6+
exports.postMultipart = postMultipart;
7+
exports.postWithForm = postWithForm;
8+
exports.postWithoutForm = postWithoutForm;
9+
10+
function postMultipart(requestURI, requestForm, callbackFunc) {
11+
return post(requestURI, requestForm, requestForm.headers(), callbackFunc);
12+
}
13+
14+
function postWithForm(requestURI, requestForm, token, callbackFunc) {
15+
var headers = {
16+
'Content-Type': 'application/x-www-form-urlencoded'
17+
};
18+
if (token) {
19+
headers.Authorization = token;
20+
}
21+
return post(requestURI, requestForm, headers, callbackFunc);
22+
}
23+
24+
function postWithoutForm(requestURI, token, callbackFunc) {
25+
var headers = {
26+
'Content-Type': 'application/x-www-form-urlencoded'
27+
};
28+
if (token) {
29+
headers.Authorization = token;
30+
}
31+
return post(requestURI, null, headers, callbackFunc);
32+
}
33+
34+
function post(requestURI, requestForm, headers, callbackFunc) {
35+
// var start = parseInt(Date.now() / 1000);
36+
headers = headers || {};
37+
headers['User-Agent'] = headers['User-Agent'] || conf.USER_AGENT;
38+
headers.Connection = 'keep-alive';
39+
40+
var data = {
41+
headers: headers,
42+
method: 'POST',
43+
dataType: 'json',
44+
timeout: conf.RPC_TIMEOUT,
45+
gzip: true
46+
// timing: true,
47+
};
48+
49+
if (conf.RPC_HTTP_AGENT) {
50+
data.agent = conf.RPC_HTTP_AGENT;
51+
}
52+
53+
if (conf.RPC_HTTPS_AGENT) {
54+
data.httpsAgent = conf.RPC_HTTPS_AGENT;
55+
}
56+
57+
if (Buffer.isBuffer(requestForm) || typeof requestForm === 'string') {
58+
data.content = requestForm;
59+
} else if (requestForm) {
60+
data.stream = requestForm;
61+
} else {
62+
data.headers['Content-Length'] = 0;
63+
}
64+
65+
var req = urllib.request(requestURI, data, function (respErr, respBody,
66+
respInfo) {
67+
callbackFunc(respErr, respBody, respInfo);
68+
});
69+
70+
return req;
71+
}
72+
73+
function put(requestURL, requestForm, headers, callbackFunc) {
74+
// var start = parseInt(Date.now() / 1000);
75+
headers = headers || {};
76+
headers['User-Agent'] = headers['User-Agent'] || conf.USER_AGENT;
77+
headers.Connection = 'keep-alive';
78+
79+
var data = {
80+
headers: headers,
81+
method: 'PUT',
82+
dataType: 'json',
83+
timeout: conf.RPC_TIMEOUT,
84+
gzip: true
85+
// timing: true,
86+
};
87+
88+
if (conf.RPC_HTTP_AGENT) {
89+
data.agent = conf.RPC_HTTP_AGENT;
90+
}
91+
92+
if (conf.RPC_HTTPS_AGENT) {
93+
data.httpsAgent = conf.RPC_HTTPS_AGENT;
94+
}
95+
96+
if (Buffer.isBuffer(requestForm) || typeof requestForm === 'string') {
97+
data.content = requestForm;
98+
} else if (requestForm) {
99+
data.stream = requestForm;
100+
} else {
101+
data.headers['Content-Length'] = 0;
102+
}
103+
104+
var req = urllib.request(requestURL, data, function (err, ret, info) {
105+
callbackFunc(err, ret, info);
106+
});
107+
108+
return req;
109+
}

0 commit comments

Comments
 (0)