Skip to content

Commit

Permalink
update request configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
huanghaiyang committed Oct 7, 2019
1 parent 25bf139 commit 17eb145
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 21 deletions.
13 changes: 11 additions & 2 deletions lib/resources/configure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,17 @@ application:
router:
defaultMapping: /<string>
request:
unSupportedContentTypes: <array<string>>
unSupportedMethods: <array<string>>
unAllowedContentTypes: <array<string>>
unAllowedMethods: <array<string>>
unAllowedOrigins: <array<string>>
unAllowedHeaders: <array<string>>
unAllowedCredentials: <array<string>>
allowedContentTypes: <array<string>>
allowedMethods: <array<string>>
allowedOrigins: <array<string>>
allowedHeaders: <array<string>>
allowedCredentials: <array<string>>
maxAge: <int>
multipart:
maxFileUploadSize: 10mb<sizeunit>
fixNameSuffixIfArray: true<bool>
Expand Down
13 changes: 11 additions & 2 deletions lib/src/configure/ApplicationConfigurationNames.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@ final String APPLICATION_AUTHORS = 'application.author';
final String APPLICATION_CREATE_TIME = 'application.createTime';
final String APPLICATION_INTERCEPTOR_TIMEOUT = 'application.configuration.interceptor.timeout';
final String APPLICATION_ROUTER_DEFAULT_MAPPING = 'application.configuration.router.defaultMapping';
final String APPLICATION_REQUEST_UN_SUPPORTED_CONTENT_TYPES = 'application.configuration.request.unSupportedContentTypes';
final String APPLICATION_REQUEST_UN_SUPPORTED_METHODS = 'application.configuration.request.unSupportedMethods';
final String APPLICATION_REQUEST_UN_ALLOWED_CONTENT_TYPES = 'application.configuration.request.unAllowedContentTypes';
final String APPLICATION_REQUEST_UN_ALLOWED_METHODS = 'application.configuration.request.unAllowedMethods';
final String APPLICATION_REQUEST_UN_ALLOWED_ORIGINS = 'application.configuration.request.unAllowedOrigins';
final String APPLICATION_REQUEST_UN_ALLOWED_HEADERS = 'application.configuration.request.unAllowedHeaders';
final String APPLICATION_REQUEST_UN_ALLOWED_CREDENTIALS = 'application.configuration.request.unAllowedCredentials';
final String APPLICATION_REQUEST_ALLOWED_CONTENT_TYPES = 'application.configuration.request.allowedContentTypes';
final String APPLICATION_REQUEST_ALLOWED_METHODS = 'application.configuration.request.allowedMethods';
final String APPLICATION_REQUEST_ALLOWED_ORIGINS = 'application.configuration.request.allowedOrigins';
final String APPLICATION_REQUEST_ALLOWED_HEADERS = 'application.configuration.request.allowedHeaders';
final String APPLICATION_REQUEST_ALLOWED_CREDENTIALS = 'application.configuration.request.allowedCredentials';
final String APPLICATION_REQUEST_MAX_AGE = 'application.configuration.request.maxAge';
final String APPLICATION_MULTIPART_MAX_FILE_UPLOAD_SIZE = 'application.configuration.request.multipart.maxFileUploadSize';
final String APPLICATION_MULTIPART_FIX_NAME_SUFFIX_IF_ARRAY = 'application.configuration.request.multipart.fixNameSuffixIfArray';
final String APPLICATION_MULTIPART_DEFAULT_UPLOAD_TEMP_DIR_PATH = 'application.configuration.request.multipart.defaultUploadTempDirPath';
Expand Down
116 changes: 106 additions & 10 deletions lib/src/configure/HttpRequestConfigure.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,130 @@ abstract class HttpRequestConfigure extends AbstractConfigure {
factory HttpRequestConfigure() => _HttpRequestConfigure();

// 当前不支持的请求类型
List<ContentType> get unSupportedContentTypes;
List<ContentType> get unAllowedContentTypes;

// 当前支持的请求类型
List<HttpMethod> get unSupportedMethods;
List<HttpMethod> get unAllowedMethods;

List<String> get unAllowedOrigins;

List<String> get unAllowedHeaders;

List<String> get unAllowedCredentials;

List<ContentType> get allowedContentTypes;

List<HttpMethod> get allowedMethods;

List<String> get allowedOrigins;

List<String> get allowedHeaders;

List<String> get allowedCredentials;

int get maxAge;
}

class _HttpRequestConfigure implements HttpRequestConfigure {
List<ContentType> unSupportedContentTypes_ = List();
List<ContentType> unAllowedContentTypes_ = List();

List<HttpMethod> unAllowedMethods_ = List();

List<String> unAllowedOrigins_ = List();

List<String> unAllowedHeaders_ = List();

List<String> unAllowedCredentials_ = List();

List<HttpMethod> unSupportedMethods_ = List();
List<ContentType> allowedContentTypes_ = List();

List<HttpMethod> allowedMethods_ = List();

List<String> allowedOrigins_ = List();

List<String> allowedHeaders_ = List();

List<String> allowedCredentials_ = List();

int maxAge_;

@override
List<ContentType> get unSupportedContentTypes {
return List.unmodifiable(this.unSupportedContentTypes_);
List<ContentType> get unAllowedContentTypes {
return List.unmodifiable(this.unAllowedContentTypes_);
}

@override
List<HttpMethod> get unSupportedMethods {
return List.unmodifiable(this.unSupportedMethods_);
List<HttpMethod> get unAllowedMethods {
return List.unmodifiable(this.unAllowedMethods_);
}

@override
List<String> get unAllowedOrigins {
return unAllowedOrigins_;
}

@override
List<String> get unAllowedHeaders {
return unAllowedHeaders_;
}

@override
List<String> get unAllowedCredentials {
return unAllowedCredentials_;
}

@override
List<ContentType> get allowedContentTypes {
return allowedContentTypes_;
}

@override
List<HttpMethod> get allowedMethods {
return allowedMethods_;
}

@override
List<String> get allowedOrigins {
return allowedOrigins_;
}

@override
List<String> get allowedHeaders {
return allowedHeaders_;
}

@override
List<String> get allowedCredentials {
return allowedCredentials_;
}

@override
int get maxAge {
return maxAge_;
}

@override
Future<dynamic> init(ApplicationConfiguration applicationConfiguration) async {
unSupportedContentTypes_.addAll(List.from(applicationConfiguration.get(APPLICATION_REQUEST_UN_SUPPORTED_CONTENT_TYPES)).map((value) {
unAllowedContentTypes_.addAll(List.from(applicationConfiguration.get(APPLICATION_REQUEST_UN_ALLOWED_CONTENT_TYPES)).map((value) {
return ContentType.parse(value.toString());
}));
unSupportedMethods_.addAll(List.from(applicationConfiguration.get(APPLICATION_REQUEST_UN_SUPPORTED_METHODS)).map((value) {
unAllowedMethods_.addAll(List.from(applicationConfiguration.get(APPLICATION_REQUEST_UN_ALLOWED_METHODS)).map((value) {
return HttpMethodHelper.fromMethod(value.toString().toUpperCase());
}));
unAllowedHeaders_.addAll(applicationConfiguration.get(APPLICATION_REQUEST_UN_ALLOWED_HEADERS));
unAllowedOrigins_.addAll(applicationConfiguration.get(APPLICATION_REQUEST_UN_ALLOWED_ORIGINS));
unAllowedCredentials_.addAll(applicationConfiguration.get(APPLICATION_REQUEST_UN_ALLOWED_CREDENTIALS));

allowedContentTypes_.addAll(List.from(applicationConfiguration.get(APPLICATION_REQUEST_ALLOWED_CONTENT_TYPES)).map((value) {
return ContentType.parse(value.toString());
}));
allowedMethods_.addAll(List.from(applicationConfiguration.get(APPLICATION_REQUEST_ALLOWED_METHODS)).map((value) {
return HttpMethodHelper.fromMethod(value.toString().toUpperCase());
}));
allowedHeaders_.addAll(applicationConfiguration.get(APPLICATION_REQUEST_ALLOWED_HEADERS));
allowedOrigins_.addAll(applicationConfiguration.get(APPLICATION_REQUEST_ALLOWED_ORIGINS));
allowedCredentials_.addAll(applicationConfiguration.get(APPLICATION_REQUEST_ALLOWED_CREDENTIALS));

maxAge_ = applicationConfiguration.get(APPLICATION_REQUEST_MAX_AGE);
}
}
8 changes: 4 additions & 4 deletions lib/src/helpers/UnSupportedContentTypeHelper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import 'package:Q/src/Application.dart';

class UnSupportedContentTypeHelper {
static bool checkSupported(HttpRequest req) {
List<ContentType> unSupportedContentTypes =
Application.getApplicationContext().configuration.httpRequestConfigure.unSupportedContentTypes;
if (unSupportedContentTypes.isEmpty) return true;
return unSupportedContentTypes.indexWhere((contentType) => contentType.mimeType == req.headers.contentType.mimeType) == -1;
List<ContentType> unAllowedContentTypes =
Application.getApplicationContext().configuration.httpRequestConfigure.unAllowedContentTypes;
if (unAllowedContentTypes.isEmpty) return true;
return unAllowedContentTypes.indexWhere((contentType) => contentType.mimeType == req.headers.contentType.mimeType) == -1;
}
}
6 changes: 3 additions & 3 deletions lib/src/helpers/UnSupportedMethodHelper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import 'package:Q/src/helpers/HttpMethodHelper.dart';

class UnSupportedMethodHelper {
static bool checkSupported(HttpRequest req) {
List<HttpMethod> unSupportedMethods = Application.getApplicationContext().configuration.httpRequestConfigure.unSupportedMethods;
if (unSupportedMethods.isEmpty) return true;
return unSupportedMethods.indexWhere((method) => HttpMethodHelper.getMethodName(method) == req.method.toUpperCase()) == -1;
List<HttpMethod> unAllowedMethods = Application.getApplicationContext().configuration.httpRequestConfigure.unAllowedMethods;
if (unAllowedMethods.isEmpty) return true;
return unAllowedMethods.indexWhere((method) => HttpMethodHelper.getMethodName(method) == req.method.toUpperCase()) == -1;
}
}

0 comments on commit 17eb145

Please # to comment.