forked from PerfectlySoft/Perfect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFastCGI.swift
271 lines (215 loc) · 6.65 KB
/
FastCGI.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//
// FastCGI.swift
// PerfectLib
//
// Created by Kyle Jessup on 7/6/15.
// Copyright (C) 2015 PerfectlySoft, Inc.
//
//===----------------------------------------------------------------------===//
//
// This source file is part of the Perfect.org open source project
//
// Copyright (c) 2015 - 2016 PerfectlySoft Inc. and the Perfect project authors
// Licensed under Apache License v2.0
//
// See http://perfect.org/licensing.html for license information
//
//===----------------------------------------------------------------------===//
//
// values which are part of the FastCGI protocol but are unused in this implementation are commented out
#if os(Linux)
import SwiftGlibc
#endif
let fcgiVersion1: UInt8 = 1
let fcgiBeginRequest: UInt8 = 1
//let FCGI_ABORT_REQUEST: UInt8 = 2
let fcgiEndRequest: UInt8 = 3
let fcgiParams: UInt8 = 4
let fcgiStdin: UInt8 = 5
let fcgiStdout: UInt8 = 6
//let FCGI_STDERR: UInt8 = 7
let fcgiData: UInt8 = 8
//let FCGI_GET_VALUES: UInt8 = 9
//let FCGI_GET_VALUES_RESULT: UInt8 = 10
//let FCGI_UNKNOWN_TYPE: UInt8 = 11
let fcgiXStdin: UInt8 = 50
//let FCGI_KEEP_CONN = 1
//let FCGI_RESPONDER = 1
//let FCGI_AUTHORIZE = 2
//let FCGI_FILTER = 3
let fcgiRequestComplete = 0
//let FCGI_CANT_MPX_CONN = 1
//let FCGI_OVERLOADED = 2
//let FCGI_UNKNOWN_ROLE = 3
//let FCGI_MAX_CONNS = "FCGI_MAX_CONNS"
//let FCGI_MAX_REQS = "FCGI_MAX_REQS"
//let FCGI_MPXS_CONNS = "FCGI_MPXS_CONNS"
let fcgiTimeoutSeconds = 5.0
let fcgiBaseRecordSize = 8
let fcgiBodyChunkSize = 0xFFFF
class FastCGIRecord {
var version: UInt8 = 0
var recType: UInt8 = 0
var requestId: UInt16 = 0
var contentLength: UInt16 = 0
var paddingLength: UInt8 = 0
var reserved: UInt8 = 0
var content: [UInt8]? = nil
var padding: [UInt8]? = nil
}
class FastCGIRequest : WebConnection {
var connection: NetTCP
var requestId: UInt16 = 0
var requestParams: Dictionary<String, String> = Dictionary<String, String>()
var stdin: [UInt8]? = nil
var mimes: MimeReader? = nil
var statusCode: Int
var statusMsg: String
var header: String = ""
var wroteHeader: Bool = false
var lastRecordType: UInt8 = 0
init(net: NetTCP) {
self.connection = net
self.statusCode = 200
self.statusMsg = "OK"
}
func setStatus(code: Int, msg: String) {
self.statusCode = code
self.statusMsg = msg
}
func getStatus() -> (Int, String) {
return (self.statusCode, self.statusMsg)
}
func putStdinData(b: [UInt8]) {
if self.stdin == nil && self.mimes == nil {
let contentType = requestParams["CONTENT_TYPE"]
if contentType == nil || !contentType!.hasPrefix("multipart/form-data") {
self.stdin = b
} else {
self.mimes = MimeReader(contentType!)//, Int(requestParams["CONTENT_LENGTH"] ?? "0")!)
self.mimes!.addToBuffer(b)
}
} else if self.stdin != nil {
self.stdin!.appendContentsOf(b)
} else {
self.mimes!.addToBuffer(b)
}
}
func writeHeaderLine(h: String) {
self.header += h + "\r\n"
}
func writeHeaderBytes(b: [UInt8]) {
if !wroteHeader {
wroteHeader = true
let statusLine = "Status: \(statusCode) \(statusMsg)\r\n"
let firstBytes = makeStdoutBody(Int(requestId), data: [UInt8](statusLine.utf8) + b)
writeBytes(firstBytes)
} else if b.count > 0 {
let furtherBytes = makeStdoutBody(Int(requestId), data: b)
writeBytes(furtherBytes)
}
}
func writeBodyBytes(b: [UInt8]) {
if !wroteHeader {
header += "\r\n" // final CRLF
writeHeaderBytes([UInt8](header.utf8))
header = ""
}
let b = makeStdoutBody(Int(requestId), data: b)
writeBytes(b)
}
func writeBytes(b: [UInt8]) {
self.connection.writeBytesFully(b)
}
func makeEndRequestBody(requestId: Int, appStatus: Int, protocolStatus: Int) -> [UInt8] {
let b = Bytes()
b.import8Bits(fcgiVersion1)
.import8Bits(fcgiEndRequest)
.import16Bits(htons(UInt16(requestId)))
.import16Bits(htons(UInt16(8)))
.import8Bits(0)
.import8Bits(0)
.import32Bits(htonl(UInt32(appStatus)))
.import8Bits(UInt8(protocolStatus))
.import8Bits(0)
.import8Bits(0)
.import8Bits(0)
return b.data
}
func makeStdoutBody(requestId: Int, data: [UInt8], firstPos: Int, count: Int) -> [UInt8] {
let b = Bytes()
if count > fcgiBodyChunkSize {
b.importBytes(makeStdoutBody(requestId, data: data, firstPos: firstPos, count: fcgiBodyChunkSize))
b.importBytes(makeStdoutBody(requestId, data: data, firstPos: fcgiBodyChunkSize + firstPos, count: count - fcgiBodyChunkSize))
} else {
let padBytes = count % 8
b.import8Bits(fcgiVersion1)
.import8Bits(fcgiStdout)
.import16Bits(htons(UInt16(requestId)))
.import16Bits(htons(UInt16(count)))
.import8Bits(UInt8(padBytes))
.import8Bits(0)
if firstPos == 0 && count == data.count {
b.importBytes(data)
} else {
b.importBytes(data[firstPos..<count])
}
if padBytes > 0 {
for _ in 1...padBytes {
b.import8Bits(0)
}
}
}
return b.data
}
func makeStdoutBody(requestId: Int, data: [UInt8]) -> [UInt8] {
return makeStdoutBody(requestId, data: data, firstPos: 0, count: data.count)
}
func readRecord(continuation: (FastCGIRecord?) -> ()) {
self.connection.readBytesFully(fcgiBaseRecordSize, timeoutSeconds: fcgiTimeoutSeconds) {
[weak self] (b: [UInt8]?) -> () in
guard let recBytes = b else {
continuation(nil)
return
}
let record = FastCGIRecord()
record.version = recBytes[0]
record.recType = recBytes[1]
record.requestId = ntohs((UInt16(recBytes[3]) << 8) | UInt16(recBytes[2]))
record.contentLength = ntohs((UInt16(recBytes[5]) << 8) | UInt16(recBytes[4]))
record.paddingLength = recBytes[6];
record.reserved = recBytes[7]
self?.readRecordContent(record, continuation: continuation)
}
}
func readRecordContent(record: FastCGIRecord, continuation: (FastCGIRecord?) -> ()) {
if record.contentLength > 0 {
self.connection.readBytesFully(Int(record.contentLength), timeoutSeconds: fcgiTimeoutSeconds, completion: {
[weak self] (b:[UInt8]?) -> () in
if let contentBytes = b {
record.content = contentBytes
self?.readRecordPadding(record, continuation: continuation)
} else {
continuation(nil)
}
})
} else {
self.readRecordPadding(record, continuation: continuation)
}
}
func readRecordPadding(record: FastCGIRecord, continuation: (FastCGIRecord?) -> ()) {
if record.paddingLength > 0 {
self.connection.readBytesFully(Int(record.paddingLength), timeoutSeconds: fcgiTimeoutSeconds, completion: {
(b:[UInt8]?) -> () in
if let paddingBytes = b {
record.padding = paddingBytes
continuation(record)
} else {
continuation(nil)
}
})
} else {
continuation(record)
}
}
}