|
| 1 | +import 'dart:io'; |
| 2 | + |
| 3 | +import 'package:http/http.dart'; |
| 4 | +import 'package:http/io_client.dart'; |
| 5 | +import 'package:http_parser/http_parser.dart'; |
| 6 | + |
| 7 | +class HttpStatusException implements Exception { |
| 8 | + final int statusCode; |
| 9 | + |
| 10 | + final String? body; |
| 11 | + |
| 12 | + const HttpStatusException(this.statusCode, [this.body]); |
| 13 | + |
| 14 | + @override |
| 15 | + String toString() { |
| 16 | + var result = |
| 17 | + "HttpStatusException: the HTTP response's status code is $statusCode"; |
| 18 | + if (body != null) { |
| 19 | + result += ", the body is '$body'"; |
| 20 | + } |
| 21 | + |
| 22 | + return result; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +class Multipart extends MultipartRequest { |
| 27 | + Multipart(String url) : super('POST', Uri.parse(url)); |
| 28 | + |
| 29 | + void add(String field, Object value) => fields[field] = value.toString(); |
| 30 | + |
| 31 | + void addBytes(String field, List<int> value, |
| 32 | + {String? filename, String? contentType}) => |
| 33 | + files.add(MultipartFile.fromBytes(field, value, |
| 34 | + filename: filename, |
| 35 | + contentType: |
| 36 | + contentType != null ? MediaType.parse(contentType) : null)); |
| 37 | +} |
| 38 | + |
| 39 | +class Client extends IOClient { |
| 40 | + static const Duration _idleTimeout = Duration(seconds: 90); |
| 41 | + |
| 42 | + static const String _userAgent = 'xdnmb'; |
| 43 | + |
| 44 | + Client({Duration timeout = const Duration(seconds: 15)}) |
| 45 | + : super(HttpClient() |
| 46 | + ..connectionTimeout = timeout |
| 47 | + ..idleTimeout = _idleTimeout); |
| 48 | + |
| 49 | + @override |
| 50 | + Future<IOStreamedResponse> send(BaseRequest request) { |
| 51 | + request.headers[HttpHeaders.userAgentHeader] = _userAgent; |
| 52 | + |
| 53 | + return super.send(request); |
| 54 | + } |
| 55 | + |
| 56 | + Future<Response> xGet(String url, [String? cookie]) async { |
| 57 | + final response = await this.get(Uri.parse(url), |
| 58 | + headers: cookie != null ? {HttpHeaders.cookieHeader: cookie} : null); |
| 59 | + _checkStatusCode(response); |
| 60 | + |
| 61 | + return response; |
| 62 | + } |
| 63 | + |
| 64 | + Future<Response> xPostForm(String url, Map<String, String>? form, |
| 65 | + [String? cookie]) async { |
| 66 | + final response = await this.post(Uri.parse(url), |
| 67 | + headers: cookie != null ? {HttpHeaders.cookieHeader: cookie} : null, |
| 68 | + body: form); |
| 69 | + _checkStatusCode(response); |
| 70 | + |
| 71 | + return response; |
| 72 | + } |
| 73 | + |
| 74 | + Future<Response> xPostMultipart(Multipart multipart, [String? cookie]) async { |
| 75 | + if (cookie != null) { |
| 76 | + multipart.headers[HttpHeaders.cookieHeader] = cookie; |
| 77 | + } |
| 78 | + final streamedResponse = await send(multipart); |
| 79 | + final response = await Response.fromStream(streamedResponse); |
| 80 | + _checkStatusCode(response); |
| 81 | + |
| 82 | + return response; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +void _checkStatusCode(Response response) { |
| 87 | + if (response.statusCode != HttpStatus.ok) { |
| 88 | + final body = response.body; |
| 89 | + if (body.isEmpty) { |
| 90 | + throw HttpStatusException(response.statusCode); |
| 91 | + } |
| 92 | + throw HttpStatusException(response.statusCode, body); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +/* import 'dart:collection'; |
| 97 | +import 'dart:convert'; |
| 98 | +import 'dart:io'; |
| 99 | +
|
| 100 | +import 'package:form_data/form_data.dart'; |
| 101 | +
|
| 102 | +class HttpStatusException implements Exception { |
| 103 | + final int statusCode; |
| 104 | +
|
| 105 | + final String? body; |
| 106 | +
|
| 107 | + const HttpStatusException(this.statusCode, [this.body]); |
| 108 | +
|
| 109 | + @override |
| 110 | + String toString() { |
| 111 | + var result = |
| 112 | + "HttpStatusException: the HTTP response's status code is $statusCode"; |
| 113 | + if (body != null) { |
| 114 | + result += ", the body is '$body'"; |
| 115 | + } |
| 116 | +
|
| 117 | + return result; |
| 118 | + } |
| 119 | +} |
| 120 | +
|
| 121 | +enum Method { get, post } |
| 122 | +
|
| 123 | +class Request { |
| 124 | + final Method method; |
| 125 | +
|
| 126 | + final String url; |
| 127 | +
|
| 128 | + final List<String>? cookies; |
| 129 | +
|
| 130 | + late final Map<String, String>? headers; |
| 131 | +
|
| 132 | + late final List<int>? body; |
| 133 | +
|
| 134 | + Request( |
| 135 | + {required this.method, |
| 136 | + required this.url, |
| 137 | + this.cookies, |
| 138 | + this.headers, |
| 139 | + this.body}); |
| 140 | +
|
| 141 | + Request.get(this.url, {this.cookies, this.headers}) |
| 142 | + : method = Method.get, |
| 143 | + body = null; |
| 144 | +
|
| 145 | + Request.postForm(this.url, Map<String, String> form, |
| 146 | + {this.cookies, Map<String, String>? headers}) |
| 147 | + : method = Method.post { |
| 148 | + var pairs = <List<String>>[]; |
| 149 | + form.forEach((key, value) { |
| 150 | + pairs.add( |
| 151 | + [Uri.encodeQueryComponent(key), Uri.encodeQueryComponent(value)]); |
| 152 | + }); |
| 153 | + final bodyString = pairs.map((pair) => '${pair[0]}=${pair[1]}').join('&'); |
| 154 | + body = utf8.encode(bodyString); |
| 155 | +
|
| 156 | + this.headers = headers ?? HashMap() |
| 157 | + ..[HttpHeaders.contentTypeHeader] = 'application/x-www-form-urlencoded'; |
| 158 | + } |
| 159 | +
|
| 160 | + Request.postMultipart(this.url, FormData multipart, |
| 161 | + {this.cookies, Map<String, String>? headers}) |
| 162 | + : method = Method.post { |
| 163 | + body = multipart.body; |
| 164 | +
|
| 165 | + this.headers = headers ?? HashMap() |
| 166 | + ..[HttpHeaders.contentTypeHeader] = multipart.contentType; |
| 167 | + } |
| 168 | +} |
| 169 | +
|
| 170 | +class Response { |
| 171 | + final HttpHeaders headers; |
| 172 | +
|
| 173 | + final String body; |
| 174 | +
|
| 175 | + const Response(this.headers, this.body); |
| 176 | +} |
| 177 | +
|
| 178 | +class Client { |
| 179 | + final HttpClient _httpClient; |
| 180 | +
|
| 181 | + static const Duration _idleTimeout = Duration(seconds: 90); |
| 182 | +
|
| 183 | + static const String _userAgent = 'xdnmb'; |
| 184 | +
|
| 185 | + Client({Duration timeout = const Duration(seconds: 15)}) |
| 186 | + : _httpClient = HttpClient() |
| 187 | + ..connectionTimeout = timeout |
| 188 | + ..idleTimeout = _idleTimeout; |
| 189 | +
|
| 190 | + Future<Response> send(Request request) async { |
| 191 | + final httpRequest = |
| 192 | + await _httpClient.openUrl(request.method.name, Uri.parse(request.url)); |
| 193 | +
|
| 194 | + httpRequest.headers.set(HttpHeaders.userAgentHeader, _userAgent); |
| 195 | +
|
| 196 | + if (request.cookies != null) { |
| 197 | + for (final cookie in request.cookies!) { |
| 198 | + httpRequest.headers.add(HttpHeaders.cookieHeader, cookie); |
| 199 | + } |
| 200 | + } |
| 201 | +
|
| 202 | + if (request.headers != null) { |
| 203 | + request.headers! |
| 204 | + .forEach((key, value) => httpRequest.headers.add(key, value)); |
| 205 | + } |
| 206 | +
|
| 207 | + if (request.body != null) { |
| 208 | + httpRequest.headers |
| 209 | + .set(HttpHeaders.contentLengthHeader, request.body!.length); |
| 210 | + httpRequest.add(request.body!); |
| 211 | + } |
| 212 | +
|
| 213 | + final response = await httpRequest.close(); |
| 214 | + final body = await response.transform(utf8.decoder).join(); |
| 215 | +
|
| 216 | + if (response.statusCode != HttpStatus.ok) { |
| 217 | + if (body.isEmpty) { |
| 218 | + throw HttpStatusException(response.statusCode); |
| 219 | + } |
| 220 | + throw HttpStatusException(response.statusCode, body); |
| 221 | + } |
| 222 | +
|
| 223 | + return Response(response.headers, body); |
| 224 | + } |
| 225 | +
|
| 226 | + Future<Response> get(String url, [String? cookie]) async { |
| 227 | + final request = Request.get(url, cookies: cookie == null ? null : [cookie]); |
| 228 | +
|
| 229 | + return await send(request); |
| 230 | + } |
| 231 | +
|
| 232 | + Future<Response> postForm(String url, Map<String, String> form, |
| 233 | + [String? cookie]) async { |
| 234 | + final request = |
| 235 | + Request.postForm(url, form, cookies: cookie == null ? null : [cookie]); |
| 236 | +
|
| 237 | + return await send(request); |
| 238 | + } |
| 239 | +
|
| 240 | + Future<Response> postMultipart(String url, FormData multipart, |
| 241 | + [String? cookie]) async { |
| 242 | + final request = Request.postMultipart(url, multipart, |
| 243 | + cookies: cookie == null ? null : [cookie]); |
| 244 | +
|
| 245 | + return await send(request); |
| 246 | + } |
| 247 | +
|
| 248 | + void close({bool force = false}) { |
| 249 | + _httpClient.close(force: force); |
| 250 | + } |
| 251 | +} |
| 252 | + */ |
0 commit comments