-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
137 lines (107 loc) · 5.23 KB
/
Server.java
File metadata and controls
137 lines (107 loc) · 5.23 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
package Proxy;
import java.net.*;
import java.io.*;
import java.util.ArrayList;
/**
*Class that manages the petitions to the proxy and creates a process to handle each petition
*
*/
public class Server implements Runnable{
private Socket server;
public Server(Socket socket) throws IOException{
this.server = socket;
}
public OutputStream getOutputStream() throws IOException{
return this.server.getOutputStream();
}
public InputStream getInputStream() throws IOException{
return this.server.getInputStream();
}
//Here are the modifications of the content returned to the client
private BufferedWriter modify(BufferedReader input)throws IOException{
int trueSize = 0;
BufferedWriter output = new BufferedWriter
(new OutputStreamWriter(this.server.getOutputStream()));
boolean onText = false;
//responseLines contains 1 line of the response in each element of the array
ArrayList<String> responseLines = new ArrayList<>();
//initializing responseLines
do{
responseLines.add(input.readLine());
}while(responseLines.get(responseLines.size() - 1) != null);
responseLines.remove(responseLines.size()-1);
//Checks for possible modifications in each line of the content
//unless it is still reading the http header info
for(int i = 0; i < responseLines.size(); i++){
if(onText){
responseLines.set(i, responseLines.get(i).replaceAll("(?i)\\.\\/.*stockholm.*\\.(jpg|jpeg|png|gif)",
"https://c8.alamy.com/comp/FYC0H3/spring-in-the-iconic-park-tradgardsforeningen-in-linkoping-sweden-FYC0H3.jpg"));
responseLines.set(i, responseLines.get(i).replace("./smiley.jpg",
"https://statuecollectibles.cz/images_upd/products/7/paj3z7hnrlty.jpg"));
responseLines.set(i, responseLines.get(i).replace("Smiley", "Trolly"));
responseLines.set(i, responseLines.get(i).replace("Stockholm", "Linköping"));
byte[] bytes = (responseLines.get(i) + "\r\n").getBytes("UTF-8");
trueSize += bytes.length;
}
if(responseLines.get(i).matches("")){
onText = true;
}
}
//Corrects the content length after the modifications are done and writes the message to output
for(int i = 0; i < responseLines.size(); i++){
if(responseLines.get(i).contains("Content-Length:"))
responseLines.set(i, responseLines.get(i).replace(responseLines.get(i), "Content-Length: " + trueSize));
System.out.println("RESPONSELINE: " + responseLines.get(i));
output.write(responseLines.get(i));
output.newLine();
}
return output;
}
@Override
public void run(){
try {
System.out.println("Thread created for a new connection.");
//Buffers to read the HTTP request from the browser
BufferedReader inFromBrowser = new BufferedReader
(new InputStreamReader(this.server.getInputStream()));
BufferedWriter outToBrowser;
String header = inFromBrowser.readLine();
System.out.println("Browsers request: " + header);
if(header == null || header.isEmpty()){
this.server.close();
return;
}
String[] headerList = header.split(" ");
if(headerList.length < 2){
this.server.close();
}
URI url = new URI(headerList[1]);
URL target = url.toURL();
String host = target.getHost();
int port;
if(target.getPort() == -1){
port = 80;
} else {
port = target.getPort();
}
System.out.println("Connecting to: " + host + ":" + port);
try{
Client client = new Client(host, port);
//Buffers manage the connection to the server
BufferedWriter outToWeb;
BufferedReader inFromWeb = new BufferedReader
(new InputStreamReader(client.getInputStream()));
outToWeb = client.write(inFromBrowser, header);
outToWeb.flush();
outToBrowser = modify(inFromWeb);
outToBrowser.flush();
System.out.println("Response sent.");
} catch (IOException ex){
System.err.println("Web Connection error: " + ex.getMessage());
}
this.server.close();
} catch (IOException | URISyntaxException ex) {
System.err.println("Browser Connection error: " + ex.getMessage());
}
}
}