<commit_before>package http

import (
	"encoding/json"
	"fmt"
	"github.com/getlantern/flashlight/client"
	"github.com/getlantern/flashlight/config"
	"github.com/getlantern/flashlight/util"
	"github.com/getlantern/golog"
	"github.com/getlantern/whitelist"
	"github.com/skratchdot/open-golang/open"
	"net/http"
	"reflect"
	"time"

	"github.com/getlantern/tarfs"
)

const (
	UiUrl = "http://%s"
	UiDir = "src/github.com/getlantern/ui/app"
)

var (
	log = golog.LoggerFor("http")
)

type JsonResponse struct {
	Error     string   `json:"Error, omitempty"`
	Whitelist []string `json:"Whitelist, omitempty"`
	Original  []string `json:"Original, omitempty"`
}

type WhitelistHandler struct {
	http.HandlerFunc
	whitelist *whitelist.Whitelist
	wlChan    chan *whitelist.Config
}

func sendJsonResponse(w http.ResponseWriter, response *JsonResponse, indent bool) {

	enc := json.NewEncoder(w)
	err := enc.Encode(response)
	if err != nil {
		log.Fatalf("error sending json response %v", err)
	}
}

func setResponseHeaders(w http.ResponseWriter) {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	w.Header().Set("Access-Control-Allow-Origin", "*")
	w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token")
	w.Header().Set("Access-Control-Allow-Credentials", "True")
	w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")

}

func (wlh WhitelistHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	var response JsonResponse

	setResponseHeaders(w)

	switch r.Method {
	case "OPTIONS":
		// return if it's a preflight OPTIONS request
		// this is mainly for testing the UI when it's running on
		// a separate port
		return
	case "POST":
		// update whitelist
		decoder := json.NewDecoder(r.Body)
		var entries []string
		err := decoder.Decode(&entries)
		if err != nil {
			log.Error(err)
			response.Error = fmt.Sprintf("Error decoding whitelist: %s", err.Error())
		} else {
			wl := wlh.whitelist.UpdateEntries(entries)
			copy := wlh.whitelist.Copy()
			log.Debugf("New whitelist is %+v", copy)
			wlh.wlChan <- wlh.whitelist.Copy()
			response.Whitelist = wl
		}
	case "GET":
		response.Whitelist = wlh.whitelist.RefreshEntries()
	default:
		log.Debugf("Received %s", response.Error)
		response.Error = "Invalid whitelist HTTP request"
		response.Whitelist = nil
	}
	sendJsonResponse(w, &response, false)<commit_msg>update comment style<commit_after>package http

import (
	"encoding/json"
	"fmt"
	"github.com/getlantern/flashlight/client"
	"github.com/getlantern/flashlight/config"
	"github.com/getlantern/flashlight/util"
	"github.com/getlantern/golog"
	"github.com/getlantern/whitelist"
	"github.com/skratchdot/open-golang/open"
	"net/http"
	"reflect"
	"time"

	"github.com/getlantern/tarfs"
)

const (
	UiUrl = "http://%s"
	UiDir = "src/github.com/getlantern/ui/app"
)

var (
	log = golog.LoggerFor("http")
)

type JsonResponse struct {
	Error     string   `json:"Error, omitempty"`
	Whitelist []string `json:"Whitelist, omitempty"`
	Original  []string `json:"Original, omitempty"`
}

type WhitelistHandler struct {
	http.HandlerFunc
	whitelist *whitelist.Whitelist
	wlChan    chan *whitelist.Config
}

func sendJsonResponse(w http.ResponseWriter, response *JsonResponse, indent bool) {

	enc := json.NewEncoder(w)
	err := enc.Encode(response)
	if err != nil {
		log.Fatalf("error sending json response %v", err)
	}
}

func setResponseHeaders(w http.ResponseWriter) {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	w.Header().Set("Access-Control-Allow-Origin", "*")
	w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token")
	w.Header().Set("Access-Control-Allow-Credentials", "True")
	w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")

}

func (wlh WhitelistHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	var response JsonResponse

	setResponseHeaders(w)

	switch r.Method {
	case "OPTIONS":
		// return if it's a preflight OPTIONS request
		// this is mainly for testing the UI when it's running on
		// a separate port
		return
	case "POST":
		// update whitelist
		decoder := json.NewDecoder(r.Body)
		var entries []string
		err := decoder.Decode(&entries)
		if err != nil {
			log.Error(err)
			response.Error = fmt.Sprintf("Error decoding whitelist: %s", err.Error())
		} else {
			wl := wlh.whitelist.UpdateEntries(entries)
			copy := wlh.whitelist.Copy()
			log.Debug("Propagating whitelist changes..")
			wlh.wlChan <- copy
			response.Whitelist = wl
		}
	case "GET":
		response.Whitelist = wlh.whitelist.RefreshEntries()
	default:
		log.Debugf("Received %s", response.Error)
		response.Error = "Invalid whitelist HTTP request"
		response.Whitelist = nil
	}
	sendJsonResponse(w, &response, false)
{'fraction_non_alphanumeric': 0.14634651210929414, 'fraction_numerical': 0.00041399296211964395, 'mean_word_length': 4.423120089786757, 'pattern_counts': {'":': 6, '<': 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': '7653337', 'n_tokens_mistral': 1809, 'n_tokens_neox': 1685, 'n_words': 353}