Skip to content

Commit

Permalink
fix(WebDAVLoginView): Fixes #17 and make server port optional
Browse files Browse the repository at this point in the history
  • Loading branch information
tmaegel committed Feb 3, 2024
1 parent 34a2ea6 commit f377f27
Show file tree
Hide file tree
Showing 4 changed files with 279 additions and 160 deletions.
2 changes: 1 addition & 1 deletion lib/client/webdav_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class WebDAVClient {
WebDAVClient({
String schema = 'http',
required String host,
required int port,
int? port,
required String baseUrl,
required String username,
required String password,
Expand Down
10 changes: 6 additions & 4 deletions lib/data/todo/todo_list_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,15 @@ class WebDAVTodoListApi extends LocalTodoListApi {
required String password,
}) {
late WebDAVClient client;
final RegExp exp =
RegExp(r'(?<schema>^(http|https)):\/\/(?<host>\w+):(?<port>\d+)$');
final RegExp exp = RegExp(
r'(?<schema>^(http|https)):\/\/(?<host>[a-zA-Z0-9.-]+)(:(?<port>\d+)){0,1}$');
final RegExpMatch? match = exp.firstMatch(server);
if (match != null) {
String schema = match.namedGroup('schema')!;
String host = match.namedGroup('host')!;
int port = int.parse(match.namedGroup('port')!);
int? port = match.namedGroup('port') != null
? int.parse(match.namedGroup('port')!)
: null;
client = WebDAVClient(
schema: schema,
host: host,
Expand All @@ -211,7 +213,7 @@ class WebDAVTodoListApi extends LocalTodoListApi {
password: password,
);
} else {
throw const FormatException('Invalid server foramt.');
throw const FormatException('Invalid server format');
}

return WebDAVTodoListApi._(
Expand Down
10 changes: 2 additions & 8 deletions lib/presentation/login/pages/login_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class WebDAVLoginView extends StatelessWidget {
controller: serverTextFieldController,
decoration: const InputDecoration(
labelText: 'Server',
hintText: 'http[s]://<server>:<port>',
hintText: 'http[s]://<server>[:<port>]',
),
validator: (String? value) {
if (value == null || value.isEmpty) {
Expand All @@ -108,14 +108,8 @@ class WebDAVLoginView extends StatelessWidget {
!value.startsWith('https://')) {
return 'Missing protocol';
}
final strippedValue = value
.replaceAll('http://', '')
.replaceAll('https://', '');
if (strippedValue.split(':').length < 2) {
return 'Missing server port';
}
if (!RegExp(
r'(?<proto>^(http|https):\/\/)(?<host>[a-zA-Z0-9.-]+):(?<port>\d+)$')
r'(?<proto>^(http|https):\/\/)(?<host>[a-zA-Z0-9.-]+)(:(?<port>\d+)){0,1}$')
.hasMatch(value)) {
return 'Invalid format';
}
Expand Down
Loading

0 comments on commit f377f27

Please sign in to comment.