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

This question isn't so much about Perl.
Let me know if I should take them elsewhere.
But it begins with PERL. I'm running ActiveState5.10 on my Windows XP laptop. Running a Perl program that is attempting to HTTP POST to a file at my website. When posting to a x.txt file with 777 I get:

405 Method Not Allowed
Method Not Allowed
The requested method POST is not allowed for the URL /x.txt.

Seemed clear to me....So I open a question/problem with my ISP about POSTing. And they reply that a Perl program can only POST to a file that ends with .pl. I replied that I didn't think the type of code that issued the POST was relevant but gave their suggestion a try.
That round cause some errors that looked as if the file being posted to was being executed as a Perl program. ISP then advised me to update my permissions to 755. Which ended in the message below:

C:\Perl\perlscripts>put05.pl
HTTP/1.1 500 Internal Server Error
Connection: close

It makes since that not just anyone can POST to any file on a website and so I suspect it's more of a permission/security issue. But just wanted to make sure I had a good overall understanding. Must Perl only POST to .pl files? Anyone have a area about POSTing that I should look at first?
Thanks Monks!
kc

Replies are listed 'Best First'.
Re: HTTP POST
by zentara (Cardinal) on Mar 10, 2008 at 16:14 UTC
    Must Perl only POST to .pl files?

    That's not true at all. Google for "cgi post vs get", or see get vs post .So you can post to a script in any language. The thing about a post, is that it contains a message body after the header, usually a file encoded for upload. Now, the rumor you heard that only a .pl file can accept a post, is just confusion over how the web server is setup. You can configure a web server, so that perl scripts must end in .pl, but it is not a hard-and-fast rule, some servers are setup so that scripts must end in .cgi. Basically, a GET request contains all information in the url string that is sent, while a POST sends a url string, followed by a block of encoded information, which is usually the file.

    Now a script can be written to NOT allow posts, as a security measure, so that may be the source of your first error. If a script does allow posts. it needs permissions to write the file on the server. Usually you need to write to a directory with 777 permissions, since a webserver usually runs with the lowest of permission levels, like "user:nobody, group:nobody".

    You can test posting with this script, which will print out whatever you post to it. Name it showpost.cgi or showpost.pl

    #!/usr/bin/perl -wT use strict; my $buffer; read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); print "Content-type: text/plain\n\n"; print $buffer;

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: HTTP POST
by Corion (Patriarch) on Mar 10, 2008 at 14:27 UTC

    The error HTTP/1.1 500 Internal Server Error means that some internal error occurred. Look in the web server error log to find a more detailed error description.

    Most likely, you have an error in your receiving Perl program.

    As for the terminology - you don't "POST to a file" on a webserver, you "send a POST request to an URL on a webserver". That URL is commonly handled by a program, and it does not matter in what language that program is written.

      Maybe I got the basic concept wrong. Isn't POST about the same a 'write'? I want to write a message from my PC over the internet, to a file on a webserver.
      And the final goal, there is a file (.txt) that I just want to zero out. After reading the info remotely I want to empty it and then write an new record in it.

      I'm off studying. UPDATE: # The request URI is not a resource to retrieve; it's usually a program to handle the data you're sending.

        POST requests don't modify files on the server, unless you write a server side script that modifies them.

        Maybe you're confusing it with the PUT request that is used for example in WebDAV?

        A post is not going to write to a file, a post sends data to a program that then does something with it.

        what it sounds like you need to do is write a Perl program on your server, that will accept the data, then write it to your .txt file.

        g_White
Re: HTTP POST
by moritz (Cardinal) on Mar 10, 2008 at 14:28 UTC
    The URL doesn't have to be tied to file type that handles it.

    It's no problem to have a perl script handle a URL that ends in /x.txt - usually I don't show any file extensions for HTML files because I don't think the user should be bothered with implementation details.

    But of course it doesn't make any sense to send a POST request to static file. POST requests are meant for activities that change the status of a server in some way, so normally a POST request is handled by some sort of CGI script.

      POST requests are meant for activities that change the status of a server in some way, so normally a POST request is handled by some sort of CGI script

      There are other legitimate reasons to use POST over GET, even when you're not changing state.

      For instance, if doing user authentication, if we were to send the password and other authentication information via a GET, it could be cached, making it a problem on public machines (eg, computer labs in schools). Now, in this case, most people don't link to a static file directly, but indirectly through some program that handles the authentication and authorization layers, but it could be handled by apache filters instead of a CGI or similar.

      If you're trying to support older clients that don't correctly handle cache control headers (not as big an issues these days), and you don't want them to cache the content, forcing a POST request can get around the issue.

      ...

      But I agree with not revealing file extensions if you don't have to -- it then requires URL rewriting it you decide down the road that you're going to change the backend, and the host requires file extensions to correctly process the files.

        For instance, if doing user authentication, if we were to send the password and other authentication information via a GET, it could be cached,

        One of the reasons it should not be cached is that authentication does change state on the server - it usually creates a session, and stores the "this user is logged in" information in the session.

        Of course security is another reason, but your example shows that not all state changes are obvious ;-)