Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added Content-Length header to response and fixed some buffer offset …

…issues
  • Loading branch information...
commit a249747a62e3b806bf8962bcedf0fb249aa40b4a 1 parent 07963dc
@koto koto authored
Showing with 17 additions and 6 deletions.
  1. +17 −6 proxy-backend/malaria/MalariaServer.java
View
23 proxy-backend/malaria/MalariaServer.java
@@ -84,24 +84,35 @@ public void serveSocket(Socket client, ServerSocket proxySocket, String hostname
continue handleProxyRequests;
}
if (dl == -1) {
+ // first packet, prepended with total length
String fl = new String(buffer, "UTF8").toString().split(":", 2)[0];
dl = Integer.parseInt(fl);
System.out.println("DL: " + dl);
+ int prefixLength = fl.length() + 1;
read -= fl.length() + 1;
- byte[] bytes = new byte[buffer.length - fl.length() - 1];
- for(int i = fl.length() + 1; i < buffer.length; i++) {
- bytes[i - fl.length() - 1] = buffer[i];
- }
+ byte[] bytes = new byte[length - prefixLength];
+ System.arraycopy(buffer, prefixLength, bytes, 0, length - prefixLength);
fullBuffer.add(bytes);
} else {
- fullBuffer.add(buffer);
+ byte[] tempBuffer = new byte[length];
+ System.arraycopy(buffer, 0, tempBuffer, 0, length);
+ fullBuffer.add(tempBuffer);
}
read += length;
System.out.println("<- Read " + length + ":" + read + "/" + dl);
if (read >= dl)
done = true;
}
- proxyOut.write("HTTP/1.1 200 OK\r\n\r\n".getBytes("UTF8"));
+ int totalSize = 0;
+ for (int i = 0; i < fullBuffer.size(); i++) {
+ totalSize += ((byte[]) fullBuffer.get(i)).length;
+ }
+
+ System.out.println("<- Sending " + totalSize + " to proxy client");
+
+ String header = "HTTP/1.1 200 OK\r\n" + "Content-Length: " + totalSize + "\r\n\r\n";
+ proxyOut.write(header.getBytes("UTF8"));
+
for (int i = 0; i < fullBuffer.size(); i++) {
proxyOut.write(fullBuffer.get(i));
}
Please sign in to comment.
Something went wrong with that request. Please try again.