@@ -58,15 +58,10 @@ var filenames = {
5858 cached : "_Cached" ,
5959} ;
6060
61- // GET helper function
62- function get ( config , pathname , query , proxy , agent , callback , encoding ) {
61+ // GET/POST helper function
62+ function get ( config , pathname , data , proxy , agent , callback , encoding ) {
6363 var protocol , options ;
6464
65- pathname = url . format ( {
66- pathname : pathname ,
67- query : query ,
68- } ) ;
69-
7065 if ( proxy ) {
7166 var proxyUrl = url . parse ( proxy ) ;
7267 var pathForProxy = config . protocol + "//" ;
@@ -85,6 +80,8 @@ function get(config, pathname, query, proxy, agent, callback, encoding) {
8580 headers : {
8681 Host : config . hostname ,
8782 } ,
83+ method : config . method
84+
8885 } ;
8986 } else {
9087 protocol = config . protocol === "https:" ? https : http ;
@@ -93,117 +90,13 @@ function get(config, pathname, query, proxy, agent, callback, encoding) {
9390 host : config . hostname ,
9491 auth : config . auth ,
9592 port : config . port ,
93+ method : config . method ,
9694 headers : { } ,
9795 } ;
9896 }
9997
100- if ( encoding !== "binary" ) {
101- options . headers [ "X-WPT-API-KEY" ] = this . config . key ;
102- options . headers [ "accept-encoding" ] = "gzip,deflate" ;
103- options . headers [ "User-Agent" ] = "WebpagetestNodeWrapper/v0.6.0" ;
104- }
105-
106- if ( agent ) {
107- options . agent = agent ;
108- }
109-
110- return protocol
111- . get ( options , function getResponse ( res ) {
112- var data ,
113- length ,
114- statusCode = res . statusCode ;
115-
116- if ( statusCode !== 200 ) {
117- callback (
118- new helper . WPTAPIError ( statusCode , http . STATUS_CODES [ statusCode ] )
119- ) ;
120- } else {
121- data = [ ] ;
122- length = 0 ;
123-
124- encoding = res . headers [ "content-encoding" ] || encoding || "uft8" ;
125-
126- res . on ( "data" , function onData ( chunk ) {
127- data . push ( chunk ) ;
128- length += chunk . length ;
129- } ) ;
130-
131- res . on ( "end" , function onEnd ( ) {
132- var i ,
133- len ,
134- pos ,
135- buffer = new Buffer . alloc ( length ) ,
136- type = ( res . headers [ "content-type" ] || "" ) . split ( ";" ) [ 0 ] ;
137-
138- for ( i = 0 , len = data . length , pos = 0 ; i < len ; i += 1 ) {
139- data [ i ] . copy ( buffer , pos ) ;
140- pos += data [ i ] . length ;
141- }
142-
143- if ( encoding === "gzip" || encoding === "deflate" ) {
144- // compressed response (gzip,deflate)
145- zlib . unzip ( buffer , function unzip ( err , buffer ) {
146- if ( err ) {
147- callback ( err ) ;
148- } else {
149- callback ( undefined , buffer . toString ( ) , {
150- type : type ,
151- encoding : encoding ,
152- } ) ;
153- }
154- } ) ;
155- } else {
156- // uncompressed response
157- callback ( undefined , buffer , {
158- type : type ,
159- encoding : encoding ,
160- } ) ;
161- }
162- } ) ;
163- }
164- } )
165- . on ( "error" , function onError ( err ) {
166- callback ( err ) ;
167- } ) ;
168- }
169-
170- // execute runTest using POST request
171- function post ( config , pathname , query , proxy , agent , callback , encoding ) {
172- var protocol , options ;
173-
174- if ( proxy ) {
175- var proxyUrl = url . parse ( proxy ) ;
176- var pathForProxy = config . protocol + "//" ;
177-
178- if ( config . auth ) {
179- pathForProxy += config . auth + "@" ;
180- }
181-
182- pathForProxy += config . hostname + ":" + config . port + pathname ;
183- protocol = proxyUrl . protocol === "https:" ? https : http ;
184-
185- options = {
186- host : proxyUrl . hostname ,
187- port : proxyUrl . port ,
188- path : pathForProxy ,
189- method : "POST" ,
190- headers : {
191- Host : config . hostname ,
192- } ,
193-
194- } ;
195- } else {
196- protocol = config . protocol === "https:" ? https : http ;
197- options = {
198- path : pathname ,
199- host : config . hostname ,
200- auth : config . auth ,
201- port : config . port ,
202- method : "POST" ,
203- headers : {
204- 'Content-Type' : 'application/x-www-form-urlencoded' ,
205- } ,
206- } ;
98+ if ( options . method == "POST" ) {
99+ options . headers [ 'Content-Type' ] = 'application/x-www-form-urlencoded' ;
207100 }
208101
209102 if ( encoding !== "binary" ) {
@@ -216,9 +109,7 @@ function post(config, pathname, query, proxy, agent, callback, encoding) {
216109 options . agent = agent ;
217110 }
218111
219- postData = qs . stringify ( query )
220-
221- return protocol
112+ var request = protocol
222113 . request ( options , function getResponse ( res ) {
223114 var data ,
224115 length ,
@@ -276,7 +167,13 @@ function post(config, pathname, query, proxy, agent, callback, encoding) {
276167 . on ( "error" , function onError ( err ) {
277168 callback ( err ) ;
278169 } )
279- . end ( postData ) ;
170+
171+ if ( options . method == "POST" ) {
172+ return request . end ( qs . stringify ( data ) ) ;
173+ } else {
174+ return request . end ( ) ;
175+ }
176+
280177}
281178
282179// execute callback properly normalizing optional args
@@ -306,14 +203,27 @@ function api(pathname, callback, query, options) {
306203
307204 pathname = url . resolve ( config . pathname , pathname ) ;
308205
206+ config . method = url . format ( {
207+ pathname : pathname ,
208+ query : query ,
209+ } ) . toString ( ) . length > 6 * 1024 ? "POST" : "GET" ;
210+
211+ if ( config . method == "GET" ) {
212+ pathname = url . format ( {
213+ pathname : pathname ,
214+ query : query ,
215+ } ) ;
216+ query = undefined ;
217+ }
218+
309219 if ( options . dryRun ) {
310220 // dry run: return the API url (string) only
311221 if ( typeof callback === "function" ) {
312222 callback . apply ( callback , [ undefined , helper . dryRun ( config , pathname , query ) ] ) ;
313223 }
314224 } else {
315225 // make the real API call
316- ( options . custom !== undefined ? post : get ) . call (
226+ get . call (
317227 this ,
318228 config ,
319229 pathname ,
0 commit comments