Skip to content

Commit

Permalink
Use abstract request/response classes
Browse files Browse the repository at this point in the history
  • Loading branch information
gregw committed May 29, 2024
1 parent e9cb9a9 commit d6986ee
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 224 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.springframework.http.server.reactive;

import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.security.cert.X509Certificate;
Expand All @@ -37,23 +36,20 @@
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.server.RequestPath;
import org.springframework.http.support.JettyHeadersAdapter;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;

import static org.springframework.http.server.reactive.AbstractServerHttpRequest.QUERY_PATTERN;

/**
* Adapt an Eclipse Jetty {@link Request} to a {@link org.springframework.http.server.ServerHttpRequest}.
*
* @author Greg Wilkins
* @since 6.2
*/
class JettyCoreServerHttpRequest implements ServerHttpRequest {
class JettyCoreServerHttpRequest extends AbstractServerHttpRequest {
private static final MultiValueMap<String, String> EMPTY_QUERY = CollectionUtils.unmodifiableMultiValueMap(new LinkedMultiValueMap<>());

private static final MultiValueMap<String, HttpCookie> EMPTY_COOKIES = CollectionUtils.unmodifiableMultiValueMap(new LinkedMultiValueMap<>());
Expand All @@ -62,120 +58,80 @@ class JettyCoreServerHttpRequest implements ServerHttpRequest {

private final Request request;

private final HttpHeaders headers;

private final RequestPath path;

@Nullable
private URI uri;

@Nullable
MultiValueMap<String, String> queryParameters;

@Nullable
private MultiValueMap<String, HttpCookie> cookies;

public JettyCoreServerHttpRequest(DataBufferFactory dataBufferFactory, Request request) {
super(HttpMethod.valueOf(request.getMethod()),
request.getHttpURI().toURI(),
request.getContext().getContextPath(),
new HttpHeaders(new JettyHeadersAdapter(request.getHeaders())));
this.dataBufferFactory = dataBufferFactory;
this.request = request;
this.headers = new HttpHeaders(new JettyHeadersAdapter(request.getHeaders()));
this.path = RequestPath.parse(request.getHttpURI().getPath(), request.getContext().getContextPath());
}

@Override
public HttpHeaders getHeaders() {
return this.headers;
}

@Override
public HttpMethod getMethod() {
return HttpMethod.valueOf(this.request.getMethod());
}

@Override
public URI getURI() {
if (this.uri == null) {
this.uri = this.request.getHttpURI().toURI();
}
return this.uri;
}

@Override
public Flux<DataBuffer> getBody() {
// We access the request body as a Flow.Publisher, which is wrapped as an org.reactivestreams.Publisher and
// then wrapped as a Flux.
return Flux.from(FlowAdapters.toPublisher(Content.Source.asPublisher(this.request)))
.map(this::wrap);
return Flux.from(FlowAdapters.toPublisher(Content.Source.asPublisher(this.request))).map(this::chunkToDataBuffer);
}

private DataBuffer wrap(Content.Chunk chunk) {
private DataBuffer chunkToDataBuffer(Content.Chunk chunk) {
return new JettyRetainedDataBuffer(this.dataBufferFactory, chunk);
}

@Override
public String getId() {
protected String initId() {
return this.request.getId();
}

@Override
public RequestPath getPath() {
return this.path;
}
protected MultiValueMap<String, String> initQueryParams() {
String query = this.request.getHttpURI().getQuery();
if (StringUtil.isBlank(query)) {
return EMPTY_QUERY;
}

@Override
public MultiValueMap<String, String> getQueryParams() {
if (this.queryParameters == null) {
String query = this.request.getHttpURI().getQuery();
if (StringUtil.isBlank(query)) {
this.queryParameters = EMPTY_QUERY;
}
else {
this.queryParameters = new LinkedMultiValueMap<>();
Matcher matcher = QUERY_PATTERN.matcher(query);
while (matcher.find()) {
String name = URLDecoder.decode(matcher.group(1), StandardCharsets.UTF_8);
String eq = matcher.group(2);
String value = matcher.group(3);
value = (value != null ? URLDecoder.decode(value, StandardCharsets.UTF_8) : (StringUtils.hasLength(eq) ? "" : null));
this.queryParameters.add(name, value);
}
}
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
Matcher matcher = QUERY_PATTERN.matcher(query);
while (matcher.find()) {
String name = URLDecoder.decode(matcher.group(1), StandardCharsets.UTF_8);
String eq = matcher.group(2);
String value = matcher.group(3);
value = (value != null ? URLDecoder.decode(value, StandardCharsets.UTF_8) : (StringUtils.hasLength(eq) ? "" : null));
map.add(name, value);
}
return this.queryParameters;
return map;
}

@Override
public MultiValueMap<String, HttpCookie> getCookies() {
if (this.cookies == null) {
List<org.eclipse.jetty.http.HttpCookie> httpCookies = Request.getCookies(this.request);
if (httpCookies.isEmpty()) {
this.cookies = EMPTY_COOKIES;
}
else {
this.cookies = new LinkedMultiValueMap<>();
for (org.eclipse.jetty.http.HttpCookie c : httpCookies) {
this.cookies.add(c.getName(), new HttpCookie(c.getName(), c.getValue()));
}
this.cookies = CollectionUtils.unmodifiableMultiValueMap(this.cookies);
}
protected MultiValueMap<String, HttpCookie> initCookies() {
List<org.eclipse.jetty.http.HttpCookie> httpCookies = Request.getCookies(this.request);
if (httpCookies.isEmpty()) {
return EMPTY_COOKIES;
}

MultiValueMap<String, HttpCookie> map =new LinkedMultiValueMap<>();
for (org.eclipse.jetty.http.HttpCookie c : httpCookies) {
map.add(c.getName(), new HttpCookie(c.getName(), c.getValue()));
}
return this.cookies;

return map;
}

@Override
@Nullable
public InetSocketAddress getLocalAddress() {
return this.request.getConnectionMetaData().getLocalSocketAddress() instanceof InetSocketAddress inet
? inet : null;
return this.request.getConnectionMetaData().getLocalSocketAddress() instanceof InetSocketAddress inet ? inet : null;
}

@Override
@Nullable
public InetSocketAddress getRemoteAddress() {
return this.request.getConnectionMetaData().getRemoteSocketAddress() instanceof InetSocketAddress inet
? inet : null;
return this.request.getConnectionMetaData().getRemoteSocketAddress() instanceof InetSocketAddress inet ? inet : null;
}

@Override
public SslInfo getSslInfo() {
@Nullable
public SslInfo initSslInfo() {
if (this.request.getConnectionMetaData().isSecure() && this.request.getAttribute(EndPoint.SslSessionData.ATTRIBUTE) instanceof EndPoint.SslSessionData sslSessionData) {
return new SslInfo() {
@Override
Expand Down
Loading

0 comments on commit d6986ee

Please sign in to comment.