|
|
@@ -7,8 +7,9 @@
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
-enum HttpParserError: Error {
|
|
|
+enum HttpParserError: Error, Equatable {
|
|
|
case invalidStatusLine(String)
|
|
|
+ case negativeContentLength
|
|
|
}
|
|
|
|
|
|
public class HttpParser {
|
|
|
@@ -29,6 +30,11 @@ public class HttpParser {
|
|
|
request.queryParams = urlComponents?.queryItems?.map { ($0.name, $0.value ?? "") } ?? []
|
|
|
request.headers = try readHeaders(socket)
|
|
|
if let contentLength = request.headers["content-length"], let contentLengthValue = Int(contentLength) {
|
|
|
+ // Prevent a buffer overflow and runtime error trying to create an `UnsafeMutableBufferPointer` with
|
|
|
+ // a negative length
|
|
|
+ guard contentLengthValue >= 0 else {
|
|
|
+ throw HttpParserError.negativeContentLength
|
|
|
+ }
|
|
|
request.body = try readBody(socket, size: contentLengthValue)
|
|
|
}
|
|
|
return request
|