forked from laomafeima/WebJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequest.java
More file actions
93 lines (80 loc) · 2.64 KB
/
Request.java
File metadata and controls
93 lines (80 loc) · 2.64 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package Web;
import java.util.HashMap;
import java.util.StringTokenizer;
/**
* 处理接受到的数据
*
* @author Ma
*/
public class Request {
public HashMap Header;
public HashMap Arguments;
public HashMap Cookies;
public String Path;
public String Method;
public String RemoteIP;
/**
* 处理接收数据
* @param receive 接受
*/
public Request(String receive) {
StringTokenizer stringTokenizer = new StringTokenizer(receive, "\n");
String[] receiveArray = new String[stringTokenizer.countTokens()];
HashMap<String, String> hashMap = new HashMap<String, String>();
this.Header = new HashMap();
this.Arguments = new HashMap();
this.Cookies = new HashMap();
String[] URLStrings = stringTokenizer.nextToken().split(" ");
this.Method = URLStrings[0];//请求方式
String href = URLStrings[1]; //URL
String[] hrefSplit = href.split("\\?", 2);
this.Path = hrefSplit[0];
//分析URL传值
if (hrefSplit.length > 1) {
String query = hrefSplit[1];
this.Arguments = ResolveQuery.query(query);
}
while (stringTokenizer.hasMoreTokens()) {
String temp = stringTokenizer.nextToken().trim();
//长度小于1说明是个空白行,说明header可以结束了,如果是POST请求下面的就是参数了
if (temp.length() < 1) {
break;
}
String[] split = temp.split(":", 2);
if (split.length < 2) {
;
}
if (split[0].toLowerCase().equals("cookie")) {
//解析Cookie,不记录到Header中
Cookie.analysis(split[1].trim());
continue;
}
this.Header.put(split[0], split[1].trim());
}
while (stringTokenizer.hasMoreTokens()) {
String temp = stringTokenizer.nextToken().trim();
//长度小于1说明是个空白行,说明header可以结束了,如果是POST请求下面的就是参数了
if (temp.length() < 1) {
continue;
}
this.Arguments.putAll(ResolveQuery.query(temp));
}
}
/**
* 获取用户远程IP
* @return 远程IP
*/
public String getRemoteIP() {
if(this.Header.containsKey("True-ip")){
this.RemoteIP = (String) this.Header.get("True-ip");
}
return this.RemoteIP;
}
/**
* 设置远程IP
* @param RemoteIP
*/
public void setRemoteIP(String RemoteIP) {
this.RemoteIP = RemoteIP;
}
}