2

I am trying to make a Java servlet that will download a file to be opened in a browser window (or downloaded if the browser can't view it). As an example, I'm trying to fetch and view a PNG image in my browser.

I tried sending a request with no headers (except Host: localhost) via Fiddler, and I got back the image I was looking for.

When I try to view the image in my browser with the same URL, I get an invalid (transparent) image back. I tried adding the headers from my browser (Chrome) to Fiddler, and it looks like it fails when I add the following header: Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8

When I remove that header, or change it to Accept: */*, the image loads again. I looked at the raw response in Fiddler and found there is a small difference in the actual data sent back.

This is what my servlet's code looks like. I'm not manually checking the Accept header for anything:

byte[] data = getData();

response.setContentType("image/png");
response.setHeader("Content-disposition", "inline; filename=\"my_image.png\"");
response.setContentLength(data.length);

OutputStream output = response.getOutputStream();
output.write(data);
output.close();

Why does Chrome's default Accept header make my response unreadable? How can I make my servlet ignore that header and send back the same data every time?

EDIT:

This is what Fiddler shows in the "Raw" view when I request the servlet without the Accept header (good):

HTTP/1.1 200 OK
x-xr-bookmark: ad70ff81-7415-4d46-954d-b79f98056729
content-disposition: inline; filename="image.PNG"
content-type: image/png
content-length: 82726
date: Tue, 21 Mar 2017 19:49:32 GMT
connection: close

 PNG


IHDR        5-   sRGB       gAMA    
 a      pHYs  t  t fx    IDATx^  {p\ }  &M $mg I  K;    N3    t`

*** FIDDLER: RawDisplay truncated at 128 characters. Right-click to disable truncation. ***

And this is what Fiddler shows when I request it with the Accept header mentioned above (bad):

HTTP/1.1 200 OK
x-xr-bookmark: 5a1b1e6d-6b68-42ac-a2cb-545811f9a879
content-disposition: inline; filename="image.PNG"
content-type: image/png
date: Tue, 21 Mar 2017 19:49:46 GMT
connection: close
Transfer-Encoding: chunked

258ec
 PNG


IHDR        5-   sRGB       gAMA    
 a      pHYs  t  t fx    IDATx^  {p\ }  &M $mg I  K;    N3 

*** FIDDLER: RawDisplay truncated at 128 characters. Right-click to disable truncation. ***
John Brink
  • 546
  • 2
  • 8
  • 23
  • is there something specific that needs to be done in fiddler for chunked encoding – Khanna111 Mar 21 '17 at 22:16
  • Well, keep in mind that the image is not showing up in Chrome, either. According to http://stackoverflow.com/a/16533006/2444020 I can avoid chunked encoding if I set the content length before writing the content, but you can see in my code snippet that's exactly what I do. How mysterious. – John Brink Mar 22 '17 at 13:38
  • Update: I pressed "Decode response" in Fiddler on one of the bad responses (with the Accept header in the request and the chunked response), and the resulting raw data after decryption looks just like the good response, except `Content-Length: 153836` instead of `82726`. Could this have anything to do with it? – John Brink Mar 22 '17 at 16:44
  • Update again: I made an tag whose src attribute pointed to this servlet, and the image appeared correctly. It would seem from this experiment that the problem lies in Chrome (or any other browser) not realizing what the data it's receiving is, despite the HTTP header `content-type:image/png` – John Brink Mar 22 '17 at 18:51

0 Answers0