Reputation: 2241
No idea what i could be doing wrong here. When I try to look at the response with firebug, it's just saying that I need to reload the page to get it's source. I tried using GZIPOutputStream instead, but then it just wrote some weird chars at the beginning, and didnt even produce a valid header.
Tried also several other random things, none of which did any help, so out with the trial and error and in with the enlightened stackoverflow wisdom.
How is this still complaining that I need to add context?
Well, I'm really tired and tilted, and my coding skills have degenerated to just shouting obscenities at the monitor. How's that for a context?
Oh, and the server I'm using is a mod of NanoHTTPD, which i need to use for this because reasons.
private void sendResponse( String status, String mime, Properties header, InputStream data,boolean acceptEncoding)
{
try
{
if ( status == null )
throw new Error( "sendResponse(): Status can't be null." );
OutputStream out = mySocket.getOutputStream();
PrintWriter pw = new PrintWriter( out );
if(acceptEncoding){
out = new java.util.zip.DeflaterOutputStream(out);//tried using GZIPInputStream here
header.setProperty("Content-Encoding","deflate"); // and gzip here, worked even worse
}
pw.print("HTTP/1.1 " + status + " \r\n");
if ( mime != null )
pw.print("Content-Type: " + mime + "\r\n");
if ( header == null || header.getProperty( "Date" ) == null )
pw.print( "Date: " + gmtFrmt.format( new Date()) + "\r\n");
if ( header != null )
{
Enumeration e = header.keys();
while ( e.hasMoreElements())
{
String key = (String)e.nextElement();
String value = header.getProperty( key );
pw.print( key + ": " + value + "\r\n");
}
}
pw.print("\r\n");
pw.flush();
if ( data != null )
{
byte[] buff = new byte[2048];
while (true)
{
int read = data.read( buff, 0, 2048 );
if (read <= 0)
break;
out.write( buff, 0, read );
}
}
out.flush();
out.close();
if ( data != null )
data.close();
}
catch( IOException ioe )
{
// Couldn't write? No can do.
try { mySocket.close(); } catch( Throwable t ) {}
}
}
Upvotes: 0
Views: 1584
Reputation: 298143
When creating a GZIPOutputStream
it will write the header bytes right in the constructor. Since you create the GZIPOutputStream
instance before writing the HTTP header, the GZip header is written before the HTTP header. You must create the GZIPOutputStream
after you have written the HTTP header completely.
Upvotes: 3