beyod自带了一个http服务器, 位于beyoioprotocolhttp目录中,支持以下功能:

  1. 静态文件服务功能
  2. 支持大文件下载和断点续传
  3. 目录列表
  4. 自定义错误页
  5. 索引页面配置
  6. PHP文件解析运行
  7. 支持文件上传
  8. $_GET $_POST $_FILES $_COOKIE $_RAWBODY封装实现
  9. 地址重写及拦截器机制
  10. 基于主机名的虚拟主机
  11. gzip压缩输出
  12. 自定义响应Cookie
  13. 良好的扩展机制

1. 配置

config/main.php

//...
'components'=>[
    'server'=>[
        'listeners'=>[
            'http' => [
                'listen' => 'tcp://0.0.0.0:80',
                'parser' => 'beyod\protocol\http\Parser',
                'handler' => [
                    'class'=>'beyod\protocol\http\Handler',
                    'document_root' => dirname(__DIR__).'/../app/webroot', //网站根目录
                    'gzip' => true, //启用静态文件gzip压缩
                    'auto_index' => true, //启用目录列表
                    'index' => ['index.htm', 'index.html', 'index.php'], //索引页
                    'error_pages' => [ //定义错误页
                        '40x' => __DIR__.'/40x.html',
                        '400' => __DIR__.'/400.html',
                        '50x' => __DIR__.'/50x.html'
                    ],
                    
                    //地址拦截器机制 .md后缀路径由回调函数处理
                    /**
                    * @params $event beyoio\MessageEvent
                    * @params $req beyoio\protocol\http\Request
                    * @params $res beyoio\protocol\http\Response
                    * @params $path request file path
                    */
                    'callback' =>[
                            '#\.md$#' => function($event, $req, $res, $path){
                                $res->body = Markdown::process(file_get_contents($path), 'gfm-comment');
                                return true; //返回true表示处理完毕,不再匹配后续规则
                            },
                        ]
                ]
            ]
        ]
    ]
]

2. php文件的解析

在站点根目录下建立php文件就可以自动解析执行之。
内置了几个特殊变量, 可以在PHP文件中使用:
$event beyodMessageEvent 当前message事件对象
$request beyodprotocolhttpRequest 当前请求消息的封装
$response beyodprotocolhttpResponse 当前响应消息的封装

几个注意事项:
==1. php文件中的输出,将以缓冲区捕获的方式作为响应正文==

==2. 因为beyod是长驻内存方式运行的,php文件中类、函数必须使用自动加载或require_once方式加载,否则会导致函数/类重复载入而造成运行错误==

==3. 完全支持数十GB级别大文件的传输和断点续传,实际观测没有内存泄露产生==

==4. 无法在beyod实例上再次运行WEB框架,因为beyod本质是基于命令行的长驻进程,和IIS/Apache/php-fpm运行环境有差别, 而应该直接使用框架实现业务功能。==