http权威指南之五web服务器

1  前言

        • Survey the many different types of software and hardware web servers.
        • Describe how to write a simple diagnostic web server in Perl.
        • Explain how web servers process HTTP transactions, step by step.


2  Web Servers Come in All Shapes and Sizes


    2.1、Web Server Implementations

            Web servers implement HTTP and the related TCP connection handling. They also
        manage the resources served by the web server and provide administrative features to
        configure, control, and enhance the web server.

    2.2、General-Purpose Software Web Servers

    2.3 Web Server Appliances

    2.4 、Embedded Web Servers


    2.5、A Minimal Perl Web Server


#!/usr/bin/perl -w
use Socket;
use Carp;
use FileHandle;
# (1) use port 8080 by default, unless overridden on command line
$port = (@ARGV ? $ARGV[0] : 8080);
# (2) create local TCP socket and set it to listen for connections
$proto = getprotobyname('tcp');
socket(S, PF_INET, SOCK_STREAM, $proto) || die;
setsockopt(S, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) || die;
bind(S, sockaddr_in($port, INADDR_ANY)) || die;
listen(S, SOMAXCONN) || die;
# (3) print a startup message
printf("<<<Type-O-Serve Accepting on Port %d>>>\n\n",$port);
while (1)
{
    # (4) wait for a connection C
    $cport_caddr = accept(C, S);
    ($cport,$caddr) = sockaddr_in($cport_caddr);
    ->autoflush(1);
    # (5) print who the connection is from
    $cname = gethostbyaddr($caddr,AF_INET);
    printf("<<<Request From '%s'>>>\n",$cname);
    # (6) read request msg until blank line, and print on screen
    while ($line = <C>)
    {
        print $line;
        if ($line =~ /^\r/) { last; }
    }
    # (7) prompt for response message, and input response lines,
    # sending response lines to client, until solitary "."
    printf("<<<Type Response Followed by '.'>>>\n");
    while ($line = <STDIN>)
    {
        $line =~ s/\r//;
        $line =~ s/\n//;
        if ($line =~ /^\./) { last; }
        print C $line . "\r\n";
    }
    close(C);
}

3、What Real Web Servers Do

        1. Set up connection—accept a client connection, or close if the client is unwanted.
        2. Receive request—read an HTTP request message from the network.
        3. Process request—interpret the request message and take action.
        4. Access resource—access the resource specified in the message.
        5. Construct response—create the HTTP response message with the right headers.
        6. Send response—send the response back to the client.
        7. Log transaction—place notes about the completed transaction in a log file.


转载于:https://my.oschina.net/daleshen128/blog/97432

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值