0001    #if os(Linux)
0002    import Glibc
0003    #else
0004    import Darwin.C
0005    #endif
0006    
0007    import Nest
0008    import Inquiline
0009    
0010    
0011    public final class SynchronousWorker : WorkerType {
0012      let configuration
SynchronousWorker.swift:17
    return configuration.timeout / 2
: Configuration 0013 let logger
SynchronousWorker.swift:45
    logger.info("Booting worker process with pid: \(getpid())")
SynchronousWorker.swift:50
      logger.info("Failed to boot \(error)")
SynchronousWorker.swift:66
      logger.info("Parent changed, shutting down")
: Logger 0014 let listeners
SynchronousWorker.swift:55
    listeners.forEach { $0.blocking = false }
SynchronousWorker.swift:57
    if listeners.count == 1 {
SynchronousWorker.swift:58
      runOne(listeners.first!)
SynchronousWorker.swift:60
      runMultiple(listeners)
SynchronousWorker.swift:123
    let (read, _, _) = select(listeners + [sharedHandler!.pipe[0]], [], [], timeout: timeout)
: [Socket] 0015 0016 var timeout
SynchronousWorker.swift:117
    if self.timeout > 0 {
SynchronousWorker.swift:118
      timeout = timeval(tv_sec: self.timeout, tv_usec: 0)
: Int { 0017 return configuration.timeout / 2 0018 } 0019 0020 let notify
SynchronousWorker.swift:76
      notify()
SynchronousWorker.swift:90
      notify()
: Void -> Void 0021 0022 let application: RequestType -> ResponseType 0023 var isAlive
SynchronousWorker.swift:53
    isAlive = true
SynchronousWorker.swift:74
    while isAlive {
SynchronousWorker.swift:88
    while isAlive {
SynchronousWorker.swift:107
    isAlive = false
SynchronousWorker.swift:111
    isAlive = false
: Bool = false 0024 let parentPid
SynchronousWorker.swift:65
    if getppid() != parentPid {
: pid_t 0025 0026 public init(configuration: Configuration, logger: Logger, listeners: [Socket], notify: Void -> Void, application: Application) { 0027 self.parentPid = getpid() 0028 self.logger = logger 0029 self.listeners = listeners 0030 self.configuration = configuration 0031 self.notify = notify 0032 self.application = application 0033 } 0034 0035 func registerSignals
SynchronousWorker.swift:48
      try registerSignals()
() throws { 0036 let signals = try SignalHandler() 0037 signals.register(.Interrupt, handleQuit) 0038 signals.register(.Quit, handleQuit) 0039 signals.register(.Terminate, handleTerminate) 0040 sharedHandler = signals 0041 SignalHandler.registerSignals() 0042 } 0043 0044 public func run() { 0045 logger.info("Booting worker process with pid: \(getpid())") 0046 0047 do { 0048 try registerSignals() 0049 } catch { 0050 logger.info("Failed to boot \(error)") 0051 return 0052 } 0053 isAlive = true 0054 0055 listeners.forEach { $0.blocking = false } 0056 0057 if listeners.count == 1 { 0058 runOne(listeners.first!) 0059 } else { 0060 runMultiple(listeners) 0061 } 0062 } 0063 0064 func isParentAlive
SynchronousWorker.swift:79
      if !isParentAlive() {
SynchronousWorker.swift:98
      if !isParentAlive() {
() -> Bool { 0065 if getppid() != parentPid { 0066 logger.info("Parent changed, shutting down") 0067 return false 0068 } 0069 0070 return true 0071 } 0072 0073 func runOne
SynchronousWorker.swift:58
      runOne(listeners.first!)
(listener: Socket) { 0074 while isAlive { 0075 sharedHandler?.process() 0076 notify() 0077 accept(listener) 0078 0079 if !isParentAlive() { 0080 return 0081 } 0082 0083 wait() 0084 } 0085 } 0086 0087 func runMultiple
SynchronousWorker.swift:60
      runMultiple(listeners)
(listeners: [Socket]) { 0088 while isAlive { 0089 sharedHandler?.process() 0090 notify() 0091 0092 let sockets = wait().filter { 0093 $0.descriptor != sharedHandler!.pipe[0].descriptor 0094 } 0095 0096 sockets.forEach(accept) 0097 0098 if !isParentAlive() { 0099 return 0100 } 0101 } 0102 } 0103 0104 // MARK: Signal Handling 0105 0106 func handleQuit
SynchronousWorker.swift:37
    signals.register(.Interrupt, handleQuit)
SynchronousWorker.swift:38
    signals.register(.Quit, handleQuit)
() { 0107 isAlive = false 0108 } 0109 0110 func handleTerminate
SynchronousWorker.swift:39
    signals.register(.Terminate, handleTerminate)
() { 0111 isAlive = false 0112 } 0113 0114 func wait
SynchronousWorker.swift:83
      wait()
SynchronousWorker.swift:92
      let sockets = wait().filter {
() -> [Socket] { 0115 let timeout: timeval 0116 0117 if self.timeout > 0 { 0118 timeout = timeval(tv_sec: self.timeout, tv_usec: 0) 0119 } else { 0120 timeout = timeval(tv_sec: 120, tv_usec: 0) 0121 } 0122 0123 let (read, _, _) = select(listeners + [sharedHandler!.pipe[0]], [], [], timeout: timeout) 0124 return read 0125 } 0126 0127 func accept
SynchronousWorker.swift:77
      accept(listener)
SynchronousWorker.swift:96
      sockets.forEach(accept)
(listener: Socket) { 0128 if let client = try? listener.accept() { 0129 client.blocking = true 0130 handle(client) 0131 } 0132 } 0133 0134 func handle
SynchronousWorker.swift:130
      handle(client)
(client: Socket) { 0135 let parser = HTTPParser(socket: client) 0136 0137 let response: ResponseType 0138 0139 do { 0140 let request = try parser.parse() 0141 response = application(request) 0142 print("[worker] \(request.method) \(request.path) - \(response.statusLine)") 0143 } catch let error as HTTPParserError { 0144 response = error.response() 0145 } catch { 0146 print("[worker] Unknown error: \(error)") 0147 response = Response(.InternalServerError, contentType: "text/plain", body: "Internal Server Error") 0148 } 0149 0150 sendResponse(client, response: response) 0151 0152 client.shutdown() 0153 client.close() 0154 } 0155 } 0156 0157 0158 func sendResponse
SynchronousWorker.swift:150
    sendResponse(client, response: response)
(client: Socket, response: ResponseType) { 0159 client.send("HTTP/1.1 \(response.statusLine)\r\n") 0160 0161 client.send("Connection: close\r\n") 0162 var hasLength = false 0163 0164 for (key, value) in response.headers { 0165 if key != "Connection" { 0166 client.send("\(key): \(value)\r\n") 0167 } 0168 0169 if key == "Content-Length" { 0170 hasLength = true 0171 } 0172 } 0173 0174 if !hasLength { 0175 if let body = response.body { 0176 client.send("Content-Length: \(body.utf8.count)\r\n") 0177 } else { 0178 client.send("Content-Length: 0\r\n") 0179 } 0180 } 0181 0182 client.send("\r\n") 0183 0184 if let body = response.body { 0185 client.send(body) 0186 } 0187 } 0188