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.) | [reply] [Watch: Dir/Any] [d/l] |
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)
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
|
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.". | [reply] [Watch: Dir/Any] [d/l] |
|
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)
| [reply] [Watch: Dir/Any] [d/l] |
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.
| [reply] [Watch: Dir/Any] |
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.
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
use CGI;
my $header = new CGI;
print $header->header();
I hope this helps | [reply] [Watch: Dir/Any] [d/l] |
|
~>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.) | [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |
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!
| [reply] [Watch: Dir/Any] |