I have written one proxy for intercepting the http requests for my tomcat.
every request would go through my proxy and do some checking before it reaches the tomcat server. i am doing it by binding the port using TCP/IP written in java.
All the requests(GET and POST) are successfully able to route to tomcat server except the file upload (multipart POST form) submissions.
even though i am able to get all the bytes in the TCP/IP and able to flush the data back to tomcat server, somehow the data is getting truncated/lost
is there any special things like encoding etc.. that i need to do when dealing with file stream content??
below is my sample code...
protected void processData(InputStream input, OutputStream output) throws IOException
{
// reads a line of text from an InputStream
StringBuffer data = new StringBuffer("");
StringBuffer data2 = new StringBuffer("");
StringBuffer data3 = new StringBuffer("");
StringBuffer data4 = new StringBuffer("");
int c;
try
{
while ((c = input.read()) >= 0)
{
data.append((char) c);
// check for an end-of-line character
if ((c == 0) || (c == 10) || (c == 13))
{
output.write(data.toString().getBytes(), 0, data.length());
data4.append(data.toString());
data = new StringBuffer();
count = 0;
}
else
{
if (count > 6)
{
if (input.available() == 1)
{
data.append((char) input.read());
}
data2.append(data.toString());
data4.append(data.toString());
output.write(data.toString().getBytes(), 0, data
.toString().length());
data = new StringBuffer();
}
else
{
if (count == 6)
{
if (data.toString().toLowerCase()
.indexOf("get /") == 0
|| data.toString().toLowerCase()
.indexOf("post /") == 0)
{
count = 0;
contentLength = -1;
// continue read data(header info)
while ((line = readLine(input, data)) != null)
{
data = new StringBuffer();
// do my own stuff here dealing with headers
if (StringUtils.isBlank(line))
{
data4.append(line);
output.write(line.getBytes(), 0,
line.length());
break;
}
line += "\r\n";
output.write(line.getBytes(), 0,
line.length());
data4.append(line);
output.flush();
}
}
else
{
if (input.available() == 1)
{
data.append((char) input.read());
}
}
}
else
{
if (input.available() == 1)
{
data.append((char) input.read());
output.write(data.toString().getBytes(), 0,
data.toString().length());
data4.append(data.toString());
data3.append(data.toString());
data = new StringBuffer();
}
}
}
count++;
}
if (processbody)
total++;
if (contentLength > 0 && contentLength == total)
{
log.debug("post data2: "
+ (data2.toString() != null ? data2.toString() : " "));
log.debug("post data3: "
+ (data3.toString() != null ? data3.toString() : " "));
log.debug("post data4: "
+ (data4.toString() != null ? data4.toString() : " "));
output.flush();
}
}
}
catch (Exception e)
{
log.error("Error ", e);
}
finally
{
}
}
InputStream/OutputStreamonly (and thus noReader/Writer). As to the concrete problem, it would help if you update the question to include the smallest possible code snippet which (re)produces the problem, complete with real examples of actual input and output bytes.