@@ -2,15 +2,16 @@ var urllib = require('urllib');
22var conf = require ( './conf' ) ;
33
44exports . post = post ;
5+ exports . put = put ;
56exports . postMultipart = postMultipart ;
67exports . postWithForm = postWithForm ;
78exports . postWithoutForm = postWithoutForm ;
89
9- function postMultipart ( requestURI , requestForm , callbackFunc ) {
10+ function postMultipart ( requestURI , requestForm , callbackFunc ) {
1011 return post ( requestURI , requestForm , requestForm . headers ( ) , callbackFunc ) ;
1112}
1213
13- function postWithForm ( requestURI , requestForm , token , callbackFunc ) {
14+ function postWithForm ( requestURI , requestForm , token , callbackFunc ) {
1415 var headers = {
1516 'Content-Type' : 'application/x-www-form-urlencoded'
1617 } ;
@@ -20,7 +21,7 @@ function postWithForm (requestURI, requestForm, token, callbackFunc) {
2021 return post ( requestURI , requestForm , headers , callbackFunc ) ;
2122}
2223
23- function postWithoutForm ( requestURI , token , callbackFunc ) {
24+ function postWithoutForm ( requestURI , token , callbackFunc ) {
2425 var headers = {
2526 'Content-Type' : 'application/x-www-form-urlencoded'
2627 } ;
@@ -30,7 +31,7 @@ function postWithoutForm (requestURI, token, callbackFunc) {
3031 return post ( requestURI , null , headers , callbackFunc ) ;
3132}
3233
33- function post ( requestURI , requestForm , headers , callbackFunc ) {
34+ function post ( requestURI , requestForm , headers , callbackFunc ) {
3435 // var start = parseInt(Date.now() / 1000);
3536 headers = headers || { } ;
3637 headers [ 'User-Agent' ] = headers [ 'User-Agent' ] || conf . USER_AGENT ;
@@ -42,7 +43,7 @@ function post (requestURI, requestForm, headers, callbackFunc) {
4243 dataType : 'json' ,
4344 timeout : conf . RPC_TIMEOUT ,
4445 gzip : true
45- // timing: true,
46+ // timing: true,
4647 } ;
4748
4849 if ( conf . RPC_HTTP_AGENT ) {
@@ -62,18 +63,47 @@ function post (requestURI, requestForm, headers, callbackFunc) {
6263 }
6364
6465 var req = urllib . request ( requestURI , data , function ( respErr , respBody ,
65- respInfo ) {
66- // var end = parseInt(Date.now() / 1000);
67- // console.log((end - start) + " seconds");
68- // console.log("queuing:\t" + respInfo.timing.queuing);
69- // console.log("dnslookup:\t" + respInfo.timing.dnslookup);
70- // console.log("connected:\t" + respInfo.timing.connected);
71- // console.log("requestSent:\t" + respInfo.timing.requestSent);
72- // console.log("waiting:\t" + respInfo.timing.waiting);
73- // console.log("contentDownload:\t" + respInfo.timing.contentDownload);
74-
66+ respInfo ) {
7567 callbackFunc ( respErr , respBody , respInfo ) ;
7668 } ) ;
7769
7870 return req ;
7971}
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