@@ -9,7 +9,7 @@ import 'common.dart';
9
9
import 'event.dart' ;
10
10
import 'dart:io' ;
11
11
12
- enum RequestBodyEncoding { JSON , FormURLEncoded }
12
+ enum RequestBodyEncoding { JSON , FormURLEncoded , PlainText }
13
13
14
14
final Logger log = Logger ('requests' );
15
15
@@ -28,8 +28,7 @@ class Response {
28
28
29
29
throwForStatus () {
30
30
if (! success) {
31
- throw HTTPException (
32
- "Invalid HTTP status code $statusCode for url ${url }" , this );
31
+ throw HTTPException ("Invalid HTTP status code $statusCode for url ${url }" , this );
33
32
}
34
33
}
35
34
@@ -69,31 +68,17 @@ class Requests {
69
68
static const String HTTP_METHOD_HEAD = "head" ;
70
69
static const int DEFAULT_TIMEOUT_SECONDS = 10 ;
71
70
72
- static const RequestBodyEncoding DEFAULT_BODY_ENCODING =
73
- RequestBodyEncoding .FormURLEncoded ;
71
+ static const RequestBodyEncoding DEFAULT_BODY_ENCODING = RequestBodyEncoding .FormURLEncoded ;
74
72
75
- static Set _cookiesKeysToIgnore = Set .from ([
76
- "SameSite" ,
77
- "Path" ,
78
- "Domain" ,
79
- "Max-Age" ,
80
- "Expires" ,
81
- "Secure" ,
82
- "HttpOnly"
83
- ]);
73
+ static Set _cookiesKeysToIgnore = Set .from (["SameSite" , "Path" , "Domain" , "Max-Age" , "Expires" , "Secure" , "HttpOnly" ]);
84
74
85
75
static Map <String , String > _extractResponseCookies (responseHeaders) {
86
76
Map <String , String > cookies = {};
87
77
for (var key in responseHeaders.keys) {
88
78
if (Common .equalsIgnoreCase (key, 'set-cookie' )) {
89
79
String cookie = responseHeaders[key];
90
80
cookie.split ("," ).forEach ((String one) {
91
- cookie
92
- .split (";" )
93
- .map ((x) => x.trim ().split ("=" ))
94
- .where ((x) => x.length == 2 )
95
- .where ((x) => ! _cookiesKeysToIgnore.contains (x[0 ]))
96
- .forEach ((x) => cookies[x[0 ]] = x[1 ]);
81
+ cookie.split (";" ).map ((x) => x.trim ().split ("=" )).where ((x) => x.length == 2 ).where ((x) => ! _cookiesKeysToIgnore.contains (x[0 ])).forEach ((x) => cookies[x[0 ]] = x[1 ]);
97
82
});
98
83
break ;
99
84
}
@@ -102,11 +87,9 @@ class Requests {
102
87
return cookies;
103
88
}
104
89
105
- static Future <Map > _constructRequestHeaders (
106
- String hostname, Map <String , String > customHeaders) async {
90
+ static Future <Map > _constructRequestHeaders (String hostname, Map <String , String > customHeaders) async {
107
91
var cookies = await getStoredCookies (hostname);
108
- String cookie =
109
- cookies.keys.map ((key) => "$key =${cookies [key ]}" ).join ("; " );
92
+ String cookie = cookies.keys.map ((key) => "$key =${cookies [key ]}" ).join ("; " );
110
93
Map <String , String > requestHeaders = Map ();
111
94
requestHeaders['cookie' ] = cookie;
112
95
@@ -123,14 +106,12 @@ class Requests {
123
106
var cookies = Common .fromJson (cookiesJson);
124
107
return Map <String , String >.from (cookies);
125
108
} catch (e) {
126
- log.shout (
127
- "problem reading stored cookies. fallback with empty cookies $e " );
109
+ log.shout ("problem reading stored cookies. fallback with empty cookies $e " );
128
110
return Map <String , String >();
129
111
}
130
112
}
131
113
132
- static Future setStoredCookies (
133
- String hostname, Map <String , String > cookies) async {
114
+ static Future setStoredCookies (String hostname, Map <String , String > cookies) async {
134
115
String hostnameHash = Common .hashStringSHA256 (hostname);
135
116
String cookiesJson = Common .toJson (cookies);
136
117
await Common .storageSet ('cookies-$hostnameHash ' , cookiesJson);
@@ -146,8 +127,7 @@ class Requests {
146
127
return "${uri .host }:${uri .port }" ;
147
128
}
148
129
149
- static Future <Response > _handleHttpResponse (
150
- String hostname, http.Response rawResponse, bool persistCookies) async {
130
+ static Future <Response > _handleHttpResponse (String hostname, http.Response rawResponse, bool persistCookies) async {
151
131
if (persistCookies) {
152
132
var responseCookies = _extractResponseCookies (rawResponse.headers);
153
133
if (responseCookies.isNotEmpty) {
@@ -166,106 +146,31 @@ class Requests {
166
146
return response;
167
147
}
168
148
169
- static Future <Response > head (String url,
170
- {headers,
171
- bodyEncoding = DEFAULT_BODY_ENCODING ,
172
- timeoutSeconds = DEFAULT_TIMEOUT_SECONDS ,
173
- persistCookies = true ,
174
- verify = true }) {
175
- return _httpRequest (HTTP_METHOD_HEAD , url,
176
- bodyEncoding: bodyEncoding,
177
- headers: headers,
178
- timeoutSeconds: timeoutSeconds,
179
- persistCookies: persistCookies,
180
- verify: verify);
149
+ static Future <Response > head (String url, {headers, bodyEncoding = DEFAULT_BODY_ENCODING , timeoutSeconds = DEFAULT_TIMEOUT_SECONDS , persistCookies = true , verify = true }) {
150
+ return _httpRequest (HTTP_METHOD_HEAD , url, bodyEncoding: bodyEncoding, headers: headers, timeoutSeconds: timeoutSeconds, persistCookies: persistCookies, verify: verify);
181
151
}
182
152
183
- static Future <Response > get (String url,
184
- {headers,
185
- bodyEncoding = DEFAULT_BODY_ENCODING ,
186
- timeoutSeconds = DEFAULT_TIMEOUT_SECONDS ,
187
- persistCookies = true ,
188
- verify = true }) {
189
- return _httpRequest (HTTP_METHOD_GET , url,
190
- bodyEncoding: bodyEncoding,
191
- headers: headers,
192
- timeoutSeconds: timeoutSeconds,
193
- persistCookies: persistCookies,
194
- verify: verify);
153
+ static Future <Response > get (String url, {headers, bodyEncoding = DEFAULT_BODY_ENCODING , timeoutSeconds = DEFAULT_TIMEOUT_SECONDS , persistCookies = true , verify = true }) {
154
+ return _httpRequest (HTTP_METHOD_GET , url, bodyEncoding: bodyEncoding, headers: headers, timeoutSeconds: timeoutSeconds, persistCookies: persistCookies, verify: verify);
195
155
}
196
156
197
- static Future <Response > patch (String url,
198
- {headers,
199
- bodyEncoding = DEFAULT_BODY_ENCODING ,
200
- timeoutSeconds = DEFAULT_TIMEOUT_SECONDS ,
201
- persistCookies = true ,
202
- verify = true }) {
203
- return _httpRequest (HTTP_METHOD_PATCH , url,
204
- bodyEncoding: bodyEncoding,
205
- headers: headers,
206
- timeoutSeconds: timeoutSeconds,
207
- persistCookies: persistCookies,
208
- verify: verify);
157
+ static Future <Response > patch (String url, {headers, bodyEncoding = DEFAULT_BODY_ENCODING , timeoutSeconds = DEFAULT_TIMEOUT_SECONDS , persistCookies = true , verify = true }) {
158
+ return _httpRequest (HTTP_METHOD_PATCH , url, bodyEncoding: bodyEncoding, headers: headers, timeoutSeconds: timeoutSeconds, persistCookies: persistCookies, verify: verify);
209
159
}
210
160
211
- static Future <Response > delete (String url,
212
- {headers,
213
- bodyEncoding = DEFAULT_BODY_ENCODING ,
214
- timeoutSeconds = DEFAULT_TIMEOUT_SECONDS ,
215
- persistCookies = true ,
216
- verify = true }) {
217
- return _httpRequest (HTTP_METHOD_DELETE , url,
218
- bodyEncoding: bodyEncoding,
219
- headers: headers,
220
- timeoutSeconds: timeoutSeconds,
221
- persistCookies: persistCookies,
222
- verify: verify);
161
+ static Future <Response > delete (String url, {headers, bodyEncoding = DEFAULT_BODY_ENCODING , timeoutSeconds = DEFAULT_TIMEOUT_SECONDS , persistCookies = true , verify = true }) {
162
+ return _httpRequest (HTTP_METHOD_DELETE , url, bodyEncoding: bodyEncoding, headers: headers, timeoutSeconds: timeoutSeconds, persistCookies: persistCookies, verify: verify);
223
163
}
224
164
225
- static Future <Response > post (String url,
226
- {json,
227
- body,
228
- bodyEncoding = DEFAULT_BODY_ENCODING ,
229
- headers,
230
- timeoutSeconds = DEFAULT_TIMEOUT_SECONDS ,
231
- persistCookies = true ,
232
- verify = true }) {
233
- return _httpRequest (HTTP_METHOD_POST , url,
234
- bodyEncoding: bodyEncoding,
235
- json: json,
236
- body: body,
237
- headers: headers,
238
- timeoutSeconds: timeoutSeconds,
239
- persistCookies: persistCookies,
240
- verify: verify);
165
+ static Future <Response > post (String url, {json, body, bodyEncoding = DEFAULT_BODY_ENCODING , headers, timeoutSeconds = DEFAULT_TIMEOUT_SECONDS , persistCookies = true , verify = true }) {
166
+ return _httpRequest (HTTP_METHOD_POST , url, bodyEncoding: bodyEncoding, json: json, body: body, headers: headers, timeoutSeconds: timeoutSeconds, persistCookies: persistCookies, verify: verify);
241
167
}
242
168
243
- static Future <Response > put (String url,
244
- {json,
245
- body,
246
- bodyEncoding = DEFAULT_BODY_ENCODING ,
247
- headers,
248
- timeoutSeconds = DEFAULT_TIMEOUT_SECONDS ,
249
- persistCookies = true ,
250
- verify = true }) {
251
- return _httpRequest (HTTP_METHOD_PUT , url,
252
- bodyEncoding: bodyEncoding,
253
- json: json,
254
- body: body,
255
- headers: headers,
256
- timeoutSeconds: timeoutSeconds,
257
- persistCookies: persistCookies,
258
- verify: verify);
169
+ static Future <Response > put (String url, {json, body, bodyEncoding = DEFAULT_BODY_ENCODING , headers, timeoutSeconds = DEFAULT_TIMEOUT_SECONDS , persistCookies = true , verify = true }) {
170
+ return _httpRequest (HTTP_METHOD_PUT , url, bodyEncoding: bodyEncoding, json: json, body: body, headers: headers, timeoutSeconds: timeoutSeconds, persistCookies: persistCookies, verify: verify);
259
171
}
260
172
261
- static Future <Response > _httpRequest (String method, String url,
262
- {json,
263
- body,
264
- bodyEncoding = DEFAULT_BODY_ENCODING ,
265
- headers,
266
- timeoutSeconds = DEFAULT_TIMEOUT_SECONDS ,
267
- persistCookies = true ,
268
- verify = true }) async {
173
+ static Future <Response > _httpRequest (String method, String url, {json, body, bodyEncoding = DEFAULT_BODY_ENCODING , headers, timeoutSeconds = DEFAULT_TIMEOUT_SECONDS , persistCookies = true , verify = true }) async {
269
174
http.Client client;
270
175
if (! verify) {
271
176
// Ignore SSL errors
@@ -280,13 +185,12 @@ class Requests {
280
185
var uri = Uri .parse (url);
281
186
282
187
if (uri.scheme != 'http' && uri.scheme != 'https' ) {
283
- throw ArgumentError (
284
- "invalid url, must start with 'http://' or 'https://' sheme (e.g. 'http://example.com')" );
188
+ throw ArgumentError ("invalid url, must start with 'http://' or 'https://' sheme (e.g. 'http://example.com')" );
285
189
}
286
190
287
191
String hostname = getHostname (url);
288
192
headers = await _constructRequestHeaders (hostname, headers);
289
- dynamic bodyString = "" ;
193
+ dynamic requestBody ;
290
194
291
195
if (body != null && json != null ) {
292
196
throw ArgumentError ('cannot use both "json" and "body" choose only one.' );
@@ -299,22 +203,24 @@ class Requests {
299
203
300
204
if (body != null ) {
301
205
String contentTypeHeader;
302
- bodyString = body;
303
- if (body is String ) {
206
+
207
+ if (bodyEncoding == RequestBodyEncoding .JSON ) {
208
+ requestBody = Common .toJson (body);
209
+ contentTypeHeader = "application/json" ;
210
+ }
211
+ else if (bodyEncoding == RequestBodyEncoding .FormURLEncoded ) {
212
+ requestBody = Common .encodeMap (body);
213
+ contentTypeHeader = "application/x-www-form-urlencoded" ;
214
+ }
215
+ else if (bodyEncoding == RequestBodyEncoding .PlainText ) {
216
+ requestBody = body;
304
217
contentTypeHeader = "text/plain" ;
305
- } else if (body is Map || body is List ) {
306
- if (bodyEncoding == RequestBodyEncoding .JSON ) {
307
- bodyString = Common .toJson (body);
308
- contentTypeHeader = "application/json" ;
309
- } else if (bodyEncoding == RequestBodyEncoding .FormURLEncoded ) {
310
- contentTypeHeader = "application/x-www-form-urlencoded" ;
311
- } else {
312
- throw Exception ('unsupported bodyEncoding "$bodyEncoding "' );
313
- }
218
+ }
219
+ else {
220
+ throw Exception ('unsupported bodyEncoding "$bodyEncoding "' );
314
221
}
315
222
316
- if (contentTypeHeader != null &&
317
- ! Common .hasKeyIgnoreCase (headers, "content-type" )) {
223
+ if (contentTypeHeader != null && ! Common .hasKeyIgnoreCase (headers, "content-type" )) {
318
224
headers["content-type" ] = contentTypeHeader;
319
225
}
320
226
}
@@ -326,19 +232,19 @@ class Requests {
326
232
future = client.get (uri, headers: headers);
327
233
break ;
328
234
case HTTP_METHOD_PUT :
329
- future = client.put (uri, body: bodyString , headers: headers);
235
+ future = client.put (uri, body: requestBody , headers: headers);
330
236
break ;
331
237
case HTTP_METHOD_DELETE :
332
238
future = client.delete (uri, headers: headers);
333
239
break ;
334
240
case HTTP_METHOD_POST :
335
- future = client.post (uri, body: bodyString , headers: headers);
241
+ future = client.post (uri, body: requestBody , headers: headers);
336
242
break ;
337
243
case HTTP_METHOD_HEAD :
338
244
future = client.head (uri, headers: headers);
339
245
break ;
340
246
case HTTP_METHOD_PATCH :
341
- future = client.patch (uri, body: bodyString , headers: headers);
247
+ future = client.patch (uri, body: requestBody , headers: headers);
342
248
break ;
343
249
default :
344
250
throw Exception ('unsupported http method "$method "' );
0 commit comments