Skip to content

Commit

Permalink
Merge pull request #37 from klemenkosir/master
Browse files Browse the repository at this point in the history
Fix empty request.httpBody causing PulseUI to not show it on the summary view
  • Loading branch information
kean authored Sep 12, 2021
2 parents bbb72c2 + 68722dc commit 00d17cc
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion Pulse/Sources/PulseCore/LoggerStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ extension LoggerStore {
request: NetworkLoggerRequest(urlRequest: urlRequest),
response: context.response.map(NetworkLoggerResponse.init),
error: context.error.map(NetworkLoggerError.init),
requestBody: urlRequest.httpBody,
requestBody: urlRequest.httpBody ?? urlRequest.httpBodyStreamData(),
responseBody: context.data,
metrics: context.metrics
)
Expand Down Expand Up @@ -551,3 +551,32 @@ public enum LoggerStoreError: Error, LocalizedError {
}
}
}


fileprivate extension URLRequest {

func httpBodyStreamData() -> Data? {
guard let bodyStream = self.httpBodyStream else {
return nil
}

bodyStream.open()
defer {
buffer.deallocate()
bodyStream.close()
}

// Will read 16 chars per iteration. Can use bigger buffer if needed
let bufferSize: Int = 16
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: bufferSize)

var bodyStreamData = Data()

while bodyStream.hasBytesAvailable {
let readData = bodyStream.read(buffer, maxLength: bufferSize)
bodyStreamData.append(buffer, count: readData)
}

return bodyStreamData
}
}

0 comments on commit 00d17cc

Please sign in to comment.