Browse Source

Fix how the server handles unknown methods (broken by me in ac7979d5, I had forgotten that sending initial metadata is required).

Daniel Alm 7 years ago
parent
commit
c1b54e679a

+ 1 - 1
Sources/SwiftGRPC/Core/Handler.swift

@@ -137,7 +137,7 @@ public class Handler {
                                       .receiveCloseOnServer,
                                       .sendStatusFromServer(statusCode, statusMessage, trailingMetadata),
                                       .sendMessage(messageBuffer)
-    ]) { operationGroup in
+    ]) { _ in
       self.shutdown()
     }
     try call.perform(operations)

+ 1 - 0
Sources/SwiftGRPC/Core/OperationGroup.swift

@@ -29,6 +29,7 @@ class OperationGroup {
   let tag: Int64
 
   /// The call associated with the operation group. Retained while the operations are running.
+  // FIXME(danielalm): Is this property needed?
   private let call: Call
 
   /// An array of operation objects that are passed into the initializer.

+ 15 - 6
Sources/SwiftGRPC/Runtime/ServiceServer.swift

@@ -65,13 +65,22 @@ open class ServiceServer {
         + " calling " + unwrappedMethod
         + " from " + unwrappedCaller
         + " with " + handler.requestMetadata.description)
-
+      
       do {
-        if try !strongSelf.handleMethod(unwrappedMethod, handler: handler, queue: queue) {
-          // handle unknown requests
-          try handler.sendStatus(statusCode: .unimplemented,
-                                 statusMessage: "unknown method " + unwrappedMethod,
-                                 trailingMetadata: Metadata())
+        if !(try strongSelf.handleMethod(unwrappedMethod, handler: handler, queue: queue)) {
+          do {
+            try handler.call.perform(OperationGroup(
+              call: handler.call,
+              operations: [
+                .sendInitialMetadata(Metadata()),
+                .receiveCloseOnServer,
+                .sendStatusFromServer(.unimplemented, "unknown method " + unwrappedMethod, Metadata())
+            ]) { _ in
+              handler.shutdown()
+            })
+          } catch {
+            print("ServiceServer.start error sending status for unknown method: \(error)")
+          }
         }
       } catch {
         print("Server error: \(error)")