Get streaming approach to work after all

This commit is contained in:
Matthias Neeracher 2018-10-07 02:09:31 +02:00
parent 3a6a02deb5
commit 399ec66bda
1 changed files with 22 additions and 18 deletions

View File

@ -3,15 +3,6 @@ import Vapor
import Foundation import Foundation
import ZIPFoundation import ZIPFoundation
private extension Data {
mutating func appendLittleEndian(_ word: UInt32) {
self.append(UInt8(word & 0xFF))
self.append(UInt8((word >> 8) & 0xFF))
self.append(UInt8((word >> 16) & 0xFF))
self.append(UInt8((word >> 24) & 0xFF))
}
}
/// Server Gzip middlere: /// Server Gzip middlere:
/// 1. checks if the "Accept-Encoding" header contains "gzip" /// 1. checks if the "Accept-Encoding" header contains "gzip"
/// 2. if so, compresses the body and sets the response header "Content-Encoding" to "gzip", /// 2. if so, compresses the body and sets the response header "Content-Encoding" to "gzip",
@ -39,22 +30,35 @@ public struct GzipServerMiddleware: Middleware, ServiceType {
var headers = response.http.headers var headers = response.http.headers
headers.replaceOrAdd(name: .contentEncoding, value: "gzip") headers.replaceOrAdd(name: .contentEncoding, value: "gzip")
return response.http.body.consumeData(on: request).map { data in return response.http.body.consumeData(on: request).map { data in
var buffer = Data() let stream = HTTPChunkedStream(on: request)
let bufSize = 4096
var buffer = ByteBufferAllocator().buffer(capacity: 16)
let header : [UInt8] = [0x1f, 0x8b, 0x08, 0x00] let header : [UInt8] = [0x1f, 0x8b, 0x08, 0x00]
let header2 : [UInt8] = [0x00, 0x03] let header2 : [UInt8] = [0x00, 0x03]
buffer.append(contentsOf: header) buffer.write(bytes: header)
buffer.appendLittleEndian(UInt32(Date().timeIntervalSince1970)) buffer.write(integer: UInt32(Date().timeIntervalSince1970), endianness: .little)
buffer.append(contentsOf: header2) buffer.write(bytes: header2)
let crc32 = try Data.compress(size: data.count, bufferSize: 4096, var write = stream.write(.chunk(buffer))
let crc32 = try Data.compress(size: data.count, bufferSize: bufSize,
provider: {(offset, readSize) -> Data in provider: {(offset, readSize) -> Data in
return data.subdata(in: offset..<offset+readSize) return data.subdata(in: offset..<offset+readSize)
}, },
consumer: { data -> Void in consumer: { data -> Void in
buffer.append(data) var buffer = ByteBufferAllocator().buffer(capacity: bufSize)
buffer.write(bytes: data)
write = write.flatMap {
return stream.write(.chunk(buffer))
}
}) })
buffer.appendLittleEndian(crc32) write.always {
buffer.appendLittleEndian(UInt32(data.count)) buffer.clear()
let httpResponse = HTTPResponse(status: response.http.status, headers: headers, body: buffer) buffer.write(integer: crc32, endianness: .little)
buffer.write(integer: UInt32(data.count), endianness: .little)
stream.write(.chunk(buffer)).always {
_ = stream.write(.end)
}
}
let httpResponse = HTTPResponse(status: response.http.status, headers: headers, body: stream)
return request.response(http: httpResponse) return request.response(http: httpResponse)
} }
} }