• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

coditory / quark-uri / #3

pending completion
#3

push

github-actions

ogesaku
init

1212 of 1212 new or added lines in 21 files covered. (100.0%)

926 of 1212 relevant lines covered (76.4%)

0.76 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.84
/src/main/java/com/coditory/quark/uri/UriPartValidator.java
1
package com.coditory.quark.uri;
2

3
import java.util.regex.Matcher;
4
import java.util.regex.Pattern;
5

6
import static com.coditory.quark.uri.InetAddressValidator.isValidInetV4Address;
7

8
final class UriPartValidator {
×
9
    private static final String SPECIAL_CHARS = ";/@&=,.?:+$";
10
    private static final String VALID_CHARS = "[^\\s" + SPECIAL_CHARS + "]";
11
    private static final String ATOM = VALID_CHARS + '+';
12
    private static final Pattern SCHEME_PATTERN = Pattern.compile("^\\p{Alpha}[\\p{Alnum}\\+\\-\\.]*");
1✔
13
    private static final Pattern ATOM_PATTERN = Pattern.compile("^(" + ATOM + ").*?$");
1✔
14
    private static final Pattern DOMAIN_PATTERN = Pattern.compile("^" + ATOM + "(\\." + ATOM + ")*$");
1✔
15
    private static final Pattern ALPHA_PATTERN = Pattern.compile("^[a-zA-Z]");
1✔
16

17
    static boolean isValidScheme(String scheme) {
18
        return scheme != null && SCHEME_PATTERN.matcher(scheme).matches();
1✔
19
    }
20

21
    static void checkScheme(String scheme) {
22
        if (!isValidScheme(scheme)) {
1✔
23
            throw new InvalidUriException("Invalid scheme: " + scheme);
1✔
24
        }
25
    }
1✔
26

27
    static boolean isValidHost(String host) {
28
        boolean hostname = false;
1✔
29
        boolean ipV4Address = isValidInetV4Address(host);
1✔
30
        if (!ipV4Address) {
1✔
31
            hostname = DOMAIN_PATTERN.matcher(host).matches();
1✔
32
        }
33
        if (hostname) {
1✔
34
            char[] chars = host.toCharArray();
1✔
35
            int size = 1;
1✔
36
            for (char element : chars) {
1✔
37
                if (element == '.') {
1✔
38
                    size++;
1✔
39
                }
40
            }
41
            String[] domainSegment = new String[size];
1✔
42
            boolean match = true;
1✔
43
            int segmentCount = 0;
1✔
44
            int segmentLength = 0;
1✔
45

46
            while (match) {
1✔
47
                Matcher atomMatcher = ATOM_PATTERN.matcher(host);
1✔
48
                match = atomMatcher.matches();
1✔
49
                if (match) {
1✔
50
                    domainSegment[segmentCount] = atomMatcher.group(1);
1✔
51
                    segmentLength = domainSegment[segmentCount].length() + 1;
1✔
52
                    host = segmentLength >= host.length()
1✔
53
                            ? ""
1✔
54
                            : host.substring(segmentLength);
1✔
55

56
                    segmentCount++;
1✔
57
                }
58
            }
1✔
59
            String topLevel = domainSegment[segmentCount - 1];
1✔
60
            if (topLevel.length() < 2 || topLevel.length() > 4) {
1✔
61
                return false;
×
62
            }
63
            if (!ALPHA_PATTERN.matcher(topLevel.substring(0, 1)).matches()) {
1✔
64
                return false;
1✔
65
            }
66
            if (segmentCount < 2) {
1✔
67
                return false;
×
68
            }
69
        }
70
        return hostname || ipV4Address;
1✔
71
    }
72

73
    static void checkHost(String host) {
74
        if (!isValidHost(host)) {
1✔
75
            throw new InvalidUriException("Invalid host: " + host);
1✔
76
        }
77
    }
1✔
78

79
    static boolean isValidPort(int port) {
80
        return Ports.isValidPortNumber(port);
1✔
81
    }
82

83
    static void checkPort(int port) {
84
        if (!isValidPort(port)) {
1✔
85
            throw new InvalidUriException("Invalid port: " + port);
×
86
        }
87
    }
1✔
88
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc