@@ -13,7 +13,8 @@ var http = require("http"),
1313 specs = require ( "./specs" ) ,
1414 helper = require ( "./helper" ) ,
1515 server = require ( "./server" ) ,
16- mapping = require ( "./mapping" ) ;
16+ mapping = require ( "./mapping" ) ,
17+ qs = require ( 'querystring' ) ;
1718
1819var reSpace = / \s / ,
1920 reConnectivity =
@@ -58,9 +59,14 @@ var filenames = {
5859} ;
5960
6061// GET helper function
61- function get ( config , pathname , proxy , agent , callback , encoding ) {
62+ function get ( config , pathname , query , proxy , agent , callback , encoding ) {
6263 var protocol , options ;
6364
65+ pathname = url . format ( {
66+ pathname : pathname ,
67+ query : query ,
68+ } ) ;
69+
6470 if ( proxy ) {
6571 var proxyUrl = url . parse ( proxy ) ;
6672 var pathForProxy = config . protocol + "//" ;
@@ -161,6 +167,118 @@ function get(config, pathname, proxy, agent, callback, encoding) {
161167 } ) ;
162168}
163169
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+ } ;
207+ }
208+
209+ if ( encoding !== "binary" ) {
210+ options . headers [ "X-WPT-API-KEY" ] = this . config . key ;
211+ options . headers [ "accept-encoding" ] = "gzip,deflate" ;
212+ options . headers [ "User-Agent" ] = "WebpagetestNodeWrapper/v0.6.0" ;
213+ }
214+
215+ if ( agent ) {
216+ options . agent = agent ;
217+ }
218+
219+ postData = qs . stringify ( query )
220+
221+ return protocol
222+ . request ( options , function getResponse ( res ) {
223+ var data ,
224+ length ,
225+ statusCode = res . statusCode ;
226+
227+ if ( statusCode !== 200 ) {
228+ callback (
229+ new helper . WPTAPIError ( statusCode , http . STATUS_CODES [ statusCode ] )
230+ ) ;
231+ } else {
232+ data = [ ] ;
233+ length = 0 ;
234+
235+ encoding = res . headers [ "content-encoding" ] || encoding || "uft8" ;
236+
237+ res . on ( "data" , function onData ( chunk ) {
238+ data . push ( chunk ) ;
239+ length += chunk . length ;
240+ } ) ;
241+
242+ res . on ( "end" , function onEnd ( ) {
243+ var i ,
244+ len ,
245+ pos ,
246+ buffer = new Buffer . alloc ( length ) ,
247+ type = ( res . headers [ "content-type" ] || "" ) . split ( ";" ) [ 0 ] ;
248+
249+ for ( i = 0 , len = data . length , pos = 0 ; i < len ; i += 1 ) {
250+ data [ i ] . copy ( buffer , pos ) ;
251+ pos += data [ i ] . length ;
252+ }
253+
254+ if ( encoding === "gzip" || encoding === "deflate" ) {
255+ // compressed response (gzip,deflate)
256+ zlib . unzip ( buffer , function unzip ( err , buffer ) {
257+ if ( err ) {
258+ callback ( err ) ;
259+ } else {
260+ callback ( undefined , buffer . toString ( ) , {
261+ type : type ,
262+ encoding : encoding ,
263+ } ) ;
264+ }
265+ } ) ;
266+ } else {
267+ // uncompressed response
268+ callback ( undefined , buffer , {
269+ type : type ,
270+ encoding : encoding ,
271+ } ) ;
272+ }
273+ } ) ;
274+ }
275+ } )
276+ . on ( "error" , function onError ( err ) {
277+ callback ( err ) ;
278+ } )
279+ . end ( postData ) ;
280+ }
281+
164282// execute callback properly normalizing optional args
165283function callbackYield ( callback , err , data , options ) {
166284 if ( typeof callback === "function" ) {
@@ -186,22 +304,20 @@ function api(pathname, callback, query, options) {
186304 config = this . config ;
187305 }
188306
189- pathname = url . format ( {
190- pathname : url . resolve ( config . pathname , pathname ) ,
191- query : query ,
192- } ) ;
307+ pathname = url . resolve ( config . pathname , pathname ) ;
193308
194309 if ( options . dryRun ) {
195310 // dry run: return the API url (string) only
196311 if ( typeof callback === "function" ) {
197- callback . apply ( callback , [ undefined , helper . dryRun ( config , pathname ) ] ) ;
312+ callback . apply ( callback , [ undefined , helper . dryRun ( config , pathname , query ) ] ) ;
198313 }
199314 } else {
200315 // make the real API call
201- get . call (
316+ ( options . custom !== undefined ? post : get ) . call (
202317 this ,
203318 config ,
204319 pathname ,
320+ query ,
205321 options . proxy ,
206322 options . agent ,
207323 function apiCallback ( err , data , info ) {
0 commit comments