Skip to content

Commit 4f8371a

Browse files
committed
SocketUtils update
1 parent fcd50d4 commit 4f8371a

File tree

1 file changed

+45
-32
lines changed

1 file changed

+45
-32
lines changed

src/main/java/com/epam/reportportal/util/test/SocketUtils.java

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
import org.slf4j.LoggerFactory;
2323

2424
import javax.annotation.Nonnull;
25-
import java.io.BufferedReader;
26-
import java.io.IOException;
27-
import java.io.InputStreamReader;
25+
import java.io.*;
2826
import java.net.ServerSocket;
2927
import java.net.Socket;
3028
import java.nio.charset.StandardCharsets;
@@ -37,6 +35,7 @@
3735
import static java.util.Optional.*;
3836

3937
public class SocketUtils {
38+
public static final String CONTENT_LENGTH_HEADER = "Content-Length: ";
4039
public static final String WEB_DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss z";
4140

4241
private static final Logger LOGGER = LoggerFactory.getLogger(SocketUtils.class);
@@ -63,41 +62,55 @@ public ServerCallable(@Nonnull ServerSocket serverSocket, @Nonnull Map<String, O
6362
}
6463

6564
@Override
66-
public List<String> call() throws IOException {
67-
final Socket s = ss.accept();
65+
public List<String> call() throws Exception {
6866
final List<String> results = new ArrayList<>();
69-
for (String responseFile : responseFiles) {
70-
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream(), StandardCharsets.UTF_8));
71-
StringBuilder builder = new StringBuilder();
72-
String line;
73-
while ((line = in.readLine()) != null) {
74-
if (line.isEmpty()) {
75-
break;
67+
try (Socket s = ss.accept(); InputStream is = s.getInputStream(); OutputStream os = s.getOutputStream()) {
68+
for (String responseFile : responseFiles) {
69+
BufferedReader in = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
70+
StringBuilder builder = new StringBuilder();
71+
while (true) {
72+
String line = in.readLine();
73+
if (line == null || line.isEmpty()) {
74+
break; // End of headers or connection closed
75+
}
76+
builder.append(line);
77+
builder.append(System.lineSeparator());
7678
}
77-
builder.append(line);
78-
builder.append(System.lineSeparator());
79-
}
80-
results.add(builder.toString());
81-
String rs = ofNullable(getClass().getClassLoader().getResourceAsStream(responseFile)).flatMap(stream -> {
82-
try {
83-
String responseStr = IOUtils.toString(stream, StandardCharsets.UTF_8);
84-
for (String k : model.keySet()) {
85-
responseStr = responseStr.replace("{" + k + "}", model.get(k).toString());
79+
String headers = builder.toString();
80+
if (headers.isEmpty()) {
81+
throw new IOException("No headers received from client");
82+
}
83+
int lengthIdx =
84+
headers.indexOf(CONTENT_LENGTH_HEADER) + CONTENT_LENGTH_HEADER.length(); // Find the Content-Length header
85+
int contentLength = Integer.parseInt(headers.substring(lengthIdx, headers.indexOf(System.lineSeparator(), lengthIdx))
86+
.trim());
87+
if (contentLength > 0) {
88+
char[] body = new char[contentLength];
89+
int actualREad = in.read(body, 0, contentLength);
90+
if (actualREad < contentLength) {
91+
throw new IOException("Expected " + contentLength + " bytes, but only read " + actualREad + " bytes");
8692
}
87-
return of(responseStr);
88-
} catch (IOException ignore) {
89-
return empty();
93+
builder.append(System.lineSeparator());
94+
builder.append(new String(body));
9095
}
91-
}).orElseThrow(() -> new IOException("Unable to read file: " + responseFile));
92-
IOUtils.write(rs, s.getOutputStream(), StandardCharsets.UTF_8);
93-
}
94-
if (!s.isClosed()) {
95-
try {
96-
s.close();
97-
} catch (IOException e) {
98-
LOGGER.warn("Unable to close server socket", e);
96+
results.add(builder.toString());
97+
98+
String rs = ofNullable(getClass().getClassLoader().getResourceAsStream(responseFile)).flatMap(stream -> {
99+
try {
100+
String responseStr = IOUtils.toString(stream, StandardCharsets.UTF_8);
101+
for (String k : model.keySet()) {
102+
responseStr = responseStr.replace("{" + k + "}", model.get(k).toString());
103+
}
104+
return of(responseStr);
105+
} catch (IOException ignore) {
106+
return empty();
107+
}
108+
}).orElseThrow(() -> new IOException("Unable to read file: " + responseFile)).replaceAll("\r?\n", "\r\n");
109+
IOUtils.write(rs, os, StandardCharsets.UTF_8);
110+
os.flush();
99111
}
100112
}
113+
101114
return results;
102115
}
103116
}

0 commit comments

Comments
 (0)