Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Non-CGI perl scripts on a web server?

by Amblikai (Scribe)
on Feb 23, 2015 at 13:12 UTC ( #1117538=perlquestion: print w/replies, xml ) Need Help??

Amblikai has asked for the wisdom of the Perl Monks concerning the following question:

So i may have this completely wrong and if so i apologise for the daft question! But...

I'm no web developer but have a really basic web site that i'm messing around with. I write a lot of perl and i want a button on the website to execute a perl script.

So far i've managed to so far get a hello_world.pl to execute. And so comes my question: The only way i could get it to work was by inserting the line: print "Content-type: text/html\n\n"; into the script

What if the perl script is not generating any html or anything? Say it's just a script which accesses a mySQL database and writes a file or runs some kind of system command. Do i still need the print "Content-type: text/html\n\n"; Are there other types of "Content-type"

Feel free to tell me to p*** off and do some more reading but my google-fu is clearly lacking and i might just need a nudge in the right direction. If you have any links, they'd be greatly appreciated!

Thanks!

Replies are listed 'Best First'.
Re: Non-CGI perl scripts on a web server?
by dsheroh (Monsignor) on Feb 23, 2015 at 14:08 UTC
    The Common Gateway Interface (CGI) requires that the program invoked via CGI (i.e., your script) must return an appropriate HTTP response to the web server, which is then passed back to the client (i.e., the user's web browser). Failing to do so will produce the all-too-familiar "500 Internal Server Error" response.

    Or, coming at it from the other side, when the user clicks a button to submit a form, their browser takes them away from that page, so your CGI script needs to provide a new page for the browser to display. Even if the purpose of the script is just to access a database or run a system command, it's still appropriate to return a page stating that the operation was completed.

    (Granted, the button could be an <INPUT type=button> or such rather than a traditional submit button, in which case it won't take the user away from the page and, unless you're using AJAX, the response won't be displayed to the user. In that case, returning a minimal response with just the Content-Type header is likely appropriate. But you still need to return something to avoid a 500 error, even if the error wouldn't be displayed anywhere other than in the web server's error log.)

Re: Non-CGI perl scripts on a web server?
by Happy-the-monk (Canon) on Feb 23, 2015 at 13:27 UTC

    What if the perl script is not generating any html or anything? ... Are there other types of "Content-type"

    Yes, what if? There are all sorts of Content-type depending of what it is you want the web server to output towards the requesting browser. You could be calculating an individual animated gif image file and hand that back, then you'd use Content-type: image/gif

    Cheers, Sören

    Créateur des bugs mobiles - let loose once, run everywhere.
    (hooked on the Perl Programming language)

      Thanks for your reply, so if there's nothing being returned to the browser i don't need the line at all?

        Every request to a HTTP server is supposed to return something. There is no empty reply.

        If there really is "nothing" to return, consider either outputting a redirect:

        Status: 302 Location: http://somewhere.example.com/

        ... or alternatively, outputting a page that says "Request submitted, thank you.".

        Thanks for your reply, so if there's nothing being returned to the browser i don't need the line at all?

        If you don't mind the browser possibly displaying a "500" Internal Server Error and a "404" Not found in return, you don't.

        A simple way of stopping it from doing that may be a print "Content-Type: text/plain\n\n"; to deliver back an empty text document.

        Cheers, Sören

        Créateur des bugs mobiles - let loose once, run everywhere.
        (hooked on the Perl Programming language)

Re: Non-CGI perl scripts on a web server?
by pobocks (Chaplain) on Feb 23, 2015 at 18:22 UTC

    Feel free to disregard if you've already considered this, but what you're doing here seems potentially really dangerous - even if the script itself is innocuous, it could be used to DOS your system.

    Make sure that your web site is behind some kind of authentication, and that your script isn't able to be subverted to do damage to the system. If you're taking any kind of user input, it needs to be carefully sanitized. Etc, etc.

Re: Non-CGI perl scripts on a web server?
by mr_mischief (Monsignor) on Feb 23, 2015 at 19:08 UTC

    There's text/plain if you just want to return a text document. Other file types include image/png.

    The Content-type header takes a MIME type. There's a list of many of those at Wikipedia. Many of those are valid and understood as document types by popular web browsers. Some require external applications be triggered for rendering. A few of them are more appropriate to other uses such as documents being sent back to the web server or messages parsed by email clients.

    There are also XMLHTTPRequest and related methods for JavaScript. If you want to just return a notification in the current page, you could return plaintext, HTML, JSON, or XML (or, indeed, other things) and update the current page using JavaScript to show the results. Libraries like jQuery make this simple.

      The other way to out put a header would be:

      use CGI; my $header = new CGI; print $header->header();

      I hope this helps

        And at least a few more ways of which I donít doubt mr_mischief was aware. :P

        ~>perl -MCGI -e 'print CGI->header' Content-Type: text/html ~>perl -MCGI -e 'print CGI->header("text/plain")' Content-Type: text/plain ~>perl -MCGI=:standard -e 'print header("text/plain; charset=UTF-8")' Content-Type: text/plain; charset=UTF-8

        (Update: chopped p rint fixed.)

        I don't recall anyone asking nor answering about alternative ways to output headers. The question I answered was about alternate values for the header.

Re: Non-CGI perl scripts on a web server?
by Amblikai (Scribe) on Feb 23, 2015 at 14:23 UTC

    Thanks everyone for your replies. I think i understand the main points! Hopefully not too daft a question. Cheers!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1117538]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2022-09-28 03:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (124 votes). Check out past polls.

    Notices?