<commit_before>package inout

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"net/http"

	"github.com/JREAMLU/core/global"
	"github.com/JREAMLU/core/guid"
	"github.com/JREAMLU/core/sign"
	"github.com/beego/i18n"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
	"github.com/astaxie/beego/validation"
	"github.com/pquerna/ffjson/ffjson"
)

type Header struct {
	Source      []string `json:"Source" valid:"Required"`
	Version     []string `json:"Version" `
	SecretKey   []string `json:"Secret-Key" `
	RequestID   []string `json:"Request-Id" valid:"Required"`
	ContentType []string `json:"Content-Type" valid:"Required"`
	Accept      []string `json:"Accept" valid:"Required"`
	Token       []string `json:"Token" `
	IP          []string `json:"Ip" valid:"Required"`
}

type Result struct {
	CheckRes  map[string]string
	RequestID string
	Message   string
}

func InputParams(r *context.Context) map[string]interface{} {
	r.Request.ParseForm()

	headerMap := r.Request.Header
	id := []string{"123"}
	headerMap["Request-Id"] = id
	fmt.Println("-------", headerMap["Request-Id"][0])
	header, _ := json.Marshal(headerMap)

	body := r.Input.RequestBody

	cookiesSlice := r.Request.Cookies()
	cookies, _ := json.Marshal(cookiesSlice)

	querystrMap := r.Request.Form
	querystr, _ := json.Marshal(querystrMap)

	beego.Trace("input params header" + string(header))
	beego.Trace("input params body" + string(body))
	beego.Trace("input params cookies" + string(cookies))
	beego.Trace("input params querystr" + string(querystr))

	data := make(map[string]interface{})
	mu.Lock()
	data["header"] = header
	data["body"] = body
	data["cookies"] = cookies
	data["querystr"] = querystr
	data["headermap"] = headerMap
	data["cookiesslice"] = cookiesSlice
	data["querystrmap"] = querystrMap
	mu.Unlock()

	return data
}

/**
 *	@auther		jream.lu
 *	@intro		入参验证
 *	@logic
 *	@todo		返回值
 *	@meta		meta map[string][]string	   rawMetaHeader
 *	@data		data []byte 					rawDataBody 签名验证
 *	@data		data ...interface{}	切片指针	rawDataBody
 *	@return 	返回 true, metaMap, error
 */
func InputParamsCheck(data map[string]interface{}, stdata ...interface{}) (result Result, err error) {
	headerRes, err := HeaderCheck(data)
	if err != nil {
		return headerRes, err
	}

	result.CheckRes = nil
	result.Message = ""
	result.RequestID = headerRes.RequestID

	valid := validation.Validation{}

	for _, val := range stdata {
		is, err := valid.Valid(val)
		if err != nil {
			beego.Trace(
				i18n.Tr(
					global.Lang,
					"outputParams.SYSTEMILLEGAL") + err.Error(),
			)
			result.Message = i18n.Tr(global.Lang, "outputParams.SYSTEMILLEGAL")

			return result, err

		}

		if !is {
			for _, err := range valid.Errors {
				beego.Trace(
					i18n.Tr(
						global.Lang,
						"outputParams.DATAPARAMSILLEGAL") + err.Key + ":" + err.Message)
				result.Message = i18n.Tr(
					global.Lang,
					"outputParams.DATAPARAMSILLEGAL") + " " + err.Key + ":" + err.Message

				return result, errors.New(i18n.Tr(global.Lang, "outputParams.DATAPARAMSILLEGAL"))
			}
		}
	}

	//sign check
	if is, _ := beego.AppConfig.Bool("sign.onOff"); is {
		err = sign.ValidSign(data["body"].([]byte), beego.AppConfig.String("sign.secretKey"))
		if err != nil {
			result.Message = err.Error()
			return result, err
		}
	}

	return headerRes, nil
}

/**
 * header参数验证
 * 将header 放入map 返回
 *
 * @meta 	meta  map[string][]string 	header信息 map格式
 */
func HeaderCheck(data map[string]interface{}) (result Result, err error) {
	var h Header
	ffjson.Unmarshal(data["header"].([]byte), &h)

	rid := GetRequestID()
	if len(h.RequestID) > 0 && h.RequestID[0] != "" {
		rid = h.RequestID[0]
	}

	result.CheckRes = nil
	result.Message = ""
	result.RequestID = rid
<commit_msg>add request-id<commit_after>package inout

import (
	"bytes"
	"encoding/json"
	"errors"
	"net/http"

	"github.com/JREAMLU/core/global"
	"github.com/JREAMLU/core/guid"
	"github.com/JREAMLU/core/sign"
	"github.com/beego/i18n"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
	"github.com/astaxie/beego/validation"
	"github.com/pquerna/ffjson/ffjson"
)

type Header struct {
	Source      []string `json:"Source" valid:"Required"`
	Version     []string `json:"Version" `
	SecretKey   []string `json:"Secret-Key" `
	RequestID   []string `json:"Request-Id" valid:"Required"`
	ContentType []string `json:"Content-Type" valid:"Required"`
	Accept      []string `json:"Accept" valid:"Required"`
	Token       []string `json:"Token" `
	IP          []string `json:"Ip" valid:"Required"`
}

type Result struct {
	CheckRes  map[string]string
	RequestID string
	Message   string
}

var Rid string

func InputParams(r *context.Context) map[string]interface{} {
	r.Request.ParseForm()

	headerMap := r.Request.Header
	if _, ok := headerMap["Request-Id"]; !ok {
		rid := GetRequestID()
		headerMap["Request-Id"] = []string{rid}
	}
	Rid = headerMap["Request-Id"][0]
	header, _ := json.Marshal(headerMap)
	body := r.Input.RequestBody
	cookiesSlice := r.Request.Cookies()
	cookies, _ := json.Marshal(cookiesSlice)
	querystrMap := r.Request.Form
	querystr, _ := json.Marshal(querystrMap)

	beego.Trace(Rid + ":" + "input params header" + string(header))
	beego.Trace(Rid + ":" + "input params body" + string(body))
	beego.Trace(Rid + ":" + "input params cookies" + string(cookies))
	beego.Trace(Rid + ":" + "input params querystr" + string(querystr))

	data := make(map[string]interface{})
	mu.Lock()
	data["header"] = header
	data["body"] = body
	data["cookies"] = cookies
	data["querystr"] = querystr
	data["headermap"] = headerMap
	data["cookiesslice"] = cookiesSlice
	data["querystrmap"] = querystrMap
	mu.Unlock()

	return data
}

/**
 *	@auther		jream.lu
 *	@intro		入参验证
 *	@logic
 *	@todo		返回值
 *	@meta		meta map[string][]string	   rawMetaHeader
 *	@data		data []byte 					rawDataBody 签名验证
 *	@data		data ...interface{}	切片指针	rawDataBody
 *	@return 	返回 true, metaMap, error
 */
func InputParamsCheck(data map[string]interface{}, stdata ...interface{}) (result Result, err error) {
	headerRes, err := HeaderCheck(data)
	if err != nil {
		return headerRes, err
	}

	result.CheckRes = nil
	result.Message = ""
	result.RequestID = headerRes.RequestID

	valid := validation.Validation{}

	for _, val := range stdata {
		is, err := valid.Valid(val)
		if err != nil {
			beego.Trace(
				i18n.Tr(
					global.Lang,
					"outputParams.SYSTEMILLEGAL") + err.Error(),
			)
			result.Message = i18n.Tr(global.Lang, "outputParams.SYSTEMILLEGAL")

			return result, err

		}

		if !is {
			for _, err := range valid.Errors {
				beego.Trace(
					i18n.Tr(
						global.Lang,
						"outputParams.DATAPARAMSILLEGAL") + err.Key + ":" + err.Message)
				result.Message = i18n.Tr(
					global.Lang,
					"outputParams.DATAPARAMSILLEGAL") + " " + err.Key + ":" + err.Message

				return result, errors.New(i18n.Tr(global.Lang, "outputParams.DATAPARAMSILLEGAL"))
			}
		}
	}

	//sign check
	if is, _ := beego.AppConfig.Bool("sign.onOff"); is {
		err = sign.ValidSign(data["body"].([]byte), beego.AppConfig.String("sign.secretKey"))
		if err != nil {
			result.Message = err.Error()
			return result, err
		}
	}

	return headerRes, nil
}

/**
 * header参数验证
 * 将header 放入map 返回
 *
 * @meta 	meta  map[string][]string 	header信息 map格式
 */
func HeaderCheck(data map[string]interface{}) (result Result, err error) {
	var h Header
	ffjson.Unmarshal(data["header"].([]byte), &h)

	rid := h.RequestID[0]

	result.CheckRes = nil
	result.Message = ""
	result.RequestID = rid

{'fraction_non_alphanumeric': 0.16166622019823199, 'fraction_numerical': 0.004420037503348513, 'mean_word_length': 3.8392741412832145, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 4, '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': '4698348', 'n_tokens_mistral': 2980, 'n_tokens_neox': 2765, 'n_words': 537}