<commit_before>package gows

import (
	"fmt"
	"net"
	"strings"
)

type Connection struct {
	state    State
	conn     *net.Conn
	WaitPong bool
	addr     string
}

func NewConnection(conn *net.Conn, addr string) (connection *Connection) {
	connection = &Connection{OPEN, conn, false, addr}
	return

}

func (self *Connection) ValidateHandshake() (validate bool) {
	self.openingHandshake()
	validate = true
	for {
		buffer, _ := self.Read(256)
		af := strings.Split(string(buffer), "\n")
		for _, v := range af {
			if strings.Contains(v, "HTTP/1.1") {
				continue
			}

			header := strings.Split(v, ": ")
			fmt.Printf("%v ", header)
			if header[0] == "Upgrade" && header[1] != "websocket" {
				validate = false
			} else if header[0] == "Connection" && header[1] != "Upgrade" {
				validate = false
			} else if header[0] == "Sec-Websocket-Accept" && header[1] != "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=" {
				validate = false
			}
		}
		return
		// validate handshake response from peer
		// finally break
	}
}

func (self *Connection) openingHandshake() {
	(*self.conn).Write(HandshakeRequest)
	self.state = CONNECTING
}

func (self *Connection) ResponseOpeningHandshake() {
	(*self.conn).Write(HandshakeResponse)
}

func (self *Connection) Send(data []byte, opc Opcode) {
	if opc == CLOSE {
		self.state = CLOSING
	} else if opc == PING {
		self.WaitPong = true
	}
	(*self.conn).Write(Pack(data, opc))
}

func (self *Connection) Read(length uint32) (buffer []byte, err error) {
	buffer = make([]byte, length)
	_, err = (*self.conn).Read(buffer)
	if err != nil {
		//panic(err)
	}
	return
}

func (self *Connection) ReceiveLoop() {
	for {
		frame, err := Parse(self)
		if err != nil {
			break
		}
		fmt.Printf("Opcode=%s, %s\n", frame.opc.String(), frame.Payload)
		if frame.opc == CLOSE {
			self.state = CLOSING
			self.Send([]byte(""), CLOSE)
			break
		} else if frame.opc == PING {
			fmt.Printf("%s\n", frame.Payload)
			self.Send(frame.Payload, PONG)
		} else if frame.opc == PONG {
			self.WaitPong = false
		}
	}

	(*self.conn).Close()
	self.state = CLOSED
}
<commit_msg>remove validate method and add connection close method<commit_after>package gows

import (
	"fmt"
	"net"
)

type Connection struct {
	state    State
	conn     *net.Conn
	WaitPong bool
	addr     string
}

func NewConnection(conn *net.Conn, addr string) (connection *Connection) {
	connection = &Connection{OPEN, conn, false, addr}
	return

}

func (self *Connection) SendHandshake() {
	(*self.conn).Write(HandshakeRequest)
	self.state = CONNECTING
}

func (self *Connection) ResponseOpeningHandshake() {
	(*self.conn).Write(HandshakeResponse)
}

func (self *Connection) Close() {
	(*self.conn).Close()
	self.state = CLOSED
}

func (self *Connection) Send(data []byte, opc Opcode) {
	if opc == CLOSE {
		self.state = CLOSING
	} else if opc == PING {
		self.WaitPong = true
	}
	(*self.conn).Write(Pack(data, opc))
}

func (self *Connection) Read(length uint32) (buffer []byte, err error) {
	buffer = make([]byte, length)
	_, err = (*self.conn).Read(buffer)
	if err != nil {
		//panic(err)
	}
	return
}

func (self *Connection) ReceiveLoop() {
	for {
		frame, err := Parse(self)
		if err != nil {
			break
		}
		fmt.Printf("Opcode=%s, %s\n", frame.opc.String(), frame.Payload)
		if frame.opc == CLOSE {
			self.state = CLOSING
			self.Send([]byte(""), CLOSE)
			break
		} else if frame.opc == PING {
			fmt.Printf("%s\n", frame.Payload)
			self.Send(frame.Payload, PONG)
		} else if frame.opc == PONG {
			self.WaitPong = false
		}
	}

	self.Close()
}

{'fraction_non_alphanumeric': 0.15943262411347517, 'fraction_numerical': 0.004822695035460993, 'mean_word_length': 3.537966537966538, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6458537', 'n_tokens_mistral': 1401, 'n_tokens_neox': 1312, 'n_words': 313}