Merge branch 'pr/14611'

This commit is contained in:
Daniel Mitterdorfer 2015-11-09 12:08:56 +01:00
commit 1c5b0b3c5b
2 changed files with 21 additions and 1 deletions

View File

@ -273,8 +273,14 @@ public class XContentFactory {
return XContentType.CBOR;
}
int jsonStart = 0;
// JSON may be preceded by UTF-8 BOM
if (length > 3 && first == (byte) 0xEF && bytes.get(1) == (byte) 0xBB && bytes.get(2) == (byte) 0xBF) {
jsonStart = 3;
}
// a last chance for JSON
for (int i = 0; i < length; i++) {
for (int i = jsonStart; i < length; i++) {
byte b = bytes.get(i);
if (b == '{') {
return XContentType.JSON;

View File

@ -97,4 +97,18 @@ public class XContentFactoryTests extends ESTestCase {
is = new ByteArrayInputStream(new byte[] {(byte) 1});
assertNull(XContentFactory.xContentType(is));
}
public void testJsonFromBytesOptionallyPrecededByUtf8Bom() throws Exception {
byte[] bytes = new byte[] {(byte) '{', (byte) '}'};
assertThat(XContentFactory.xContentType(bytes), equalTo(XContentType.JSON));
bytes = new byte[] {(byte) 0x20, (byte) '{', (byte) '}'};
assertThat(XContentFactory.xContentType(bytes), equalTo(XContentType.JSON));
bytes = new byte[] {(byte) 0xef, (byte) 0xbb, (byte) 0xbf, (byte) '{', (byte) '}'};
assertThat(XContentFactory.xContentType(bytes), equalTo(XContentType.JSON));
bytes = new byte[] {(byte) 0xef, (byte) 0xbb, (byte) 0xbf, (byte) 0x20, (byte) '{', (byte) '}'};
assertThat(XContentFactory.xContentType(bytes), equalTo(XContentType.JSON));
}
}