bfenetworks/bfe

http_conn.go有段代码写得不规范或者逻辑不合理

cherishman2005 opened this issue · 1 comments

  • 问题描述:在判定err != nil 异常后 request应该是无效值,后面逻辑不应该继续正常使用request。最好是处理好异常后return
		request, err := c.readRequest()
		if err != nil {
			if err == errTooLarge {
				session.SetError(bfe_basic.ErrClientLongHeader, "request entity too large")
				proxyState.ErrClientLongHeader.Inc(1)
				// Their HTTP client may or may not be
				// able to read this if we're
				// responding to them and hanging up
				// while they're still writing their
				// request.  Undefined behavior.
				io.WriteString(c.rwc, "HTTP/1.1 413 Request Entity Too Large\r\n\r\n")
				c.closeWriteAndWait()
				break
			} else if strings.Contains(err.Error(), "exceed maxUriBytes") {
				session.SetError(bfe_basic.ErrClientLongUrl, err.Error())
				proxyState.ErrClientLongUrl.Inc(1)
				io.WriteString(c.rwc, "HTTP/1.1 414 Request-URI Too Long\r\n\r\n")
				break
			} else if err == io.EOF {
				session.SetError(bfe_basic.ErrClientClose, err.Error())
				proxyState.ErrClientClose.Inc(1)
				break // Don't reply
			} else if neterr, ok := err.(net.Error); ok && neterr.Timeout() {
				session.SetError(bfe_basic.ErrClientTimeout, err.Error())
				proxyState.ErrClientTimeout.Inc(1)
				break // Don't reply
			} else if strings.Contains(err.Error(), "connection reset by peer") {
				session.SetError(bfe_basic.ErrClientReset, err.Error())
				proxyState.ErrClientReset.Inc(1)
				break
			}

			session.SetError(bfe_basic.ErrClientBadRequest, err.Error())
			proxyState.ErrClientBadRequest.Inc(1)
			io.WriteString(c.rwc, "HTTP/1.1 400 Bad Request\r\n\r\n")
			break
		}

		req := request.HttpRequest

注意:进入if err!=nil 分支后,最终都将break并退出外部的for循环,而不是继续执行 req := request.HttpRequest
https://github.com/bfenetworks/bfe/blob/develop/bfe_server/http_conn.go#L392