0001    /*
0002     The MIT License (MIT)
0003    
0004     Copyright (c) 2015 Shun Takebayashi
0005    
0006     Permission is hereby granted, free of charge, to any person obtaining a copy
0007     of this software and associated documentation files (the "Software"), to deal
0008     in the Software without restriction, including without limitation the rights
0009     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0010     copies of the Software, and to permit persons to whom the Software is
0011     furnished to do so, subject to the following conditions:
0012    
0013     The above copyright notice and this permission notice shall be included in all
0014     copies or substantial portions of the Software.
0015    
0016     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0017     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0018     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0019     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0020     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0021     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0022     SOFTWARE.
0023    */
0024    
0025    import Nest
0026    
0027    enum HTTPRequestParserError
HTTPRequestParser.swift:88
            throw HTTPRequestParserError.InvalidRequest(details: "Missing HTTP request method")
HTTPRequestParser.swift:92
            throw HTTPRequestParserError.InvalidRequest(details: "Missing HTTP request path")
HTTPRequestParser.swift:96
            throw HTTPRequestParserError.InvalidRequest(details: "Missing HTTP request version")
: ErrorType { 0028 case InvalidRequest
HTTPRequestParser.swift:88
            throw HTTPRequestParserError.InvalidRequest(details: "Missing HTTP request method")
HTTPRequestParser.swift:92
            throw HTTPRequestParserError.InvalidRequest(details: "Missing HTTP request path")
HTTPRequestParser.swift:96
            throw HTTPRequestParserError.InvalidRequest(details: "Missing HTTP request version")
(details: String) 0029 } 0030 0031 protocol HTTPRequestParser
HTTPRequestParser.swift:37
class DefaultHTTPRequestParser: HTTPRequestParser {
{ 0032 0033 func parse<R: Reader where R.Entry == Int8>(reader: R) throws -> HTTPRequest 0034 0035 } 0036 0037 class DefaultHTTPRequestParser
HTTPRequestParser.swift:61
            let str = (String.fromCString(bytes) ?? "").trimRight(DefaultHTTPRequestParser.CRLF)
HTTPServer.swift:76
                try handler(DefaultHTTPRequestParser().parse(reader), writer)
: HTTPRequestParser { 0038 0039 static let CRLF
HTTPRequestParser.swift:61
            let str = (String.fromCString(bytes) ?? "").trimRight(DefaultHTTPRequestParser.CRLF)
= Character("\r\n") 0040 0041 enum Mode
HTTPRequestParser.swift:50
        var mode = Mode.First
{ 0042 case First
HTTPRequestParser.swift:50
        var mode = Mode.First
HTTPRequestParser.swift:63
            case .First:
0043 case Header
HTTPRequestParser.swift:68
                mode = .Header
HTTPRequestParser.swift:69
            case .Header:
0044 case Empty
HTTPRequestParser.swift:71
                    mode = .Empty
HTTPRequestParser.swift:79
            case .Empty:
0045 case Body
HTTPRequestParser.swift:80
                mode = .Body
HTTPRequestParser.swift:82
            case .Body:
0046 } 0047 0048 func parse
HTTPServer.swift:76
                try handler(DefaultHTTPRequestParser().parse(reader), writer)
<R: Reader where R.Entry == Int8>(reader: R) throws -> HTTPRequest { 0049 let bufferedReader = BufferedReader(reader: reader) 0050 var mode = Mode.First 0051 0052 var method: String? 0053 var path: String? 0054 var version: String? 0055 var headers = [Header]() 0056 var body = [Int8]() 0057 0058 while let line = try bufferedReader.read() { 0059 var bytes = line 0060 bytes.append(0) 0061 let str = (String.fromCString(bytes) ?? "").trimRight(DefaultHTTPRequestParser.CRLF) 0062 switch mode { 0063 case .First: 0064 let fields = str.characters.split(" ", maxSplit: 3, allowEmptySlices: true) 0065 method = String(fields[0]) 0066 path = String(fields[1]) 0067 version = String(fields[2]) 0068 mode = .Header 0069 case .Header: 0070 if str.isEmpty { 0071 mode = .Empty 0072 } 0073 else { 0074 let field = str.characters.split(":", maxSplit: 2, allowEmptySlices: true) 0075 let name = String(field[0]) 0076 let value = String(field[1]).trimLeft(" ", maxCount: 1) 0077 headers.append(Header(name, value)) 0078 } 0079 case .Empty: 0080 mode = .Body 0081 fallthrough 0082 case .Body: 0083 body.appendContentsOf(line) 0084 } 0085 } 0086 0087 guard let parsedMethod = method else { 0088 throw HTTPRequestParserError.InvalidRequest(details: "Missing HTTP request method") 0089 } 0090 0091 guard let parsedPath = path else { 0092 throw HTTPRequestParserError.InvalidRequest(details: "Missing HTTP request path") 0093 } 0094 0095 guard let parsedVersion = version else { 0096 throw HTTPRequestParserError.InvalidRequest(details: "Missing HTTP request version") 0097 } 0098 0099 return HTTPRequest(method: parsedMethod, path: parsedPath, version: parsedVersion, headers: headers, body: body) 0100 } 0101 0102 } 0103