承接 tourze/workerman-stream-http 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tourze/workerman-stream-http

最新稳定版本:0.0.1

Composer 安装命令:

composer require tourze/workerman-stream-http

包简介

Workerman的另一个HTTP协议实现

README 文档

README

English | 中文

Latest Version Build Status Code Coverage PHP Version License

A streaming HTTP protocol implementation for Workerman that processes HTTP requests progressively as data arrives, providing better memory efficiency and performance for high-concurrency scenarios.

Features

  • 🚀 Streaming HTTP processing - Processes requests in stages (request line, headers, body)
  • 📋 PSR-7 compliant - Full PSR-7 request and response handling
  • 📦 Chunked transfer encoding - Support for HTTP chunked transfer encoding
  • High performance - Optimized for Workerman's event-driven architecture
  • 🔄 Keep-alive support - HTTP/1.1 persistent connections
  • 🔧 Extensible - Support for custom HTTP methods
  • 🛡️ Security features - Built-in request size limits and error handling
  • 🧪 Well-tested - Comprehensive test coverage

Dependencies

This package requires:

Installation

composer require tourze/workerman-stream-http

Quick Start

<?php

use Nyholm\Psr7\Response;
use Tourze\Workerman\StreamHTTP\Protocol\HttpProtocol;
use Workerman\Connection\TcpConnection;
use Workerman\Worker;

require_once 'vendor/autoload.php';

// Create a worker using TCP
$worker = new Worker("tcp://0.0.0.0:8080");

// Set the protocol to our HTTP implementation
$worker->protocol = HttpProtocol::class;
$worker->name = 'StreamHTTPServer';

// Set the number of processes
$worker->count = 4;

// Handle requests
$worker->onMessage = function(TcpConnection $connection, $request) {
    // $request is a PSR-7 Request object
    $method = $request->getMethod();
    $uri = (string)$request->getUri();
    $headers = $request->getHeaders();

    // Create a PSR-7 response
    $response = new Response(
        200,
        ['Content-Type' => 'text/plain'],
        "Hello World!\nMethod: $method\nURI: $uri"
    );

    // Send the response back to the client
    $connection->send($response);
};

// Run the worker
Worker::runAll();

Documentation

Streaming Request Handling

This implementation processes HTTP requests in stages:

  1. Request Line - Processes HTTP method, URI, and protocol version
  2. Headers - Processes HTTP headers
  3. Body - Processes HTTP body (if any)

Each stage emits a PSR-7 Request object that progressively builds up as more data is received.

Adding Custom HTTP Methods

You can add support for custom HTTP methods:

use Tourze\Workerman\StreamHTTP\Protocol\HttpProtocol;

// Add support for a custom method
HttpProtocol::addAllowedMethod('CUSTOM');

// Get all currently supported methods
$methods = HttpProtocol::getAllowedMethods();

Error Handling

The implementation provides automatic error handling for invalid requests:

  • Invalid request lines (400 Bad Request)
  • Oversized headers (431 Request Header Fields Too Large)
  • Oversized body (413 Request Entity Too Large)
  • Request timeout (408 Request Timeout)
  • Server errors (500 Internal Server Error)

Advanced Usage

Custom Protocol Configuration

You can customize the protocol behavior by extending the HttpProtocol class:

<?php

use Tourze\Workerman\StreamHTTP\Protocol\HttpProtocol;

class CustomHttpProtocol extends HttpProtocol
{
    // Override default behavior
    public static function input(string $buffer, ConnectionInterface $connection): int
    {
        // Custom input processing logic
        return parent::input($buffer, $connection);
    }
}

Request/Response Middleware

You can implement middleware pattern for request/response processing:

<?php

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class AuthMiddleware
{
    public function process(RequestInterface $request): RequestInterface
    {
        // Authentication logic
        return $request;
    }
}

$worker->onMessage = function($connection, $request) {
    $middleware = new AuthMiddleware();
    $request = $middleware->process($request);
    
    // Handle authenticated request
    // ...
};

Performance Tuning

For high-performance scenarios, consider these optimizations:

<?php

// Increase worker processes
$worker->count = 8;

// Set connection limits
$worker->connections = 1000;

// Configure buffer sizes
$worker->sendMsgToWorker = true;

// Enable SSL for secure connections
$worker = new Worker("tcp://0.0.0.0:443", [
    'ssl' => [
        'local_cert' => '/path/to/cert.pem',
        'local_pk' => '/path/to/private.key',
    ]
]);

Contributing

Please see CONTRIBUTING.md for details.

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-14