Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewind tls.Conn #18

Closed
WeidiDeng opened this issue Apr 1, 2022 · 1 comment
Closed

Rewind tls.Conn #18

WeidiDeng opened this issue Apr 1, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@WeidiDeng
Copy link
Contributor

tls.Conn can be rewound by using reflect to modify its internal buffer.

func RewindTlsConn(conn *tls.Conn, read []byte) {
	var (
		tlsInput, _ = reflect.TypeOf(tls.Conn{}).FieldByName("input")
		input     = (*bytes.Reader)(unsafe.Add(unsafe.Pointer(conn), tlsInput.Offset))
		remaining = input.Len()
		size      = int(input.Size())
		buffered  = len(read)
	)
	if buffered <= size {
		_, _ = input.Seek(0, 0)
	} else {
		buf := make([]byte, buffered+remaining)
		copy(buf, read)
		_, _ = input.Read(buf[buffered:])
		input.Reset(buf)
	}
}

There is no need to turn on allow h2c if caddy is using https.

@imgk
Copy link
Owner

imgk commented Apr 1, 2022

Hi, I'm not familiar with reflect. Can you create a PR?

@imgk imgk closed this as completed Apr 1, 2022
@imgk imgk added the enhancement New feature or request label Apr 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants