Рефакторинг

This commit is contained in:
serega6531
2020-04-15 21:11:51 +03:00
parent 070f6b7673
commit 7dd4c6f468

View File

@@ -1,6 +1,6 @@
package ru.serega6531.packmate.service.optimization;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import ru.serega6531.packmate.model.Packet;
@@ -14,18 +14,20 @@ import java.util.Arrays;
import java.util.List;
@Slf4j
@AllArgsConstructor
@RequiredArgsConstructor
public class HttpChunksProcessor {
private final List<Packet> packets;
public void processChunkedEncoding() {
boolean chunkStarted = false;
int start = -1;
List<Packet> chunk = new ArrayList<>();
private int position;
private boolean chunkStarted = false;
private final List<Packet> chunkPackets = new ArrayList<>();
for (int i = 0; i < packets.size(); i++) {
Packet packet = packets.get(i);
public void processChunkedEncoding() {
int start = -1;
for (position = 0; position < packets.size(); position++) {
Packet packet = packets.get(position);
if (!packet.isIncoming()) {
String content = packet.getContentString();
@@ -37,38 +39,32 @@ public class HttpChunksProcessor {
boolean chunked = headers.contains("Transfer-Encoding: chunked\r\n");
if (chunked) {
chunkStarted = true;
start = i;
chunk.add(packet);
start = position;
chunkPackets.add(packet);
if (checkCompleteChunk(chunk, start)) {
chunkStarted = false;
chunk.clear();
i = start + 1;
}
checkCompleteChunk(chunkPackets, start);
} else {
chunkStarted = false;
chunk.clear();
chunkPackets.clear();
}
} else if (chunkStarted) {
chunk.add(packet);
if (checkCompleteChunk(chunk, start)) {
chunkStarted = false;
chunk.clear();
i = start + 1;
}
chunkPackets.add(packet);
checkCompleteChunk(chunkPackets, start);
}
}
}
}
/**
* @return true если чанк завершен
*/
@SneakyThrows
private boolean checkCompleteChunk(List<Packet> packets, int start) {
private void checkCompleteChunk(List<Packet> packets, int start) {
boolean end = packets.get(packets.size() - 1).getContentString().endsWith("\r\n0\r\n\r\n");
if (end) {
processChunk(packets, start);
}
}
@SneakyThrows
private void processChunk(List<Packet> packets, int start) {
//noinspection OptionalGetWithoutIsPresent
final byte[] content = PacketUtils.mergePackets(packets).get();
@@ -96,13 +92,17 @@ public class HttpChunksProcessor {
this.packets.removeAll(packets);
this.packets.add(start, result);
return true;
resetChunk();
position = start + 1;
return;
}
if (chunkSize > buf.remaining()) {
log.warn("Failed to merge chunks, chunk size too big: {} + {} > {}",
buf.position(), chunkSize, buf.capacity());
return true; // обнулить список, но не заменять пакеты
resetChunk();
return;
}
byte[] chunk = new byte[chunkSize];
@@ -111,23 +111,28 @@ public class HttpChunksProcessor {
if (buf.remaining() < 2) {
log.warn("Failed to merge chunks, chunk doesn't end with \\r\\n");
return true; // обнулить список, но не заменять пакеты
resetChunk();
return;
}
int c1 = buf.get();
int c2 = buf.get();
if(c1 != '\r' || c2 != '\n') {
log.warn("Failed to merge chunks, chunk trailer is not equal to \\r\\n");
return true; // обнулить список, но не заменять пакеты
resetChunk();
return;
}
} else {
log.warn("Failed to merge chunks, next chunk size not found");
return true; // обнулить список, но не заменять пакеты
resetChunk();
return;
}
}
}
return false;
private void resetChunk() {
chunkStarted = false;
chunkPackets.clear();
}
private String readChunkSize(ByteBuffer buf) {