Skip to content

Commit

Permalink
代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
smthing committed Nov 14, 2024
1 parent 60bfb47 commit e2282c5
Showing 1 changed file with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ public class HttpServletRequestImpl implements SmartHttpServletRequest {

private ServletMappingInfo servletMappingInfo;
private LoginAccount principal;
private final InetSocketAddress remoteAddress;
private final InetSocketAddress localAddress;


public HttpServletRequestImpl(HttpRequest request, ServletContextRuntime runtime, CompletableFuture<Object> completableFuture) {
this.request = request;
Expand All @@ -133,6 +136,8 @@ public HttpServletRequestImpl(HttpRequest request, ServletContextRuntime runtime
this.requestUri = request.getRequestURI().substring(0, index);
this.requestedSessionId = request.getRequestURI().substring(index + URL_JSESSION_ID.length());
}
this.remoteAddress = request.getRemoteAddress();
this.localAddress = request.getLocalAddress();
}

public void setHttpServletResponse(HttpServletResponse httpServletResponse) {
Expand Down Expand Up @@ -680,7 +685,7 @@ public String getScheme() {
public String getServerName() {
String host = getHeader(HeaderNameEnum.HOST.getName());
if (StringUtils.isBlank(host)) {
return request.getLocalAddress().getHostName();
return localAddress.getHostName();
}
int index = host.indexOf(":");
if (index < 0) {
Expand All @@ -698,7 +703,7 @@ public int getServerPort() {
}
int index = host.indexOf(":");
if (index < 0) {
return request.getRemoteAddress().getPort();
return localAddress.getPort();
} else {
return NumberUtils.toInt(host.substring(index + 1), -1);
}
Expand All @@ -722,12 +727,12 @@ public BufferedReader getReader() throws IOException {

@Override
public String getRemoteAddr() {
return getAddress(request.getRemoteAddress());
return getAddress(remoteAddress);
}

@Override
public String getRemoteHost() {
return request.getRemoteAddress().getHostString();
return remoteAddress.getHostString();
}

@Override
Expand Down Expand Up @@ -783,17 +788,17 @@ public RequestDispatcher getRequestDispatcher(String path) {

@Override
public int getRemotePort() {
return request.getRemoteAddress().getPort();
return remoteAddress.getPort();
}

@Override
public String getLocalName() {
return request.getLocalAddress().getHostString();
return localAddress.getHostString();
}

@Override
public String getLocalAddr() {
return getAddress(request.getLocalAddress());
return getAddress(localAddress);
}

private String getAddress(InetSocketAddress inetSocketAddress) {
Expand All @@ -805,7 +810,7 @@ private String getAddress(InetSocketAddress inetSocketAddress) {

@Override
public int getLocalPort() {
return request.getLocalAddress().getPort();
return localAddress.getPort();
}

public void setServletContext(ServletContextImpl servletContext) {
Expand Down

0 comments on commit e2282c5

Please sign in to comment.