mirror of
https://github.com/RevenueCat/purchases-ios
synced 2026-11-10 22:45:55 +05:45
78 lines
2.5 KiB
Swift
78 lines
2.5 KiB
Swift
//
|
|
// URLMessage+Description.swift
|
|
// RevenueCat
|
|
//
|
|
// Created by Dave DeLong on 8/7/26.
|
|
//
|
|
|
|
#if DEBUG
|
|
|
|
import Foundation
|
|
|
|
/*
|
|
These are convenience methods to stringify an HTTP message, and are exceptionally useful
|
|
when debugging networking calls. Do not rely on them for anything more than purely informational purposes.
|
|
|
|
These methods are only present in DEBUG builds.
|
|
*/
|
|
|
|
extension URLRequest {
|
|
|
|
private var sortedHeaders: [(String, String)] {
|
|
guard let headers = allHTTPHeaderFields else { return [] }
|
|
return headers.sorted(by: { $0.key < $1.key })
|
|
}
|
|
|
|
internal func httpDescription(maxBodySize: Int = 2048) -> String {
|
|
var lines = [String]()
|
|
lines.append("\(httpMethod ?? "GET") \(url?.absoluteString ?? "<none>")")
|
|
for (key, value) in sortedHeaders {
|
|
lines.append("\(key): \(value)")
|
|
}
|
|
if let body = self.httpBody, let str = String(bytes: body.prefix(maxBodySize), encoding: .utf8) {
|
|
lines.append("")
|
|
lines.append(str)
|
|
}
|
|
|
|
return lines.joined(separator: "\n")
|
|
}
|
|
|
|
}
|
|
|
|
extension URLResponse {
|
|
|
|
private var sortedHeaders: [(AnyHashable, Any)] {
|
|
guard let http = self as? HTTPURLResponse else { return [] }
|
|
let headers = http.allHeaderFields
|
|
return headers.sorted(by: { $0.key.description < $1.key.description })
|
|
}
|
|
|
|
internal func httpDescription(with body: Data?, maxBodySize: Int = 2048) -> String {
|
|
var lines = [String]()
|
|
|
|
if let http = self as? HTTPURLResponse {
|
|
lines.append("\(http.statusCode) \(HTTPURLResponse.localizedString(forStatusCode: http.statusCode))")
|
|
for (key, value) in sortedHeaders {
|
|
lines.append("\(key): \(value)")
|
|
}
|
|
} else {
|
|
lines.append("\(type(of: self))")
|
|
lines.append("URL: \(url?.absoluteString ?? "<none>")")
|
|
if let mimeType { lines.append("Mime-Type: \(mimeType)") }
|
|
if expectedContentLength > 0 { lines.append("Expected-Content-Length: \(expectedContentLength)") }
|
|
if let textEncodingName { lines.append("Text-Encoding-Name: \(textEncodingName)") }
|
|
if let suggestedFilename { lines.append("Suggested-File-Name: \(suggestedFilename)") }
|
|
}
|
|
|
|
if let body, body.count > 0, let str = String(bytes: body.prefix(maxBodySize), encoding: .utf8) {
|
|
lines.append("")
|
|
lines.append(str)
|
|
}
|
|
|
|
return lines.joined(separator: "\n")
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|