-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTTPRequest.java
234 lines (202 loc) · 6.76 KB
/
HTTPRequest.java
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import java.io.InputStream;
import java.util.Iterator;
import java.util.LinkedList;
import java.io.IOException;
/*
* Object type that encapsulates an HTTP request
*/
public class HTTPRequest {
private static final String GET = "GET";
private static final String PUT = "PUT";
private static final String POST = "POST";
private String type; // operation type EX: GET, PUT or POST
private String requestedObject; // Ex: /index.hmtl
public String version; // protocol version
public String urlHost;
public String post;
public String contentype;
public String contentlength;
public LinkedList<String> header; // remainder of the request's header
/*
* Recupera os dados necessario para as variaveis
*/
private HTTPRequest(String type, String requestedObject, String version) {
this.type = type;
this.requestedObject = requestedObject;
int primeirosSlash = requestedObject.indexOf("//");
String semHttp = requestedObject.substring(primeirosSlash+2);
int valorUrl = semHttp.indexOf("/");
this.urlHost = semHttp.substring(0,valorUrl);
this.version = version;
this.header = new LinkedList<String>();
}
private void addHeader(String header) {
this.header.add(header);
}
/*
* Devolva o tipo de pedido
*/
public String requestType() {
return type;
}
/*
* Devolva o post
*/
public String requestPost() {
return post;
}
/*
* Metes os valores do post nas string post
*/
public void setPost(String post) {
this.post = post;
}
/*
* Metes os valores do content type nas string na string do mesmo nome
*/
public void setContentTypePost(String contentype)
{
this.contentype = contentype;
}
/*
* Devolva o content type
*/
public String requestContentType() {
return contentype;
}
/*
* Metes os valores do content type nas string na string do mesmo nome
*/
public void setContentLengthPost(String contentlength)
{
this.contentlength = contentlength;
}
/*
* Devolva o content type
*/
public String requestContentLength() {
return contentlength;
}
/*
* Devolva o objecto pedido
*/
public String requestedObject() {
return requestedObject;
}
/*
* Devolva a versão do http
*/
public String httpVersion() {
return version;
}
/*
* String representation of this HTTPRequest
*/
public String toString() {
String res = type + " " + requestedObject + " " + version + "\r\n";
Iterator<String> i = header.iterator();
while(i.hasNext()) {
res += i.next() + "\r\n";
}
return res;
}
/*
* Converte para a versão http 1.0
* E remove todas as linhas do header que comporta connection e keep-alive
*/
static HTTPRequest parseHTTPRequestAs1_0(InputStream is) throws IOException {
java.util.Scanner sc = new java.util.Scanner(is);
String operation = sc.next();
String requestedObject = sc.next();
sc.next(); // skeep http version
sc.next(); // limpa o \r\n do final da primeira linha
HTTPRequest request = new HTTPRequest(operation, requestedObject, "HTTP/1.0");
String line = "";
while(!(line = sc.nextLine()).equals("")) {
if(!(line.contains("connection")||line.contains("Connection")))
request.addHeader(line);
}
return request;
}
static HTTPRequest readLine(InputStream is) throws IOException {
StringBuffer sb = new StringBuffer();
String operation = "";
String requestedObject = "";
int c;
int i= 0;
boolean fim = false;
while ((c = is.read()) >= 0) {
if(c == ' ' && i < 2)
{
if(i == 1)
{
requestedObject = new String(sb);
i++;
sb = new StringBuffer();
fim = true;
}
if(i == 0)
{
operation = new String(sb);
i++;
sb = new StringBuffer();
}
}
if(fim)
break;
sb.append(new Character((char) c));
}
HTTPRequest request = new HTTPRequest(operation, requestedObject, "HTTP/1.0");
fim = false;
while ((c = is.read()) >= 0) {
if(i == 10 && c == '\r')
break;
i =c;
if (c == '\n') {
sb.append(new Character((char) c));
String headerpedido = new String (sb);
if(!(headerpedido.contains("HTTP") || headerpedido.contains("connection") || headerpedido.contains("Connection")))
{
if(headerpedido.contains("Content-Type"))
{
int r = headerpedido.indexOf("\r");
String ct = headerpedido.substring(0,r);
request.setContentTypePost(ct);
}
if(headerpedido.contains("Content-Length"))
{
//para saber o valor do content-length
request.addHeader(headerpedido);
int doispontos = headerpedido.indexOf(":");
String tamanho = headerpedido.substring(doispontos+2);
int r = tamanho.indexOf("\r");
tamanho = tamanho.substring(0,r);
int pl = Integer.parseInt(tamanho);
r = headerpedido.indexOf("\r");
String cl = headerpedido.substring(0,r);
request.setContentLengthPost(cl);
sb = new StringBuffer();
for(int s=0;s<pl;s++)
{
c = is.read();
sb.append(new Character((char) c));
}
request.setPost(new String (sb));
request.addHeader(new String (sb));
fim = true;
}else{
request.addHeader(new String (sb));
sb = new StringBuffer();
}
}else{
sb = new StringBuffer();
}
}else{
sb.append(new Character((char) c));
}
if(fim)
break;
}
return request;
}
}