Git Product home page Git Product logo

Comments (8)

tourze avatar tourze commented on June 1, 2024

感觉你写的更加合理点。

from workerman.

twomiao avatar twomiao commented on June 1, 2024
  1. 你这代码看似完整性,但功能来说他那个没问题的,你失去了可读性。
  2. 如何解决这问题,添加一行英文注释说明下10的含义就行了。

from workerman.

tianyiw2013 avatar tianyiw2013 commented on June 1, 2024
  1. 你这代码看似完整性,但功能来说他那个没问题的,你失去了可读性。
  2. 如何解决这问题,添加一行英文注释说明下10的含义就行了。

源代码是$length += (int)substr($header, $pos + 18, 10);截取了10个字符长度,应该不正确,不一定是10个字符长度呢,然后强制转换为int,如果不强制转换的话,有可能截取到的结果是999\nxxx

if ($pos = stripos($header, "\r\nContent-Length: ")) {
$length += (int)substr($header, $pos + 18, 10);
$hasContentLength = true;
} else if (preg_match("/\r\ncontent-length: ?(\d+)/i", $header, $match)) {
$length += (int)$match[1];
$hasContentLength = true;
} else {

两个判断重复,第一个强制截取了10个字节,第二个好像永远不会执行。

from workerman.

twomiao avatar twomiao commented on June 1, 2024
  1. 你这代码看似完整性,但功能来说他那个没问题的,你失去了可读性。
  2. 如何解决这问题,添加一行英文注释说明下10的含义就行了。

源代码是$length += (int)substr($header, $pos + 18, 10);截取了10个字符长度,应该不正确,不一定是10个字符长度呢,然后强制转换为int,如果不强制转换的话,有可能截取到的结果是999\nxxx

int 32 位最大才多少,他这个极限是999999999字节,你见过这么大的网页文本(9G)?
截取到字符串的情况,强制转换int类型他不是已经处理这情况了?(int) substr(xxx);

from workerman.

tianyiw2013 avatar tianyiw2013 commented on June 1, 2024
  1. 你这代码看似完整性,但功能来说他那个没问题的,你失去了可读性。
  2. 如何解决这问题,添加一行英文注释说明下10的含义就行了。

源代码是$length += (int)substr($header, $pos + 18, 10);截取了10个字符长度,应该不正确,不一定是10个字符长度呢,然后强制转换为int,如果不强制转换的话,有可能截取到的结果是999\nxxx

int 32 位最大才多少,他这个极限是999999999字节,你见过这么大的网页文本(9G)? 截取到字符串的情况,强制转换int类型他不是已经处理这情况了?(int) substr(xxx);

你这么说确实没问题,但我忘记是因为什么发现的这个bug了,这行代码报过错~_~

from workerman.

twomiao avatar twomiao commented on June 1, 2024
  1. 你这代码看似完整性,但功能来说他那个没问题的,你失去了可读性。
  2. 如何解决这问题,添加一行英文注释说明下10的含义就行了。

源代码是$length += (int)substr($header, $pos + 18, 10);截取了10个字符长度,应该不正确,不一定是10个字符长度呢,然后强制转换为int,如果不强制转换的话,有可能截取到的结果是999\nxxx

复现问题给作者,然后pr修复一下。你这样写作者应该不会采取的(增加开销不说,可读性差太多了。)

from workerman.

twomiao avatar twomiao commented on June 1, 2024

标准http报文测试没有问题

<?php
$header = "HTTP/1.1 200 OK \r\nAccess-Control-Allow-Methods: POST, GET \r\nAccess-Control-Allow-Origin: https://www.baidu.com"." \r\nContent-Length: 105 \r\nContent-Type: application/json; charset=utf-8\r\n\r\n";
$pos = strpos($header, "\r\nContent-Length: ");

$value = substr($header, $pos + 18, 10);

$length = (int)$value;

var_dump($value, $length, $length === 105); 

输出结果:

string(10) "105 
Cont"
int(105)
bool(true)

from workerman.

xpader avatar xpader commented on June 1, 2024

有些看似不合理的代码,实则很巧妙,当然你的出发点也是没错的,更加严谨。

from workerman.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.