-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyRunnable.java
More file actions
107 lines (86 loc) · 4.18 KB
/
MyRunnable.java
File metadata and controls
107 lines (86 loc) · 4.18 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
// Author @Tenzin Sangpo Choedon
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import tcpclient.TCPClient;
public class MyRunnable implements Runnable{
Socket connectionSocket;
public MyRunnable(Socket connectionSocket){
this.connectionSocket = connectionSocket;
}
public void run(){
int BUFFER_SIZE = 1024;
String hostname = null;
Integer port = null;
boolean shutdown = false;
Integer limit = null;
Integer timeout = null;
String msgServer = "";
try {
// Pre-allocate buffer for receiving bytes from client
byte[] fromClientBytes = new byte[BUFFER_SIZE];
ByteArrayOutputStream fromClientBuffer = new ByteArrayOutputStream();
int fromClientBytesLength;
// Read client request
while((fromClientBytesLength = connectionSocket.getInputStream().read(fromClientBytes)) != -1){
fromClientBuffer.write(fromClientBytes, 0, fromClientBytesLength);
String tmpClientBuffer = new String(fromClientBytes);
if(tmpClientBuffer.contains("HTTP/1.1")){
break;
}
}
String output = new String(fromClientBuffer.toByteArray(), StandardCharsets.UTF_8);
System.out.println("-----Client request-----\n" + output);
// Split up client request
String[] request = output.split("[=&+? \\r\\n]");
boolean foundHttp = false;
// Extract parameters for TCPClient
for (int i = 0; i < request.length; i++) {
switch(request[i]){
case "hostname" -> hostname = request[++i];
case "port" -> port = Integer.valueOf(request[++i]);
case "string" -> msgServer = request[++i];
case "shutdown" -> shutdown = Boolean.parseBoolean(request[++i]);
case "limit" -> limit = Integer.valueOf(request[++i]);
case "timeout" -> timeout = Integer.valueOf(request[++i]);
case "HTTP/1.1" -> foundHttp = true;
}
}
String status = "";
if(!request[0].equals("GET") || !foundHttp || port == null || hostname == null){
status = "HTTP/1.1 400 Bad Request\r\n";
connectionSocket.getOutputStream().write(status.getBytes());
}
else if (!request[1].equals("/ask")) {
status = "HTTP/1.1 404 Not Found\r\n";
connectionSocket.getOutputStream().write(status.getBytes());
}
else{
status = "HTTP/1.1 200 OK\r\n\r\n";
try{
// TCPclient instance
TCPClient client = new tcpclient.TCPClient(shutdown, timeout, limit);
if(msgServer.length()>0 && msgServer.charAt(msgServer.length()-1) != '\n') msgServer+="\n";
// Buffer for message to server
byte[] toServerBytes = (msgServer).getBytes(StandardCharsets.UTF_8);
// Response from the server
byte[] fromServerBytes = client.askServer(hostname, port, toServerBytes);
String fromServer = new String(fromServerBytes);
// Computing HTTP response
String response = status + fromServer;
// Buffer for HTTP response
byte[] toClientBytes = response.getBytes(StandardCharsets.UTF_8);
// Send HTTP response
connectionSocket.getOutputStream().write(toClientBytes);
}catch (IOException e) {
status = "HTTP/1.1 400 Bad Request\r\n";
connectionSocket.getOutputStream().write(status.getBytes());
}
}
connectionSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}