Enforce UTF-8 response encoding in SseEmitter
This commit sets the response content type to "text/event-stream;charset=UTF-8". Even if the SSE spec says that the encoding should always be UTF-8 and that specifying the charset is optional, we're setting it in all cases, since some containers might use the default encoding "ISO-8859-1" and confuse HTTP clients. Issue: SPR-14407
This commit is contained in:
parent
15c96b8efd
commit
a25c43f695
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -38,6 +38,8 @@ public class SseEmitter extends ResponseBodyEmitter {
|
|||
|
||||
static final MediaType TEXT_PLAIN = new MediaType("text", "plain", Charset.forName("UTF-8"));
|
||||
|
||||
static final MediaType UTF8_TEXT_EVENTSTREAM = new MediaType("text", "event-stream", Charset.forName("UTF-8"));
|
||||
|
||||
|
||||
/**
|
||||
* Create a new SseEmitter instance.
|
||||
|
@ -65,7 +67,7 @@ public class SseEmitter extends ResponseBodyEmitter {
|
|||
|
||||
HttpHeaders headers = outputMessage.getHeaders();
|
||||
if (headers.getContentType() == null) {
|
||||
headers.setContentType(new MediaType("text", "event-stream"));
|
||||
headers.setContentType(UTF8_TEXT_EVENTSTREAM);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -148,7 +148,7 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
|
|||
|
||||
assertTrue(this.request.isAsyncStarted());
|
||||
assertEquals(200, this.response.getStatus());
|
||||
assertEquals("text/event-stream", this.response.getContentType());
|
||||
assertEquals("text/event-stream;charset=UTF-8", this.response.getContentType());
|
||||
|
||||
SimpleBean bean1 = new SimpleBean();
|
||||
bean1.setId(1L);
|
||||
|
@ -178,7 +178,7 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
|
|||
|
||||
assertTrue(this.request.isAsyncStarted());
|
||||
assertEquals(200, this.response.getStatus());
|
||||
assertEquals("text/event-stream", this.response.getContentType());
|
||||
assertEquals("text/event-stream;charset=UTF-8", this.response.getContentType());
|
||||
assertEquals("bar", this.response.getHeader("foo"));
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue