package wsutil

import (
	
	
	
	
	

	
)

// DebugUpgrader is a wrapper around ws.Upgrader. It tracks I/O of a
// WebSocket handshake.
//
// Note that it must not be used in production applications that requires
// Upgrade() to be efficient.
type DebugUpgrader struct {
	// Upgrader contains upgrade to WebSocket options.
	Upgrader ws.Upgrader

	// OnRequest and OnResponse are the callbacks that will be called with the
	// HTTP request and response respectively.
	OnRequest, OnResponse func([]byte)
}

// Upgrade calls Upgrade() on underlying ws.Upgrader and tracks I/O on conn.
func ( *DebugUpgrader) ( io.ReadWriter) ( ws.Handshake,  error) {
	var (
		// Take the Reader and Writer parts from conn to be probably replaced
		// below.
		 io.Reader = 
		 io.Writer = 
	)
	if  := .OnRequest;  != nil {
		var  bytes.Buffer
		// First, we must read the entire request.
		,  := http.ReadRequest(bufio.NewReader(
			io.TeeReader(, &),
		))
		if  == nil {
			// Fulfill the buffer with the response body.
			io.Copy(ioutil.Discard, .Body)
			.Body.Close()
		}
		(.Bytes())

		 = io.MultiReader(
			&, ,
		)
	}

	if  := .OnResponse;  != nil {
		var  bytes.Buffer
		// Intercept the response stream written by the Upgrade().
		 = io.MultiWriter(
			, &,
		)
		defer func() {
			(.Bytes())
		}()
	}

	return .Upgrader.Upgrade(struct {
		io.Reader
		io.Writer
	}{, })
}