改进代码

This commit is contained in:
e1732a364fed
2000-01-01 00:00:00 +00:00
parent ee4ddcdebe
commit c05f8b8b53
3 changed files with 28 additions and 15 deletions

View File

@@ -103,11 +103,10 @@ func (s *Server) Handshake(underlay net.Conn) (net.Conn, error) {
if re != nil {
if errors.Is(re, httpLayer.ErrNotHTTP_Request) {
if rp.Failreason == -12 && s.UseEarlyData {
//-12 是没有读到http尾部的错误在earlyData开启时是可能存在的一次读取到的数据不够长没法完整表达整个http头时就会有这种情况。
//读到的长度可能是1186
if rp.Failreason == httpLayer.Fail_no_endMark && s.UseEarlyData {
//Fail_no_endMark 是没有读到http尾部的错误在earlyData开启时是可能存在的一次读取到的数据不够长没法完整表达整个http头时就会有这种情况。
if ce := utils.CanLogDebug("ws, rp.Failreason == -12"); ce != nil {
if ce := utils.CanLogDebug("ws, check http Fail_no_endMark"); ce != nil {
ce.Write(zap.Int("len", rp.WholeRequestBuf.Len()), zap.String("header", rp.WholeRequestBuf.String()))
}

View File

@@ -12,6 +12,20 @@ type RawHeader struct {
Value []byte
}
const (
Fail_tooShort = iota + 1
Fail_not_for_h2c
Fail_method_len_wrong
Fail_unexpected_proxy
Fail_space_index_wrong
Fail_is_11proxy_butNot_11
Fail_no_slash
Fail_earlyLinefeed
Fail_firstline_less_than_10
)
const Fail_no_endMark = -12
// 从数据中试图获取 http1.1, http1.0或 http0.9 请求的 version, path, method 和 headers.
// failreason!=0 表示获取失败, 即表示不是合法的h1请求.
//
@@ -19,12 +33,12 @@ type RawHeader struct {
func ParseH1Request(bs []byte, isproxy bool) (version, method, path string, headers []RawHeader, failreason int) {
if len(bs) < 16 { //http0.9 最小长度为16 http1.0及1.1最小长度为18
failreason = 1
failreason = Fail_tooShort
return
}
if bs[4] == '*' {
failreason = 2 //this method doesn't support h2c
failreason = Fail_not_for_h2c //this method doesn't support h2c
return
}
@@ -46,7 +60,7 @@ func ParseH1Request(bs []byte, isproxy bool) (version, method, path string, head
//但是 Get / HTTP/1.1 也会遇到第6字节为空格的情况, put/pri 也一样, 所以还要过滤.
if bs[3] != ' ' {
failreason = 3
failreason = Fail_method_len_wrong
return
}
}
@@ -88,13 +102,13 @@ func ParseH1Request(bs []byte, isproxy bool) (version, method, path string, head
method = "CONNECT"
if !isproxy {
//connect 只可能出现于 proxy代理中
failreason = 4
failreason = Fail_unexpected_proxy
}
}
}
if shouldSpaceIndex == 0 || bs[shouldSpaceIndex] != ' ' {
failreason = 5
failreason = Fail_space_index_wrong
return
}
shouldSlashIndex := shouldSpaceIndex + 1
@@ -106,13 +120,13 @@ func ParseH1Request(bs []byte, isproxy bool) (version, method, path string, head
} else {
//http
if string(bs[shouldSlashIndex:shouldSlashIndex+7]) != "http://" {
failreason = 6
failreason = Fail_is_11proxy_butNot_11
return
}
}
} else {
if bs[shouldSlashIndex] != '/' {
failreason = 7
failreason = Fail_no_slash
return
}
}
@@ -130,14 +144,14 @@ func ParseH1Request(bs []byte, isproxy bool) (version, method, path string, head
for i := shouldSlashIndex; i < last; i++ {
b := bs[i]
if b == '\r' || b == '\n' {
failreason = 8
failreason = Fail_earlyLinefeed
return
}
if b == ' ' {
// 空格后面至少还有 HTTP/1.1\r\n 这种字样,也就是说空格后长度至少为 10
//https://stackoverflow.com/questions/25047905/http-request-minimum-size-in-bytes/25065089
if len(bs)-i-1 < 10 {
failreason = 9
failreason = Fail_firstline_less_than_10
return
}
@@ -158,7 +172,7 @@ func ParseH1Request(bs []byte, isproxy bool) (version, method, path string, head
indexOfEnding := bytes.Index(leftBs, HeaderENDING_bytes)
if indexOfEnding < 0 {
failreason = -12
failreason = Fail_no_endMark
return
}

View File

@@ -153,7 +153,7 @@ func (rhr *H1RequestParser) ReadAndParse_2(r net.Conn) error {
rhr.WholeRequestBuf = buf
rhr.Version, rhr.Method, rhr.Path, rhr.Headers, rhr.Failreason = ParseH1Request(data, false)
if rhr.Failreason == -12 {
if rhr.Failreason == Fail_no_endMark {
r.SetReadDeadline(time.Now().Add(time.Millisecond * 200))
n2, e2 := r.Read(bs[n:])