Skip to content
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## 0.13.3-dev

* Validate that the `method` parameter of BaseRequest is a valid "token".

## 0.13.2

* Add `package:http/retry.dart` with `RetryClient`. This is the same
Expand Down
13 changes: 11 additions & 2 deletions lib/src/base_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,17 @@ abstract class BaseRequest {
bool get finalized => _finalized;
bool _finalized = false;

BaseRequest(this.method, this.url)
: headers = LinkedHashMap(
static final _tokenRE = RegExp(r"^[\w!#%&'*+\-.^`|~]+$");
static String _validateMethod(String method) {
if (!_tokenRE.hasMatch(method)) {
throw ArgumentError.value(method, 'method', 'Not a valid method');
}
return method;
}

BaseRequest(String method, this.url)
: method = _validateMethod(method),
headers = LinkedHashMap(
equals: (key1, key2) => key1.toLowerCase() == key2.toLowerCase(),
hashCode: (key) => key.toLowerCase().hashCode);

Expand Down
6 changes: 6 additions & 0 deletions test/request_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,10 @@ void main() {
expect(request.toString(), 'POST $dummyUrl');
});
});

group('#method', () {
test('must be a token', () {
expect(() => http.Request('LLAMA[0]', dummyUrl), throwsArgumentError);
});
});
}
6 changes: 6 additions & 0 deletions test/streamed_request_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ void main() {
expect(() => request.contentLength = 10, throwsStateError);
});
});
group('#method', () {
test('must be a token', () {
expect(() => http.StreamedRequest('SUPER LLAMA', dummyUrl),
throwsArgumentError);
});
});
}