Gossamer Forum
Home : General : Perl Programming :

Printing images and html together?

Quote Reply
Printing images and html together?
Is it possible to print a gif and html code on one page?....ie I can do:

print "Content-type: image/gif\n\n";
print $code;

or...

print "Content-type: text/html\n\n";
print $code;

...but I need to print the image inside the html...is this possible?
Quote Reply
Re: [Paul] Printing images and html together? In reply to
What you are asking is not really a Perl question. It's a question about the HTTP protocol.

A web server sends one header with each browser request. Therefore, you can not mix and match data within a specific HTTP header. Once you've set an image/gif header then you're stuck with it. All the data after that header is expected by the browser to be image/gif until the connection is closed.

Your only solution would be to send a text/html header and then incorporate your image into your webpage using HTML tags. The image will then be requested by the browser.

I suggest you read RFC 2616 (ftp://ftp.isi.edu/in-notes/rfc2616.txt) which should give you more detailed information about the HTTP 1.1 specification.

- wil
Quote Reply
Re: [Paul] Printing images and html together? In reply to
I figured it out...for anyone interested....

Code:
#!/perl/bin/perl
#==========================================================

use strict;
use CGI qw/:param :header/;
use CGI::Carp qw/fatalsToBrowser/;

param('img') ? img() : main();

#==========================================================

sub main {
#----------------------------------------------------------
# Print the html.

print header();
print q|<img src="test.cgi?img=title.gif">|;
}

sub img {
#----------------------------------------------------------
# Read and print the image.

my $path = "/apache/htdocs/images/desk/@{ [ param('img') ] }";

open IMG, "<$path" or die "Can't open $path: $!";
binmode IMG;
read IMG, my $bin, -s IMG;
close IMG;

print header('image/gif');
print $bin;
}

Last edited by:

Paul: Jul 28, 2002, 11:15 AM
Quote Reply
Re: [Paul] Printing images and html together? In reply to
But if you do end up using that (or anyone copying it), make double sure to check your form input before passing it to open. =)

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Printing images and html together? In reply to
I almost edited the post after I made it because I knew someone would point that out...it was just my test script to make sure it worked and not the actual code....although it is for an installation script which is deleted when installation is complete so it isn't really an issue.

But it would just print a dead image if you tried to print anything other than an image wouldn't it?

Last edited by:

Paul: Jul 28, 2002, 4:14 PM
Quote Reply
Re: [Paul] Printing images and html together? In reply to
Yup, I was more concerned about someone copying it. It would display a dead image, but if you used something like lynx -source to get the output you could view a users passwd file. Try:

lynx -source http://yourdomain/.../../../../etc/passwd

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Printing images and html together? In reply to
But the path ""/apache/htdocs/images/desk" was hard coded, so they couldn't go back beyond that could they?
Cheers,
Michael Bray
Quote Reply
Re: [Michael_Bray] Printing images and html together? In reply to
I guess they could using ../../../blah as the path, but it was on windows so they wouldn't be getting my passwd file Cool
Quote Reply
Re: [Alex] Printing images and html together? In reply to
or..

test.cgi?img=boo;%20rm%20/home/paul/public_html/index.html;%20owned%20>%20/home/paul/public_html/index.html

Wink
Quote Reply
Re: [widgetz] Printing images and html together? In reply to
Can't do that as the file open mode is forced to read so it doesn't go to shell.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Printing images and html together? In reply to
doh.

well yea.. the thing you mentioned reminds me of http://try2hack.nl level 8.

uses the same type of exploit.