<commit_before>package main

import (
	"bytes"
	"encoding/base64"
	"fmt"
	"io/ioutil"
	"net/http"
	"regexp"
	"strings"

	"github.com/elazarl/goproxy"
)

type BasicAuthFunc func(authData *BasicAuthData) *BasicAuthResponse
type DigestAuthFunc func(authData *DigestAuthData, op int) *DigestAuthResponse

var unauthorizedMsg = []byte("407 Proxy Authentication Required")
var proxyAuthorizatonHeader = "Proxy-Authorization"

func basicUnauthorized(req *http.Request, realm string) *http.Response {
	h := fmt.Sprintf("Basic realm=%s", realm)

	return &http.Response{
		StatusCode:    407,
		ProtoMajor:    1,
		ProtoMinor:    1,
		Request:       req,
		Header:        http.Header{"Proxy-Authenticate": []string{h}},
		Body:          ioutil.NopCloser(bytes.NewBuffer(unauthorizedMsg)),
		ContentLength: int64(len(unauthorizedMsg)),
	}
}

func digestUnauthorized(req *http.Request, realm string, authFunc DigestAuthFunc) *http.Response {
	authResult := authFunc(nil, getNonce)
	nonce := authResult.data
	header := fmt.Sprintf("Digest realm=\"%s\", nonce=\"%s\"", realm, nonce)

	return &http.Response{
		StatusCode:    407,
		ProtoMajor:    1,
		ProtoMinor:    1,
		Request:       req,
		Header:        http.Header{"Proxy-Authenticate": []string{header}},
		Body:          ioutil.NopCloser(bytes.NewBuffer(unauthorizedMsg)),
		ContentLength: int64(len(unauthorizedMsg)),
	}
}

func getDigestAuthData(req *http.Request) *DigestAuthData {
	authHeader := strings.SplitN(req.Header.Get(proxyAuthorizatonHeader), " ", 2)
	req.Header.Del(proxyAuthorizatonHeader)

	if len(authHeader) != 2 || authHeader[0] != "Digest" {
		return nil
	}

	m := make(map[string]string)
	tokens := regexp.MustCompile("\",").Split(authHeader[1], -1)

	for _, v := range tokens {
		s := strings.Trim(v, " \r\n")
		kv := strings.SplitN(s, "=", 2)
		if len(kv) != 2 {<commit_msg>use const. values instead of magic string literals<commit_after>package main

import (
	"bytes"
	"encoding/base64"
	"fmt"
	"io/ioutil"
	"net/http"
	"regexp"
	"strings"

	"github.com/elazarl/goproxy"
)

type BasicAuthFunc func(authData *BasicAuthData) *BasicAuthResponse
type DigestAuthFunc func(authData *DigestAuthData, op int) *DigestAuthResponse

var unauthorizedMsg = []byte("407 Proxy Authentication Required")

const proxyAuthorizatonHeader = "Proxy-Authorization"
const proxyAuthenticateHeader = "Proxy-Authenticate"

func basicUnauthorized(req *http.Request, realm string) *http.Response {
	header := fmt.Sprintf("Basic realm=%s", realm)

	return &http.Response{
		StatusCode:    407,
		ProtoMajor:    1,
		ProtoMinor:    1,
		Request:       req,
		Header:        http.Header{proxyAuthenticateHeader: []string{header}},
		Body:          ioutil.NopCloser(bytes.NewBuffer(unauthorizedMsg)),
		ContentLength: int64(len(unauthorizedMsg)),
	}
}

func digestUnauthorized(req *http.Request, realm string, authFunc DigestAuthFunc) *http.Response {
	authResult := authFunc(nil, getNonce)
	nonce := authResult.data
	header := fmt.Sprintf("Digest realm=\"%s\", nonce=\"%s\"", realm, nonce)

	return &http.Response{
		StatusCode:    407,
		ProtoMajor:    1,
		ProtoMinor:    1,
		Request:       req,
		Header:        http.Header{proxyAuthenticateHeader: []string{header}},
		Body:          ioutil.NopCloser(bytes.NewBuffer(unauthorizedMsg)),
		ContentLength: int64(len(unauthorizedMsg)),
	}
}

func getDigestAuthData(req *http.Request) *DigestAuthData {
	authHeader := strings.SplitN(req.Header.Get(proxyAuthorizatonHeader), " ", 2)
	req.Header.Del(proxyAuthorizatonHeader)

	if len(authHeader) != 2 || authHeader[0] != "Digest" {
		return nil
	}

	m := make(map[string]string)
	tokens := regexp.MustCompile("\",").Split(authHeader[1], -1)

	for _, v := range tokens {
		s := strings.Trim(v, " \r\n")
		kv := strings.SplitN(s, "=", 2)
		if len(kv) != 2 {
{'fraction_non_alphanumeric': 0.14376154049063572, 'fraction_numerical': 0.013716697441308361, 'mean_word_length': 4.535766423357664, 'pattern_counts': {'":': 2, '<': 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': '2378055', 'n_tokens_mistral': 1390, 'n_tokens_neox': 1309, 'n_words': 266}