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