-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPage.java
More file actions
202 lines (181 loc) · 4.06 KB
/
Copy pathPage.java
File metadata and controls
202 lines (181 loc) · 4.06 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package WebCrawler;
import java.util.HashMap;
import java.util.LinkedList;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
/**
* The Class Page.
*/
public class Page
{
/** The header. */
private String headers;
/** Store all headers in a hash map. */
private HashMap<String, String> header;
/** The protocol. */
private String protocol;
/** The response code. */
private int responseCode;
/** The body. */
private Document body;
/**
* Instantiates a new page.
* Read all content get from the website, separate headers and body
* @param content the content
*/
public Page(String content)
{
if (content != null)
{
headers = content.substring(0, content.indexOf("\n\n")).trim();
header = new HashMap<String, String>();
processHeader(headers);
body = Jsoup.parse(content.substring(content.indexOf("\n\n")).trim());
}
}
/**
* Process header.
* Read each header and store them in the header collection
* @param headers the headers
*/
private void processHeader(String headers)
{
String delimsNewLine = "\n+";
String[] headPerLine = headers.split(delimsNewLine);
String delimsSpace = "[ ]+";
String[] headFirstLine = headPerLine[0].split(delimsSpace);
responseCode = Integer.parseInt(headFirstLine[1]);
protocol = headFirstLine[0];
for (int i = 1; i < headPerLine.length; i++)
{
String key = headPerLine[i].substring(0, headPerLine[i].indexOf(":")).trim();
String value = headPerLine[i].substring(headPerLine[i].indexOf(":") + 2).trim();
if (header.containsKey(key))
{
value = header.get(key) + "||" + value; // Just for cookies
}
header.put(key, value);
}
}
/**
* Gets the response code.
*
* @return the response code
*/
public int getResponseCode()
{
return responseCode;
}
/**
* Gets the protocol.
*
* @return the protocol
*/
public String getProtocol()
{
return protocol;
}
/**
* Gets the secret flags.
*
* @return the secret flags
*/
public LinkedList<String> getSecretFlags()
{
Elements secretFlagsEle = body.select("h2.secret_flag");
LinkedList<String> flags = new LinkedList<String>();
for (Element flag : secretFlagsEle)
{
//System.out.println(flag.text());
flags.add(flag.text());
}
return flags;
}
/**
* Gets the links.
*
* @return the links
*/
public LinkedList<String> getLinks()
{
Elements linksEle = body.select("a[href]");
LinkedList<String> links = new LinkedList<String>();
for (Element link : linksEle)
{
links.add(link.attr("href"));
}
return links;
}
/**
* Gets the csrf code.
*
* @return the csrf code
*/
public String getCsrfCode()
{
Elements inputElements = body.getElementsByTag("input");
for (Element inputElement : inputElements)
{
String nameOfInputElement = inputElement.attr("name");
if (nameOfInputElement.equals("csrfmiddlewaretoken"))
{
return inputElement.attr("value");
}
}
return null;
}
/**
* Gets the cookies.
*
* @return the cookies
*/
public LinkedList<Cookie> getCookies()
{
LinkedList<Cookie> cookieList = new LinkedList<Cookie>();
for (HashMap.Entry<String, String> entry : header.entrySet())
{
String key = entry.getKey();
if (key.equals("Set-Cookie"))
{
String allCookies = (String) entry.getValue();
String delim = "[||]+";
String[] cookies = allCookies.split(delim);
for (String everyCookieContent:cookies)
{
String cookieName = everyCookieContent.substring(0, everyCookieContent.indexOf("="));
String cookieValue = everyCookieContent.substring(everyCookieContent.indexOf("=")+1, everyCookieContent.indexOf(";"));
Cookie cookie = new Cookie(cookieName, cookieValue);
cookieList.add(cookie);
}
}
}
if (cookieList.size() > 0)
{
return cookieList;
}
else
{
return null;
}
}
/**
* Gets the body.
*
* @return the body
*/
public Document getBody()
{
return body;
}
/**
* Gets the location.
*
* @return the location
*/
public String getLocation()
{
return header.get("Location");
}
}