Skip to content

Commit

Permalink
Adapt flutter 3.13
Browse files Browse the repository at this point in the history
  • Loading branch information
tayoji committed Sep 12, 2023
1 parent 9d0e07c commit e9878dc
Show file tree
Hide file tree
Showing 6 changed files with 303 additions and 46 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
```dart
import 'dart:convert';
import 'dart:io';
import 'package:socks_proxy/socks_proxy.dart';
import 'package:flutter_socks_proxy/socks_proxy.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
Expand Down Expand Up @@ -45,7 +45,8 @@ void main() async {
``` dart
import 'dart:convert';
import 'dart:io';
import 'package:socks_proxy/socks_proxy.dart';
import 'package:flutter_socks_proxy/socks_proxy.dart';
void requset() async {
// proxy -> "SOCKS5/SOCKS4/PROXY username:password@host:port;" or "DIRECT"
Expand Down
260 changes: 248 additions & 12 deletions lib/http_headers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,7 @@ class _Cookie implements Cookie {
String? _path;
bool httpOnly = false;
bool secure = false;
SameSite? sameSite;

_Cookie(String name, String value)
: _name = _validateName(name),
Expand Down Expand Up @@ -1025,7 +1026,7 @@ class _Cookie implements Cookie {
value = parseAttributeValue();
}
if (name == "expires") {
expires = _parseCookieDate(value);
expires = HttpDate._parseCookieDate(value);
} else if (name == "max-age") {
maxAge = int.parse(value);
} else if (name == "domain") {
Expand All @@ -1036,6 +1037,26 @@ class _Cookie implements Cookie {
httpOnly = true;
} else if (name == "secure") {
secure = true;
} else if (name == "samesite") {
switch (value) {
case "lax":
sameSite = SameSite.lax;
break;
case "none":
sameSite = SameSite.none;
break;
case "strict":
sameSite = SameSite.strict;
break;
default:
}
// sameSite = switch (value) {
// "lax" => SameSite.lax,
// "none" => SameSite.none,
// "strict" => SameSite.strict,
// _ => throw HttpException(
// 'SameSite value should be one of Lax, Strict or None.')
// };
}
if (!done()) index++; // Skip the ; character
}
Expand Down Expand Up @@ -1081,6 +1102,8 @@ class _Cookie implements Cookie {
}
if (secure) sb.write("; Secure");
if (httpOnly) sb.write("; HttpOnly");
if (sameSite != null) sb.write("; $sameSite");

return sb.toString();
}

Expand Down Expand Up @@ -1162,9 +1185,222 @@ class _Cookie implements Cookie {
}
}
}
}

class HttpDate {
// From RFC-2616 section "3.3.1 Full Date",
// http://tools.ietf.org/html/rfc2616#section-3.3.1
//
// HTTP-date = rfc1123-date | rfc850-date | asctime-date
// rfc1123-date = wkday "," SP date1 SP time SP "GMT"
// rfc850-date = weekday "," SP date2 SP time SP "GMT"
// asctime-date = wkday SP date3 SP time SP 4DIGIT
// date1 = 2DIGIT SP month SP 4DIGIT
// ; day month year (e.g., 02 Jun 1982)
// date2 = 2DIGIT "-" month "-" 2DIGIT
// ; day-month-year (e.g., 02-Jun-82)
// date3 = month SP ( 2DIGIT | ( SP 1DIGIT ))
// ; month day (e.g., Jun 2)
// time = 2DIGIT ":" 2DIGIT ":" 2DIGIT
// ; 00:00:00 - 23:59:59
// wkday = "Mon" | "Tue" | "Wed"
// | "Thu" | "Fri" | "Sat" | "Sun"
// weekday = "Monday" | "Tuesday" | "Wednesday"
// | "Thursday" | "Friday" | "Saturday" | "Sunday"
// month = "Jan" | "Feb" | "Mar" | "Apr"
// | "May" | "Jun" | "Jul" | "Aug"
// | "Sep" | "Oct" | "Nov" | "Dec"

/// Format a date according to
/// [RFC-1123](http://tools.ietf.org/html/rfc1123 "RFC-1123"),
/// e.g. `Thu, 1 Jan 1970 00:00:00 GMT`.
static String format(DateTime date) {
const List wkday = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
const List month = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
];

DateTime d = date.toUtc();
StringBuffer sb = StringBuffer()
..write(wkday[d.weekday - 1])
..write(", ")
..write(d.day <= 9 ? "0" : "")
..write(d.day.toString())
..write(" ")
..write(month[d.month - 1])
..write(" ")
..write(d.year.toString())
..write(d.hour <= 9 ? " 0" : " ")
..write(d.hour.toString())
..write(d.minute <= 9 ? ":0" : ":")
..write(d.minute.toString())
..write(d.second <= 9 ? ":0" : ":")
..write(d.second.toString())
..write(" GMT");
return sb.toString();
}

/// Parse a date string in either of the formats
/// [RFC-1123](http://tools.ietf.org/html/rfc1123 "RFC-1123"),
/// [RFC-850](http://tools.ietf.org/html/rfc850 "RFC-850") or
/// ANSI C's asctime() format. These formats are listed here.
///
/// Thu, 1 Jan 1970 00:00:00 GMT
/// Thursday, 1-Jan-1970 00:00:00 GMT
/// Thu Jan 1 00:00:00 1970
///
/// For more information see [RFC-2616 section
/// 3.1.1](http://tools.ietf.org/html/rfc2616#section-3.3.1
/// "RFC-2616 section 3.1.1").
static DateTime parse(String date) {
final int SP = 32;
const List wkdays = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
const List weekdays = [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
];
const List months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
];

final int formatRfc1123 = 0;
final int formatRfc850 = 1;
final int formatAsctime = 2;

int index = 0;
String tmp;

void expect(String s) {
if (date.length - index < s.length) {
throw HttpException("Invalid HTTP date $date");
}
String tmp = date.substring(index, index + s.length);
if (tmp != s) {
throw HttpException("Invalid HTTP date $date");
}
index += s.length;
}

int expectWeekday() {
int weekday;
// The formatting of the weekday signals the format of the date string.
int pos = date.indexOf(",", index);
if (pos == -1) {
int pos = date.indexOf(" ", index);
if (pos == -1) throw HttpException("Invalid HTTP date $date");
tmp = date.substring(index, pos);
index = pos + 1;
weekday = wkdays.indexOf(tmp);
if (weekday != -1) {
return formatAsctime;
}
} else {
tmp = date.substring(index, pos);
index = pos + 1;
weekday = wkdays.indexOf(tmp);
if (weekday != -1) {
return formatRfc1123;
}
weekday = weekdays.indexOf(tmp);
if (weekday != -1) {
return formatRfc850;
}
}
throw HttpException("Invalid HTTP date $date");
}

int expectMonth(String separator) {
int pos = date.indexOf(separator, index);
if (pos - index != 3) throw HttpException("Invalid HTTP date $date");
tmp = date.substring(index, pos);
index = pos + 1;
int month = months.indexOf(tmp);
if (month != -1) return month;
throw HttpException("Invalid HTTP date $date");
}

int expectNum(String separator) {
int pos;
if (separator.isNotEmpty) {
pos = date.indexOf(separator, index);
} else {
pos = date.length;
}
String tmp = date.substring(index, pos);
index = pos + separator.length;
try {
int value = int.parse(tmp);
return value;
} on FormatException {
throw HttpException("Invalid HTTP date $date");
}
}

void expectEnd() {
if (index != date.length) {
throw HttpException("Invalid HTTP date $date");
}
}

int format = expectWeekday();
int year;
int month;
int day;
int hours;
int minutes;
int seconds;
if (format == formatAsctime) {
month = expectMonth(" ");
if (date.codeUnitAt(index) == SP) index++;
day = expectNum(" ");
hours = expectNum(":");
minutes = expectNum(":");
seconds = expectNum(" ");
year = expectNum("");
} else {
expect(" ");
day = expectNum(format == formatRfc1123 ? " " : "-");
month = expectMonth(format == formatRfc1123 ? " " : "-");
year = expectNum(" ");
hours = expectNum(":");
minutes = expectNum(":");
seconds = expectNum(" ");
expect("GMT");
}
expectEnd();
return DateTime.utc(year, month + 1, day, hours, minutes, seconds, 0);
}

DateTime _parseCookieDate(String date) {
const List monthsLowerCase = const [
// Parse a cookie date string.
static DateTime _parseCookieDate(String date) {
const List monthsLowerCase = [
"jan",
"feb",
"mar",
Expand All @@ -1181,8 +1417,8 @@ class _Cookie implements Cookie {

int position = 0;

void error() {
throw new HttpException("Invalid cookie date $date");
Never error() {
throw HttpException("Invalid cookie date $date");
}

bool isEnd() => position == date.length;
Expand Down Expand Up @@ -1226,7 +1462,7 @@ class _Cookie implements Cookie {
return int.parse(s.substring(0, index));
}

var tokens = [];
var tokens = <String>[];
while (!isEnd()) {
while (!isEnd() && isDelimiter(date[position])) position++;
int start = position;
Expand All @@ -1241,7 +1477,7 @@ class _Cookie implements Cookie {
String? yearStr;

for (var token in tokens) {
if (token.length < 1) continue;
if (token.isEmpty) continue;
if (timeStr == null &&
token.length >= 5 &&
isDigit(token[0]) &&
Expand All @@ -1266,18 +1502,18 @@ class _Cookie implements Cookie {
error();
}

int year = toInt(yearStr!);
int year = toInt(yearStr);
if (year >= 70 && year <= 99)
year += 1900;
else if (year >= 0 && year <= 69) year += 2000;
if (year < 1601) error();

int dayOfMonth = toInt(dayOfMonthStr!);
int dayOfMonth = toInt(dayOfMonthStr);
if (dayOfMonth < 1 || dayOfMonth > 31) error();

int month = getMonth(monthStr!) + 1;
int month = getMonth(monthStr) + 1;

var timeList = timeStr!.split(":");
var timeList = timeStr.split(":");
if (timeList.length != 3) error();
int hour = toInt(timeList[0]);
int minute = toInt(timeList[1]);
Expand All @@ -1286,6 +1522,6 @@ class _Cookie implements Cookie {
if (minute > 59) error();
if (second > 59) error();

return new DateTime.utc(year, month, dayOfMonth, hour, minute, second, 0);
return DateTime.utc(year, month, dayOfMonth, hour, minute, second, 0);
}
}
5 changes: 3 additions & 2 deletions lib/http_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2306,7 +2306,8 @@ class _HttpClientConnection {
key, secureSocket, request._httpClient, true);
});
}
Future<_HttpClientConnection> createSocksProxyTunnel(

Future<_HttpClientConnection> createSocksProxyTunnel(
String host,
int port,
_Proxy proxy,
Expand Down Expand Up @@ -2338,6 +2339,7 @@ class _HttpClientConnection {
_SocksAuthMethod.NoAuth.value,
_SocksAuthMethod.UsernamePassword.value
]);

await _httpParser._socksStateController.stream.firstWhere((a) {
if (a == _SocksState.AuthStarted) {
if (username.length > 255 || username.length > 255) {
Expand Down Expand Up @@ -2390,7 +2392,6 @@ class _HttpClientConnection {
});
}


HttpConnectionInfo? get connectionInfo => _HttpConnectionInfo.create(_socket);

static makeKey(bool isSecure, String host, int port) {
Expand Down
Loading

0 comments on commit e9878dc

Please sign in to comment.