From ab2bd075f26ac38566b9202ac492c5e6dbecd025 Mon Sep 17 00:00:00 2001 From: wanghongenpin Date: Sat, 2 Nov 2024 21:12:01 +0800 Subject: [PATCH] Fix HTTP protocol request line URI and header host inconsistency (#361) --- lib/network/http/codec.dart | 10 +++++++++- lib/network/http/http.dart | 30 +++++++++++++----------------- lib/network/http_client.dart | 2 +- lib/ui/mobile/mobile.dart | 3 +-- 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/lib/network/http/codec.dart b/lib/network/http/codec.dart index 710f901..514a357 100644 --- a/lib/network/http/codec.dart +++ b/lib/network/http/codec.dart @@ -19,6 +19,7 @@ import 'dart:math'; import 'dart:typed_data'; import 'package:proxypin/network/channel.dart'; +import 'package:proxypin/network/host_port.dart'; import 'package:proxypin/network/http/body_reader.dart'; import 'package:proxypin/network/http/constants.dart'; import 'package:proxypin/network/http/h2/codec.dart'; @@ -225,11 +226,18 @@ class HttpRequestCodec extends HttpCodec { @override void initialLine(BytesBuilder buffer, HttpRequest message) { + String uri = message.uri; + + //http scheme 输入地址和host不一致 + if (uri.startsWith(HostAndPort.httpScheme) && message.requestUri?.host != message.headers.host) { + uri = message.requestUri?.replace(host: message.headers.host).toString() ?? uri; + } + //请求行 buffer ..add(message.method.name.codeUnits) ..addByte(HttpConstants.sp) - ..add(message.uri.codeUnits) + ..add(uri.codeUnits) ..addByte(HttpConstants.sp) ..add(message.protocolVersion.codeUnits) ..addByte(HttpConstants.cr) diff --git a/lib/network/http/http.dart b/lib/network/http/http.dart index b391c9e..24e7af4 100644 --- a/lib/network/http/http.dart +++ b/lib/network/http/http.dart @@ -143,9 +143,15 @@ class HttpRequest extends HttpMessage { String get requestUrl => HostAndPort.startsWithScheme(uri) ? uri : '${remoteDomain()}$uri'; /// 请求的uri + Uri? _requestUri; + Uri? get requestUri { + if (_requestUri != null && _requestUri.toString() == requestUrl) { + return _requestUri; + } try { - return Uri.parse(requestUrl); + _requestUri ??= Uri.parse(requestUrl); + return _requestUri; } catch (e) { return null; } @@ -155,22 +161,12 @@ class HttpRequest extends HttpMessage { String get domainPath => '${remoteDomain()}$path'; /// 请求的path - String get path { - try { - var requestPath = Uri.parse(requestUrl).path; - return requestPath.isEmpty ? "" : requestPath; - } catch (e) { - return "/"; - } - } + String get path => requestUri?.path ?? ''; - Map get queries { - try { - return Uri.parse(requestUrl).queryParameters; - } catch (e) { - return {}; - } - } + /// path and query + String get pathAndQuery => '${requestUri?.path}${requestUri?.hasQuery == true ? '?${requestUri?.query}' : ''}'; + + Map get queries => requestUri?.queryParameters ?? {}; ///获取消息体编码 @override @@ -215,7 +211,7 @@ class HttpRequest extends HttpMessage { @override String toString() { - return 'HttpRequest{version: $protocolVersion, url: $uri, method: ${method.name}, headers: $headers, contentLength: $contentLength, bodyLength: ${body?.length}}'; + return 'HttpRequest{version: $protocolVersion, uri: $uri, method: ${method.name}, headers: $headers, contentLength: $contentLength, bodyLength: ${body?.length}}'; } } diff --git a/lib/network/http_client.dart b/lib/network/http_client.dart index dddc7fb..a35ea47 100644 --- a/lib/network/http_client.dart +++ b/lib/network/http_client.dart @@ -51,7 +51,7 @@ class HttpClients { HostAndPort connectHost = proxyInfo == null ? hostAndPort : HostAndPort.host(proxyInfo.host, proxyInfo.port!); var channel = await client.connect(connectHost, channelContext); - if (proxyInfo != null && hostAndPort.isSsl()) { + if (proxyInfo != null) { await connectRequest(hostAndPort, channel, proxyInfo: proxyInfo); } diff --git a/lib/ui/mobile/mobile.dart b/lib/ui/mobile/mobile.dart index d6c29c3..33ca71f 100644 --- a/lib/ui/mobile/mobile.dart +++ b/lib/ui/mobile/mobile.dart @@ -33,7 +33,6 @@ import 'package:proxypin/network/http/websocket.dart'; import 'package:proxypin/network/http_client.dart'; import 'package:proxypin/ui/component/memory_cleanup.dart'; import 'package:proxypin/ui/component/toolbox.dart'; -import 'package:proxypin/ui/component/widgets.dart'; import 'package:proxypin/ui/configuration.dart'; import 'package:proxypin/ui/content/panel.dart'; import 'package:proxypin/ui/launch/launch.dart'; @@ -184,7 +183,7 @@ class MobileHomeState extends State implements EventListener, Li child: ValueListenableBuilder( valueListenable: _selectIndex, builder: (context, index, child) => Scaffold( - body: LazyIndexedStack(index: index, children: navigationView), + body: IndexedStack(index: index, children: navigationView), bottomNavigationBar: widget.appConfiguration.bottomNavigation ? Container( constraints: const BoxConstraints(maxHeight: 80),