11package love .wangqi .codec ;
22
3+ import io .netty .handler .codec .http .HttpMethod ;
34import io .netty .handler .codec .http .HttpRequest ;
45import io .netty .handler .codec .http .multipart .HttpPostRequestEncoder ;
56import love .wangqi .route .Route ;
67
8+ import java .net .InetSocketAddress ;
9+ import java .net .MalformedURLException ;
710import java .net .URL ;
11+ import java .util .Arrays ;
12+
13+ import static love .wangqi .context .Constants .HTTP ;
14+ import static love .wangqi .context .Constants .HTTPS ;
815
916/**
1017 * @author: wangqi
@@ -23,4 +30,62 @@ public RequestHolder(Route route, URL url, HttpRequest request, HttpPostRequestE
2330 this .request = request ;
2431 this .bodyRequestEncoder = bodyRequestEncoder ;
2532 }
33+
34+ public String getHost () {
35+ if (url .getHost () == null ) {
36+ throw new RuntimeException ("no host found" );
37+ }
38+ return url .getHost ();
39+ }
40+
41+ public int getPort () {
42+ String protocol = url .getProtocol () == null ? HTTP : url .getProtocol ();
43+ int port = url .getPort ();
44+ if (port == -1 ) {
45+ if (HTTP .equalsIgnoreCase (protocol )) {
46+ port = 80 ;
47+ } else if (HTTPS .equalsIgnoreCase (protocol )) {
48+ port = 443 ;
49+ }
50+ }
51+ return port ;
52+ }
53+
54+ public String getProtocol () {
55+ return url .getProtocol ();
56+ }
57+
58+ public InetSocketAddress getSocketAddress () {
59+ return new InetSocketAddress (getHost (), getPort ());
60+ }
61+
62+ @ Override
63+ public int hashCode () {
64+ return Arrays .hashCode (new Object []{route , url });
65+ }
66+
67+ @ Override
68+ public boolean equals (Object obj ) {
69+ if (this == obj ) {
70+ return true ;
71+ }
72+ if (obj instanceof RequestHolder ) {
73+ return this .route .equals (((RequestHolder ) obj ).route ) && this .url .equals (((RequestHolder ) obj ).url );
74+ }
75+ return false ;
76+ }
77+
78+ public static void main (String [] args ) throws MalformedURLException {
79+ RequestHolder requestHolder1 = new RequestHolder (new Route (9L , HttpMethod .GET , "/html" , new URL ("http://10.100.64.71/html/user.json" )), new URL ("http://10.100.64.71/html/user.json" ), null , null );
80+ RequestHolder requestHolder2 = new RequestHolder (new Route (9L , HttpMethod .GET , "/html" , new URL ("http://10.100.64.71/html/user.json" )), new URL ("http://10.100.64.71/html/user.json" ), null , null );
81+ System .out .println (requestHolder1 .hashCode ());
82+ System .out .println (requestHolder2 .hashCode ());
83+ System .out .println (requestHolder1 .equals (requestHolder2 ));
84+
85+ InetSocketAddress socketAddress1 = new InetSocketAddress ("127.0.0.1" , 80 );
86+ InetSocketAddress socketAddress2 = new InetSocketAddress ("127.0.0.1" , 80 );
87+ System .out .println (socketAddress1 .hashCode ());
88+ System .out .println (socketAddress2 .hashCode ());
89+ System .out .println (socketAddress1 .equals (socketAddress2 ));
90+ }
2691}
0 commit comments