<commit_before>package etcdhttp

import (
	"io"
	"net/http"
	"time"

	"code.google.com/p/go.net/context"
	etcdserver "github.com/coreos/etcd/etcdserver2"
	"github.com/coreos/etcd/raft"
)

func SendWithPrefix(prefix string, send etcdserver.SendFunc) etcdserver.SendFunc {
	return etcdserver.SendFunc(func(m []raft.Message) {
		/*
			url = parseurl
			u.Path = prefix + u.Path
			for maxTrys {
				resp, err := http.Post(u.String(), ...)
				if err...
					backoff?
			}
		*/
	})
}

const DefaultTimeout = 500 * time.Millisecond

type Handler struct {
	Timeout time.Duration
	Server  etcdserver.Server
}

func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	// TODO: set read/write timeout?

	timeout := h.Timeout
	if timeout == 0 {
		timeout = DefaultTimeout
	}

	ctx, _ := context.WithTimeout(context.Background(), timeout)
	// TODO(bmizerany): watch the closenotify chan in another goroutine can
	// call cancel when it closes. be sure to watch ctx.Done() too so we
	// don't leak a ton of these goroutines.

	rr, err := parseRequest(r)
	if err != nil {
		http.Error(w, err.Error(), 400)
		return
	}

	resp, err := h.Server.Do(ctx, rr)
	if err != nil {
		// TODO(bmizerany): switch on store errors and etcdserver.ErrUnknownMethod
		panic("TODO")
	}

	encodeResponse(w, resp)
}

func parseRequest(r *http.Request) (etcdserver.Request, error) {
	return etcdserver.Request{}, nil
}

func encodeResponse(w io.Writer, resp etcdserver.Response) {

}
<commit_msg>etcdserver/etcdhttp: encode response<commit_after>package etcdhttp

import (
	"encoding/json"
	"fmt"
	"net/http"
	"time"

	"code.google.com/p/go.net/context"
	etcdserver "github.com/coreos/etcd/etcdserver2"
	"github.com/coreos/etcd/store"
)

const DefaultTimeout = 500 * time.Millisecond

type Handler struct {
	Timeout time.Duration
	Server  etcdserver.Server
}

func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	// TODO: set read/write timeout?

	timeout := h.Timeout
	if timeout == 0 {
		timeout = DefaultTimeout
	}

	ctx, _ := context.WithTimeout(context.Background(), timeout)
	// TODO(bmizerany): watch the closenotify chan in another goroutine can
	// call cancel when it closes. be sure to watch ctx.Done() too so we
	// don't leak a ton of these goroutines.

	rr, err := parseRequest(r)
	if err != nil {
		http.Error(w, err.Error(), 400)
		return
	}

	resp, err := h.Server.Do(ctx, rr)
	if err != nil {
		// TODO(bmizerany): switch on store errors and etcdserver.ErrUnknownMethod
		panic("TODO")
	}

	encodeResponse(ctx, w, resp)
}

func parseRequest(r *http.Request) (etcdserver.Request, error) {
	return etcdserver.Request{}, nil
}

func encodeResponse(ctx context.Context, w http.ResponseWriter, resp etcdserver.Response) error {
	var ev *store.Event
	switch {
	case resp.Event != nil:
		ev = resp.Event
	case resp.Watcher != nil:
		// TODO(bmizerany): support streaming?
		defer resp.Watcher.Remove()
		select {
		case ev = <-resp.Watcher.EventChan:
		case <-ctx.Done():
			return ctx.Err()
		}
	default:
		panic("should not be rechable")
	}

	w.Header().Set("Content-Type", "application/json")
	w.Header().Add("X-Etcd-Index", fmt.Sprint(ev.Index()))

	if ev.IsCreated() {
		w.WriteHeader(http.StatusCreated)
	} else {
		w.WriteHeader(http.StatusOK)
	}

	return json.NewEncoder(w).Encode(ev)
}

{'fraction_non_alphanumeric': 0.13632232656770676, 'fraction_numerical': 0.004847016055740685, 'mean_word_length': 3.7647907647907646, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1526342', 'n_tokens_mistral': 1260, 'n_tokens_neox': 1169, 'n_words': 292}