Browse Source

fix SwiftNIO json encoding (#5460)

Tanner 5 years ago
parent
commit
60fa22eefd
1 changed files with 12 additions and 5 deletions
  1. 12 5
      frameworks/Swift/swift-nio/app/Sources/main.swift

+ 12 - 5
frameworks/Swift/swift-nio/app/Sources/main.swift

@@ -14,7 +14,6 @@ enum Constants {
     static let plainTextResponseLength = plainTextResponse.utf8CodeUnitCount
     static let plainTextResponseLength = plainTextResponse.utf8CodeUnitCount
     static let plainTextResponseLengthString = String(plainTextResponseLength)
     static let plainTextResponseLengthString = String(plainTextResponseLength)
 
 
-    static let jsonResponse = try! JSONEncoder().encode(JSONTestResponse())
     static let jsonResponseLength = try! JSONEncoder().encode(JSONTestResponse()).count
     static let jsonResponseLength = try! JSONEncoder().encode(JSONTestResponse()).count
     static let jsonResponseLengthString = String(jsonResponseLength)
     static let jsonResponseLengthString = String(jsonResponseLength)
 }
 }
@@ -23,7 +22,9 @@ private final class HTTPHandler: ChannelInboundHandler {
     public typealias InboundIn = HTTPServerRequestPart
     public typealias InboundIn = HTTPServerRequestPart
     public typealias OutboundOut = HTTPServerResponsePart
     public typealias OutboundOut = HTTPServerResponsePart
 
 
+    let jsonEncoder: JSONEncoder
     let dateCache: RFC1123DateCache
     let dateCache: RFC1123DateCache
+
     var plaintextBuffer: ByteBuffer
     var plaintextBuffer: ByteBuffer
     var jsonBuffer: ByteBuffer
     var jsonBuffer: ByteBuffer
 
 
@@ -32,7 +33,7 @@ private final class HTTPHandler: ChannelInboundHandler {
         self.plaintextBuffer = allocator.buffer(capacity: Constants.plainTextResponseLength)
         self.plaintextBuffer = allocator.buffer(capacity: Constants.plainTextResponseLength)
         self.plaintextBuffer.writeStaticString(Constants.plainTextResponse)
         self.plaintextBuffer.writeStaticString(Constants.plainTextResponse)
         self.jsonBuffer = allocator.buffer(capacity: Constants.jsonResponseLength)
         self.jsonBuffer = allocator.buffer(capacity: Constants.jsonResponseLength)
-        self.jsonBuffer.writeBytes(Constants.jsonResponse)
+        self.jsonEncoder = .init()
         self.dateCache = .on(channel.eventLoop)
         self.dateCache = .on(channel.eventLoop)
     }
     }
 
 
@@ -43,9 +44,13 @@ private final class HTTPHandler: ChannelInboundHandler {
             case "/p":
             case "/p":
                 self.processPlaintext(context: context)
                 self.processPlaintext(context: context)
             case "/j":
             case "/j":
-                self.processJSON(context: context)
+                do {
+                    try self.processJSON(context: context)
+                } catch {
+                    context.close(promise: nil)
+                }
             default:
             default:
-                _ = context.close()
+                context.close(promise: nil)
             }
             }
         case .body:
         case .body:
             break
             break
@@ -65,9 +70,11 @@ private final class HTTPHandler: ChannelInboundHandler {
         context.write(self.wrapOutboundOut(.body(.byteBuffer(self.plaintextBuffer))), promise: nil)
         context.write(self.wrapOutboundOut(.body(.byteBuffer(self.plaintextBuffer))), promise: nil)
     }
     }
 
 
-    private func processJSON(context: ChannelHandlerContext) {
+    private func processJSON(context: ChannelHandlerContext) throws {
         let responseHead = self.responseHead(contentType: "application/json", contentLength: Constants.jsonResponseLengthString)
         let responseHead = self.responseHead(contentType: "application/json", contentLength: Constants.jsonResponseLengthString)
         context.write(self.wrapOutboundOut(.head(responseHead)), promise: nil)
         context.write(self.wrapOutboundOut(.head(responseHead)), promise: nil)
+        self.jsonBuffer.clear()
+        try self.jsonBuffer.writeBytes(self.jsonEncoder.encode(JSONTestResponse()))
         context.write(self.wrapOutboundOut(.body(.byteBuffer(self.jsonBuffer))), promise: nil)
         context.write(self.wrapOutboundOut(.body(.byteBuffer(self.jsonBuffer))), promise: nil)
     }
     }