HttpUtil.java
1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.github.monkeywie.proxyee.util;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.util.AsciiString;
public class HttpUtil {
/**
* 检测url是否匹配
*/
public static boolean checkUrl(HttpRequest httpRequest, String regex) {
String host = httpRequest.headers().get(HttpHeaderNames.HOST);
if (host != null && regex != null) {
String url;
if (httpRequest.uri().indexOf("/") == 0) {
if (httpRequest.uri().length() > 1) {
url = host + httpRequest.uri();
} else {
url = host;
}
} else {
url = httpRequest.uri();
}
return url.matches(regex);
}
return false;
}
/**
* 检测头中的值是否为预期
*
* @param httpHeaders
* @param name
* @param regex
* @return
*/
public static boolean checkHeader(HttpHeaders httpHeaders, AsciiString name, String regex) {
String s = httpHeaders.get(name);
return s != null && s.matches(regex);
}
/**
* 检测是否为请求网页资源
*/
public static boolean isHtml(HttpRequest httpRequest, HttpResponse httpResponse) {
String accept = httpRequest.headers().get(HttpHeaderNames.ACCEPT);
String contentType = httpResponse.headers().get(HttpHeaderNames.CONTENT_TYPE);
return httpResponse.status().code() == 200 && accept != null && accept
.matches("^.*text/html.*$") && contentType != null && contentType
.matches("^text/html.*$");
}
}