@@ -58,15 +58,10 @@ var filenames = {
58
58
cached : "_Cached" ,
59
59
} ;
60
60
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 ) {
63
63
var protocol , options ;
64
64
65
- pathname = url . format ( {
66
- pathname : pathname ,
67
- query : query ,
68
- } ) ;
69
-
70
65
if ( proxy ) {
71
66
var proxyUrl = url . parse ( proxy ) ;
72
67
var pathForProxy = config . protocol + "//" ;
@@ -85,6 +80,8 @@ function get(config, pathname, query, proxy, agent, callback, encoding) {
85
80
headers : {
86
81
Host : config . hostname ,
87
82
} ,
83
+ method : config . method
84
+
88
85
} ;
89
86
} else {
90
87
protocol = config . protocol === "https:" ? https : http ;
@@ -93,117 +90,13 @@ function get(config, pathname, query, proxy, agent, callback, encoding) {
93
90
host : config . hostname ,
94
91
auth : config . auth ,
95
92
port : config . port ,
93
+ method : config . method ,
96
94
headers : { } ,
97
95
} ;
98
96
}
99
97
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' ;
207
100
}
208
101
209
102
if ( encoding !== "binary" ) {
@@ -216,9 +109,7 @@ function post(config, pathname, query, proxy, agent, callback, encoding) {
216
109
options . agent = agent ;
217
110
}
218
111
219
- postData = qs . stringify ( query )
220
-
221
- return protocol
112
+ var request = protocol
222
113
. request ( options , function getResponse ( res ) {
223
114
var data ,
224
115
length ,
@@ -276,7 +167,13 @@ function post(config, pathname, query, proxy, agent, callback, encoding) {
276
167
. on ( "error" , function onError ( err ) {
277
168
callback ( err ) ;
278
169
} )
279
- . end ( postData ) ;
170
+
171
+ if ( options . method == "POST" ) {
172
+ return request . end ( qs . stringify ( data ) ) ;
173
+ } else {
174
+ return request . end ( ) ;
175
+ }
176
+
280
177
}
281
178
282
179
// execute callback properly normalizing optional args
@@ -306,14 +203,27 @@ function api(pathname, callback, query, options) {
306
203
307
204
pathname = url . resolve ( config . pathname , pathname ) ;
308
205
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
+
309
219
if ( options . dryRun ) {
310
220
// dry run: return the API url (string) only
311
221
if ( typeof callback === "function" ) {
312
222
callback . apply ( callback , [ undefined , helper . dryRun ( config , pathname , query ) ] ) ;
313
223
}
314
224
} else {
315
225
// make the real API call
316
- ( options . custom !== undefined ? post : get ) . call (
226
+ get . call (
317
227
this ,
318
228
config ,
319
229
pathname ,
0 commit comments