Last active
July 24, 2024 14:25
-
-
Save AlvaroOlave/2ddf2b1e99d4477a51146530b7801aa0 to your computer and use it in GitHub Desktop.
Custom LLDB command that generates a cURL command from any URLRequest in Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
command regex curl 's/(.+)/expr -l Swift -O -- let anyRequest = %1; if let unwrappedRequest = (anyRequest as? URLRequest), let url = unwrappedRequest.url { var baseCommand = #"curl "\#(url.absoluteString)""#; let quote = String(UnicodeScalar(UInt8(39))); if unwrappedRequest.httpMethod == "HEAD" { baseCommand += " --head"; }; var command = [baseCommand]; if let method = unwrappedRequest.httpMethod, (method != "GET" && method != "HEAD") { command.append("-X \(method)"); }; if let headers = unwrappedRequest.allHTTPHeaderFields { for (key, value) in headers where key != "Cookie" { command.append("-H \(quote)\(key): \(value)\(quote)"); }; }; if let data = unwrappedRequest.httpBody, let body = String(data: data, encoding: .utf8) { command.append("-d \(quote)\(body)\(quote)"); }; print(command.joined(separator: " \\\n\t"));}/' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@chrisvanbuskirk please elaborate. Since this is a LLVM console command, I don't see how this version would affect performance at all? Even my original implementation wouldn't affect performance more than generally printing all over the place. But again, logging is standard practice and in most app usage, negligible.