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 struct http4swift.HTTPRequest
0026    import struct http4swift.HTTPServer
0027    import struct http4swift.SocketAddress
0028    import struct http4swift.Socket
0029    
0030    public func get(path: String, handler: Handler) {
0031        Router.sharedRouter.addPattern(method: "GET", pattern: path, handler: handler)
0032    }
0033    
0034    public func post(path: String, handler: Handler) {
0035        Router.sharedRouter.addPattern(method: "POST", pattern: path, handler: handler)
0036    }
0037    
0038    public func put(path: String, handler: Handler) {
0039        Router.sharedRouter.addPattern(method: "PUT", pattern: path, handler: handler)
0040    }
0041    
0042    public func delete(path: String, handler: Handler) {
0043        Router.sharedRouter.addPattern(method: "DELETE", pattern: path, handler: handler)
0044    }
0045    
0046    public func head(path: String, handler: Handler) {
0047        Router.sharedRouter.addPattern(method: "HEAD", pattern: path, handler: handler)
0048    }
0049    
0050    public func serve(port: UInt16) {
0051        let addr = SocketAddress(port: port)
0052        guard let sock = Socket() else {
0053            return
0054        }
0055        guard let server = HTTPServer(socket: sock, addr: addr) else {
0056            return
0057        }
0058    
0059        server.serve { (request, writer) in
0060            var response: Response? = Router.sharedRouter.dispatch(RawRequest(underlying: request))
0061            if response == nil {
0062                response = Response(.NotFound)
0063                response!.body = Response.Status.NotFound.description.bytes()
0064            }
0065            let size = response!.body.filter({ c in return c != 0 }).count
0066            try writer.write("HTTP/1.0 \(response!.status.rawValue) \(response!.status)\r\n")
0067            try writer.write("Content-Length: \(size)\r\n")
0068            for header in response!.headers {
0069                try writer.write("\(header.0): \(header.1)\r\n")
0070            }
0071            try writer.write("\r\n")
0072            try writer.write(response!.body)
0073        }
0074    }
0075