收到HTTP POST请求后,如何在Perl中的空白行后获取该行?

时间:2011-07-09 04:54:11

标签: perl sockets webserver httprequest

检测到空行后我的程序似乎停止,直到我按下broswer中的停止按钮。

按下停止按钮之前:

enter image description here

按下停止按钮后:

enter image description here

以下是代码的一部分:

while (accept CONNECTION, SERVER ) {
  select CONNECTION; $| = 1; select STDOUT;
  print "\n>> Client connected at ", scalar(localtime), "\n";

  my $isGet = 1;
  my $isPostAndBlankLineDetected = 0;
  while (<CONNECTION>) {
    s/\r?\n//;
    my $msg = $_;
    rubyP "$msg";

    if ($msg  =~ /GET/) {
      processGet($msg);
      last;
    }

    if ($msg  =~ /POST/) {
      setReqMethodAndReturnUri($msg);
      $isGet = 0;
    }

    if ($isPostAndBlankLineDetected) {
      pp "isPostAndBlankLineDetected is true";
      last;
    }

    if( ! $isGet) { #isPost
      if ($msg  =~ /Content-Length/) {
        setContentLength($msg);
      }

      if ($msg eq "") {
        $isPostAndBlankLineDetected = 1;
        pp "done setting isPostAndBlankLineDetected";
      }
    }
  }

  close CONNECTION;
  print ">> Client disconnected\n";
}

我在last中有一个if ($isPostAndBlankLineDetected)声明。

这是套接字部分:

use Socket;

require "helper.pl";

sub rubyP { #print raw string
  my $arg = $_;

  use Data::Dumper;
  $Data::Dumper::Useqq = 1;
  print Dumper $arg;

}


sub pp {
  print "DEBUG: '$_[0]'\n";
}



my $protocol = getprotobyname 'tcp';

my $port = 15032;
my $server_addr = sockaddr_in($port, INADDR_ANY);

socket SERVER, AF_INET, SOCK_STREAM, $protocol
  or die "Unable to create socket: $!";

bind SERVER, $server_addr
  or die "Unable to bind: $!";

listen SERVER, SOMAXCONN;

1 个答案:

答案 0 :(得分:3)

你错误地认为后面有一条“线”。即使有事情发生,也可能不会因为换行而结束。

read Content-Length字节。