Hello monks, I have this code that's driving me nuts... I have a code that works well with most sites but as soon as I try to POST, it just waits and would eventually timeout. Please help!
#!c:\bin\perl -w use URI; use IO::Socket; #use threads('yield', 'stack_size' => 64*4096); use threads; use strict; my $debug = 1; #suppress PIPE signal $SIG{PIPE} = 'IGNORE'; # command-line arguments my $proxy_port=shift(@ARGV); $proxy_port=8080 unless $proxy_port =~ /\d+/; # create socket connection for proxy my $proxy = IO::Socket::INET->new ( LocalPort => $proxy_port, Type => SOCK_STREAM, proto => 'tcp', Reuse => 1, Listen => 10); binmode $proxy; # create thread for each browser request while (my $browser_request = $proxy->accept()) { binmode $browser_request; my $t = threads->new(\&fetch, $browser_request); $t->detach; } # sub routine called by each thread sub fetch{ my $browser = $_[0]; my $method =""; my $content_length = 0; my $content = 0; my $accu_content_length = 0; my $host; my $hostAddr; my $httpVer; #my $count; #my $request; while (my $browser_line = <$browser>) { # check method from browser request unless ($method) { ($method, $hostAddr, $httpVer) = $browser_line =~ /^(\w+) +(\S+) +(\S+ +)/; # get host name and port from browser and create new socket connection + to host my $uri = URI->new($hostAddr); $host = IO::Socket::INET->new ( PeerAddr=> $uri->host, PeerPort=> $uri->port ); die "couldn’t open $hostAddr" unless $host; binmode $host; print $host "$method ".$uri->path_query." $httpVer\n"; print "METJHOD: $method ".$uri->path_query." $httpVer\n"; next; } $content_length = $1 if $browser_line=~/Content-length: +(\d+)/i; $accu_content_length+=length $browser_line; if ($debug == 1){ print "[$count] $browser_line \n"; $count++ } print $host $browser_line; # check if last line last if $browser_line =~ /^\s*$/ and $method ne "POST"; if ($browser_line =~ /^\s*$/ and $method eq "POST") { $content = 1; last unless $content_length; next; } #print "COUNTER POST: $count\n"; #$request .= $browser_line; if ($content) { $accu_content_length+=length $browser_line; last if $accu_content_length >= $content_length; } } #print "\n\n\n REQUEST: $request"; $content_length = 0; $content = 0; $accu_content_length = 0; while (my $host_line = <$host>) { if ($debug == 2){ print $host_line; } print $browser $host_line; $content_length = $1 if $host_line=~/Content-length: +(\d+)/i; if ($host_line =~ m/^\s*$/ and not $content) { $content = 1; #last unless $content_length; next; } if ($content) { if ($content_length) { $accu_content_length+=length $host_line; #print "\nContent Length: $content_length, accu: $accu_content_length\ +n"; last if $accu_content_length >= $content_length; } } } $browser-> close; $host -> close; }

In reply to cannot do a POST with proxy by majinbis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.