<commit_before>package phpprocess

import (
	"bufio"
	"errors"
	"fmt"
	"math"
	"net/http"
	"os/exec"
	"strings"
	"time"
)

type PhpProcess struct {
	dir string
	port int
	host string
	cmd *exec.Cmd
}

func NewPhpProcess(dir string) (ph *PhpProcess, err error) {
	for p := 8001; p < int(math.Pow(2, 16)); p++ {
		ph = &PhpProcess{
			dir: dir,
			// Use 127.0.0.1 here instead of localhost
			// otherwise PHP only listens on ::1
			host: fmt.Sprintf("127.0.0.1:%d", p),
		}
		ph.cmd, err = runPhp(ph.dir, ph.host)
		if err == nil {
			return
		}
	}
	return nil, errors.New("No free ports found")
}

func (ph *PhpProcess) Close() {
	err := ph.cmd.Process.Kill()
	if err != nil {
		panic(err)
	}
}

func (ph *PhpProcess) MakeRequest(w http.ResponseWriter, r *http.Request) (err error) {
	u := r.URL
	u.Scheme = "http"
	u.Host = ph.host

	// Set up a client which won't redirect
	client := &http.Client{
		CheckRedirect: func (req *http.Request, via []*http.Request) error {
			return errors.New("STOP")
		},
	}

	// Make the request
	resp, err := client.Get(u.String())
	if err != nil && !strings.HasSuffix(err.Error(), "STOP") {
		fmt.Println(err)
		return
	} else {
		err = nil
	}
	defer resp.Body.Close()

	// Headers
	headers := w.Header()
	for k, v := range resp.Header {
		headers[k] = v
	}

	// Status code
	w.WriteHeader(resp.StatusCode)

	// Body
	bufWriter := bufio.NewWriter(w)
	bufWriter.ReadFrom(resp.Body)
	bufWriter.Flush()

	return
}

// A low-level command
// Starts PHP running, waits one second, returns an error if PHP stopped during that time
func runPhp(dir string, host string) (cmd *exec.Cmd, err error) {
	cmd = exec.Command(<commit_msg>Do the request a bit more cleanly<commit_after>package phpprocess

import (
	"bufio"
	"errors"
	"fmt"
	"math"
	"net/http"
	"os/exec"
	"time"
)

type PhpProcess struct {
	dir string
	port int
	host string
	cmd *exec.Cmd
}

func NewPhpProcess(dir string) (ph *PhpProcess, err error) {
	for p := 8001; p < int(math.Pow(2, 16)); p++ {
		ph = &PhpProcess{
			dir: dir,
			// Use 127.0.0.1 here instead of localhost
			// otherwise PHP only listens on ::1
			host: fmt.Sprintf("127.0.0.1:%d", p),
		}
		ph.cmd, err = runPhp(ph.dir, ph.host)
		if err == nil {
			return
		}
	}
	return nil, errors.New("No free ports found")
}

func (ph *PhpProcess) Close() {
	err := ph.cmd.Process.Kill()
	if err != nil {
		panic(err)
	}
}

func (ph *PhpProcess) MakeRequest(w http.ResponseWriter, r *http.Request) (err error) {
	r.URL.Scheme = "http"
	r.URL.Host = ph.host

	// Make the request
	tr := &http.Transport{}

	resp, err := tr.RoundTrip(r)
	if err != nil {
		return
	}
	defer resp.Body.Close()

	// Headers
	headers := w.Header()
	for k, v := range resp.Header {
		headers[k] = v
	}

	// Status code
	w.WriteHeader(resp.StatusCode)

	// Body
	bufWriter := bufio.NewWriter(w)
	bufWriter.ReadFrom(resp.Body)
	bufWriter.Flush()

	return
}

// A low-level command
// Starts PHP running, waits one second, returns an error if PHP stopped during that time
func runPhp(dir string, host string) (cmd *exec.Cmd, err error) {
	cmd = exec.Command(
{'fraction_non_alphanumeric': 0.1526742301458671, 'fraction_numerical': 0.012965964343598054, 'mean_word_length': 3.198639455782313, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '7523941', 'n_tokens_mistral': 1277, 'n_tokens_neox': 1194, 'n_words': 336}