Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix empty request.httpBody causing PulseUI to not show it on the summary view #37

Merged
merged 2 commits into from
Sep 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}