Skip to content

Commit

Permalink
Merge pull request #58 from ProxymanApp/bug/crash-big-files
Browse files Browse the repository at this point in the history
Add Maximum Package size
  • Loading branch information
NghiaTranUIT authored Feb 18, 2021
2 parents baffe8a + b4d224d commit 5016bd6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Sources/Packages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ struct ConnectionPackage: Codable, Serializable {
}
}



public final class TrafficPackage: Codable, CustomDebugStringConvertible, Serializable {

// Should not change the variable names
Expand All @@ -56,6 +58,24 @@ public final class TrafficPackage: Codable, CustomDebugStringConvertible, Serial
public private(set) var endAt: TimeInterval?
public private(set) var lastData: Data?

// MARK: - Variables

private var isLargeReponseBody: Bool {
if responseBodyData.count > NetServiceTransport.MaximumSizePackage {
return true
}
return false
}

private var isLargeRequestBody: Bool {
if let requestBody = request.body, requestBody.count > NetServiceTransport.MaximumSizePackage {
return true
}
return false
}

// MARK: - Init

init(id: String, request: Request, response: Response? = nil, responseBodyData: Data? = nil) {
self.id = id
self.request = request
Expand Down Expand Up @@ -133,6 +153,22 @@ public final class TrafficPackage: Codable, CustomDebugStringConvertible, Serial
}

func toData() -> Data? {
// Set nil to prevent being encode to JSON
// It might increase the size of the message
lastData = nil

// For some reason, JSONEncoder could not allocate enough RAM to encode a large body
// It crashes the app if the body might be > 100Mb
// We decice to skip the body, but send the request/response
// https://github.com/ProxymanApp/atlantis/issues/57
if isLargeReponseBody {
self.responseBodyData = "<Skip Large Body>".data(using: String.Encoding.utf8)!
}
if isLargeRequestBody {
self.request.resetBody()
}

// Encode to JSON
do {
return try JSONEncoder().encode(self)
} catch let error {
Expand Down Expand Up @@ -222,6 +258,10 @@ public final class Request: Codable {
}
self.body?.append(data)
}

func resetBody() {
self.body = nil
}
}

public struct Response: Codable {
Expand Down
3 changes: 3 additions & 0 deletions Sources/Transporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ final class NetServiceTransport: NSObject {

// MARK: - Variabls

// For some reason, Stream Task could send a big file
// https://github.com/ProxymanApp/atlantis/issues/57
static let MaximumSizePackage = 52428800 // 50Mb
private let serviceBrowser: NetServiceBrowser
private var services: [NetService] = []
private let queue = DispatchQueue(label: "com.proxyman.atlantis.netservices") // Serial on purpose
Expand Down

0 comments on commit 5016bd6

Please sign in to comment.