mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2025-09-03 16:56:18 +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 {
|
||||
c.firstBuf = make([]byte, 2048)
|
||||
n, err := c.Conn.Read(c.firstBuf)
|
||||
c.firstBuf = c.firstBuf[:n]
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
c.firstBuf = c.firstBuf[:n]
|
||||
reader := bytes.NewReader(c.firstBuf)
|
||||
bufReader := bufio.NewReader(reader)
|
||||
request, err := http.ReadRequest(bufReader)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
resp := http.Response{
|
||||
Header: http.Header{},
|
||||
resp := &http.Response{
|
||||
StatusCode: http.StatusTemporaryRedirect,
|
||||
Header: make(http.Header),
|
||||
}
|
||||
resp.StatusCode = http.StatusTemporaryRedirect
|
||||
location := fmt.Sprintf("https://%v%v", request.Host, request.RequestURI)
|
||||
resp.Header.Set("Location", location)
|
||||
resp.Header.Set("Location", fmt.Sprintf("https://%v%v", request.Host, request.RequestURI))
|
||||
resp.Write(c.Conn)
|
||||
c.Close()
|
||||
c.firstBuf = nil
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *AutoHttpsConn) Read(buf []byte) (int, error) {
|
||||
var err error
|
||||
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 {
|
||||
n := copy(buf, c.firstBuf[c.bufStart:])
|
||||
c.bufStart += n
|
||||
if c.bufStart >= len(c.firstBuf) {
|
||||
if c.bufStart == len(c.firstBuf) {
|
||||
c.firstBuf = nil
|
||||
}
|
||||
return n, nil
|
||||
|
|
Loading…
Reference in a new issue