下面列出了io.netty.channel.socket.SocketChannel#localAddress ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
static ConnectionInfo parseForwardedInfo(String forwardedHeader, SocketChannel channel, boolean secured,
InetSocketAddress remoteAddress) {
InetSocketAddress hostAddress = channel.localAddress();
String scheme = secured ? "https" : "http";
String forwarded = forwardedHeader.split(",", 2)[0];
Matcher hostMatcher = FORWARDED_HOST_PATTERN.matcher(forwarded);
if (hostMatcher.find()) {
hostAddress = parseAddress(hostMatcher.group(1), hostAddress.getPort());
}
Matcher protoMatcher = FORWARDED_PROTO_PATTERN.matcher(forwarded);
if (protoMatcher.find()) {
scheme = protoMatcher.group(1).trim();
}
Matcher forMatcher = FORWARDED_FOR_PATTERN.matcher(forwarded);
if (forMatcher.find()) {
remoteAddress = parseAddress(forMatcher.group(1).trim(), remoteAddress.getPort());
}
return new ConnectionInfo(hostAddress, remoteAddress, scheme);
}
/**
* Retrieve the connection information from the current connection directly
* @param c the current channel
* @param secured is transport secure (SSL)
* @return the connection information
*/
static ConnectionInfo newConnectionInfo(Channel c, boolean secured, InetSocketAddress remoteAddress) {
SocketChannel channel = (SocketChannel) c;
InetSocketAddress hostAddress = channel.localAddress();
String scheme = secured ? "https" : "http";
return new ConnectionInfo(hostAddress, remoteAddress, scheme);
}
static ConnectionInfo parseXForwardedInfo(HttpRequest request, SocketChannel channel, boolean secured,
InetSocketAddress remoteAddress) {
InetSocketAddress hostAddress = channel.localAddress();
String scheme = secured ? "https" : "http";
String ipHeader = request.headers().get(XFORWARDED_IP_HEADER);
if (ipHeader != null) {
remoteAddress = parseAddress(ipHeader.split(",", 2)[0], remoteAddress.getPort());
}
String hostHeader = request.headers().get(XFORWARDED_HOST_HEADER);
if (hostHeader != null) {
String portHeader = request.headers().get(XFORWARDED_PORT_HEADER);
if (portHeader != null) {
int port;
try {
port = Integer.parseInt(portHeader.split(",", 2)[0].trim());
}
catch (NumberFormatException e) {
log.debug(format(channel, "Invalid value [" + portHeader + "] for the header [X-Forwarded-Port]"));
port = hostAddress.getPort();
}
hostAddress = AddressUtils.createUnresolved(
hostHeader.split(",", 2)[0].trim(), port);
}
else {
hostAddress = AddressUtils.createUnresolved(
hostHeader.split(",", 2)[0].trim(),
hostAddress.getPort());
}
}
String protoHeader = request.headers().get(XFORWARDED_PROTO_HEADER);
if (protoHeader != null) {
scheme = protoHeader.split(",", 2)[0].trim();
}
return new ConnectionInfo(hostAddress, remoteAddress, scheme);
}
@Override
protected CC initRemoteConnection(SocketChannel channel){
local=channel.localAddress();
remote=channel.remoteAddress();
return null;
}
@Override
protected SC initRemoteConnection(SocketChannel channel) {
local = channel.localAddress();
remote = channel.remoteAddress();
return null;
}