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

I recently discovered Makino Takaki's boilerplade Perl proxy program (http://www.snowelm.com/~t/doc/tips/simple-proxy.en.html). I found this code to be relatively straight forward to use for the purpose of better understanding this mechanism and to experiment with.

i've added a few lines of my own code to this boilerplate program, and am having trouble getting...

$c->send_redirect("file:///C:/http_proxy_server/log.html", 303)

to work as I'm wanting it to work. according to documentation, above command redirects the client to a new url, and the HTTP header code set to (in this case) 303 (meaning 'See Other'). when my Perl proxy is running, I am wanting to use it in a fun way (where I can send it 'commands' - encoded as URL get requests).

with my web browser viewing a local file 'log.html' file (which contains many HTML forms). When I click one particular form, the form sends a get request like this 'http://proxycommand.code?function=AppendNewRecordToLogHtmlFile&newrecord=helloworld' to my Perl proxy server.

my proxy program can then search the requested URL to see if it contains 'proxycommand.code'. if so, it then parses the GET query string to discover the special function I want my proxy to execute. In this case, the special function is to append a new record to the 'log.html' file, where the new record is 'hello world'.

in my code, im able to successfully append the new record to 'log.html'. i sleep for a few seconds. i then send the above Perl command to tell the browser to redisplay the newly modified 'log.html' file

in the chrome web browser, the browser itself is satisfied that its received a wellformed HTTP reply, the address field of the browser does reflect the newly requested local file. but since the browser was previosly viewing the exact same file (prior to my clicking the form submit button) - perhaps some caching mechanism is preventing the browser from showing the newly added record at the bottom of the 'log.html' file. if i manually click the chrome browsers 'refresh' button - this causes the appended new record to display in the browser.

does anyone know why the browser is not displaying the newest version of the 'log.html' file? is this due to browser caching issues? if so, what is the solution? I would think this LWP-UserAGent module would automatically include a newer HTTP 'Date' header, but maybe its not, and i need to manually set this header myself? One website suggested setting a NON-standard HTTP header, 'Refresh: 1' which supposedly is used by most browsers but not an official part of RFC2616.

any help here would be appreciated, as i'm a little confused

Replies are listed 'Best First'.
Re: Perl HTTP proxy experts
by Anonymous Monk on Sep 17, 2014 at 18:57 UTC

    Since you're using a file:// URL to display your log.html, support for auto-refreshing that seems to vary between browsers. For Chrome, the Tincr extension at http://tin.cr/ sounds like it might do the trick. Otherwise, Google browser refresh local file or similar.

    Of course, you could just set up a regular refresh of the page, but then it'll refresh itself constantly, even when the file doesn't change.

    If you want a more general solution, perhaps you should serve up your log.html through an HTTP server, where you can control cache timeout values.

      like you, i initially thought this was a matter due to caching. i then changed my script to serve a file that has NEVER PREVIOUSLY been opened in the browser (to see if in fact, the client browser would request in response to the redirect HTTP headers). unfortunately, the file referenced in the HTTP redirect was NOT opened in the browser - so this tells me the problem has nothing to do with caching.

      ive distilled the program down to its 'bare-bones' version to show the relevant code. this is not my actual program, but only serves to demonstrate the problem.

      use HTTP::Daemon; use LWP::UserAgent; my $ua = LWP::UserAgent->new(); my $d = HTTP::Daemon->new( LocalHost => "localhost", # remove this to listen from other + machines # (i.e. open-relay... be careful o +f spammers!) LocalPort => 8888 #always best to run 'netstat -ano | fi +nd "8888"' prior to running this command (to find an unused port) ) || die; #after this program is launched ru +n again 'netstat -ano | find "8888"' to confirm this port is now bein +g bound to this program #then to actually use this with a +browser must go to browsers proxy setting (a global windows setting): + set server=127.0.0.1 port=8888 #main 'accept-getrequest-process' loop is the next two lines while (my $c = $d->accept) { while (my $request = $c->get_request) { #we wish to send the web client browser a redirect to a local fi +le on his same machine #as a test of the nifty 'send_redirect' function #since this particular file has never before been opened in the +web browser #if it does NOT open/display in the client web browser #the fault for this can NOT be attributed to caching issues #unfortunately, when this proxy program is ran, and the computer + environment told to use this proxy #this page never displays in the client web browser window (i am + unsure as to why it does not display. #ive also tried various "300" codes, 301, 302,303, 307 with no c +hange in the result.) #because 'Wireshark' cant capture traffic thats on the localhost + (eg: not being sent out of the network card) #i have no way to 'view' the actual data being sent between the +client program and the proxy program (on Windows) $c->send_redirect("file:///C:/http_proxy_server/FileThatsNeverBe +foreBeenOpenedInTheBrowser_eg-NotCached.html", 303); } $c->close; undef($c); }

      note because the client (browser app) and server (perl proxy server program) are both running on the same machine - this is particularly hard to troubleshoot as 'wireshark' does not capture any TCP packets that are not actually sent out of the network card. does anyone know how to monitor TCP/IP traffic sent/received to the 'localhost' only?

        According to the Browser Security Handbook, it's not possible for websites to redirect to file: URLs. The rationale is explained a bit in the section "URL scheme access rules" just before the linked section.

        Since you've already got a working HTTP server, why not serve the file over that, via a special URL?

Re: Perl HTTP proxy experts
by FloydATC (Deacon) on Sep 19, 2014 at 08:47 UTC

    Just a thought; have you made sure the log file has been properly written and closed before you send the "redirect" response? If not, you may be redirecting the browser to a file that isn't ready yet, and you will get the old log file (or no logfile) ...unless the browser happens to be just slow enough for the intended mechanism to work anyway.

    -- FloydATC

    Time flies when you don't know what you're doing