Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Sending data to browser's URI field via POST

by hacker (Priest)
on Mar 12, 2005 at 22:54 UTC ( [id://439016]=perlquestion: print w/replies, xml ) Need Help??

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

I've got this new application I'm piecing together, and I've got all of the innards coded and working, and it works well (minus some scaling issues with my understanding of mod_perl; more on that in another future node).

I'm assigning new content to a row in MySQL with an id (auto_increment) and a unique_id (some hashing of Data::UUID, thanks to revdiablo for the hint there), along with several other columns.

The goal, is that the content in the row, would be accessed by hitting a url like the following, in a browser:

http://pss.example.com/86e1b327ccc39a38

That unique_id would be the key to return the URl, content, and other items from that row in the database. Right now, I'm using an HTML form to submit that URL for fetch and conversion, using POST. If the URL submitted is unique, a new row is added. If it is not unique (already exists in the database), the duplicate row is updated.

Is it possible to submit an HTML form, which insert this row in the database, and tack on the UUID hash to the end of the URI field in the POST'd page, so that when the submitted page is rendered, the URL in the browser's URI field, is that hash, as above?

I know this is confusing-sounding, so here's an example:

  1. User hits http://pss.example.com in their browser.
  2. User enters a URL in a form field, and clicks submit
  3. System accepts URL, validates it, fetches it, and converts it, and then renders a page with that URL's content converted for the user.

Currently, using POST for the HTML form, the user's browser would continue to show http://pss.example.com in their browser's URI field.

I would like it to show something like: http://pss.example.com/86e1b327ccc39a38 instead, so they can bookmark it and use it for future fetches/conversions without having to use the form interactively.

The catch is, I can't populate the form action with that UUID, because I won't know it until after the user submits the form and the URL they enter is fetched and validated. (Before you suggest it merlyn, PATH_INFO is not an option in this case).

Any way to do this? Maybe switching to GET + some mod_rewrite fu? Something else?

Replies are listed 'Best First'.
Using PATH_INFO as your full docroot (was Re: Sending data to browser's URI field via POST)
by merlyn (Sage) on Mar 13, 2005 at 00:50 UTC
    (Before you suggest it merlyn, PATH_INFO is not an option in this case).
    Really? Why? Can you explain? I just added this to my httpd.conf (updated slightly for simplicity):
    Listen 127.0.0.1:8801 <VirtualHost 127.0.0.1:8801> Alias /images/ /Users/merlyn/Sites/virtualroots/images/ Alias /icons/ /usr/share/httpd/icons/ ScriptAlias / /Users/merlyn/Sites/virtualroots/virtualroot/ </VirtualHost>
    And then I created /Users/merlyn/Sites/virtualroots/virtualroot as a shell executable script:
    #!/bin/sh echo content-type: text/plain echo printenv
    And now I can visit http://localhost:8801/some/place?key=value, and my CGI script is called with /some/place as the PATH_INFO and key=value for the QUERY_STRING. And I can visit /images/ and get a directory listing with the right icons, and files there are also served properly.

    So, explain again why you're allergic to PATH_INFO?

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: Sending data to browser's URI field via POST
by Corion (Patriarch) on Mar 12, 2005 at 23:00 UTC

    How about a 302 redirect then?

    my $rewritten_url = 'http://pss.example.com/23487129378378123'; print "302 Moved\n"; print "Location: $rewritten_url\n"; print "\n"

      I'd agree on using an HTTP redirect, but not necessarily a 302:

      Note: RFC 1945 and RFC 2068 specify that the client is not allowed to change the method on the redirected request. However, most existing user agent implementations treat 302 as if it were a 303 response, performing a GET on the Location field-value regardless of the original request method. The status codes 303 and 307 have been added for servers that wish to make unambiguously clear which kind of reaction is expected of the client

      You'd want to use a 303 if the browser supports HTTP/1.1, 302 of they support HTTP/1.0, and no status, just the Location: header if it's pre-HTTP/1.0.

      You may have to change the logic of your program to deal with GET to the rewritten URL differently from a POST to it, to deal with someone being sent there the first time, vs. coming back to it later for an update.

Re: Sending data to browser's URI field via POST
by chromatic (Archbishop) on Mar 12, 2005 at 23:04 UTC

    Have you considered redirecting to the URL you prefer after receiving the POST? That could also have the nice effect of preventing some types of multiple submissions.

      I don't think this will work well, because it will cause a double-fetch and double-render of the content. Once the form is submitted, the content is fetched, converted, and displayed to the user.

      If I then use a 302, I will end up doing an additional SELECT to pull the url out of the db with that unique hash, then fetch, convert, and render the content to the user again at that final redirected url....

      Though, I store the original and converted content in the db also, so I could avoid a re-fetch over port 80 at least, if I just do a SELECT from there...

      But I'll have to do a pretty significant rewrite to avoid this from happening.

      I'll have to think about this a bit more. Thanks for the suggestion.

        Once the form is submitted, the content is fetched, converted, and displayed to the user. If I then use a 302, I will end up doing an additional SELECT to pull the url out of the db with that unique hash, then fetch, convert, and render the content to the user again at that final redirected url....

        Then don't send the redirect after fetching and rendering the data, do it instead, that way you only render it once.


        We're not surrounded, we're in a target-rich environment!
Re: Sending data to browser's URI field via POST
by TedPride (Priest) on Mar 12, 2005 at 23:19 UTC
    Process the form data, submit it to the mySQL database, collect the ID, and forward the browser to the proper URL. As mentioned above, a Location header will do this:

    print "Location: $newurl\n\n";

    but I personally wouldn't mess around with header codes. All you have to send from your script to make this work is the Location header.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2024-04-18 16:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found