<commit_before>package server

// transfer2go agent server implementation
// Copyright (c) 2017 - <NAME> <<EMAIL>>

import (
	"encoding/hex"
	"encoding/json"
	"fmt"
	"hash/adler32"
	"io"
	"log"
	"net/http"
	"os"
	"strings"
	"time"

	"github.com/vkuznet/transfer2go/core"
	"github.com/vkuznet/transfer2go/utils"
)

// GET methods

// FilesHandler provides information about files in catalog
func FilesHandler(w http.ResponseWriter, r *http.Request) {

	if r.Method != "GET" {
		w.WriteHeader(http.StatusMethodNotAllowed)
		return
	}
	lfn := r.FormValue("lfn")
	dataset := r.FormValue("dataset")
	block := r.FormValue("block")
	files := core.TFC.Files(dataset, block, lfn)
	data, err := json.Marshal(files)<commit_msg>Implement CMS authentication via SiteDB<commit_after>package server

// transfer2go agent server implementation
// Copyright (c) 2017 - <NAME> <<EMAIL>>

import (
	"encoding/hex"
	"encoding/json"
	"fmt"
	"hash/adler32"
	"io"
	"log"
	"net/http"
	"net/http/httputil"
	"os"
	"strings"
	"time"

	"github.com/vkuznet/transfer2go/core"
	"github.com/vkuznet/transfer2go/utils"
)

// global variable which we initialize once
var _userDNs []string

func userDNs() []string {
	var out []string
	rurl := "https://cmsweb.cern.ch/sitedb/data/prod/people"
	resp := utils.FetchResponse(rurl, []byte{})
	if resp.Error != nil {
		log.Println("ERROR unable to fetch SiteDB records", resp.Error)
		return out
	}
	/*
		headers, _, _, err := jsonparser.Get(resp.Data, "desc", "columns")
		if err != nil {
			log.Println("ERROR unable to fetch SiteDB columns", err)
			return out
		}
		var idx int
		for i, h := range headers {
			if string(h) == "dn" {
				idx = i
				break
			}
		}
		jsonparser.ArrayEach(resp.Data, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
			dn, err := jsonparser.GetString(value, fmt.Sprintf("[%s]", idx))
			if err == nil {
				out = append(out, dn)
			}
		}, "result")
	*/
	var rec map[string]interface{}
	err := json.Unmarshal(resp.Data, &rec)
	if err != nil {
		log.Println("ERROR unable to unmarshal response", err)
		return out
	}
	desc := rec["desc"].(map[string]interface{})
	headers := desc["columns"].([]interface{})
	var idx int
	for i, h := range headers {
		if h.(string) == "dn" {
			idx = i
			break
		}
	}
	values := rec["result"].([]interface{})
	for _, item := range values {
		val := item.([]interface{})
		v := val[idx]
		if v != nil {
			out = append(out, v.(string))
		}
	}
	return out
}

func init() {
	_userDNs = userDNs()
}

// custom logic for CMS authentication, users may implement their own logic here
func auth(r *http.Request) bool {
	if utils.VERBOSE > 1 {
		dump, err := httputil.DumpRequest(r, true)
		log.Println("AuthHandler HTTP request", r, string(dump), err)
	}
	userDN := utils.UserDN(r)
	log.Println("auth userDN", userDN)
	return utils.InList(userDN, _userDNs)
}

func AuthHandler(w http.ResponseWriter, r *http.Request) {
	// check if server started with hkey file (auth is required)
	status := auth(r)
	if !status {
		log.Println(_userDNs)
		msg := "You are not allowed to access this resource"
		http.Error(w, msg, http.StatusForbidden)
		return
	}
	arr := strings.Split(r.URL.Path, "/")
	path := arr[len(arr)-1]
	switch path {
	case "status":
		StatusHandler(w, r)
	case "agents":
		AgentsHandler(w, r)
	case "files":
		FilesHandler(w, r)
	case "reset":
		ResetHandler(w, r)
	case "tfc":
		TFCHandler(w, r)
	case "upload":
		UploadDataHandler(w, r)
	case "request":
		RequestHandler(w, r)
	case "register":
		RegisterAgentHandler(w, r)
	case "protocol":
		RegisterProtocolHandler(w, r)
	default:
		DefaultHandler(w, r)
	}
}

// GET methods

// FilesHandler provides information about files in catalog
func FilesHandler(w http.ResponseWriter, r *http.Request) {

	if r.Method != "GET" {
		w.WriteHeader(http.StatusMethodNotAllowed)
		return
	}
	lfn := r.FormValue("lfn")
	dataset := r.FormValue("dataset")
	block := r.FormValue("block")
	files := core.TFC.Files(dataset, block, lfn)
	data, err := json.Marshal(files)
{'fraction_non_alphanumeric': 0.15582149090002492, 'fraction_numerical': 0.004986287708800797, 'mean_word_length': 3.8986568986568986, 'pattern_counts': {'":': 9, '<': 9, '<?xml version=': 0, '>': 10, 'https://': 1, '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': '1259483', 'n_tokens_mistral': 1551, 'n_tokens_neox': 1466, 'n_words': 372}