Parse byte array as cookie header. @param bytes Source @param offset Start point in array @param len Number of bytes to read @param serverCookies Structure to store results @param cookiesWithoutEquals How to handle a cookie name-value-pair that d
(byte[] bytes, int offset, int len, ServerCookies serverCookies,
CookiesWithoutEquals cookiesWithoutEquals)
| 92 | * @param cookiesWithoutEquals How to handle a cookie name-value-pair that does not contain an equals character |
| 93 | */ |
| 94 | public static void parseCookie(byte[] bytes, int offset, int len, ServerCookies serverCookies, |
| 95 | CookiesWithoutEquals cookiesWithoutEquals) { |
| 96 | |
| 97 | // ByteBuffer is used throughout this parser as it allows the byte[] |
| 98 | // and position information to be easily passed between parsing methods |
| 99 | ByteBuffer bb = new ByteBuffer(bytes, offset, len); |
| 100 | |
| 101 | boolean moreToProcess = true; |
| 102 | |
| 103 | while (moreToProcess) { |
| 104 | skipLWS(bb); |
| 105 | |
| 106 | int start = bb.position(); |
| 107 | ByteBuffer name = readToken(bb); |
| 108 | ByteBuffer value = null; |
| 109 | |
| 110 | skipLWS(bb); |
| 111 | |
| 112 | SkipResult skipResult = skipByte(bb, EQUALS_BYTE); |
| 113 | if (skipResult == SkipResult.FOUND) { |
| 114 | skipLWS(bb); |
| 115 | value = readCookieValueRfc6265(bb); |
| 116 | if (value == null) { |
| 117 | // Invalid cookie value. Skip to the next semicolon |
| 118 | skipUntilSemiColon(bb); |
| 119 | logInvalidHeader(start, bb); |
| 120 | continue; |
| 121 | } |
| 122 | skipLWS(bb); |
| 123 | } |
| 124 | |
| 125 | skipResult = skipByte(bb, SEMICOLON_BYTE); |
| 126 | if (skipResult == SkipResult.FOUND) { |
| 127 | // NO-OP |
| 128 | } else if (skipResult == SkipResult.NOT_FOUND) { |
| 129 | // Invalid cookie. Ignore it and skip to the next semicolon |
| 130 | skipUntilSemiColon(bb); |
| 131 | logInvalidHeader(start, bb); |
| 132 | continue; |
| 133 | } else { |
| 134 | // SkipResult.EOF |
| 135 | moreToProcess = false; |
| 136 | } |
| 137 | |
| 138 | if (name.hasRemaining()) { |
| 139 | if (value == null) { |
| 140 | switch (cookiesWithoutEquals) { |
| 141 | case IGNORE: { |
| 142 | // This name-value-pair is a NO-OP |
| 143 | break; |
| 144 | } |
| 145 | case NAME: { |
| 146 | ServerCookie sc = serverCookies.addCookie(); |
| 147 | sc.getName().setBytes(name.array(), name.position(), name.remaining()); |
| 148 | sc.getValue().setBytes(EMPTY_BYTES, 0, EMPTY_BYTES.length); |
| 149 | break; |
| 150 | } |
| 151 | } |
no test coverage detected