Include text header in Image HTTP response Jetty - http

I'm building a simple server program that needs to return both an image and some text in the response, however, I'm having an issue with Jetty. The text should be included in headers of the HTTP response, but isn't.
Here's the code to return the image:
override fun doPost(request: HttpServletRequest, response: HttpServletResponse) {
response.contentType = "image/png"
response.status = HttpServletResponse.SC_OK
val diff = ImgDiff.getDifference("img1", "img2", tolerance)
//response.writer.println(diff.toString())
ImageIO.write(ImageIO.read(File("diffedFile.png")), "PNG", response.outputStream)
response.addHeader("diff", diff.toString())
}
This works fine, however, the header doesn't contain diff. When I use comment out the ImageIO line and uncomment the one above it, the already commented out one, and change the content type to text/plain diff is included in the headers.
The headers with the image:
Date: Mon, 13 May 2019 22:03:35 GMT
Content-Type: image/png
Transfer-Encoding: chunked
Server: Jetty(9.4.18.v20190429)
The headers without the image (As described in the latter case)
Date: Mon, 13 May 2019 22:10:32 GMT
Content-Type: text/plain;charset=iso-8859-1
diff: 62.62626262626263
Content-Length: 19
Server: Jetty(9.4.18.v20190429)
Am I doing something wrong with Jetty? Can HTTP response images not contain images? I realize I could just return a zip file containing the image and text but I think that's a bit much. Am I ignoring something fundamental to HTTP requests? Please let me know.

It seems to work if I add the headers before I print the image into the stream.
override fun doPost(request: HttpServletRequest, response: HttpServletResponse) {
response.contentType = "image/png"
response.status = HttpServletResponse.SC_OK
val diff = ImgDiff.getDifference("img1", "img2", tolerance)
response.addHeader("diff", diff.toString())
ImageIO.write(ImageIO.read(File("diffedFile.png")), "PNG", response.outputStream)
}

Related

Trying to download page in python with urllib2 and requests but keep getting redirected

I am trying to simply download a page with python.
http://webapps.rrc.state.tx.us/CMPL/viewPdfReportFormAction.do?method=cmplP4FormPdf&packetSummaryId=97770
If i get the response code from the server i get 200
import urllib2
url = 'http://webapps.rrc.state.tx.us/CMPL/viewPdfReportFormAction.do?method=cmplP4FormPdf&packetSummaryId=97770'
file_pointer = urllib2.urlopen(url)
print file_pointer.getcode()
However if i get the url i get the redirect page
file_pointer.geturl()
I have tried urllib, urllib2,requests, and mechanize all separately and can not get any to work. I am obviously missing something because other people in the office have code that works. SOS
Also here is more information provided by requests
import requests
url = 'http://webapps.rrc.state.tx.us/CMPL/viewPdfReportFormAction.do?method=cmplP4FormPdf&packetSummaryId=97770'
proxy = { 'https': '200.35.152.93:1212'}
response = requests.get(url, proxies=proxy)
send: 'GET /CMPL/viewPdfReportFormAction.do?method=cmplP4FormPdf&packetSummaryId=97770 HTTP/1.1\r\nHost: webapps.rrc.state.tx.us\r\nConnection: keep-alive\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nUser-Agent: python-requests/2.7.0 CPython/2.7.10 Windows/7\r\n\r\n'
reply: 'HTTP/1.1 302 Found\r\n'
header: Date: Wed, 26 Aug 2015 19:33:12 GMT
header: Server: Apache/2.2.15 (Red Hat)
header: Location: http://www.rrc.state.tx.us/site-policies/railroad-commission-of-texas-site-policies/?method=cmplP4FormPdf&packetSummaryId=97770
header: Content-Length: 405
header: Connection: close
header: Content-Type: text/html; charset=iso-8859-1
send: 'GET /site-policies/railroad-commission-of-texas-site-policies/?method=cmplP4FormPdf&packetSummaryId=97770 HTTP/1.1\r\nHost: www.rrc.state.tx.us\r\nConnection: keep-alive\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nUser-Agent: python-requests/2.7.0 CPython/2.7.10 Windows/7\r\n\r\n'
reply: 'HTTP/1.1 200 OK\r\n'
header: Cache-Control: private
header: Content-Type: text/html; charset=utf-8
header: server: one
header: Date: Wed, 26 Aug 2015 19:33:11 GMT
header: Content-Length: 41216
The problem is that this specific site is looking for your User Agent header, and since you're a python client, it disallows you to get the PDF and redirect you.
Therefore you need to mask your user agent.
Look at the following example:
url = 'http://webapps.rrc.state.tx.us/CMPL/viewPdfReportFormAction.do?method=cmplP4FormPdf&packetSummaryId=97770'
req = urllib2.Request(url)
req.add_unredirected_header('User-Agent', 'Mozilla/5.0')
file_pointer = urllib2.urlopen(req)
print file_pointer.getcode()
print file_pointer.geturl();
Okay so all one has to do with the requests module is to disable redirection.Here is my working code that is also using a proxy server.
import requests
url = 'http://webapps.rrc.state.tx.us/CMPL/viewPdfReportFormAction.do?method=cmplP4FormPdf&packetSummaryId=97770'
proxy = { 'https': '200.35.152.93:1212'}
r = requests.get(url, proxies=proxy,allow_redirects=False)
print r.url

Bootstrap BlueImp file upload Java Controller Resolved

I have added BlueImp Jquery multiple fileupload to my project.
The server side of the download uses PHP, but I want to use a Java Controller.
I've changed Blueimp's main.js to send the files to my controller - 'saveImages'. This part works and now it is sent to my Java controller.
$(function () {
'use strict';
// Initialize the jQuery File Upload widget:
$('#fileupload').fileupload({
// Uncomment the following to send cross-domain cookies:
//xhrFields: {withCredentials: true},
url: 'saveImages'
});
When selecting/uploading an image,
The headers show:
Connection close
Content-Length 5717
Content-Type text/html;charset=utf-8
Date Wed, 26 Mar 2014 19:43:42 GMT
Server Apache-Coyote/1.1
Request Headers
Accept application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Content-Length 94000
Content-Type multipart/form-data; boundary=--------------------------
The Post Information shows:
-----------------------------1873186311532063957277019191
Content-Disposition: form-data;
name="files[]";
filename="1.jpg"
Content-Type: image/jpeg
ÿØÿà�JFIF��`�`��ÿá�Exif��II*����������ÿÛ�C�ÿÛ�CÿÀ���"�ÿÄ����������������� ÿÄ�����������������ÿÄ�����������������ÿÄ�����������������ÿÚ���?�¿ÿÙ
-----------------------------1873186311532063957277019191--
This is the response from a PHP server - but I need to know how to do it with Java
{"files":[{"url":"http://jquery-file-upload.appspot.com/AMIfv97t6a-7QLgNXnhcK10mnbBojaAwHAL2oei6ySkhR_DKk6k5kDrS_tVhlAxFnCkYncEBM3IyeZ6n_oKXItO9dhoxcizw15pPtlA3-i53vc02Oh62RSqEdbK4QLvPwsFGdBOBmCjoNu8TOfl6m0eu1-Tquspryi48UOPXNe4eRfyrEJmBlJM/1.jpg",
"thumbnailUrl":"http://lh4.ggpht.com/b5ABUz-uqTUynXXNvHSlYcAj3nRnlYVIEAhcSZKWXHcwI_jaDZCaNDuNK-gPjeiIMy9nUIAiwS5l1o3IttA5MikPJOEXkQjFCg=s80",
"name":"1.jpg",
"type":"image/jpeg",
"size":309,
"deleteUrl":"http://jquery-file-upload.appspot.com/AMIfv97t6a-7QLgNXnhcK10mnbBojaAwHAL2oei6ySkhR_DKk6k5kDrS_tVhlAxFnCkYncEBM3IyeZ6n_oKXItO9dhoxcizw15pPtlA3-i53vc02Oh62RSqEdbK4QLvPwsFGdBOBmCjoNu8TOfl6m0eu1-Tquspryi48UOPXNe4eRfyrEJmBlJM/1.jpg?delete=true",
"deleteType":"DELETE"}]}
JSON
POST http://jquery-file-upload.appspot.com/
200 OK
2.06s
POST http://jquery-file-upload.appspot.com/
200 OK
1.18s
HeadersPostResponseJSON
Sort by key
files
[Object { url="http://jquery-file-uplo...PXNe4eRfyrEJmBlJM/1.jpg", thumbnailUrl="http://lh4.ggpht.com/b5...IttA5MikPJOEXkQjFCg=s80", name="1.jpg", more...}]
0
Object { url="http://jquery-file-uplo...PXNe4eRfyrEJmBlJM/1.jpg", thumbnailUrl="http://lh4.ggpht.com/b5...IttA5MikPJOEXkQjFCg=s80", name="1.jpg", more...}
url
"http://jquery-file-upload.appspot.com/AMIfv97t6a-7QLgNXnhcK10mnbBojaAwHAL2oei6ySkhR_DKk6k5kDrS_tVhlAxFnCkYncEBM3IyeZ6n_oKXItO9dhoxcizw15pPtlA3-i53vc02Oh62RSqEdbK4QLvPwsFGdBOBmCjoNu8TOfl6m0eu1-Tquspryi48UOPXNe4eRfyrEJmBlJM/1.jpg"
thumbnailUrl
"http://lh4.ggpht.com/b5ABUz-uqTUynXXNvHSlYcAj3nRnlYVIEAhcSZKWXHcwI_jaDZCaNDuNK-gPjeiIMy9nUIAiwS5l1o3IttA5MikPJOEXkQjFCg=s80"
name
"1.jpg"
type
"image/jpeg"
size
309
deleteUrl
"http://jquery-file-upload.appspot.com/AMIfv97t6a-7QLgNXnhcK10mnbBojaAwHAL2oei6ySkhR_DKk6k5kDrS_tVhlAxFnCkYncEBM3IyeZ6n_oKXItO9dhoxcizw15pPtlA3-i53vc02Oh62RSqEdbK4QLvPwsFGdBOBmCjoNu8TOfl6m0eu1-Tquspryi48UOPXNe4eRfyrEJmBlJM/1.jpg?delete=true"
deleteType
"DELETE"
RESOLVED
#RequestMapping(value = "saveImages", method = RequestMethod.POST, headers = "Accept=application/json")
public String saveImages(HttpServletRequest request,
HttpServletResponse response,
#RequestPart("files") MultipartFile files, #RequestBody String x)
throws Exception {
ByteArrayInputStream in = new ByteArrayInputStream(files.getBytes());
BufferedImage img = ImageIO.read(in);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
........
I resolved this myself and have updated the Controller to reflect it. I hope it helps others. If my answer isn't the most efficient, I would appreciate any comments. Thanks.
My environment:
Java 1.6
Spring 4.0.2.RELEASE,
Twitter Bootstrap v3.1.1
Maven 2.4.1

MVC 3 client caching

I am trying to make modifications to an existing CDN. What I am trying to do is create a short cache time and use conditional GETs to see if the file has been updated.
I am tearing my hair out because even though I am setting a last modified date and seeing it in the response headers, on subsequent get requests I am not seeing an If-Modified-Since header being returned. At first I thought it was my local development environment or the fact that I was using Fiddler as a proxy for testing so I deployed to a QA server. But what I am seeing in Firebug is so different than what I am doing. I see the last modified date, for some reason it is setting my cache-control to private, and I have cleared any header Output Caching and the only header IIS 7.5 is set to write is to enable Http keep-alive, so all the caching should be driven by the code.
This seemed like such a no-brainer, yet I've been adding and removing headers all day with no luck. I checked global.asax and anywhere else (I didn't write the app so I was looking for any hidden surprises and am stumped. Below is the current code and request and response headers. I have the expiration set to 30 seconds just for testing purposes. I have looked at several samples, I don't see myself doing anything different, but it simply won't work.
Response Headersview source
Cache-Control private, max-age=30
Content-Length 597353
Content-Type image/jpg
Date Tue, 03 Sep 2013 21:33:55 GMT
Expires Tue, 03 Sep 2013 21:34:25 GMT
Last-Modified Tue, 03 Sep 2013 21:33:55 GMT
Server Microsoft-IIS/7.5
X-AspNet-Version 4.0.30319
X-AspNetMvc-Version 3.0
X-Powered-By ASP.NET
Request Headersview source
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Connection keep-alive
Cookie __utma=1.759556114.1354835397.1377631052.1377732484.36; __utmz=1.1354835397.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
Host hqat4app1
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetLastModified(DateTime.Now);
return new FileContentResult(fileContents, contentType);
The relevant code is:
public ActionResult Resize(int id, int size, bool grayscale)
{
_logger.Debug(() => string.Format("Resize {0} {1} {2}", id, size, grayscale));
string imageFileName = null;
if (id > 0)
using (new UnitOfWorkScope())
imageFileName = RepositoryFactory.CreateReadOnly<Image>().Where(o => o.Id == id).Select(o => o.FileName).SingleOrDefault();
CacheImageSize(id, size);
if (!ImageWasModified(imageFileName))
{
Response.Cache.SetExpires(DateTime.Now.AddSeconds(30));
Response.StatusCode = (int)HttpStatusCode.NotModified;
Response.Status = "304 Not Modified";
return new HttpStatusCodeResult((int)HttpStatusCode.NotModified, "Not-Modified");
}
byte[] fileContents;
if (ShouldReturnDefaultImage(imageFileName))
fileContents = GetDefaultImageContents(size, grayscale);
else
{
bool foundImageFile;
fileContents = GetImageContents(id, size, grayscale, imageFileName, out foundImageFile);
if (!foundImageFile)
{
// No file found, clear cache, disable output cache
//ClearOutputAndRuntimeCacheForImage(id, grayscale);
//Response.DisableKernelCache();
}
}
string contentType = GetBestContentType(imageFileName);
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetLastModified(DateTime.Now);
return new FileContentResult(fileContents, contentType);
}
private bool ImageWasModified(string fileName)
{
bool foundImageFile;
string filePath = GetFileOrDefaultPath(fileName, out foundImageFile);
if (foundImageFile)
{
string header = Request.Headers["If-Modified-Since"];
if(!string.IsNullOrEmpty(header))
{
DateTime isModifiedSince;
if (DateTime.TryParse(header, out isModifiedSince))
{
return isModifiedSince < System.IO.File.GetLastWriteTime(filePath);
}
}
}
return true;
}

HttpClient request to local IIS 8.0 does not produce expected headers in the response

I'm making the following request to a local website running in IIS
var httpRequestMessage = new HttpRequestMessage();
httpRequestMessage.RequestUri = new Uri("http://localhost:8081/");
httpRequestMessage.Method = HttpMethod.Get;
var response = new HttpClient().SendAsync(httpRequestMessage).Result;
This produces the following response headers:
HTTP/1.1 200 OK
Accept-Ranges: bytes
Date: Mon, 03 Jun 2013 22:34:25 GMT
ETag: "50c7472eb342ce1:0"
Server: Microsoft-IIS/8.0
X-Powered-By: ASP.NET
An identical request made via Fiddler produces the following response headers (I've highlighted the differences):
HTTP/1.1 200 OK
Content-Type: text/html
Last-Modified: Fri, 26 Apr 2013 19:20:58 GMT
Accept-Ranges: bytes
ETag: "50c7472eb342ce1:0"
Server: Microsoft-IIS/8.0
X-Powered-By: ASP.NET
Date: Mon, 03 Jun 2013 22:29:34 GMT
Content-Length: 10
Why is there a difference in response headers?
Am I using HttpClient correctly (aside from the fact I am calling Send synchronously)?
TL;DR;
To access all response headers you need to read both HttpResponseMessage.Headers and HttpResponseMessage.Content.Headers properties.
Long(er) answer:
This, basically:
var response = new HttpClient().GetAsync("http://uri/").Result;
var allHeaders = response.Headers.Union(response.Content.Headers);
foreach (var header in allHeaders)
{
// do stuff
}
I see two issues with this:
The Headers property is not appropriately named: it should really be SomeHeaders or AllHeadersExceptContentHeaders. (I mean, really, when you see a property named Headers, do you expect it to return all headers or some headers? I am pretty sure they are in violation of their own framework design guidelines on this one.)
The MSDN page does not mention at any point the fact this is a subset of all headers and developers should also inspect Content.Headers.

Asp.Net Sending PDF to browser

I've been trying to get this aspx page to serve up a pdf. It works correctly in Firefox, but IE gives
Internet Explorer cannot download getform.aspx from SERVER_NAME
Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be found.
This is the general functionality of my code. It's spread across multiple functions (this is why we're not using WriteFile - sometimes we generate the pdf on the fly), but this is generally it:
FileStream fs = File.Open(Path.Combine(PdfBasePath, "form.pdf"), FileMode.Open, FileAccess.Read);
Stream output = Response.OutputStream;
byte[] buffer = new byte[BUFFER_SIZE];
int read_count = fs.Read(buffer, 0, BUFFER_SIZE);
while (read_count > 0)
{
output.Write(buffer, 0, read_count);
read_count = fs.Read(buffer, 0, BUFFER_SIZE);
}
fs.Close();
Response.Clear();
Response.ContentType = System.Net.Mime.MediaTypeNames.Application.Pdf;
Response.AddHeader("Content-Disposition", "attachment; filename=form.pdf");
Response.Output.Flush();
Response.End();
Looking at Fiddler, the page is being fetched using this:
GET /getform.aspx?Failure=Y&r=someencryptedstring HTTP/1.1
It is being returned to the browser thus:
HTTP/1.1 200 OK
Date: Thu, 09 Apr 2009 22:08:33 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Pragma: no-cache
Content-Disposition: attachment; filename=form.pdf
Cache-Control: no-cache, no-store
Pragma: no-cache
Expires: -1
Content-Type: application/pdf
Content-Length: 628548
This is really bugging me. I'm not using SSL, otherwise this KB article would seem to apply. Anyone have any ideas?
Is the Content-Length being returned in the header actually correct for the file you're sending? I'm just comparing this to some production code we use here and it looks like we explicitly set the Content-Length header. If I recall correctly, some browsers have a problem if the header and the actual file size don't match.
Edit
The question author found that changing the Content-Disposition header to application/download instead of application/pdf seems to work around the problem.

Resources