Browse Source

Store the root certificates as a multi-line string literal. (#372)

This makes reviews easier and avoids expensive memory allocations and Base64 conversions at runtime.

The signature of `roots_pem` changes from `String`-returning function to `String` constant; however, that method has only been made public a few days ago, so no users should be affected.
Daniel Alm 7 years ago
parent
commit
ac3e1759e5

+ 3 - 7
Sources/RootsEncoder/main.swift

@@ -98,13 +98,9 @@ if CommandLine.arguments.contains("test") {
   s += "import Foundation\n"
   s += "\n"
 
-  s += "public func roots_pem() -> String {\n"
+  s += "public let kRootCertificates: String = \"\"\"\n"
   let fileURL = URL(fileURLWithPath: "Assets/roots.pem")
-  let filedata = try Data(contentsOf: fileURL)
-  let encoding = filedata.base64EncodedString()
-  s += "  let roots = \n"
-  s += "    \"" + encoding + "\"\n"
-  s += "  return String(data: Data(base64Encoded: roots, options: [])!, encoding: .utf8)!\n"
-  s += "}\n"
+  s += try String(contentsOf: fileURL)
+  s += "\"\"\""
 }
 print(s)

+ 2 - 2
Sources/SwiftGRPC/Core/Channel.swift

@@ -49,7 +49,7 @@ public class Channel {
     underlyingChannel = withExtendedLifetime(argumentWrappers) {
         var argumentValues = argumentWrappers.map { $0.wrapped }
         if secure {
-          return cgrpc_channel_create_secure(address, roots_pem(), nil, nil, &argumentValues, Int32(arguments.count))
+          return cgrpc_channel_create_secure(address, kRootCertificates, nil, nil, &argumentValues, Int32(arguments.count))
         } else {
           return cgrpc_channel_create(address, &argumentValues, Int32(arguments.count))
         }
@@ -83,7 +83,7 @@ public class Channel {
   /// - Parameter clientCertificates: a PEM representation of the client certificates to use
   /// - Parameter clientKey: a PEM representation of the client key to use
   /// - Parameter arguments: list of channel configuration options
-  public init(address: String, certificates: String = roots_pem(), clientCertificates: String? = nil, clientKey: String? = nil, arguments: [Argument] = []) {
+  public init(address: String, certificates: String = kRootCertificates, clientCertificates: String? = nil, clientKey: String? = nil, arguments: [Argument] = []) {
     gRPC.initialize()
     host = address
     let argumentWrappers = arguments.map { $0.toCArg() }

File diff suppressed because it is too large
+ 0 - 2
Sources/SwiftGRPC/Core/Roots.swift


Some files were not shown because too many files changed in this diff