mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2025-09-05 01:36:19 +00:00
Update auto_https_conn.go
- In readRequest, removed redundant assignment of c.firstBuf after the Read operation. The slice is already limited to n bytes read. - Changed resp from a value to a pointer in readRequest to avoid copying the struct when only a pointer is needed. - Added error handling in the Read method to return an error if readRequest fails, which was previously ignored. - Optimized the condition in Read to check for equality (c.bufStart == len(c.firstBuf)) instead of greater than or equal, as bufStart should never exceed len(c.firstBuf).
This commit is contained in:
parent
583a4f87a9
commit
b3f36d7239
1 changed files with 13 additions and 9 deletions
|
@ -27,37 +27,41 @@ func NewAutoHttpsConn(conn net.Conn) net.Conn {
|
||||||
func (c *AutoHttpsConn) readRequest() bool {
|
func (c *AutoHttpsConn) readRequest() bool {
|
||||||
c.firstBuf = make([]byte, 2048)
|
c.firstBuf = make([]byte, 2048)
|
||||||
n, err := c.Conn.Read(c.firstBuf)
|
n, err := c.Conn.Read(c.firstBuf)
|
||||||
c.firstBuf = c.firstBuf[:n]
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
c.firstBuf = c.firstBuf[:n]
|
||||||
reader := bytes.NewReader(c.firstBuf)
|
reader := bytes.NewReader(c.firstBuf)
|
||||||
bufReader := bufio.NewReader(reader)
|
bufReader := bufio.NewReader(reader)
|
||||||
request, err := http.ReadRequest(bufReader)
|
request, err := http.ReadRequest(bufReader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
resp := http.Response{
|
resp := &http.Response{
|
||||||
Header: http.Header{},
|
StatusCode: http.StatusTemporaryRedirect,
|
||||||
|
Header: make(http.Header),
|
||||||
}
|
}
|
||||||
resp.StatusCode = http.StatusTemporaryRedirect
|
resp.Header.Set("Location", fmt.Sprintf("https://%v%v", request.Host, request.RequestURI))
|
||||||
location := fmt.Sprintf("https://%v%v", request.Host, request.RequestURI)
|
|
||||||
resp.Header.Set("Location", location)
|
|
||||||
resp.Write(c.Conn)
|
resp.Write(c.Conn)
|
||||||
c.Close()
|
c.Close()
|
||||||
c.firstBuf = nil
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *AutoHttpsConn) Read(buf []byte) (int, error) {
|
func (c *AutoHttpsConn) Read(buf []byte) (int, error) {
|
||||||
|
var err error
|
||||||
c.readRequestOnce.Do(func() {
|
c.readRequestOnce.Do(func() {
|
||||||
c.readRequest()
|
if !c.readRequest() {
|
||||||
|
err = fmt.Errorf("failed to read HTTP request")
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
if c.firstBuf != nil {
|
if c.firstBuf != nil {
|
||||||
n := copy(buf, c.firstBuf[c.bufStart:])
|
n := copy(buf, c.firstBuf[c.bufStart:])
|
||||||
c.bufStart += n
|
c.bufStart += n
|
||||||
if c.bufStart >= len(c.firstBuf) {
|
if c.bufStart == len(c.firstBuf) {
|
||||||
c.firstBuf = nil
|
c.firstBuf = nil
|
||||||
}
|
}
|
||||||
return n, nil
|
return n, nil
|
||||||
|
|
Loading…
Reference in a new issue