Last active
March 22, 2024 11:35
-
-
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 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"));}/' |
Oh this is a cool idea! Then you have it in any project ok that machine. Nice one!!
Thank you for this! Just FYI, this approach will affect the performance of the the app while processing. I tend to print out curl statements as is processing them.
@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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you haven't created the .lldbinit file yet, you can create it using:
touch ~/.lldbinit
It creates the .lldbinit file in your home directory.
If you already had created the .lldbinit file, just add the command line to your file.
How to use it, once created and added the .lldbinit file, put a breakpoint where you have some URLRequest instance and just type
curl yourURLRequestInstance
in the consoleThe swift code behind this command regex is an adaptation of this @shaps80 code: https://gist.github.com/shaps80/ba6a1e2d477af0383e8f19b87f53661d