📄 daemon.html
字号:
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>HTTP::Daemon - a simple http server class</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>
<body>
<script>writelinks('__top__',2);</script>
<h1><a>HTTP::Daemon - a simple http server class</a></h1>
<p><a name="__index__"></a></p>
<!-- INDEX BEGIN -->
<ul>
<li><a href="#name">NAME</a></li>
<li><a href="#synopsis">SYNOPSIS</a></li>
<li><a href="#description">DESCRIPTION</a></li>
<li><a href="#see_also">SEE ALSO</a></li>
<li><a href="#copyright">COPYRIGHT</a></li>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>HTTP::Daemon - a simple http server class</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
<span class="keyword">use</span> <span class="variable">HTTP::Daemon</span><span class="operator">;</span>
<span class="keyword">use</span> <span class="variable">HTTP::Status</span><span class="operator">;</span>
</pre>
<pre>
<span class="keyword">my</span> <span class="variable">$d</span> <span class="operator">=</span> <span class="variable">HTTP::Daemon</span><span class="operator">-></span><span class="variable">new</span> <span class="operator">||</span> <span class="keyword">die</span><span class="operator">;</span>
<span class="keyword">print</span> <span class="string">"Please contact me at: <URL:"</span><span class="operator">,</span> <span class="variable">$d</span><span class="operator">-></span><span class="variable">url</span><span class="operator">,</span> <span class="string">">\n"</span><span class="operator">;</span>
<span class="keyword">while</span> <span class="operator">(</span><span class="keyword">my</span> <span class="variable">$c</span> <span class="operator">=</span> <span class="variable">$d</span><span class="operator">-></span><span class="keyword">accept</span><span class="operator">)</span> <span class="operator">{</span>
<span class="keyword">while</span> <span class="operator">(</span><span class="keyword">my</span> <span class="variable">$r</span> <span class="operator">=</span> <span class="variable">$c</span><span class="operator">-></span><span class="variable">get_request</span><span class="operator">)</span> <span class="operator">{</span>
<span class="keyword">if</span> <span class="operator">(</span><span class="variable">$r</span><span class="operator">-></span><span class="variable">method</span> <span class="keyword">eq</span> <span class="string">'GET'</span> <span class="keyword">and</span> <span class="variable">$r</span><span class="operator">-></span><span class="variable">url</span><span class="operator">-></span><span class="variable">path</span> <span class="keyword">eq</span> <span class="string">"/xyzzy"</span><span class="operator">)</span> <span class="operator">{</span>
<span class="comment"># remember, this is *not* recommended practice :-)</span>
<span class="variable">$c</span><span class="operator">-></span><span class="variable">send_file_response</span><span class="operator">(</span><span class="string">"/etc/passwd"</span><span class="operator">);</span>
<span class="operator">}</span>
<span class="keyword">else</span> <span class="operator">{</span>
<span class="variable">$c</span><span class="operator">-></span><span class="variable">send_error</span><span class="operator">(</span><span class="variable">RC_FORBIDDEN</span><span class="operator">)</span>
<span class="operator">}</span>
<span class="operator">}</span>
<span class="variable">$c</span><span class="operator">-></span><span class="keyword">close</span><span class="operator">;</span>
<span class="keyword">undef</span><span class="operator">(</span><span class="variable">$c</span><span class="operator">);</span>
<span class="operator">}</span>
</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>Instances of the <code>HTTP::Daemon</code> class are HTTP/1.1 servers that
listen on a socket for incoming requests. The <code>HTTP::Daemon</code> is a
subclass of <code>IO::Socket::INET</code>, so you can perform socket operations
directly on it too.</p>
<p>The <a href="#item_accept"><code>accept()</code></a> method will return when a connection from a client is
available. The returned value will be an <code>HTTP::Daemon::ClientConn</code>
object which is another <code>IO::Socket::INET</code> subclass. Calling the
<a href="#item_get_request"><code>get_request()</code></a> method on this object will read data from the client and
return an <code>HTTP::Request</code> object. The ClientConn object also provide
methods to send back various responses.</p>
<p>This HTTP daemon does not <a href="../../lib/Pod/perlfunc.html#item_fork"><code>fork(2)</code></a> for you. Your application, i.e. the
user of the <code>HTTP::Daemon</code> is responsible for forking if that is
desirable. Also note that the user is responsible for generating
responses that conform to the HTTP/1.1 protocol.</p>
<p>The following methods of <code>HTTP::Daemon</code> are new (or enhanced) relative
to the <code>IO::Socket::INET</code> base class:</p>
<dl>
<dt><strong><a name="item_new">$d = HTTP::Daemon->new</a></strong>
<dt><strong>$d = HTTP::Daemon->new( %opts )</strong>
<dd>
<p>The constructor method takes the same arguments as the
<code>IO::Socket::INET</code> constructor, but unlike its base class it can also
be called without any arguments. The daemon will then set up a listen
queue of 5 connections and allocate some random port number.</p>
</dd>
<dd>
<p>A server that wants to bind to some specific address on the standard
HTTP port will be constructed like this:</p>
</dd>
<dd>
<pre>
<span class="variable">$d</span> <span class="operator">=</span> <span class="variable">HTTP::Daemon</span><span class="operator">-></span><span class="variable">new</span><span class="operator">(</span>
<span class="string">LocalAddr</span> <span class="operator">=></span> <span class="string">'www.thisplace.com'</span><span class="operator">,</span>
<span class="string">LocalPort</span> <span class="operator">=></span> <span class="number">80</span><span class="operator">,</span>
<span class="operator">);</span>
</pre>
</dd>
<dd>
<p>See <a href="../../lib/IO/Socket/INET.html">the IO::Socket::INET manpage</a> for a description of other arguments that can
be used configure the daemon during construction.</p>
</dd>
</li>
<dt><strong><a name="item_accept">$c = $d->accept</a></strong>
<dt><strong>$c = $d->accept( $pkg )</strong>
<dt><strong>($c, $peer_addr) = $d->accept</strong>
<dd>
<p>This method works the same the one provided by the base class, but it
returns an <code>HTTP::Daemon::ClientConn</code> reference by default. If a
package name is provided as argument, then the returned object will be
blessed into the given class. It is probably a good idea to make that
class a subclass of <code>HTTP::Daemon::ClientConn</code>.</p>
</dd>
<dd>
<p>The accept method will return <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a> if timeouts have been enabled
and no connection is made within the given time. The <code>timeout()</code> method
is described in <a href="../../lib/IO/Socket.html">the IO::Socket manpage</a>.</p>
</dd>
<dd>
<p>In list context both the client object and the peer address will be
returned; see the description of the accept method <a href="../../lib/IO/Socket.html">the IO::Socket manpage</a> for
details.</p>
</dd>
</li>
<dt><strong><a name="item_url">$d->url</a></strong>
<dd>
<p>Returns a URL string that can be used to access the server root.</p>
</dd>
</li>
<dt><strong><a name="item_product_tokens">$d->product_tokens</a></strong>
<dd>
<p>Returns the name that this server will use to identify itself. This
is the string that is sent with the <code>Server</code> response header. The
main reason to have this method is that subclasses can override it if
they want to use another product name.</p>
</dd>
<dd>
<p>The default is the string "libwww-perl-daemon/#.##" where "#.##" is
replaced with the version number of this module.</p>
</dd>
</li>
</dl>
<p>The <code>HTTP::Daemon::ClientConn</code> is a <code>IO::Socket::INET</code>
subclass. Instances of this class are returned by the <a href="#item_accept"><code>accept()</code></a> method
of <code>HTTP::Daemon</code>. The following methods are provided:</p>
<dl>
<dt><strong><a name="item_get_request">$c->get_request</a></strong>
<dt><strong>$c->get_request( $headers_only )</strong>
<dd>
<p>This method read data from the client and turns it into an
<code>HTTP::Request</code> object which is returned. It returns <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a>
if reading fails. If it fails, then the <code>HTTP::Daemon::ClientConn</code>
object ($c) should be discarded, and you should not try call this
method again on it. The $c->reason method might give you some
information about why $c->get_request failed.</p>
</dd>
<dd>
<p>The <a href="#item_get_request"><code>get_request()</code></a> method will normally not return until the whole
request has been received from the client. This might not be what you
want if the request is an upload of a large file (and with chunked
transfer encoding HTTP can even support infinite request messages -
uploading live audio for instance). If you pass a TRUE value as the
$headers_only argument, then <a href="#item_get_request"><code>get_request()</code></a> will return immediately
after parsing the request headers and you are responsible for reading
the rest of the request content. If you are going to call
$c->get_request again on the same connection you better read the
correct number of bytes.</p>
</dd>
</li>
<dt><strong><a name="item_read_buffer">$c->read_buffer</a></strong>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -