Apply .gitignore rules
This commit is contained in:
@@ -0,0 +1,218 @@
|
||||
package webServer
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"goutil/jsonUtil"
|
||||
"goutil/logUtilPlus"
|
||||
"goutil/typeUtil"
|
||||
"goutil/zlibUtil"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// ApiContext
|
||||
// @description: Api请求上下文对象
|
||||
type ApiContext struct {
|
||||
// 请求对象
|
||||
request *http.Request
|
||||
|
||||
// 应答写对象
|
||||
responseWriter http.ResponseWriter
|
||||
|
||||
// 请求数据
|
||||
requestBytes []byte
|
||||
|
||||
// 字典形式的请求数据
|
||||
requestDataByMap typeUtil.MapData
|
||||
|
||||
//获取头部信息
|
||||
header *http.Header
|
||||
}
|
||||
|
||||
// GetRequest
|
||||
// @description: 获取请求对象
|
||||
// parameter:
|
||||
// @receiver this: this
|
||||
// return:
|
||||
// @*http.Request: 请求对象
|
||||
func (this *ApiContext) GetRequest() *http.Request {
|
||||
return this.request
|
||||
}
|
||||
|
||||
// GetResponseWriter
|
||||
// @description: 获取应答写对象
|
||||
// parameter:
|
||||
// @receiver this: this
|
||||
// return:
|
||||
// @http.ResponseWriter: 应答写对象
|
||||
func (this *ApiContext) GetResponseWriter() http.ResponseWriter {
|
||||
return this.responseWriter
|
||||
}
|
||||
|
||||
// GetRequestBytes
|
||||
// @description: 获取请求字节数据
|
||||
// parameter:
|
||||
// @receiver this: this
|
||||
// return:
|
||||
// @[]byte: 请求字节数组
|
||||
func (this *ApiContext) GetRequestBytes() []byte {
|
||||
return this.requestBytes
|
||||
}
|
||||
|
||||
// readContent
|
||||
// @description: 读取内容
|
||||
// parameter:
|
||||
// @receiver this: this 请求对象
|
||||
// @isZlib: 是否数据压缩
|
||||
// return:
|
||||
// @content: 二进制格式的内容
|
||||
// @err: 错误对象
|
||||
func (this *ApiContext) readContent(isZlib bool) (content []byte, err error) {
|
||||
var buffer []byte
|
||||
|
||||
defer this.request.Body.Close()
|
||||
if buffer, err = ioutil.ReadAll(this.request.Body); err != nil {
|
||||
logUtilPlus.ErrorLog(fmt.Sprintf("url:%s,读取数据出错,错误信息为:%s", this.request.RequestURI, err))
|
||||
return
|
||||
}
|
||||
|
||||
// 不压缩,则直接返回
|
||||
if isZlib == false {
|
||||
this.requestBytes = buffer
|
||||
|
||||
return buffer, err
|
||||
}
|
||||
|
||||
// 解压数据
|
||||
if content, err = zlibUtil.Decompress(buffer); err != nil {
|
||||
logUtilPlus.ErrorLog(fmt.Sprintf("url:%s,解压缩数据出错,错误信息为:%s", this.request.RequestURI, err))
|
||||
return
|
||||
}
|
||||
|
||||
this.requestBytes = content
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Unmarshal
|
||||
// @description: 反序列化
|
||||
// parameter:
|
||||
// @receiver this: this
|
||||
// @obj: 反序列化结果数据
|
||||
// return:
|
||||
// @error: 反序列化错误数据
|
||||
func (this *ApiContext) Unmarshal(obj interface{}) error {
|
||||
contentData := this.GetRequestBytes()
|
||||
|
||||
var errMsg error
|
||||
|
||||
// 反序列化
|
||||
if obj, errMsg = json.Marshal(contentData); errMsg != nil {
|
||||
logUtilPlus.ErrorLog(fmt.Sprintf("反序列化%s出错,错误信息为:%s", string(contentData), errMsg.Error()))
|
||||
return errMsg
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// RequestDataByMap
|
||||
// @description: 获取请求的map格式数据
|
||||
// parameter:
|
||||
// @receiver this: this
|
||||
// return:
|
||||
// @typeUtil.MapData: map数据
|
||||
// @error: 错误信息
|
||||
func (this *ApiContext) RequestDataByMap() (typeUtil.MapData, error) {
|
||||
if this.requestDataByMap != nil {
|
||||
return this.requestDataByMap, nil
|
||||
}
|
||||
|
||||
var data typeUtil.MapData
|
||||
if err := this.Unmarshal(&data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
this.requestDataByMap = data
|
||||
|
||||
return this.requestDataByMap, nil
|
||||
}
|
||||
|
||||
// RequestDataBySlice
|
||||
// @description: 获取请求的slice格式数据
|
||||
// parameter:
|
||||
// @receiver this: this
|
||||
// return:
|
||||
// @[]interface{}: 返回的数组数据
|
||||
func (this *ApiContext) RequestDataBySlice() []interface{} {
|
||||
result := make([]interface{}, 0, 8)
|
||||
for _, value := range this.requestDataByMap {
|
||||
result = append(result, value)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// RequestDataBySlice2
|
||||
// @description: 获取请求的slice格式数据
|
||||
// parameter:
|
||||
// @receiver this: this
|
||||
// return:
|
||||
// @[]interface{}: 返回的数组数据
|
||||
// @error:
|
||||
func (this *ApiContext) RequestDataBySlice2() (interface{}, error) {
|
||||
contentData := this.GetRequestBytes()
|
||||
|
||||
// 反序列化
|
||||
var result interface{}
|
||||
var errMsg error
|
||||
if result, errMsg = jsonUtil.UnMarshalWithNumberType(string(contentData)); errMsg != nil {
|
||||
logUtilPlus.ErrorLog(fmt.Sprintf("用[]interface{}反序列化%s出错,错误信息为:%s", string(contentData), errMsg.Error()))
|
||||
return nil, errMsg
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// RequestDataBySlice2ByJson
|
||||
// @description: 获取请求的slice格式数据
|
||||
// parameter:
|
||||
// @receiver this: this
|
||||
// return:
|
||||
// @[]interface{}: 返回的数组数据
|
||||
// @error:
|
||||
func (this *ApiContext) RequestDataBySlice2ByJson() ([]interface{}, error) {
|
||||
contentData := this.GetRequestBytes()
|
||||
|
||||
// 反序列化
|
||||
result := make([]interface{}, 0, 8)
|
||||
if errMsg := json.Unmarshal(contentData, &result); errMsg != nil {
|
||||
logUtilPlus.ErrorLog(fmt.Sprintf("用[]interface{}反序列化%s出错,错误信息为:%s", string(contentData), errMsg.Error()))
|
||||
return nil, errMsg
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// NewApiContext
|
||||
// @description: 新建API上下文对象
|
||||
// parameter:
|
||||
// @_request: 请求对象
|
||||
// @_responseWriter: 应答写对象
|
||||
// @isZlib: 数据是否压缩
|
||||
// return:
|
||||
// @*ApiContext: 上下文
|
||||
// @error: 错误信息
|
||||
func NewApiContext(_request *http.Request, _responseWriter http.ResponseWriter, isZlib bool) (*ApiContext, error) {
|
||||
context := &ApiContext{
|
||||
request: _request,
|
||||
header: &_request.Header,
|
||||
responseWriter: _responseWriter,
|
||||
}
|
||||
|
||||
// 读取数据
|
||||
_, errMsg := context.readContent(isZlib)
|
||||
if errMsg != nil {
|
||||
return nil, errMsg
|
||||
}
|
||||
|
||||
return context, nil
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package securityUtil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"errors"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrInvalidBlockSize indicates hash blocksize <= 0.
|
||||
ErrInvalidBlockSize = errors.New("invalid blocksize")
|
||||
|
||||
// ErrInvalidPKCS7Data indicates bad input to PKCS7 pad or unpad.
|
||||
ErrInvalidPKCS7Data = errors.New("invalid PKCS7 data (empty or not padded)")
|
||||
|
||||
// ErrInvalidPKCS7Padding indicates PKCS7 unpad fails to bad input.
|
||||
ErrInvalidPKCS7Padding = errors.New("invalid padding on input")
|
||||
)
|
||||
|
||||
// pkcs7Pad right-pads the given byte slice with 1 to n bytes, where
|
||||
// n is the block size. The size of the result is x times n, where x
|
||||
// is at least 1.
|
||||
func pkcs7Pad(b []byte, blocksize int) ([]byte, error) {
|
||||
if b == nil || len(b) == 0 {
|
||||
return nil, ErrInvalidPKCS7Data
|
||||
}
|
||||
if blocksize <= 0 {
|
||||
return nil, ErrInvalidBlockSize
|
||||
}
|
||||
n := blocksize - (len(b) % blocksize)
|
||||
pb := make([]byte, len(b)+n)
|
||||
copy(pb, b)
|
||||
copy(pb[len(b):], bytes.Repeat([]byte{byte(n)}, n))
|
||||
|
||||
return pb, nil
|
||||
}
|
||||
|
||||
// pkcs7Unpad validates and unpads data from the given bytes slice.
|
||||
// The returned value will be 1 to n bytes smaller depending on the
|
||||
// amount of padding, where n is the block size.
|
||||
func pkcs7Unpad(b []byte, blocksize int) ([]byte, error) {
|
||||
if b == nil || len(b) == 0 {
|
||||
return nil, ErrInvalidPKCS7Data
|
||||
}
|
||||
if blocksize <= 0 {
|
||||
return nil, ErrInvalidBlockSize
|
||||
}
|
||||
if len(b)%blocksize != 0 {
|
||||
return nil, ErrInvalidPKCS7Padding
|
||||
}
|
||||
|
||||
c := b[len(b)-1]
|
||||
n := int(c)
|
||||
if n == 0 || n > len(b) {
|
||||
return nil, ErrInvalidPKCS7Padding
|
||||
}
|
||||
|
||||
for i := 0; i < n; i++ {
|
||||
if b[len(b)-n+i] != c {
|
||||
return nil, ErrInvalidPKCS7Padding
|
||||
}
|
||||
}
|
||||
|
||||
return b[:len(b)-n], nil
|
||||
}
|
||||
|
||||
func AESEncrypt_CBC_Pkcs7(src []byte, key []byte) ([]byte, error) {
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
src, err = pkcs7Pad(src, block.BlockSize())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
blockmode := cipher.NewCBCEncrypter(block, key)
|
||||
blockmode.CryptBlocks(src, src)
|
||||
|
||||
return src, nil
|
||||
}
|
||||
|
||||
func AESDecrypt_CBC_Pkcs7(src []byte, key []byte) ([]byte, error) {
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
blockmode := cipher.NewCBCDecrypter(block, key)
|
||||
blockmode.CryptBlocks(src, src)
|
||||
src, err = pkcs7Unpad(src, block.BlockSize())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return src, nil
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package httpServer
|
||||
|
||||
import (
|
||||
"common/webServer"
|
||||
"net/http"
|
||||
|
||||
"common/resultStatus"
|
||||
)
|
||||
|
||||
// 处理函数
|
||||
type HandleFunc func(context *ApiContext) *webServer.ResponseObject
|
||||
|
||||
// ApiHandler
|
||||
// @description: API处理结构
|
||||
type ApiHandler struct {
|
||||
// API完整路径名称
|
||||
apiFullName string
|
||||
|
||||
// 方法定义
|
||||
handleFun HandleFunc
|
||||
|
||||
// 方法参数名称集合
|
||||
funcParamNames []string
|
||||
}
|
||||
|
||||
// ApiFullName
|
||||
// @description: API完整路径名称
|
||||
// parameter:
|
||||
// @receiver this: this
|
||||
// return:
|
||||
// @string:
|
||||
func (this *ApiHandler) ApiFullName() string {
|
||||
return this.apiFullName
|
||||
}
|
||||
|
||||
// HandleFun
|
||||
// @description: 方法定义
|
||||
// parameter:
|
||||
// @receiver this: this
|
||||
// return:
|
||||
// @HandleFunc: 方法
|
||||
func (this *ApiHandler) HandleFun() HandleFunc {
|
||||
return this.handleFun
|
||||
}
|
||||
|
||||
// FuncParamNames
|
||||
// @description: 方法参数名称集合
|
||||
// parameter:
|
||||
// @receiver this: this
|
||||
// return:
|
||||
// @[]string: 方法参数名称集合
|
||||
func (this *ApiHandler) FuncParamNames() []string {
|
||||
return this.funcParamNames
|
||||
}
|
||||
|
||||
// CheckParam
|
||||
// @description: 检测参数数量
|
||||
// parameter:
|
||||
// @receiver this: this
|
||||
// @r:
|
||||
// return:
|
||||
// @resultStatus.ResultStatus: 状态码数据
|
||||
func (this *ApiHandler) CheckParam(r *http.Request) resultStatus.ResultStatus {
|
||||
for _, name := range this.funcParamNames {
|
||||
if r.Form[name] == nil || len(r.Form[name]) == 0 {
|
||||
return resultStatus.APIParamError
|
||||
}
|
||||
}
|
||||
|
||||
return resultStatus.Success
|
||||
}
|
||||
|
||||
// newApiHandler
|
||||
// @description: 创建新的请求方法对象
|
||||
// parameter:
|
||||
// @_apiFullName: API完整路径名称
|
||||
// @_funcDefinition: 方法定义
|
||||
// @_funcParamNames: 方法参数名称集合
|
||||
// return:
|
||||
// @*ApiHandler: 请求方法对象
|
||||
func newApiHandler(_apiFullName string, _funcDefinition HandleFunc, _funcParamNames ...string) *ApiHandler {
|
||||
return &ApiHandler{
|
||||
apiFullName: _apiFullName,
|
||||
handleFun: _funcDefinition,
|
||||
funcParamNames: _funcParamNames,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user