-
Notifications
You must be signed in to change notification settings - Fork 554
Expand file tree
/
Copy pathHttpParser.swift
More file actions
74 lines (65 loc) · 2.74 KB
/
HttpParser.swift
File metadata and controls
74 lines (65 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//
// HttpParser.swift
// Swifter
//
// Copyright (c) 2014-2016 Damian Kołakowski. All rights reserved.
//
import Foundation
enum HttpParserError: Error, Equatable {
case invalidStatusLine(String)
case negativeContentLength
}
public class HttpParser {
public init() { }
public func readHttpRequest(_ socket: Socket) throws -> HttpRequest {
let statusLine = try socket.readLine()
let statusLineTokens = statusLine.components(separatedBy: " ")
if statusLineTokens.count < 3 {
throw HttpParserError.invalidStatusLine(statusLine)
}
let request = HttpRequest()
request.method = statusLineTokens[0]
let encodedPath = Self.escapingInvalidURL(statusLineTokens[1])
let urlComponents = URLComponents(string: encodedPath)
request.path = urlComponents?.path ?? ""
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
}
/// only escaping invalid chars,valid encodedPath keep untouched
static func escapingInvalidURL(_ url: String) -> String {
var urlAllowed: CharacterSet {
var allow = CharacterSet.urlQueryAllowed
allow.insert(charactersIn: "?#%")
return allow
}
return url.addingPercentEncoding(withAllowedCharacters: urlAllowed) ?? url
}
private func readBody(_ socket: Socket, size: Int) throws -> [UInt8] {
return try socket.read(length: size)
}
private func readHeaders(_ socket: Socket) throws -> [String: String] {
var headers = [String: String]()
while case let headerLine = try socket.readLine(), !headerLine.isEmpty {
let headerTokens = headerLine.split(separator: ":", maxSplits: 1, omittingEmptySubsequences: true).map(String.init)
if let name = headerTokens.first, let value = headerTokens.last {
headers[name.lowercased()] = value.trimmingCharacters(in: .whitespaces)
}
}
return headers
}
func supportsKeepAlive(_ headers: [String: String]) -> Bool {
if let value = headers["connection"] {
return "keep-alive" == value.trimmingCharacters(in: .whitespaces)
}
return false
}
}