displaying different content types in same page

S

subha

Hi All,

I'm new to perl.. I just want to know how to display
different content types in the same page.

I want to display image and html table in a same page using perl(cgi
script).

If anyone is familiar with this, just tell me how to
implement that.

For image:
print "Content-type: image/png\n\n";

For html,
print "Content-type: text/html\n\n";

I want to display both in the same page.

Please help me out..

Thanks in advance.
 
A

A. Sinan Unur

I'm new to perl..

That does not matter because your question has nothing to do with Perl.
I just want to know how to display different content types in the same
page.

I want to display image and html table in a same page using perl(cgi
script).

If anyone is familiar with this, just tell me how to
implement that.

For image:
print "Content-type: image/png\n\n";

For html,
print "Content-type: text/html\n\n";

I want to display both in the same page.

That is not how HTTP works. Each object requires a separate request.

Sinan
 
A

A. Sinan Unur

Gunnar Hjalmarsson said:

Copied and pasted from GG:
#!c:/perl/bin/Perl.exe -T

You use -T, that is good, but
# test.pl
use strict;
use warnings;

my $imagedir = '/path/to/image/directory';
my $imagename = 'myimage.gif';
my $ctype = 'image/gif';

use CGI;
my $cgi = CGI->new;
if ( $cgi->param('img') ) {
imgprint("$imagedir/".$cgi->param('img'), $ctype);

File::Spec->catfile would be better IMHO.

....
sub imgprint {
my ($path, $type) = @_;
print $cgi->header($type);
open my $f, $path or die "Couldn't open $path: $!";

You do not untaint $cgi->param('img') at all. This script can
be used to access any file on the system.

Check your server logs. I suspect there will be a warning that
it was too late to set taint mode.

The script would be safer if you just sent $imagename when
$cgi->param('img') is set.

Sinan
 
G

Gunnar Hjalmarsson

A. Sinan Unur said:
Copied and pasted from GG:


You use -T, that is good, but


File::Spec->catfile would be better IMHO.

Agreed. (At least more portable.)
You do not untaint $cgi->param('img') at all. This script can
be used to access any file on the system.

Ouch, you are right!! I don't think it has anything to do with
(un-)tainting, though, but it has everything to do with the fact that I
used user data without validating it first. :(
Check your server logs. I suspect there will be a warning that
it was too late to set taint mode.

No, nothing there.
The script would be safer if you just sent $imagename when
$cgi->param('img') is set.

Yes, indeed it would.

Thanks for pointing out my mistake!
 
J

Jürgen Exner

subha said:
I'm new to perl.. I just want to know how to display
different content types in the same page.

I want to display image and html table in a same page using perl(cgi
script).

You would do it exactly the same way as if you CGI program was written in
any other programming language but Perl.

In short: you partitioned your problem the wrong way. You have an HTTP
question that has nothing to do with Perl. Or CGI for that matter because
the HTTP response could have come from a static file without any CGI program
involved at all.

jue
 
A

A. Sinan Unur

Agreed. (At least more portable.)


Ouch, you are right!! I don't think it has anything to do with
(un-)tainting, though,

Well, presumably, to untaint it, you would have to validate it against a
non-trivial regex ( /(.*)/ does not count ;-). That's what I had meant.
No, nothing there.

Hmmm ... I will have to back and see what I had done to trigger that. On
the other hand, I do not understand why this script runs if -T actually
does take effect.
Yes, indeed it would.

Thanks for pointing out my mistake!

Hey, I am glad I got to post something useful for a change rather than
the same old drivel about posting guidelines.

Sinan
 
G

Gunnar Hjalmarsson

A. Sinan Unur said:
Well, presumably, to untaint it, you would have to validate it against a
non-trivial regex ( /(.*)/ does not count ;-). That's what I had meant.

Well, okay. To prevent confusion I think it's wise to distinguish
between untainting and validation, even if you sometimes can do both in
one step.
 
I

Ian Wilson

subha said:
Hi All,

I'm new to perl.. I just want to know how to display
different content types in the same page.

I want to display image and html table in a same page using perl(cgi
script).

If anyone is familiar with this, just tell me how to
implement that.

For image:
print "Content-type: image/png\n\n";

For html,
print "Content-type: text/html\n\n";

I want to display both in the same page.

This isn't a perl question but what the heck ...

If the png has to be generated dynamically then I'd have separate
scripts to provide the HTML and PNG.

/\/\/\/\/\/\/\/\/\/\/\/\/\ page.pl /\/\/\/\/\/\/\/\/\/\/\/\/\/
#!perl
use strict;
use warnings;
print <<End
Content-type: text/html

<html>
<head><title>Test</title></head>
<body>
<table>
<tr><td>Apples</td><td>1</td></tr>
<tr><td>Oranges</td><td>0</td></tr>
</table>
<img src="/cgi-bin/image.pl?Orange" Alt="A delicious orange"/>
</body>
</html>
End
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
If the above were all you had, then you'd use static HTML not Perl, lets
assume you are generating the html dynamically and using appropriate CGI
modules.

/\/\/\/\/\/\/\/\/\/\/\/\/\ image.pl /\/\/\/\/\/\/\/\/\/\/\/\/\/
#!perl
use strict;
use warnings;
#
# create and emit png here
#
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Of course, you could combine both scripts onto one which decides which
part to emit (HTML or PNG) based on parameters, The users web browser
will take care of submitting two requests for both components of the
page, you'd just publish the URL for the HTML emitting behaviour
e.g. http://www.example.com/cgi-bin/mypage?HTML

YMMV
 
A

Alan J. Flavell

Aaaaaaaaargh!

;-)

Indeed -T is a valuable protection mechanism - but the protection that
it gives is confined (in some sense) to "system" activities, i.e it is
intended to protect the system against itself.

But if you're generating web transactions, -T doesn't automatically
protect you against putting tainted data into those transactions! This
maybe wasn't quite exactly that context, but it all seems to fall
under the general heading of thinking - and thinking hard - about web
security when writing these kind of scripts.

You knew all that, of course, already, but I'm just making the point
in the hope that it helps some other readers of the group.
 
X

xhoster

subha said:
Hi All,

I'm new to perl.. I just want to know how to display
different content types in the same page.

Perl generally doesn't display content types on pages. That is the job
of a web browser.
I want to display image and html table in a same page using perl
(cgi script).

Others have pointed out how you can do it using different invocations of
one or more Perl scripts. However, if you are willing to restrict yourself
to RFC 2397 capable browser (i.e. not I.E.) then you could use the data:
URI thingy to accomplish this all in one invocation. see:
http://en.wikipedia.org/wiki/Data:_URL


Xho



Xho
 
X

xhoster

Perl generally doesn't display content types on pages. That is the job
of a web browser.


Others have pointed out how you can do it using different invocations of
one or more Perl scripts. However, if you are willing to restrict
yourself to RFC 2397 capable browser (i.e. not I.E.) then you could use
the data: URI thingy to accomplish this all in one invocation. see:
http://en.wikipedia.org/wiki/Data:_URL

And since this is a Perl group afterall, I should have mentioned:
perldoc URI::data

Xho
 
T

Tad McClellan

I just want to know how to display
different content types in the same page.


You cannot display different content types in the same page.

Try to want something else. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top