Skip to content

Commit 8a5fdc8

Browse files
committed
Share character filtering logic and sanitize async input during copying
1 parent a1643d4 commit 8a5fdc8

4 files changed

Lines changed: 31 additions & 13 deletions

File tree

‎httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/SessionOutputBufferImpl.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.apache.hc.core5.util.Args;
4343
import org.apache.hc.core5.util.ByteArrayBuffer;
4444
import org.apache.hc.core5.util.CharArrayBuffer;
45+
import org.apache.hc.core5.util.TextUtils;
4546

4647
/**
4748
* Abstract base class for session output buffers that stream data to
@@ -214,7 +215,7 @@ public void writeLine(final CharArrayBuffer charbuffer, final OutputStream outpu
214215
char[] sanitized = null;
215216
for (int i = 0; i < length; i++) {
216217
final char ch = chars[i];
217-
if (ch < 0x20 && ch != '\t' || ch >= 0x7f && ch <= 0x9f) {
218+
if (!TextUtils.isPrintable(ch)) {
218219
if (sanitized == null) {
219220
sanitized = Arrays.copyOf(chars, length);
220221
}

‎httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/SessionOutputBufferImpl.java‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,10 @@ public void writeLine(final CharArrayBuffer lineBuffer) throws CharacterCodingEx
198198
// terminate the encoding process
199199
eol = true;
200200
}
201-
// Filter controls while copying into the encoder's staging buffer.
202-
final int start = this.charbuffer.position();
203-
this.charbuffer.put(lineBuffer.array(), offset, l);
204-
final int end = this.charbuffer.position();
205-
for (int i = start; i < end; i++) {
206-
final char ch = this.charbuffer.get(i);
207-
if (ch < 0x20 && ch != '\t' || ch >= 0x7f && ch <= 0x9f) {
208-
this.charbuffer.put(i, '?');
209-
}
201+
// Filter control characters while copying into the encoder buffer.
202+
for (int i = 0; i < l; i++) {
203+
final char ch = lineBuffer.charAt(offset + i);
204+
this.charbuffer.put(TextUtils.isPrintable(ch) ? ch : '?');
210205
}
211206
this.charbuffer.flip();
212207

‎httpcore5/src/main/java/org/apache/hc/core5/util/TextUtils.java‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,7 @@ public static boolean isAllASCII(final CharSequence s) {
180180
*/
181181
@Internal
182182
public static byte castAsByte(final int c) {
183-
if (c >= 0x20 && c <= 0x7E || // Visible ASCII
184-
c >= 0xA0 && c <= 0xFF || // Visible ISO-8859-1
185-
c == 0x09) { // TAB
183+
if (c <= 0xFF && isPrintable(c)) {
186184
return (byte) c;
187185
}
188186
return '?';
@@ -204,5 +202,18 @@ public static boolean isHex(final char c) {
204202
|| c >= 'a' && c <= 'f';
205203
}
206204

205+
/**
206+
* Tests whether a character can be written to an HTTP protocol line without
207+
* replacing it. Horizontal tab is permitted for compatibility.
208+
*
209+
* @param c The character to test.
210+
* @return {@code true} if the character does not need filtering.
211+
* @since 5.5
212+
*/
213+
@Internal
214+
public static boolean isPrintable(final int c) {
215+
return c == 0x09 || c >= 0x20 && (c < 0x7F || c > 0x9F);
216+
}
217+
207218

208219
}

‎httpcore5/src/test/java/org/apache/hc/core5/util/TestTextUtils.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,15 @@ void testToHexString() {
6767
Assertions.assertNull(TextUtils.toHexString(null));
6868
}
6969

70+
@Test
71+
void testPrintableCharactersAndByteConversion() {
72+
for (int c = 0; c <= 0xFF; c++) {
73+
final boolean expected = c == '\t' || c >= 0x20 && c <= 0x7E || c >= 0xA0;
74+
Assertions.assertEquals(expected, TextUtils.isPrintable(c), "Character: " + c);
75+
Assertions.assertEquals(expected ? (byte) c : (byte) '?', TextUtils.castAsByte(c), "Character: " + c);
76+
}
77+
Assertions.assertTrue(TextUtils.isPrintable(0x20AC));
78+
Assertions.assertEquals((byte) '?', TextUtils.castAsByte(0x20AC));
79+
}
80+
7081
}

0 commit comments

Comments
 (0)