<commit_before>package websocket

import (
	"net/http"

	"github.com/gorilla/websocket"
	"github.com/spring1843/chat-server/src/chat"
	"github.com/spring1843/chat-server/src/shared/logs"
)

var (
	chatServerInstance *chat.Server
	upgrader           = websocket.Upgrader{
		ReadBufferSize:  1024,
		WriteBufferSize: 1024,
	}
)

// Handler is a http handler function that implements WebSocket
func Handler(w http.ResponseWriter, r *http.Request) {
	logs.Infof("Call to websocket /wp form %s", r.RemoteAddr)
	chatConnection := NewChatConnection()
	conn, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		logs.ErrIfErrf(err, "Error upgrading websocket connection.")
		return
	}
	chatConnection.Connection = conn
	go chatServerInstance.ReceiveConnection(chatConnection)
	go chatConnection.writePump()
	chatConnection.readPump()
	logs.Infof("End of call to websocket /wp form %s", r.RemoteAddr)
}

// SetWebSocket sets the chat server instance
func SetWebSocket(chatServerParam *chat.Server) {
	chatServerInstance = chatServerParam
}
<commit_msg>Check http origin for Websocket<commit_after>package websocket

import (
	"net/http"

	"github.com/gorilla/websocket"
	"github.com/spring1843/chat-server/src/chat"
	"github.com/spring1843/chat-server/src/shared/logs"
)

var (
	chatServerInstance *chat.Server
	upgrader           = websocket.Upgrader{
		ReadBufferSize:  1024,
		WriteBufferSize: 1024,
		CheckOrigin:     checkOrigin,
	}
)

// TODO validate CORS headers here
func checkOrigin(r *http.Request) bool {
	return true
}

// Handler is a http handler function that implements WebSocket
func Handler(w http.ResponseWriter, r *http.Request) {
	logs.Infof("Call to websocket /wp form %s", r.RemoteAddr)
	chatConnection := NewChatConnection()
	conn, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		logs.ErrIfErrf(err, "Error upgrading websocket connection.")
		return
	}
	chatConnection.Connection = conn
	go chatServerInstance.ReceiveConnection(chatConnection)
	go chatConnection.writePump()
	chatConnection.readPump()
	logs.Infof("End of call to websocket /wp form %s", r.RemoteAddr)
}

// SetWebSocket sets the chat server instance
func SetWebSocket(chatServerParam *chat.Server) {
	chatServerInstance = chatServerParam
}

{'fraction_non_alphanumeric': 0.10582959641255606, 'fraction_numerical': 0.014349775784753363, 'mean_word_length': 4.691326530612245, 'pattern_counts': {'":': 0, '<': 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': '5609992', 'n_tokens_mistral': 771, 'n_tokens_neox': 723, 'n_words': 177}