Skip to content

Instantly share code, notes, and snippets.

@AlvaroOlave
Last active March 22, 2024 11:35
Show Gist options
  • Save AlvaroOlave/2ddf2b1e99d4477a51146530b7801aa0 to your computer and use it in GitHub Desktop.
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
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"));}/'
@AlvaroOlave
Copy link
Author

AlvaroOlave commented Jun 28, 2023

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 console

The swift code behind this command regex is an adaptation of this @shaps80 code: https://gist.github.com/shaps80/ba6a1e2d477af0383e8f19b87f53661d

@shaps80
Copy link

shaps80 commented Jun 28, 2023 via email

@chrisvanbuskirk
Copy link

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.

@shaps80
Copy link

shaps80 commented Aug 30, 2023

@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