0001 // 0002 // Based on HttpParser from Swifter (https://github.com/glock45/swifter) by Damian KoĊakowski. 0003 // 0004 0005 #if os(Linux) 0006 import Glibc 0007 #endif 0008 0009 import Foundation 0010 import Vaquita 0011 0012 enum SocketParserError: ErrorType { 0013 case InvalidStatusLine
SocketParser.swift:25 throw SocketParserError.InvalidStatusLine(statusLine)(String) 0014 } 0015 0016 class SocketParser
SocketParser.swift:25 throw SocketParserError.InvalidStatusLine(statusLine){ 0017 0018 func readHttpRequest
SocketServer.swift:27 private let socketParser: SocketParserSocketServer.swift:34 socketParser = SocketParser()(socket: Socket) throws -> Request { 0019 0020 let statusLine = try socket.readLine() 0021 0022 let statusLineTokens = statusLine.split(" ") 0023 0024 if statusLineTokens.count < 3 { 0025 throw SocketParserError.InvalidStatusLine(statusLine) 0026 } 0027 0028 let method = Request.Method(rawValue: statusLineTokens[0]) ?? .Unknown 0029 let request = Request(method: method) 0030 0031 request.path = statusLineTokens[1] 0032 request.data = extractQueryParams(request.path) 0033 request.headers = try readHeaders(socket) 0034 0035 if let cookieString = request.headers["cookie"] { 0036 let cookies = cookieString.split(";") 0037 for cookie in cookies { 0038 let cookieArray = cookie.split("=") 0039 if cookieArray.count == 2 { 0040 let key = cookieArray[0].stringByReplacingOccurrencesOfString(" ", withString: "") 0041 request.cookies[key] = cookieArray[1] 0042 } 0043 } 0044 } 0045 0046 if let contentLength = request.headers["content-length"], 0047 0048 let contentLengthValue = Int(contentLength) { 0049 0050 let body = try readBody(socket, size: contentLengthValue) 0051 0052 let bodyString = try body.toString() 0053 let postArray = bodyString.split("&") 0054 for postItem in postArray { 0055 let pair = postItem.split("=") 0056 if pair.count == 2 { 0057 request.data[pair[0]] = pair[1] 0058 } 0059 } 0060 0061 request.body = body 0062 } 0063 0064 return request 0065 } 0066 0067 private func extractQueryParams
SocketServer.swift:71 if let request = try? socketParser.readHttpRequest(socket) {(url: String) -> [String: Any] { 0068 var query = [String: Any]() 0069 0070 var urlParts = url.split("?") 0071 if urlParts.count < 2 { 0072 return query 0073 } 0074 0075 for subQuery in urlParts[1].split("&") { 0076 let tokens = subQuery.split(1, separator: "=") 0077 if let name = tokens.first, value = tokens.last { 0078 query[name.removePercentEncoding()] = value.removePercentEncoding() 0079 } 0080 } 0081 0082 return query 0083 } 0084 0085 private func readBody(socket: Socket, size: Int) throws -> Data { 0086 var body = [UInt8]() 0087 var counter = 0 0088 while counter < size { 0089 body.append(try socket.read()) 0090 counter += 1 0091 } 0092 return Data(bytes: body) 0093 } 0094 0095 private func readHeaders
SocketParser.swift:32 request.data = extractQueryParams(request.path)(socket: Socket) throws -> [String: String] { 0096 var requestHeaders = [String: String]() 0097 repeat { 0098 let headerLine = try socket.readLine() 0099 if headerLine.isEmpty { 0100 return requestHeaders 0101 } 0102 let headerTokens = headerLine.split(1, separator: ":") 0103 if let name = headerTokens.first, value = headerTokens.last { 0104 requestHeaders[name.lowercaseString] = value.trim() 0105 } 0106 } while true 0107 } 0108 0109 func supportsKeepAlive(headers: [String: String]) -> Bool { 0110 if let value = headers["connection"] { 0111 return "keep-alive" == value.trim() 0112 } 0113 return false 0114 } 0115 } 0116
SocketParser.swift:33 request.headers = try readHeaders(socket)