Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to pass images through http?

Status
Not open for further replies.

bivbivbiv

Programmer
Feb 28, 2006
9
NL
I have a perl 'gateway' running on my webserver passing all data through to an application server. The received data is send to the webserver. It works well with html data (text) but receiving binary data (images) does not work:

use IO::Socket;
$envString=GetEnvVariables();
$remote=IO::Socket::INET->new(Proto=>"tcp", PeerAddr=>'127.0.0.1', PeerPort=>'1099') or ShowError("[1099] Can't connect to server");
$remote->autoflush(1);
#
# Send data to the application server
#
print $remote $envString;
#
# Retrieve data from the application server and send it back to
# the web-server
#
while(<$remote>){print;
}
close($remote);

The mimetypes are send correctly (content-type header).

albert
 
You could try posting the files over HTTP using CGI, just a thought

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
This is a suggestion, I have no idea if it will help, try using binmode() on the filehandle before sending the data:

Code:
binmode($remote);
while(<$remote>){print;
}
close($remote);



 
Thanks for the answers. Unfortunately the last option doesn't work. The perl is the cgi application which should be able to send images in response to (i.e):

background-image:url('avw.pl?img=images/logohdr.jpg')

Only the request is sent to the appserver where the images are located. HTML does work, but images does not. I can put the images on the webserver itself, but actually this would increase maintenance effort.

The header is sent with the image info and length:

[[20060524 09:33:30] --- dm created
[[20060524 09:33:30] Client connected. Remote: 127.0.0.1/2631 Local: 127.0.0.1/1099
[[20060524 09:33:30] There is/are now 1 client(s) connected.
[[20060524 09:33:30] C>
[[20060524 09:33:30] I>127.0.0.1
[[20060524 09:33:30] P>IMAGES/LOGOHDR.JPG
[[20060524 09:33:30] Image document C:\data\delphi\services\IMAGES\LOGOHDR.JPG
[[20060524 09:33:30] HTTP/1.0: 200 OK
Connection: Keep-Alive
Content-Length: 776
Content-Type: image/jpeg


[[20060524 09:33:30] sending...
[[20060524 09:33:30] all done
....

Again, the html is working properly why not the images?
albert
 
Try this?

Code:
open (IMG, "./images/logohdr.jpg");
binmode IMG;
my @data = <IMG>;
close (IMG);
chomp @data;

binmode STDOUT;
print "Content-Type: image/jpeg\n\n";
print join ("\n",@data);

I can't seem to remember if @data should be joined at \n or at "" (nothing), experiment with both.
 
Thanks for the answer. yes this does send images, but point is that the image data is on the application server and not at the webserver. So html data and images are send by the application server. I need something to receive the data from the appserver and send it to the webserver/browser. With your sample I probabely would reference the images within the html page... But perhaps I need to save the data to a file first? Does anybody have experience of receiving binary data? The sample in the original post works well with the html pages, only reference to images are not treated ok.
 
Ah, I was too soon, Yes your solution does help a lot!

binmode($remote);
binmode(STDOUT);
while(<$remote>){print;
}

has showed me the first images THANKS!!!!!
 
for the statistics: the following perl script will accept html data and images from any app server sending correct mimetypes:
Code:
use IO::Socket;
#
# To let this script work, please replace the PeerAddr by the
# correct application server ip address (default 'localhost') and replace
# the PeerPort by the correct application server port 
#
$envString=GetEnvVariables();
#
# Connect to the application server
#
$remote=IO::Socket::INET->new(Proto=>"tcp", PeerAddr=>'127.0.0.1', PeerPort=>'1099') or ShowError("[1099] Can't connect to server");
$remote->autoflush(1);
#
# Send data to the application server
#
print $remote $envString;
#print 'Content-type: text/html'."\n\n";
#
# Retrieve data from the application server and send it back to 
# the web-server
#
binmode($remote);
binmode(STDOUT);
while(<$remote>){join print;
}
#
# ok... all done!
#
close($remote);
#
# e n d   o f   p r o g r a m 
#
#
# generate html header
#
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top