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    #if os(OSX)
0026        import Darwin
0027    #elseif os(Linux)
0028        import Glibc
0029    #endif
0030    
0031    import Nest
0032    
0033    public typealias HTTPHandler
HTTPServer.swift:63
    public func serve(handler: HTTPHandler) {
= (HTTPRequest, HTTPResponseWriter) throws -> () 0034 0035 public struct HTTPServer { 0036 0037 var socket
HTTPServer.swift:41
        self.socket = socket
HTTPServer.swift:65
            if (listen(socket.raw, 100) != 0) {
HTTPServer.swift:68
            let client = accept(socket.raw, nil, nil)
: Socket 0038 var address
HTTPServer.swift:42
        self.address = addr
HTTPServer.swift:49
        if !socket.bindAddress(&address.underlying, length: socklen_t(UInt8(sizeof(sockaddr_in)))) {
: SocketAddress 0039 0040 public init?(socket: Socket, addr: SocketAddress) { 0041 self.socket = socket 0042 self.address = addr 0043 0044 socket.setOption(SO_REUSEADDR, value: 1) 0045 #if !os(Linux) 0046 socket.setOption(SO_NOSIGPIPE, value: 1) 0047 #endif 0048 0049 if !socket.bindAddress(&address.underlying, length: socklen_t(UInt8(sizeof(sockaddr_in)))) { 0050 return nil 0051 } 0052 } 0053 0054 // Nest handler - recommended 0055 public func serve(handler: Application) { 0056 serve { (req: HTTPRequest, writer: HTTPResponseWriter) throws in 0057 let response = handler(req) 0058 try writer.write(response) 0059 } 0060 } 0061 0062 // native handler 0063 public func serve(handler: HTTPHandler) { 0064 while (true) { 0065 if (listen(socket.raw, 100) != 0) { 0066 return 0067 } 0068 let client = accept(socket.raw, nil, nil) 0069 defer { 0070 shutdown(client, Int32(SHUT_RDWR)) 0071 close(client) 0072 } 0073 let reader = SocketReader(socket: Socket(raw: client)) 0074 let writer = HTTPResponseWriter(socket: client) 0075 do { 0076 try handler(DefaultHTTPRequestParser().parse(reader), writer) 0077 } 0078 catch let e { 0079 fputs("error: \(e)\n", stderr) 0080 } 0081 } 0082 } 0083 } 0084