11package me.leon.toolsfx.plugin.net
22
3+ import me.leon.ext.fromJson
34import me.leon.ext.toJson
45import me.leon.toolsfx.plugin.net.HttpUrlUtil.toParams
56
67fun String.paramsParse () =
78 split(" &" ).fold(mutableMapOf<String , Any >()) { acc, param ->
8- println (param)
99 acc.apply {
1010 if (param.isNotEmpty()) {
1111 val (key, value) = param.split(" =" )
@@ -25,57 +25,53 @@ fun String.cookieParse() =
2525 }
2626
2727val separator = """ \s*[\^\\]\s+""" .toRegex()
28- val winEscapeReg = """ \^([{%\d] )""" .toRegex()
28+ val winEscapeReg = """ \^(\W )""" .toRegex()
2929
3030fun String.winEscape () = replace(" ^\\ ^\" " , " \" " ).replace(winEscapeReg, " $1" )
3131
3232fun String.parseCurl (): Request {
33- var r = this
33+ var r = this .winEscape()
3434 // 兼容旧版
3535 if (r.contains(" \n " ) && ! r.contains(separator)) {
3636 r = r.replace(" \n " , " \\\n " )
3737 }
38+
3839 return r.split(separator)
3940 .map { it.trim() }
4041 .fold(Request (" " )) { req, s ->
42+ val startIndex = s.indexOf(" " ) + 1
4143 when {
42- s.startsWith(" -X " ) -> req.method = s.removeFirstAndEndQuotes(3 ).trim()
44+ s.startsWith(" -X " ) -> req.method = s.removeFirstAndEndQuotes(startIndex ).trim()
4345 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()
4648 }
47-
48- s.startsWith(" --data-raw " ) -> {
49+ s.startsWith(" --data-binary " ) || s.startsWith(" --data-raw " ) -> {
4950 if (req.method == " GET" ) {
5051 req.method = " POST"
5152 }
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)
5460 }
55-
56- s.startsWith(" --data-binary " ) -> {
61+ s.startsWith(" --data" ) -> {
5762 if (req.method == " GET" ) {
5863 req.method = " POST"
5964 }
60- req.rawBody = s.removeFirstAndEndQuotes( 14 ).winEscape( )
65+ s.properBody(req, r, startIndex )
6166 }
62-
63- s.startsWith(" --data " ) -> {
64- if (req.method == " GET" ) {
65- req.method = " POST"
66- }
67- req.rawBody = s.removeFirstAndEndQuotes(7 ).winEscape()
68- }
69-
7067 s.startsWith(" -d " ) -> {
7168 if (req.method == " GET" ) {
7269 req.method = " POST"
7370 }
74- req.rawBody = s.removeFirstAndEndQuotes( 3 ).winEscape( )
71+ s.properBody(req, r, startIndex )
7572 }
76-
7773 s.startsWith(" curl " ) ->
78- with (s.removeFirstAndEndQuotes(5 )) {
74+ with (s.removeFirstAndEndQuotes(startIndex )) {
7975 if (startsWith(" -X" )) {
8076 val str = substring(3 )
8177 req.method = str.substringBefore(" " ).trim()
@@ -84,7 +80,6 @@ fun String.parseCurl(): Request {
8480 req.url = this
8581 }
8682 }
87-
8883 else ->
8984 if (s.startsWith(" http" )) {
9085 req.url = this
@@ -94,6 +89,21 @@ fun String.parseCurl(): Request {
9489 }
9590}
9691
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+
97107fun Request.toCurl (): String =
98108 buildString {
99109 append(" curl \" $url \" \\ " )
0 commit comments