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

cURL support #162

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CocoaDebug.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@
38CD79B029B22ADA00B973BD /* CocoaDebugDeviceInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 38CD79A529B22ADA00B973BD /* CocoaDebugDeviceInfo.m */; };
38F69F8A26A6FAFB0010953F /* GPBMessage+CocoaDebug.h in Headers */ = {isa = PBXBuildFile; fileRef = 38F69F8826A6FAFB0010953F /* GPBMessage+CocoaDebug.h */; settings = {ATTRIBUTES = (Public, ); }; };
38F69F8B26A6FAFB0010953F /* GPBMessage+CocoaDebug.m in Sources */ = {isa = PBXBuildFile; fileRef = 38F69F8926A6FAFB0010953F /* GPBMessage+CocoaDebug.m */; };
F93A16022A35F4F400F76A99 /* _HTTPModel+cURL.swift in Sources */ = {isa = PBXBuildFile; fileRef = F93A16012A35F4F400F76A99 /* _HTTPModel+cURL.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -574,6 +575,7 @@
38CD79A529B22ADA00B973BD /* CocoaDebugDeviceInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CocoaDebugDeviceInfo.m; sourceTree = "<group>"; };
38F69F8826A6FAFB0010953F /* GPBMessage+CocoaDebug.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "GPBMessage+CocoaDebug.h"; sourceTree = "<group>"; };
38F69F8926A6FAFB0010953F /* GPBMessage+CocoaDebug.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "GPBMessage+CocoaDebug.m"; sourceTree = "<group>"; };
F93A16012A35F4F400F76A99 /* _HTTPModel+cURL.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "_HTTPModel+cURL.swift"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -884,6 +886,7 @@
3826DAC024EE5BF5003DE040 /* _HttpDatasource.h */,
3826DAC124EE5BF5003DE040 /* NetworkViewController.swift */,
3826DAC224EE5BF5003DE040 /* NetworkDetailViewController.swift */,
F93A16012A35F4F400F76A99 /* _HTTPModel+cURL.swift */,
3826DAC324EE5BF5003DE040 /* _HttpModel.h */,
3826DAC424EE5BF5003DE040 /* NetworkDetailCell.swift */,
3826DAC524EE5BF5003DE040 /* _NetworkHelper.m */,
Expand Down Expand Up @@ -1358,6 +1361,7 @@
3826DC3824EE5BF5003DE040 /* _CustomHTTPProtocol.m in Sources */,
3826DC5F24EE5BF5003DE040 /* _WKWebView+Swizzling.m in Sources */,
3826DD3324EE5FD5003DE040 /* _DirectoryContentsTableViewController.m in Sources */,
F93A16022A35F4F400F76A99 /* _HTTPModel+cURL.swift in Sources */,
3826DC4524EE5BF5003DE040 /* _OCLogHelper.m in Sources */,
3826DC3124EE5BF5003DE040 /* NetworkDetailCell.swift in Sources */,
3826DC4624EE5BF5003DE040 /* _OCLogModel.m in Sources */,
Expand Down
4 changes: 2 additions & 2 deletions Example_Swift/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- CocoaDebug (1.7.6)
- CocoaDebug (1.7.7)

DEPENDENCIES:
- CocoaDebug (from `../../CocoaDebug`)
Expand All @@ -9,7 +9,7 @@ EXTERNAL SOURCES:
:path: "../../CocoaDebug"

SPEC CHECKSUMS:
CocoaDebug: cfa9c2e77fe93837289b77d9d1c7b4aac17b5843
CocoaDebug: b38d31464b91a9775928f8667d114db07b136565

PODFILE CHECKSUM: ad8f819c46597b538cb9338d8dbcbc1be59455c7

Expand Down
7 changes: 7 additions & 0 deletions Sources/Network/NetworkDetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,12 @@ class NetworkDetailViewController: UITableViewController, MFMailComposeViewContr
UIPasteboard.general.string = self?.messageBody
}

let curlAction = UIAlertAction(title: "copy cURL to clipboard", style: .default) { _ in
if let httpModel = self.httpModel {
UIPasteboard.general.string = httpModel.cURLDescription()
}
}

let moreAction: UIAlertAction = UIAlertAction(title: "more", style: .default) { [weak self] action -> Void in
_ = self?.configureMailComposer(true)
let items: [Any] = [self?.messageBody ?? ""]
Expand All @@ -392,6 +398,7 @@ class NetworkDetailViewController: UITableViewController, MFMailComposeViewContr

// add actions
alert.addAction(secondAction)
alert.addAction(curlAction)
alert.addAction(firstAction)
alert.addAction(moreAction)
alert.addAction(cancelAction)
Expand Down
33 changes: 33 additions & 0 deletions Sources/Network/_HTTPModel+cURL.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// _HTTPModel+cURL.swift
// CocoaDebug
//
// Created by Nehcgnos on 2023/6/11.
// Copyright © 2023 man. All rights reserved.
//

import Foundation

extension _HttpModel {
func cURLDescription() -> String {
var components = ["curl \"\(url?.absoluteString ?? "<unknown url>")\""]

if let method, method != "GET" {
components.append("-X \(method)")
}

for (field, value) in requestHeaderFields {
let valueString = "\(value)".replacingOccurrences(of: "\"", with: "\\\"")
components.append("-H \"\(field): \(valueString)\"")
}

if let requestBodyString = String(data: requestData, encoding: .utf8), requestBodyString.isEmpty == false {
var escapedBody = requestBodyString.replacingOccurrences(of: "\\\"", with: "\\\\\"")
escapedBody = escapedBody.replacingOccurrences(of: "\"", with: "\\\"")

components.append("-d \"\(escapedBody)\"")
}

return components.joined(separator: " \\\n")
}
}