<commit_before>package proxyproto

import (
	"bufio"
	"bytes"
	"net"
	"strconv"
	"strings"
)

const (
	crlf      = "\r\n"
	separator = " "
)

func initVersion1() *Header {
	header := new(Header)
	header.Version = 1
	// Command doesn't exist in v1
	header.Command = PROXY
	return header
}

func parseVersion1(reader *bufio.Reader) (*Header, error) {
	// Read until LF shows up, otherwise fail.
	// At this point, can't be sure CR precedes LF which will be validated next.
	line, err := reader.ReadString('\n')
	if err != nil {
		return nil, ErrLineMustEndWithCrlf
	}
	if !strings.HasSuffix(line, crlf) {
		return nil, ErrLineMustEndWithCrlf
	}
	// Check full signature.
	tokens := strings.Split(line[:len(line)-2], separator)
	transportProtocol := UNSPEC // doesn't exist in v1 but fits UNKNOWN.
	if len(tokens) > 0 {
		// Read address family and protocol
		switch tokens[1] {
		case "TCP4":
			transportProtocol = TCPv4
		case "TCP6":
			transportProtocol = TCPv6
		case "UNKNOWN": // no-op
		default:
			return nil, ErrCantReadAddressFamilyAndProtocol
		}

		// Expect 6 tokens only when UNKNOWN is not present.
		if tokens[1] != "UNKNOWN" && len(tokens) < 6 {
			return nil, ErrCantReadAddressFamilyAndProtocol
		}
	}

	// Allocation only happens when a signature is found.
	header := initVersion1()<commit_msg>v1: validate UNSPEC to show how it's used to signal UNKNOWN<commit_after>package proxyproto

import (
	"bufio"
	"bytes"
	"net"
	"strconv"
	"strings"
)

const (
	crlf      = "\r\n"
	separator = " "
)

func initVersion1() *Header {
	header := new(Header)
	header.Version = 1
	// Command doesn't exist in v1
	header.Command = PROXY
	return header
}

func parseVersion1(reader *bufio.Reader) (*Header, error) {
	// Read until LF shows up, otherwise fail.
	// At this point, can't be sure CR precedes LF which will be validated next.
	line, err := reader.ReadString('\n')
	if err != nil {
		return nil, ErrLineMustEndWithCrlf
	}
	if !strings.HasSuffix(line, crlf) {
		return nil, ErrLineMustEndWithCrlf
	}
	// Check full signature.
	tokens := strings.Split(line[:len(line)-2], separator)
	transportProtocol := UNSPEC // doesn't exist in v1 but fits UNKNOWN.
	if len(tokens) > 0 {
		// Read address family and protocol
		switch tokens[1] {
		case "TCP4":
			transportProtocol = TCPv4
		case "TCP6":
			transportProtocol = TCPv6
		case "UNKNOWN": // no-op as UNSPEC is set already
		default:
			return nil, ErrCantReadAddressFamilyAndProtocol
		}

		// Expect 6 tokens only when UNKNOWN is not present.
		if !transportProtocol.IsUnspec() && len(tokens) < 6 {
			return nil, ErrCantReadAddressFamilyAndProtocol
		}
	}

	// Allocation only happens when a signature is found.
	header := initVersion1()
{'fraction_non_alphanumeric': 0.11316568047337278, 'fraction_numerical': 0.011834319526627219, 'mean_word_length': 3.5925297113752124, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 5, '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': '2899777', 'n_tokens_mistral': 988, 'n_tokens_neox': 912, 'n_words': 285}