<commit_before>// Copyright 2013 <NAME> and the websocketd team.
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"fmt"
	"net"
	"os"
	"strconv"
	"strings"
	"time"

	"code.google.com/p/go.net/websocket"
)

const (
	gatewayInterface = "websocketd-CGI/0.1"
)

var headerNewlineToSpace = strings.NewReplacer("\n", " ", "\r", " ")
var headerDashToUnderscore = strings.NewReplacer("-", "_")

func createEnv(ws *websocket.Conn, config *Config, urlInfo *URLInfo) ([]string, error) {
	req := ws.Request()
	headers := req.Header
	url := req.URL

	remoteAddr, remotePort, err := net.SplitHostPort(req.RemoteAddr)
	if err != nil {
		return nil, err
	}

	var remoteHost string
	if config.ReverseLookup {
		remoteHosts, err := net.LookupAddr(remoteAddr)
		if err != nil {
			return nil, err
		}
		if len(remoteHosts) == 0 {
			remoteHost = remoteAddr
		} else {
			remoteHost = remoteHosts[0]
		}
	} else {
		remoteHost = remoteAddr
	}

	serverName, serverPort, err := net.SplitHostPort(req.Host)
	if err != nil {
		return nil, err
	}

	uniqueId := time.Now().UnixNano() // Just use this a unique counter.

	standardEnvCount := 20
	parentEnv := os.Environ()
	fmt.Printf("len %d\n", len(parentEnv))
	env := make([]string, 0, len(headers)+standardEnvCount+len(parentEnv))
	for _, v := range parentEnv {
		env = append(env, v)
	}

	// IMPORTANT ---> Adding a header? Make sure standardHeaderCount (above) is up to date.

	// Standard CGI specification headers.
	// As defined in http://tools.ietf.org/html/rfc3875
	env = appendEnv(env, "REMOTE_ADDR", remoteAddr)
	env = appendEnv(env, "REMOTE_HOST", remoteHost)
	env = appendEnv(env, "SERVER_NAME", serverName)
	env = appendEnv(env, "SERVER_PORT", serverPort)
	env = appendEnv(env, "SERVER_PROTOCOL", req.Proto)
	env = appendEnv(env, "SERVER_SOFTWARE", fmt.Sprintf("websocketd/%s", Version()))
	env = appendEnv(env, "GATEWAY_INTERFACE", gatewayInterface)
	env = appendEnv(env, "REQUEST_METHOD", req.Method)
	env = appendEnv(env, "SCRIPT_NAME", urlInfo.ScriptPath)
	env = appendEnv(env, "PATH_INFO", urlInfo.PathInfo)<commit_msg>remove debug logging<commit_after>// Copyright 2013 <NAME> and the websocketd team.
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"fmt"
	"net"
	"os"
	"strconv"
	"strings"
	"time"

	"code.google.com/p/go.net/websocket"
)

const (
	gatewayInterface = "websocketd-CGI/0.1"
)

var headerNewlineToSpace = strings.NewReplacer("\n", " ", "\r", " ")
var headerDashToUnderscore = strings.NewReplacer("-", "_")

func createEnv(ws *websocket.Conn, config *Config, urlInfo *URLInfo) ([]string, error) {
	req := ws.Request()
	headers := req.Header
	url := req.URL

	remoteAddr, remotePort, err := net.SplitHostPort(req.RemoteAddr)
	if err != nil {
		return nil, err
	}

	var remoteHost string
	if config.ReverseLookup {
		remoteHosts, err := net.LookupAddr(remoteAddr)
		if err != nil {
			return nil, err
		}
		if len(remoteHosts) == 0 {
			remoteHost = remoteAddr
		} else {
			remoteHost = remoteHosts[0]
		}
	} else {
		remoteHost = remoteAddr
	}

	serverName, serverPort, err := net.SplitHostPort(req.Host)
	if err != nil {
		return nil, err
	}

	uniqueId := time.Now().UnixNano() // Just use this a unique counter.

	standardEnvCount := 20
	parentEnv := os.Environ()
	env := make([]string, 0, len(headers)+standardEnvCount+len(parentEnv))
	for _, v := range parentEnv {
		env = append(env, v)
	}

	// IMPORTANT ---> Adding a header? Make sure standardHeaderCount (above) is up to date.

	// Standard CGI specification headers.
	// As defined in http://tools.ietf.org/html/rfc3875
	env = appendEnv(env, "REMOTE_ADDR", remoteAddr)
	env = appendEnv(env, "REMOTE_HOST", remoteHost)
	env = appendEnv(env, "SERVER_NAME", serverName)
	env = appendEnv(env, "SERVER_PORT", serverPort)
	env = appendEnv(env, "SERVER_PROTOCOL", req.Proto)
	env = appendEnv(env, "SERVER_SOFTWARE", fmt.Sprintf("websocketd/%s", Version()))
	env = appendEnv(env, "GATEWAY_INTERFACE", gatewayInterface)
	env = appendEnv(env, "REQUEST_METHOD", req.Method)
	env = appendEnv(env, "SCRIPT_NAME", urlInfo.ScriptPath)
	env = appendEnv(env, "PATH_INFO", urlInfo.PathInfo)
{'fraction_non_alphanumeric': 0.1424249476378869, 'fraction_numerical': 0.006981615080288574, 'mean_word_length': 4.22235722964763, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '7157284', 'n_tokens_mistral': 1600, 'n_tokens_neox': 1507, 'n_words': 392}