mirror of
https://github.com/wanghongenpin/proxypin.git
synced 2026-05-16 15:56:51 +08:00
fix curl parse
This commit is contained in:
@@ -204,7 +204,7 @@ class RequestEditorState extends State<RequestEditor> {
|
||||
onPressed: () {
|
||||
try {
|
||||
setState(() {
|
||||
request = parseCurl(text!);
|
||||
request = Curl.parse(text!);
|
||||
requestKey.currentState?.change(request!);
|
||||
requestLineKey.currentState?.change(request?.requestUrl, request?.method.name);
|
||||
});
|
||||
|
||||
@@ -102,7 +102,7 @@ class RequestEditorState extends State<MobileRequestEditor> with SingleTickerPro
|
||||
onPressed: () {
|
||||
try {
|
||||
setState(() {
|
||||
request = parseCurl(text!);
|
||||
request = Curl.parse(text!);
|
||||
requestKey.currentState?.change(request!);
|
||||
requestLineKey.currentState?.change(request?.requestUrl, request?.method.name);
|
||||
});
|
||||
@@ -193,12 +193,13 @@ class RequestEditorState extends State<MobileRequestEditor> with SingleTickerPro
|
||||
responseKey.currentState?.change(response);
|
||||
responseChange.value = 1;
|
||||
|
||||
tabController.animateTo(1);
|
||||
// FlutterToastr.show(localizations.requestSuccess, context);
|
||||
}).catchError((e) {
|
||||
responseChange.value = -1;
|
||||
FlutterToastr.show('${localizations.fail}$e', context);
|
||||
});
|
||||
|
||||
tabController.animateTo(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import 'package:proxypin/network/http/http.dart';
|
||||
import 'package:proxypin/network/http/http_headers.dart';
|
||||
import 'package:proxypin/utils/lang.dart';
|
||||
@@ -34,115 +35,114 @@ String curlRequest(HttpRequest request) {
|
||||
"${headers.join('\\\n')} \\\n $body --compressed";
|
||||
}
|
||||
|
||||
const String _h = "-H";
|
||||
const String _header = "--header";
|
||||
const String _x = "-X";
|
||||
const String _request = "--request";
|
||||
const String _data = "--data";
|
||||
const String _dataRaw = "--data-raw";
|
||||
const String _d = "-d";
|
||||
|
||||
///解析curl
|
||||
HttpRequest parseCurl(String curl) {
|
||||
var lines = curl.trim().split('\n');
|
||||
|
||||
HttpMethod method = HttpMethod.get;
|
||||
HttpHeaders headers = HttpHeaders();
|
||||
String requestUrl = '';
|
||||
String body = '';
|
||||
|
||||
bool parseBody = false;
|
||||
|
||||
for (var it in lines) {
|
||||
it = it.trim();
|
||||
if (it.endsWith("\\")) {
|
||||
it = it.substring(0, it.length - 1);
|
||||
}
|
||||
|
||||
//header
|
||||
if (it.startsWith(_h) || it.startsWith(_header)) {
|
||||
int index = it.startsWith(_h) ? _h.length : _header.length;
|
||||
var line = it.substring(index).trim();
|
||||
line = line.startsWith("'") ? line.substring(1) : line;
|
||||
var endIdx = endIndex(line);
|
||||
|
||||
it = line.substring(endIdx + 1);
|
||||
line = line.substring(0, endIdx == -1 ? line.length : endIdx);
|
||||
var pair = _split(line, ":");
|
||||
if (pair != null) {
|
||||
headers.add(pair.key, pair.value);
|
||||
}
|
||||
|
||||
if (endIdx == -1) {
|
||||
continue;
|
||||
}
|
||||
it = it.trim();
|
||||
}
|
||||
|
||||
if (it.startsWith(_data) || it.startsWith(_d)) {
|
||||
//body
|
||||
String value;
|
||||
if (it.startsWith(_dataRaw)) {
|
||||
value = it.substring(_dataRaw.length).trim();
|
||||
} else if (it.startsWith(_data)) {
|
||||
value = it.substring(_data.length).trim();
|
||||
} else {
|
||||
value = it.substring(_d.length).trim();
|
||||
}
|
||||
value = value.startsWith('\$') || value.startsWith("'") ? value.substring(1) : value;
|
||||
|
||||
int index = endIndex(value);
|
||||
if (index > 0) {
|
||||
body += value.substring(0, index);
|
||||
} else {
|
||||
parseBody = true;
|
||||
body += value;
|
||||
}
|
||||
} else if (parseBody) {
|
||||
int index = endIndex(it);
|
||||
if (index >= 0) {
|
||||
parseBody = false;
|
||||
body += "\n${it.substring(0, index)}";
|
||||
} else {
|
||||
body += "\n$it";
|
||||
}
|
||||
} else if (it.startsWith(_x) || it.startsWith(_request)) {
|
||||
//method
|
||||
int index = it.startsWith(_x) ? _x.length : _request.length;
|
||||
var value = it.substring(index).trim();
|
||||
method = HttpMethod.valueOf(Strings.trimWrap(value, "'"));
|
||||
} else if (it.trim().startsWith("'http") || it.startsWith('curl') && it.contains("'http")) {
|
||||
var index = it.indexOf("'");
|
||||
var value = it.substring(index + 1).trim();
|
||||
if (value.endsWith("'")) {
|
||||
value = value.substring(0, value.length - 1);
|
||||
}
|
||||
try {
|
||||
requestUrl = Uri.decodeFull(value);
|
||||
} catch (e) {
|
||||
requestUrl = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (body.isNotEmpty && method == HttpMethod.get) {
|
||||
method = HttpMethod.post;
|
||||
}
|
||||
HttpRequest request = HttpRequest(method, requestUrl);
|
||||
request.headers.addAll(headers);
|
||||
request.body = body.codeUnits;
|
||||
|
||||
return request;
|
||||
main() {
|
||||
print(Curl.parse(
|
||||
"curl -X POST 'https://example.com/api' -H 'Content-Type: application/json' -d '{\"key\":\"value\"}'"));
|
||||
}
|
||||
|
||||
Pair<String, String>? _split(String line, String code) {
|
||||
try {
|
||||
var index = line.codeUnits.indexOf(code.codeUnits.first);
|
||||
var key = line.substring(0, index).trim();
|
||||
var value = line.substring(index + 1).trim();
|
||||
return Pair(key, value);
|
||||
} catch (e) {
|
||||
return null;
|
||||
class Curl {
|
||||
static const String _h = "-H";
|
||||
static const String _header = "--header";
|
||||
static const String _x = "-X";
|
||||
static const String _request = "--request";
|
||||
static const String _data = "--data";
|
||||
static const String _dataRaw = "--data-raw";
|
||||
static const String _d = "-d";
|
||||
|
||||
static HttpRequest parse(String curlCommand) {
|
||||
HttpMethod method = HttpMethod.get;
|
||||
HttpHeaders headers = HttpHeaders();
|
||||
|
||||
String? url;
|
||||
String? data;
|
||||
|
||||
// 去除 "curl" 关键字并去除首尾空格
|
||||
String trimmedCommand = curlCommand.replaceFirst('curl', '').trim();
|
||||
|
||||
List<String> parts = [];
|
||||
String currentPart = '';
|
||||
bool inQuotes = false;
|
||||
bool inBody = false;
|
||||
|
||||
// 处理可能包含引号的参数
|
||||
for (int i = 0; i < trimmedCommand.length; i++) {
|
||||
String char = trimmedCommand[i];
|
||||
if (char == '"' || char == "'") {
|
||||
if (inBody) {
|
||||
currentPart += char;
|
||||
continue;
|
||||
}
|
||||
|
||||
// 如果当前字符是引号,切换 inQuotes 状态
|
||||
inQuotes = !inQuotes;
|
||||
} else if (char == ' ' && !inQuotes) {
|
||||
if (inBody && currentPart.length > 2) {
|
||||
// 如果当前部分是数据,去掉前后的引号
|
||||
currentPart = currentPart.substring(1, currentPart.length - 1);
|
||||
}
|
||||
|
||||
if (currentPart == '-d' || currentPart == '--data' || currentPart == '--data-raw') {
|
||||
inBody = true;
|
||||
} else {
|
||||
inBody = false;
|
||||
}
|
||||
|
||||
parts.add(currentPart);
|
||||
currentPart = '';
|
||||
} else {
|
||||
currentPart += char;
|
||||
}
|
||||
}
|
||||
|
||||
if (currentPart.isNotEmpty) {
|
||||
if (inBody && currentPart.length > 2) {
|
||||
// 如果当前部分是数据,去掉前后的引号
|
||||
currentPart = currentPart.substring(1, currentPart.length - 1);
|
||||
}
|
||||
|
||||
parts.add(currentPart);
|
||||
}
|
||||
|
||||
String protocolVersion = "HTTP/1.1";
|
||||
|
||||
// 遍历参数列表进行解析
|
||||
for (int i = 0; i < parts.length; i++) {
|
||||
String part = parts[i];
|
||||
if (part == _x || part == _request) {
|
||||
// 解析请求方法
|
||||
if (i + 1 < parts.length) {
|
||||
method = HttpMethod.valueOf(parts[++i]);
|
||||
}
|
||||
} else if (part == _h || part == _header) {
|
||||
// 解析请求头
|
||||
if (i + 1 < parts.length) {
|
||||
String headerStr = parts[++i];
|
||||
List<String> headerParts = headerStr.splitFirst(':'.codeUnits.first);
|
||||
if (headerParts.length == 2) {
|
||||
headers.add(headerParts[0], headerParts[1]);
|
||||
}
|
||||
}
|
||||
} else if (part == _d || part == _dataRaw || part == _data) {
|
||||
// 解析请求数据
|
||||
if (i + 1 < parts.length) {
|
||||
data = parts[++i];
|
||||
}
|
||||
} else if (!part.startsWith('-') && part.startsWith("http")) {
|
||||
// 解析请求 URL
|
||||
url = part;
|
||||
} else if ("--http2" == part) {
|
||||
// protocolVersion = "HTTP2";
|
||||
}
|
||||
}
|
||||
|
||||
if (data?.isNotEmpty == true && method == HttpMethod.get) {
|
||||
method = HttpMethod.post;
|
||||
}
|
||||
|
||||
HttpRequest request = HttpRequest(method, url ?? '', protocolVersion: protocolVersion);
|
||||
request.headers.addAll(headers);
|
||||
request.body = data?.codeUnits;
|
||||
return request;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user