-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTTPClientTCP.java
191 lines (158 loc) · 5.52 KB
/
HTTPClientTCP.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
import java.io.*;
import java.net.*;
import java.util.*;
public class HTTPClientTCP {
private Socket link;
private String serverHost;
private int serverPort;
private HTTPRequest requestType;
private String entityHeaders;
private String entityBody;
private boolean requestCompliant;
private int contentLength;
private String responseHeader;
private byte[] responseBody;
private static final String HTTPVersion = "1.0";
private static final String CRLF = "\r\n";
private static final int connectTimeout = 3000;
public enum HTTPRequest {
HEAD,GET,POST;
}
/* Constructors */
HTTPClientTCP(String serverHost, int serverPort) {
this.serverHost = serverHost;
this.serverPort = serverPort;
this.responseHeader = "";
this.contentLength = 0;
this.entityHeaders = "";
this.entityBody = null;
this.requestCompliant = false;
}
/* Getters */
public String getResponseHeader() {
return responseHeader;
}
public byte[] getResponseBody() { ///////////////////////// check on this from outside
/* ensure we got expected bytes from server (if due) */
byte[] answer = null;
if (requestCompliant) {
if (requestType == HTTPRequest.GET) {
if (responseBody.length == contentLength) {
answer = responseBody;
}
} else {
answer = responseBody;
}
}
return answer;
}
/* Setters */
public void setRequestType(HTTPRequest requestType) {
this.requestType = requestType;
}
public void setRequestHeader(String requestHeader, int requestValue) {
if (requestHeader.equals("Content-Length"))
entityHeaders += requestHeader + ": " + requestValue + CRLF;
}
/* Methods */
public void connect() throws IOException {
SocketAddress sockAddr = new InetSocketAddress(serverHost, serverPort);
link = new Socket();
link.connect(sockAddr, connectTimeout);
//link = new Socket(serverHost, serverPort);
}
// POST request
public void request(String resource, String body) throws IOException {
this.entityBody = body;
this.requestType = HTTPRequest.POST; // enforce POST when body given
request(resource);
}
// GET request
public void request(String resource) throws IOException {
/* 1st: compute request line */
String requestLine = requestType + " /" + resource
+ " HTTP/" + HTTPVersion + CRLF;
/////
System.out.println("REQUEST SENT:\n" + requestLine);
/////
/* 2nd: send request */
OutputStream out = link.getOutputStream();
PrintWriter outw = new PrintWriter(out, false);
outw.print(requestLine);
if (entityHeaders != null)
outw.print(entityHeaders); // not going to send any in this stripped version
outw.print(CRLF);
if (entityBody != null)
outw.print(entityBody);
outw.flush();
/* 3rd: save response */
InputStream input = link.getInputStream();
BufferedInputStream inputBuffer = new BufferedInputStream(input);
ByteArrayOutputStream outputBuffer = new ByteArrayOutputStream();
int actualBytesRead;
int totalBytesRead = 0;
byte[] data = new byte[512];
while ((actualBytesRead = inputBuffer.read(data, 0, data.length)) != -1) {
outputBuffer.write(data, 0, actualBytesRead);
totalBytesRead += actualBytesRead;
}
outputBuffer.flush();
byte[] fullResponse = new byte[totalBytesRead];
fullResponse = outputBuffer.toByteArray();
System.out.println("(WHOLE) READ BYTES: "+totalBytesRead);
workResponse(fullResponse, totalBytesRead);
}
private void workLine(String currentLine)
{
if (currentLine.equals("HTTP/1.1 200 OK")) requestCompliant = true;
String[] tokens = currentLine.split(": ");
if (tokens.length == 2)
{
String headerName = tokens[0];
String headerValue = tokens[1];
if (headerName.equals("Content-Length"))
contentLength = Integer.parseInt(headerValue);
}
}
private void workResponse(byte[] fullResponse, int totalBytesRead) throws UnsupportedEncodingException
{
/* converting to string to gather headers, especially Content-Length */
String responseToString = new String(fullResponse, "UTF-8");
String[] tokens = responseToString.split(CRLF);
int i = 0;
while(i < tokens.length) {
String currentLine = tokens[i];
workLine(currentLine);
responseHeader += currentLine + CRLF;
if (currentLine.matches("")) break; // blank line: headers got
i++;
}
//////
System.out.println("ANSWER RECEIVED:\n---\n" + responseHeader + "---");
//////
/* obtain and store body as a byte array */
if (requestCompliant) {
/* getting resource if any */
int resourceLength = totalBytesRead - responseHeader.length();
byte[] resource = new byte[resourceLength];
System.arraycopy(fullResponse, responseHeader.length(), resource, 0, resource.length);
/* saving body response */
responseBody = resource;
System.out.println("(BODY) GOT BYTES: " + responseBody.length);
}
}
// public static void main(String args[]) {
// try {
// HTTPClientTCP test = new HTTPClientTCP("mm.sharped.net",3300);
// test.setRequestType(HTTPRequest.POST);
// test.connect();
// test.request("cgi-bum/mmupdate.icg","?lol");
// //test.request("cgi-bum/release/golly_vectorializer.app/Contents/Java/golly_vectorializer.jar");
// byte[] a = test.getResponseBody();
// String responseBody = new String(a, "UTF-8");
// System.out.println(responseBody);
// } catch(IOException e) {
// e.printStackTrace();
// }
// }
}