1
1
package me.leon.toolsfx.plugin.net
2
2
3
- import me.leon.ext.fromJson
4
3
import me.leon.ext.toJson
5
4
import me.leon.toolsfx.plugin.net.HttpUrlUtil.toParams
6
5
@@ -25,95 +24,78 @@ fun String.cookieParse() =
25
24
}
26
25
}
27
26
28
- fun String.parseCurl () =
29
- trim()
30
- // 去掉浏览器多余的分割符
31
- .replace(""" [\^\\]""" .toRegex(), " " )
32
- .split(""" \n|\r\n""" .toRegex())
27
+ val separator = """ \s*[\^\\]\s+""" .toRegex()
28
+ val winEscapeReg = """ \^([{%\d])""" .toRegex()
29
+
30
+ fun String.winEscape () = replace(" ^\\ ^\" " , " \" " ).replace(winEscapeReg, " $1" )
31
+
32
+ fun String.parseCurl (): Request {
33
+ var r = this
34
+ // 兼容旧版
35
+ if (r.contains(" \n " ) && ! r.contains(separator)) {
36
+ r = r.replace(" \n " , " \\\n " )
37
+ }
38
+ return r.split(separator)
33
39
.map { it.trim() }
34
- .fold(Request (this )) { acc, s ->
35
- acc.apply {
36
- when {
37
- s.startsWith(" curl" ) -> acc.url = s.removeFirstAndEndQuotes(5 )
38
- s.startsWith(" -X" ) -> acc.method = s.removeFirstAndEndQuotes(3 )
39
- s.startsWith(" --data-raw" ) ->
40
- acc.method = " POST" .also { acc.rawBody = s.removeFirstAndEndQuotes(11 ) }
41
- s.startsWith(" -d" ) ->
42
- acc.method =
43
- (" POST" .takeIf { acc.method == " GET" } ? : acc.method).also {
44
- val value = s.removeFirstAndEndQuotes(3 )
45
- if (value.contains(" @file" )) {
46
- if (value.startsWith(" {" ) || value.startsWith(" [" )) {
47
- acc.params.putAll(
48
- value.fromJson(MutableMap ::class .java)
49
- as Map <out String , Any >
50
- )
51
- } else {
52
- acc.params.putAll(value.paramsParse().also { println (it) })
53
- }
54
- } else if (
55
- this @parseCurl.contains(" Content-Type: application/json" , true )
56
- ) {
57
- acc.rawBody = value
58
- } else {
59
- acc.params.putAll(value.paramsParse())
60
- }
61
- }
62
- s.startsWith(" --data-binary" ) ->
63
- acc.method =
64
- (" POST" .takeIf { acc.method == " GET" } ? : acc.method).also {
65
- acc.rawBody = s.removeFirstAndEndQuotes(14 )
66
- }
67
- s.startsWith(" --data" ) ->
68
- acc.method =
69
- (" POST" .takeIf { acc.method == " GET" } ? : acc.method).also {
70
- val value = s.removeFirstAndEndQuotes(7 )
71
- if (value.contains(" @file" )) {
72
- acc.params.putAll(
73
- value.fromJson(MutableMap ::class .java)
74
- as Map <out String , Any >
75
- )
76
- } else if (
77
- this @parseCurl.contains(" Content-Type: application/json" , true )
78
- ) {
79
- acc.rawBody = value
80
- } else {
81
- acc.params.putAll(value.paramsParse())
82
- }
83
- }
84
- s.startsWith(" -H" ) ->
85
- with (s.removeFirstAndEndQuotes(3 )) {
86
- acc.headers[substringBefore(" :" )] = substringAfter(" :" ).trim()
40
+ .fold(Request (" " )) { req, s ->
41
+ when {
42
+ s.startsWith(" -X" ) -> req.method = s.removeFirstAndEndQuotes(3 ).trim()
43
+ s.startsWith(" -H" ) ->
44
+ with (s.removeFirstAndEndQuotes(3 )) {
45
+ req.headers[substringBefore(" :" )] = substringAfter(" :" ).trim().winEscape()
46
+ }
47
+
48
+ s.startsWith(" -d" ) -> req.rawBody = s.removeFirstAndEndQuotes(3 ).winEscape()
49
+ s.startsWith(" --data-raw" ) ->
50
+ req.rawBody = s.removeFirstAndEndQuotes(11 ).winEscape().winEscape()
51
+
52
+ s.startsWith(" --data-binary" ) ->
53
+ req.rawBody = s.removeFirstAndEndQuotes(14 ).trim().winEscape()
54
+
55
+ s.startsWith(" --data" ) -> req.rawBody = s.removeFirstAndEndQuotes(7 ).winEscape()
56
+ s.startsWith(" curl" ) ->
57
+ with (s.removeFirstAndEndQuotes(5 )) {
58
+ if (startsWith(" -X" )) {
59
+ val str = substring(3 )
60
+ req.method = str.substringBefore(" " ).trim()
61
+ req.url = str.substringAfter(" " ).removeFirstAndEndQuotes()
62
+ } else {
63
+ req.url = this
87
64
}
88
- else -> {}
89
- }
65
+ }
66
+
67
+ else ->
68
+ if (s.startsWith(" http" )) {
69
+ req.url = this
70
+ }
90
71
}
72
+ req
91
73
}
92
- . also { println (it) }
74
+ }
93
75
94
76
fun Request.toCurl (): String =
95
- StringBuilder ()
96
- .append(" curl $url " )
97
- .also {
98
- if (method == " GET" && params.isNotEmpty()) it.append(" ?" ).append(params.toParams())
99
- }
100
- .appendLine()
101
- .append(" -X $method " )
102
- .appendLine()
103
- .also {
77
+ buildString {
78
+ append(" curl \" $url \" \\ " )
79
+ if (method == " GET" && params.isNotEmpty()) append(" ?" ).append(params.toParams())
80
+ appendLine()
81
+ append(" -X $method \\ " )
82
+ if (headers.isNotEmpty() || params.isNotEmpty() || rawBody.isNotEmpty()) {
83
+ appendLine()
84
+ }
104
85
for ((key, value) in headers) {
105
- it. append(" -H \" $key :$value \" " ).appendLine()
86
+ append(" -H \" $key :$value \" \\ " ).appendLine()
106
87
}
107
88
if (fileParamName.isNotEmpty()) params[fileParamName] = " @file"
108
89
val data =
109
90
if (isJson) params.toJson() else if (method != " GET" ) params.toParams() else " "
110
91
if (rawBody.isNotEmpty()) {
111
- it. append(" --data-raw $rawBody " )
92
+ append(" --data-raw $rawBody " )
112
93
} else if (data.isNotEmpty()) {
113
- it. append(" -d \" ${data.replace(" \" " , " \\\" " )} \" " )
94
+ append(" -d \" ${data.replace(" \" " , " \\\" " )} \" " )
114
95
}
115
96
}
116
- .toString()
97
+ .trimEnd(' \\ ' )
98
+ .trim()
117
99
118
100
fun String.removeFirstAndEndQuotes (from : Int = 0) =
119
101
substring(from).replace(" ^([\" '])(.*?)\\ 1?$" .toRegex(), " $2" ).trim()
0 commit comments