简体   繁体   English

HTTP ::守护进程和线程

[英]HTTP::Daemon and threads

I have the following code on Windows XP and ActiveState ActivePerl 5.8. 我在Windows XP和ActiveState ActivePerl 5.8上具有以下代码。

What could be the problem with it? 可能是什么问题? Why does it not work? 为什么不起作用?

I tried to set it as a proxy to my IE but when I connect to some URLs from my IE nothing happens. 我试图将其设置为IE的代理,但是当我从IE连接到某些URL时,没有任何反应。 The code enters the thread function and nothing happens. 代码进入线程函数,什么也没有发生。

use HTTP::Daemon;
use threads;
use HTTP::Status;
use LWP::UserAgent;

my $webServer;
my $d = HTTP::Daemon->new(
    LocalAddr => '127.0.0.1',
    LocalPort => 80,
    Listen    => 20
) || die;

print "Web Server started!\n";
print "Server Address: ", $d->sockhost(), "\n";
print "Server Port: ",    $d->sockport(), "\n";

while (my $c = $d->accept) {
    threads->create(\&process_one_req, $c)->detach();
}

sub process_one_req {
    STDOUT->autoflush(1);
    my $c = shift;
    while (my $r = $c->get_request) {
        if ($r->method eq "GET") {
            print "Session info\n", $r->header('Host');
            my $ua       = LWP::UserAgent->new;
            my $response = $ua->request($r);
            $c->send_response($response);

        } else {
            $c->send_error(RC_FORBIDDEN);
        }

    }
    $c->close;
    undef($c);
}

I added the following line to the code before LWP::UserAgent->new and it seems to be working for me (in linux). 我在LWP :: UserAgent-> new之前的代码中添加了以下代码,它似乎对我有用(在Linux中)。

$r->uri("http://" . $r->header('Host') . "/" . $r->uri());

The uri that you got from the HTTP::Request object from the original request does not have the hostname. 您从原始请求的HTTP :: Request对象获得的uri没有主机名。 So added it to make it a absolute uri. 因此添加了它,使其成为绝对uri。 Tested as follows: 测试如下:

$ curl -D - -o /dev/null -s -H 'Host: www.yahoo.com' http://localhost:8080/
HTTP/1.1 200 OK
Date: Thu, 27 Jan 2011 12:59:56 GMT
Server: libwww-perl-daemon/5.827
Cache-Control: private
Connection: close
Date: Thu, 27 Jan 2011 12:57:15 GMT
Age: 0
---snip--

UPDATE: Looks like I was completely wrong. 更新:看来我完全错了。 I didnt need to make the change to URI object. 我不需要对URI对象进行更改。 Your original code worked for me as it is in Linux 您的原始代码对我有用,就像在Linux中一样

If I recall correctly, this is because of the threading model in Windows where file handles are not passed between processes unless specifically asked for. 如果我没记错的话,这是因为Windows中的线程模型,除非特别要求,否则文件句柄不会在进程之间传递。 This PerlMonks post seems to shed some light on the underlying problem, and may lead to an approach that works for you (I imagine you may be able to call the windows API on the file descriptor of of the client connection to allow access to it within the spawned thread). 该PerlMonks帖子似乎揭示了一些潜在的问题,并可能导致一种适用于您的方法(我想您可能能够在客户端连接的文件描述符上调用Windows API,以允许在内部访问该API生成的线程)。

Perl threads on Windows generally make my head hurt, while on UNIX-list systems I find them very easy to deal with. Windows上的Perl线程通常使我头疼,而在UNIX列表系统上,我发现它们非常容易处理。 Then again, I imagine figuring out how to correctly use forked processes to emulate threads on a system that ONLY supports threads and not forking would make most people's head hurt. 再说一次,我想象着弄清楚如何正确地使用派生的进程来模拟仅支持线程的系统上的线程,不派生会伤害大多数人的头部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM