n0nag0n / fatfree-swoole

A shim so you can use swoole and fatfree together
11 stars 2 forks source link

Some suggestions #1

Closed soyleung closed 4 years ago

soyleung commented 4 years ago

This is a leap of creativity.

This will promote the revolutionary evolution of Fat-core, I fill... But !!! But !!! There's no absolute optimization, because he doesn't inherit prefab. I think we can consider the idea from \CLI\WS, it well best, I think!!

n0nag0n commented 4 years ago

So FatFree_Swoole extends Base, which extends Prefab. Is that what you were hoping it would do?

soyleung commented 4 years ago

yes, i fill it will best

n0nag0n commented 4 years ago

Cool it already does this. We're all set!

soyleung commented 4 years ago

i try move convertToFatFreeRequest in Base class. and it extends Prefab, before remove the Extra parameter code \Base $fw, it will run the leap of quality. tks!

n0nag0n commented 4 years ago

I guess I am confused. Are you saying you want FatFree to build this functionality into bcosca/fatfree?

soyleung commented 4 years ago

yes

soyleung commented 4 years ago

i found : swoole http protocol, It's incomplete. It comes from the swoole official introduction So,Their recommendation is that swoole use nginx as a proxy. but F-S use on nginx , it will not have a good advantage. What do you think about it?

soyleung commented 4 years ago

` class FatFree_Swoole extends \Prefab {

public function process(\Base $fw, \Swoole\HTTP\Request $swooleRequest, \Swoole\HTTP\Response $swooleResponse) {

    $processed_fw = $this->convertToFatFreeRequest($swooleRequest, $fw);
    $this->convertToSwooleResponse($swooleResponse, $processed_fw);
    // $slimRequest = $this->requestTransformer->toSlim($swooleRequest);
    // $slimResponse = $this->app->process($slimRequest, new Http\Response());

    // return $this->responseMerger->mergeToSwoole($slimResponse, $swooleResponse);
}

protected function convertToFatFreeRequest(\Swoole\HTTP\Request $swooleRequest, \Base $fw) {
    // $processed_fw = clone $fw;
    // $processed_fw->hive = $fw->hive;

    // copy server vars
    foreach ($swooleRequest->server as $key=>$val) {
        $tmp=strtoupper($key);
        $_SERVER[$tmp] = $val;
    }

    // copy headers
    $headers = [];
    foreach ($swooleRequest->header as $key=>$val) {
        $tmp=strtoupper(strtr($key,'-','_'));
        // TODO: use ucwords delimiters for php 5.4.32+ & 5.5.16+
        $key=strtr(ucwords(strtolower(strtr($key,'-',' '))),' ','-');
        $headers[$key]=$val;
        if (isset($_SERVER['HTTP_'.$tmp]))
            $headers[$key]=&$_SERVER['HTTP_'.$tmp];
    }

    //print_r($swooleRequest->header);
    //print_r($_SERVER);

    $base=rtrim($fw->fixslashes(
        dirname($_SERVER['SCRIPT_NAME'])),'/');
    $scheme=isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=='on' ||
        isset($headers['X-Forwarded-Proto']) &&
        $headers['X-Forwarded-Proto']=='https'?'https':'http';
    $uri=parse_url((preg_match('/^\w+:\/\//',$_SERVER['REQUEST_URI'])?'':
            $scheme.'://'.$_SERVER['SERVER_NAME']).$_SERVER['REQUEST_URI']);
    $path=preg_replace('/^'.preg_quote($base,'/').'/','',$uri['path']);

    $fw->HEADERS = $headers;
    // $fw->AGENT = $processed_fw->agent();
    // $fw->AJAX = $processed_fw->ajax();
    $fw->BODY = $swooleRequest->rawContent();
    $fw->CLI = FALSE;
    $fw->FRAGMENT = isset($uri['fragment'])?$uri['fragment']:'';
    $fw->HOST = $_SERVER['SERVER_NAME'];
    $fw->PATH = $path;
    $fw->QUERY = isset($uri['query'])?$uri['query']:'';
    $fw->ROOT = $_SERVER['DOCUMENT_ROOT'];
    $fw->SCHEME = $scheme;
    // $fw->SEED = $processed_fw->hash($_SERVER['SERVER_NAME'].$base);
    $fw->TIME = $_SERVER['REQUEST_TIME_FLOAT'];
    $fw->URI = $_SERVER['REQUEST_URI'];
    $fw->VERB = $_SERVER['REQUEST_METHOD'];
    foreach (explode('|',\Base::GLOBALS) as $global) {
        $lowercase_global = strtolower($global);
        $fw->$global = $global === 'SERVER' ? array_combine(array_map('strtoupper', array_keys($swooleRequest->{$lowercase_global})), $swooleRequest->{$lowercase_global}) : $swooleRequest->{$lowercase_global};
    }

    //print_r($processed_fw->hive);
    $fw->run();
    return $fw;
}

protected function convertToSwooleResponse(\Swoole\HTTP\Response $swooleResponse, \Base $processed_fw)  {
    if(!empty($processed_fw->RESPONSE)) {
        $swooleResponse->header('Content-Length', (string) strlen($processed_fw->RESPONSE));
    }

    // deal with cookies
    // if($processed_fw->HEADERS['Set-Cookie']) {

    // }

    // need some work on capturing the status code
    $swooleResponse->status(isset($processed_fw->ERROR['code']) ? $processed_fw->ERROR['code'] : 200);

    if($processed_fw->RESPONSE) {
        $swooleResponse->write($processed_fw->RESPONSE);
    }

}

}`

yes ,it will not need to modify any basic code, extends from Prefab.

n0nag0n commented 4 years ago

So running a reverse proxy from nginx or apache is a good idea because those webservers have a much more robust configuration than swoole. With your code though, I believe there will be problems because if you have multiple requests come in at the same time, swoole will hold the $fw object in memory while the script is running and the multiple requests will start overriding each other and cause corrupted responses to the end users. I haven't actually had enough time to test that theory, but it makes sense given the context.

soyleung commented 4 years ago

Yes,this is a test.

soyleung commented 4 years ago

yes, this is a test!!

------------------ 原始邮件 ------------------ 发件人: "Austin"<notifications@github.com>; 发送时间: 2020年6月7日(星期天) 上午10:51 收件人: "n0nag0n/fatfree-swoole"<fatfree-swoole@noreply.github.com>; 抄送: "AaronLeung"<aaronleung@vip.qq.com>; "Author"<author@noreply.github.com>; 主题: Re: [n0nag0n/fatfree-swoole] Some suggestions (#1)

So running a reverse proxy from nginx or apache is a good idea because those webservers have a much more robust configuration than swoole. With your code though, I believe there will be problems because if you have multiple requests come in at the same time, swoole will hold the $fw object in memory while the script is running and the multiple requests will start overriding each other and cause corrupted responses to the end users. I haven't actually had enough time to test that theory, but it makes sense given the context.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

soyleung commented 4 years ago

So, I think about this clone f3 whitout base class will be best!!