<commit_before>// Copyright 2012 <NAME>. All Rights Reserved.
// Author: <EMAIL> (<NAME>)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package http

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"reflect"
)

// A connection to a particular server over a particular protocol (HTTP or
// HTTPS).
type Conn interface {
	// Call the server with the supplied request, returning a response if and
	// only if a response was received from the server. (That is, a 500 error
	// from the server will be returned here as a response with a nil error).
	SendRequest(r *Request) (*Response, error)
}

// Return a connection to the supplied endpoint, based on its scheme and host
// fields.
func NewConn(endpoint *url.URL) (Conn, error) {
	switch endpoint.Scheme {
	case "http", "https":
	default:
		return nil, fmt.Errorf("Unsupported scheme: %s", endpoint.Scheme)
	}

	return &conn{endpoint}, nil
}

type conn struct {
	endpoint *url.URL
}

func makeRawQuery(r *Request) string {
	values := url.Values{}
	for key, val := range r.Parameters {
		values.Set(key, val)
	}

	return values.Encode()
}

func (c *conn) SendRequest(r *Request) (*Response, error) {
	// Create an appropriate URL.
	url := url.URL{
		Scheme:   c.endpoint.Scheme,
		Host:     c.endpoint.Host,
		Path:     r.Path,
		RawQuery: makeRawQuery(r),
	}

	urlStr := url.String()

	// Create a request to the system HTTP library.
	sysReq, err := http.NewRequest(r.Verb, urlStr, bytes.NewBuffer(r.Body))
	if err != nil {
		return nil, fmt.Errorf("http.NewRequest: %v", err)
	}

	// Copy headers.
	for key, val := range r.Headers {
		sysReq.Header.Set(key, val)
	}

	// Call the system HTTP library.
	sysResp, err := http.DefaultClient.Do(sysReq)
	if err != nil {
		// TODO(jacobsa): Remove this logging once it has yielded useful results
		// for investigating this issue:
		//
		//     https://github.com/jacobsa/comeback/issues/11
		//
		log.Println(
			"http.DefaultClient.Do:",
			reflect.TypeOf(err),
			reflect.ValueOf(err),
		)

		return nil, fmt.Errorf("http.DefaultClient.Do: %v", err)
	}

	// Convert the response.
	resp := &Response{
		StatusCode: sysResp.StatusCode,
	}

	if resp.Body, err = ioutil.ReadAll(sysResp.Body); err != nil {
		return nil, fmt.Errorf("Reading body: %v", err)
	}

	sysResp.Body.Close()

	return resp, nil
}
<commit_msg>Added more logging.<commit_after>// Copyright 2012 <NAME>. All Rights Reserved.
// Author: <EMAIL> (<NAME>)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package http

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"log"
	"net"
	"net/http"
	"net/url"
	"reflect"
)

// A connection to a particular server over a particular protocol (HTTP or
// HTTPS).
type Conn interface {
	// Call the server with the supplied request, returning a response if and
	// only if a response was received from the server. (That is, a 500 error
	// from the server will be returned here as a response with a nil error).
	SendRequest(r *Request) (*Response, error)
}

// Return a connection to the supplied endpoint, based on its scheme and host
// fields.
func NewConn(endpoint *url.URL) (Conn, error) {
	switch endpoint.Scheme {
	case "http", "https":
	default:
		return nil, fmt.Errorf("Unsupported scheme: %s", endpoint.Scheme)
	}

	return &conn{endpoint}, nil
}

type conn struct {
	endpoint *url.URL
}

func makeRawQuery(r *Request) string {
	values := url.Values{}
	for key, val := range r.Parameters {
		values.Set(key, val)
	}

	return values.Encode()
}

func (c *conn) SendRequest(r *Request) (*Response, error) {
	// Create an appropriate URL.
	url := url.URL{
		Scheme:   c.endpoint.Scheme,
		Host:     c.endpoint.Host,
		Path:     r.Path,
		RawQuery: makeRawQuery(r),
	}

	urlStr := url.String()

	// Create a request to the system HTTP library.
	sysReq, err := http.NewRequest(r.Verb, urlStr, bytes.NewBuffer(r.Body))
	if err != nil {
		return nil, fmt.Errorf("http.NewRequest: %v", err)
	}

	// Copy headers.
	for key, val := range r.Headers {
		sysReq.Header.Set(key, val)
	}

	// Call the system HTTP library.
	sysResp, err := http.DefaultClient.Do(sysReq)
	if err != nil {
		// TODO(jacobsa): Remove this logging once it has yielded useful results
		// for investigating this issue:
		//
		//     https://github.com/jacobsa/comeback/issues/11
		//
		log.Println(
			"http.DefaultClient.Do:",
			reflect.TypeOf(err),
			reflect.ValueOf(err),
		)

		if opErr, ok := err.(*net.OpError); ok {
			log.Println("Op:        ", opErr.Op)
			log.Println("Net:       ", opErr.Net)
			log.Println("Addr:      ", opErr.Addr)
			log.Println("Err:       ", opErr.Err)
			log.Println("Temporary: ", opErr.Temporary())
			log.Println("Timeout:   ", opErr.Timeout())
		}

		return nil, fmt.Errorf("http.DefaultClient.Do: %v", err)
	}

	// Convert the response.
	resp := &Response{
		StatusCode: sysResp.StatusCode,
	}

	if resp.Body, err = ioutil.ReadAll(sysResp.Body); err != nil {
		return nil, fmt.Errorf("Reading body: %v", err)
	}

	sysResp.Body.Close()

	return resp, nil
}

{'fraction_non_alphanumeric': 0.1322671997334666, 'fraction_numerical': 0.004331167749458604, 'mean_word_length': 3.2855103497501785, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '3047792', 'n_tokens_mistral': 2055, 'n_tokens_neox': 1935, 'n_words': 666}