• 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

96.43
/src/main/java/com/coditory/quark/uri/UriRfc.java
1
package com.coditory.quark.uri;
2

3
import java.util.BitSet;
4

5
import static com.coditory.quark.uri.Preconditions.expectNonNull;
6
import static java.util.Locale.ROOT;
7

8
enum UriRfc {
1✔
9
    SCHEME(UriRfcCharacters.SCHEME_ALLOWED),
1✔
10
    SCHEME_SPECIFIC_PART(UriRfcCharacters.SCHEME_SPECIFIC_PART_ALLOWED),
1✔
11
    USER_INFO(UriRfcCharacters.USER_INFO_ALLOWED),
1✔
12
    HOST(UriRfcCharacters.HOST_IPV6_ALLOWED),
1✔
13
    PORT(UriRfcCharacters.PORT_ALLOWED),
1✔
14
    PATH_SEGMENT(UriRfcCharacters.PATH_SEGMENT_ALLOWED),
1✔
15
    QUERY(UriRfcCharacters.QUERY_ALLOWED, true),
1✔
16
    QUERY_PARAM(UriRfcCharacters.QUERY_PARAM_ALLOWED, true),
1✔
17
    FRAGMENT(UriRfcCharacters.FRAGMENT_ALLOWED);
1✔
18

19
    private final BitSet allowed;
20
    private final PercentCodec codec;
21

22
    UriRfc(String allowed) {
23
        this(allowed, false);
1✔
24
    }
1✔
25

26
    UriRfc(String allowed, boolean decodeSpaceAsPlus) {
1✔
27
        this.allowed = BitSets.of(allowed);
1✔
28
        String encode = decodeSpaceAsPlus
1✔
29
                ? allowed.replaceAll("\\+", "")
1✔
30
                : allowed;
1✔
31
        this.codec = PercentCodec.builder()
1✔
32
                .safeCharacters(encode)
1✔
33
                .decodeSpaceAsPlus(decodeSpaceAsPlus)
1✔
34
                .build();
1✔
35
    }
1✔
36

37
    String validateAndDecode(String source) {
38
        checkValidEncoded(source);
1✔
39
        return decode(source);
1✔
40
    }
41

42
    String decode(String source) {
43
        StringBuilder builder = new StringBuilder();
1✔
44
        decode(source, builder);
1✔
45
        return builder.toString();
1✔
46
    }
47

48
    void decode(String source, StringBuilder builder) {
49
        codec.decode(source, builder);
1✔
50
    }
1✔
51

52
    String encode(String source) {
53
        StringBuilder builder = new StringBuilder();
1✔
54
        encode(source, builder);
1✔
55
        return builder.toString();
1✔
56
    }
57

58
    void encode(String source, StringBuilder builder) {
59
        codec.encode(source, builder);
1✔
60
    }
1✔
61

62
    void checkValidEncoded(String source) {
63
        String error = checkValidEncodedWithErrorMessage(source);
1✔
64
        if (error != null) {
1✔
65
            throw new InvalidUriException(error);
1✔
66
        }
67
    }
1✔
68

69
    private String checkValidEncodedWithErrorMessage(String source) {
70
        expectNonNull(source, "source");
1✔
71
        int length = source.length();
1✔
72
        for (int i = 0; i < length; i++) {
1✔
73
            char ch = source.charAt(i);
1✔
74
            if (ch == '%') {
1✔
75
                if ((i + 2) < length) {
1✔
76
                    char hex1 = source.charAt(i + 1);
1✔
77
                    char hex2 = source.charAt(i + 2);
1✔
78
                    int u = Character.digit(hex1, 16);
1✔
79
                    int l = Character.digit(hex2, 16);
1✔
80
                    if (u == -1 || l == -1) {
1✔
81
                        return "Invalid encoded sequence \"" + source.substring(i) + "\"";
×
82
                    }
83
                    i += 2;
1✔
84
                } else {
1✔
85
                    return "Invalid encoded sequence \"" + source.substring(i) + "\"";
×
86
                }
87
            } else if (!allowed.get(ch)) {
1✔
88
                return "Invalid character '" + ch + "' for " + name().toLowerCase(ROOT) + " in \"" + source + "\"";
1✔
89
            }
90
        }
91
        return null;
1✔
92
    }
93

94
}
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