use of org.springframework.http.server.ServletServerHttpRequest in project spring-framework by spring-projects.
the class AbstractRequestLoggingFilter method createMessage.
/**
* Create a log message for the given request, prefix and suffix.
* <p>If {@code includeQueryString} is {@code true}, then the inner part
* of the log message will take the form {@code request_uri?query_string};
* otherwise the message will simply be of the form {@code request_uri}.
* <p>The final message is composed of the inner part as described and
* the supplied prefix and suffix.
*/
protected String createMessage(HttpServletRequest request, String prefix, String suffix) {
StringBuilder msg = new StringBuilder();
msg.append(prefix);
msg.append("uri=").append(request.getRequestURI());
if (isIncludeQueryString()) {
String queryString = request.getQueryString();
if (queryString != null) {
msg.append('?').append(queryString);
}
}
if (isIncludeClientInfo()) {
String client = request.getRemoteAddr();
if (StringUtils.hasLength(client)) {
msg.append(";client=").append(client);
}
HttpSession session = request.getSession(false);
if (session != null) {
msg.append(";session=").append(session.getId());
}
String user = request.getRemoteUser();
if (user != null) {
msg.append(";user=").append(user);
}
}
if (isIncludeHeaders()) {
msg.append(";headers=").append(new ServletServerHttpRequest(request).getHeaders());
}
if (isIncludePayload()) {
ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
if (wrapper != null) {
byte[] buf = wrapper.getContentAsByteArray();
if (buf.length > 0) {
int length = Math.min(buf.length, getMaxPayloadLength());
String payload;
try {
payload = new String(buf, 0, length, wrapper.getCharacterEncoding());
} catch (UnsupportedEncodingException ex) {
payload = "[unknown]";
}
msg.append(";payload=").append(payload);
}
}
}
msg.append(suffix);
return msg.toString();
}
use of org.springframework.http.server.ServletServerHttpRequest in project spring-framework by spring-projects.
the class WebUtils method isSameOrigin.
/**
* Check if the request is a same-origin one, based on {@code Origin}, {@code Host},
* {@code Forwarded} and {@code X-Forwarded-Host} headers.
* @return {@code true} if the request is a same-origin one, {@code false} in case
* of cross-origin request.
* @since 4.2
*/
public static boolean isSameOrigin(HttpRequest request) {
String origin = request.getHeaders().getOrigin();
if (origin == null) {
return true;
}
UriComponentsBuilder urlBuilder;
if (request instanceof ServletServerHttpRequest) {
// Build more efficiently if we can: we only need scheme, host, port for origin comparison
HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
urlBuilder = new UriComponentsBuilder().scheme(servletRequest.getScheme()).host(servletRequest.getServerName()).port(servletRequest.getServerPort()).adaptFromForwardedHeaders(request.getHeaders());
} else {
urlBuilder = UriComponentsBuilder.fromHttpRequest(request);
}
UriComponents actualUrl = urlBuilder.build();
UriComponents originUrl = UriComponentsBuilder.fromOriginHeader(origin).build();
return (actualUrl.getHost().equals(originUrl.getHost()) && getPort(actualUrl) == getPort(originUrl));
}
use of org.springframework.http.server.ServletServerHttpRequest in project spring-framework by spring-projects.
the class ObjectToStringHttpMessageConverterTests method read.
@Test
public void read() throws IOException {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setContentType(MediaType.TEXT_PLAIN_VALUE);
Short shortValue = Short.valueOf((short) 781);
request.setContent(shortValue.toString().getBytes(StringHttpMessageConverter.DEFAULT_CHARSET));
assertEquals(shortValue, this.converter.read(Short.class, new ServletServerHttpRequest(request)));
Float floatValue = Float.valueOf(123);
request.setCharacterEncoding("UTF-16");
request.setContent(floatValue.toString().getBytes("UTF-16"));
assertEquals(floatValue, this.converter.read(Float.class, new ServletServerHttpRequest(request)));
Long longValue = Long.valueOf(55819182821331L);
request.setCharacterEncoding("UTF-8");
request.setContent(longValue.toString().getBytes("UTF-8"));
assertEquals(longValue, this.converter.read(Long.class, new ServletServerHttpRequest(request)));
}
use of org.springframework.http.server.ServletServerHttpRequest in project spring-framework by spring-projects.
the class UriComponentsBuilderTests method fromHttpRequestForwardedHeader.
// SPR-11856
@Test
public void fromHttpRequestForwardedHeader() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("Forwarded", "proto=https; host=84.198.58.199");
request.setScheme("http");
request.setServerName("example.com");
request.setRequestURI("/rest/mobile/users/1");
HttpRequest httpRequest = new ServletServerHttpRequest(request);
UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
assertEquals("https", result.getScheme());
assertEquals("84.198.58.199", result.getHost());
assertEquals("/rest/mobile/users/1", result.getPath());
}
use of org.springframework.http.server.ServletServerHttpRequest in project spring-framework by spring-projects.
the class UriComponentsBuilderTests method fromHttpRequestWithForwardedPortMultiValueHeader.
// SPR-12813
@Test
public void fromHttpRequestWithForwardedPortMultiValueHeader() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setScheme("http");
request.setServerName("localhost");
request.setServerPort(9090);
request.setRequestURI("/mvc-showcase");
request.addHeader("X-Forwarded-Host", "a.example.org");
request.addHeader("X-Forwarded-Port", "80,52022");
HttpRequest httpRequest = new ServletServerHttpRequest(request);
UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
assertEquals("http://a.example.org/mvc-showcase", result.toString());
}
Aggregations