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