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 #if os(OSX) 0028 import Darwin 0029 #elseif os(Linux) 0030 import Glibc 0031 #endif 0032 0033 enum WriterError: ErrorType { 0034 case GenericError
HTTPResponseWriter.swift:56 throw WriterError.GenericError(error: errno)(error: Int32) 0035 } 0036 0037 public class HTTPResponseWriter
HTTPResponseWriter.swift:56 throw WriterError.GenericError(error: errno){ 0038 0039 let socket
HTTPServer.swift:33 public typealias HTTPHandler = (HTTPRequest, HTTPResponseWriter) throws -> ()HTTPServer.swift:56 serve { (req: HTTPRequest, writer: HTTPResponseWriter) throws inHTTPServer.swift:74 let writer = HTTPResponseWriter(socket: client): Int32 0040 0041 init
HTTPResponseWriter.swift:42 self.socket = socketHTTPResponseWriter.swift:54 let sent = send(socket, bytes, rest, flags)(socket: Int32) { 0042 self.socket = socket 0043 } 0044 0045 public func write(bytes: UnsafePointer<Int8>) throws { 0046 #if os(Linux) 0047 let flags = Int32(MSG_NOSIGNAL) 0048 #else 0049 let flags = Int32(0) 0050 #endif 0051 0052 var rest = Int(strlen(bytes)) 0053 while rest > 0 { 0054 let sent = send(socket, bytes, rest, flags) 0055 if sent < 0 { 0056 throw WriterError.GenericError(error: errno) 0057 } 0058 rest -= sent 0059 } 0060 } 0061 0062 public func write(response: ResponseType) throws { 0063 try write("HTTP/1.0 \(response.statusLine)\r\n") 0064 var lengthWrote = false 0065 for header in response.headers { 0066 try write("\(header.0): \(header.1)\r\n") 0067 if header.0 == "Content-Length" { 0068 lengthWrote = true 0069 } 0070 } 0071 if !lengthWrote { 0072 if let bytes = response.body?.bytes() { 0073 try write("Content-Length: \(bytes.count - 1)\r\n") 0074 } 0075 } 0076 try write("\r\n") 0077 if let body = response.body?.bytes() { 0078 try write(body) 0079 } 0080 } 0081 0082 } 0083
HTTPServer.swift:74 let writer = HTTPResponseWriter(socket: client)