mod_body_process

package
v1.8.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 12, 2026 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ModBodyProcess               = "mod_body_process"
	BodyProcessResponseConfigKey = "mod_body_process.response_config"
)

Variables

This section is empty.

Functions

func BodyProcessConfigCheck

func BodyProcessConfigCheck(config *BodyProcessConfig) error

func GetAuditData

func GetAuditData(ev Event) ([]byte, error)

func GetEventTokens

func GetEventTokens(ev Event) int64

func GetHTTPClient

func GetHTTPClient() *http.Client

func ProductRuleConfLoad

func ProductRuleConfLoad(filename string) (productRuleConf, error)

func SetAuditData

func SetAuditData(ev Event, data []byte) error

func SetHTTPClient

func SetHTTPClient(client *http.Client)

Types

type BPError added in v1.8.1

type BPError struct {
	Err error
}

func (*BPError) Error added in v1.8.1

func (e *BPError) Error() string

func (*BPError) Unwrap added in v1.8.1

func (e *BPError) Unwrap() error

type BodyProcessConfig

type BodyProcessConfig struct {
	Dec  string
	Enc  string
	Proc []ProcConf // processing steps
}

type BodyProcessor

type BodyProcessor struct {
	// contains filtered or unexported fields
}

BodyProcessor 扩展中断支持

func NewBodyProcessor

func NewBodyProcessor(source io.ReadCloser) *BodyProcessor

NewBodyProcessor 创建处理器

func (*BodyProcessor) AddProcessor

func (bp *BodyProcessor) AddProcessor(p EventProcessor)

func (*BodyProcessor) Close

func (bp *BodyProcessor) Close() error

Close 实现io.Closer接口

func (*BodyProcessor) CreateEventDecoder

func (bp *BodyProcessor) CreateEventDecoder(fac EventDecoderFac)

func (*BodyProcessor) CreateEventEncoder

func (bp *BodyProcessor) CreateEventEncoder(fac EventEncoderFac)

func (*BodyProcessor) FillBuffer

func (bp *BodyProcessor) FillBuffer() error

FillBuffer 公开的缓冲区填充方法 安全地从源读取并处理一个数据块

func (*BodyProcessor) GetSource

func (bp *BodyProcessor) GetSource() io.ReadCloser

func (*BodyProcessor) OnReject

func (bp *BodyProcessor) OnReject(fn func(error, *BodyProcessor))

注册中断回调

func (*BodyProcessor) Read

func (bp *BodyProcessor) Read(p []byte) (n int, err error)

Read 实现io.Reader接口(支持中断)

func (*BodyProcessor) RejectionResponse

func (bp *BodyProcessor) RejectionResponse() *RejectionError

RejectionResponse 获取中断响应(如在反向代理中使用)

type ConfModBodyProcess

type ConfModBodyProcess struct {
	Basic struct {
		ProductRulePath string
	}

	Log struct {
		OpenDebug bool
	}
}

func ConfLoad

func ConfLoad(filePath string, confRoot string) (*ConfModBodyProcess, error)

func (*ConfModBodyProcess) Check

func (cfg *ConfModBodyProcess) Check(confRoot string) error

type ContentAudit

type ContentAudit struct {
	// contains filtered or unexported fields
}

func NewContentAudit

func NewContentAudit(urlStr string, replace bool) (*ContentAudit, error)

func (*ContentAudit) Process

func (caf *ContentAudit) Process(evs []Event) ([]Event, error)

type ContentTypeDecoder

type ContentTypeDecoder struct {
	// contains filtered or unexported fields
}

func (*ContentTypeDecoder) Decode

func (ctd *ContentTypeDecoder) Decode() ([]Event, error)

type Event

type Event interface {
	// GetType() string
	// GetData() []byte
	ToBytes() []byte // 转换为字节数组
}

type EventDecoder

type EventDecoder interface {
	// return:
	//  events, nil - len(events) > 0, success
	//  events, nil - len(events) = 0, 没有更多数据, eof
	//  nil, error - 发生错误
	Decode() ([]Event, error)
}

func NewContentTypeDecoder

func NewContentTypeDecoder(source io.Reader, contentType string) (EventDecoder, error)

func NewJsonDecoder

func NewJsonDecoder(source io.Reader) (EventDecoder, error)

func NewLineDecoder

func NewLineDecoder(source io.Reader) (EventDecoder, error)

func NewSSEEventDecoder

func NewSSEEventDecoder(source io.Reader) (EventDecoder, error)

type EventDecoderFac

type EventDecoderFac func(source io.Reader) (EventDecoder, error)

type EventDecoderFacWithReq

type EventDecoderFacWithReq func(source io.Reader, req bfe_basic.Request) (EventDecoder, error)

type EventEncoder

type EventEncoder interface {
	Encode(events []Event) (int, error)
}

func NewGeneralEncoder

func NewGeneralEncoder(dest io.Writer) (EventEncoder, error)

type EventEncoderFac

type EventEncoderFac func(dest io.Writer) (EventEncoder, error)

type EventProcessor

type EventProcessor interface {
	Process(events []Event) ([]Event, error)
}

type EventProcessorFunc

type EventProcessorFunc func([]Event) ([]Event, error)

ProcessorFunc 简化处理器实现

func NewCalcCompletionQuota

func NewCalcCompletionQuota(req *bfe_basic.Request) EventProcessorFunc

func (EventProcessorFunc) Process

func (f EventProcessorFunc) Process(events []Event) ([]Event, error)

type GeneralEncoder

type GeneralEncoder struct {
	// contains filtered or unexported fields
}

func (*GeneralEncoder) Encode

func (enc *GeneralEncoder) Encode(events []Event) (int, error)

type JsonDecoder

type JsonDecoder struct {
	// contains filtered or unexported fields
}

func (*JsonDecoder) Decode

func (dec *JsonDecoder) Decode() ([]Event, error)

type LineDecoder

type LineDecoder struct {
	// contains filtered or unexported fields
}

func (*LineDecoder) Decode

func (dec *LineDecoder) Decode() ([]Event, error)

type ModuleBodyProcess

type ModuleBodyProcess struct {
	// contains filtered or unexported fields
}

func NewModuleBodyProcess

func NewModuleBodyProcess() *ModuleBodyProcess

func (*ModuleBodyProcess) DoRequestProcess

func (m *ModuleBodyProcess) DoRequestProcess(req *bfe_basic.Request, conf *BodyProcessConfig) *BodyProcessor

func (*ModuleBodyProcess) DoResponseProcess

func (m *ModuleBodyProcess) DoResponseProcess(req *bfe_basic.Request, res *bfe_http.Response, conf *BodyProcessConfig) *BodyProcessor

func (*ModuleBodyProcess) Init

func (*ModuleBodyProcess) Name

func (m *ModuleBodyProcess) Name() string

type ModuleBodyProcessState

type ModuleBodyProcessState struct {
	ReqTotal   *metrics.Counter
	ReqProcess *metrics.Counter
	ResProcess *metrics.Counter
}

type ProcConf

type ProcConf struct {
	Name   string
	Params []string
}

type ProcessRuleTable

type ProcessRuleTable struct {
	// contains filtered or unexported fields
}

func NewTokenRuleTable

func NewTokenRuleTable() *ProcessRuleTable

func (*ProcessRuleTable) Search

func (t *ProcessRuleTable) Search(product string) (processRuleList, bool)

func (*ProcessRuleTable) Update

func (t *ProcessRuleTable) Update(conf productRuleConf)

type ProductRules

type ProductRules map[string]processRuleList

type ProductRulesFile

type ProductRulesFile map[string]processRuleFileList

type RawEvent

type RawEvent []byte

func (*RawEvent) ToBytes

func (e *RawEvent) ToBytes() []byte

type RejectionError

type RejectionError struct {
	Message    string
	StatusCode int
}

RejectionError 自定义错误类型

func (*RejectionError) Error

func (e *RejectionError) Error() string

type SSEEvent

type SSEEvent struct {
	ID    string
	Event string
	Data  []byte
	Retry int
	// contains filtered or unexported fields
}
func (m *ModuleBodyProcess) DoResponseProcess(req *bfe_basic.Request, res *bfe_http.Response, conf *BodyProcessConfig) *BodyProcessor {
	if conf == nil {
		return nil // 没有配置,直接返回
	}

	m.state.ResProcess.Inc(1)

	bp := NewBodyProcessor(res.Body)
	switch conf.Dec {
	case "sse":  // sse is not available for request body
		bp.CreateEventDecoder(NewSSEEventDecoder)
	case "line":
		bp.CreateEventDecoder(NewLineDecoder)
	case "json":
		bp.CreateEventDecoder(NewJsonDecoder)
	default:
		contentType := res.Header.Get("Content-Type")
		bp.CreateEventDecoder(func(source io.Reader)(EventDecoder, error) {
			return NewContentTypeDecoder(source, contentType)} ) // 使用ContentTypeDecoder根据Content-Type自动选择解码器
		// bp.CreateEventDecoder(NewJsonDecoder) // 默认使用ndJson解码
	}

	bp.CreateEventEncoder(NewGeneralEncoder)

	// 缺省添加streamcompletion处理器
	p := NewCalcCompletionQuota(req)
	if p != nil {
		bp.AddProcessor(p)
	}

	for _, proc := range conf.Proc {
		switch proc.Name {
		case "textfilter":
			caf, _ := NewContentAudit(proc.Params[0], true)
			bp.AddProcessor(caf)
		}
	}

	res.Body = bp
	res.ContentLength = -1 // 设置为-1表示不确定长度
	res.Header.Del("Content-Length")
	return bp
}

SSEEvent 表示一个SSE事件

func (*SSEEvent) ToBytes

func (e *SSEEvent) ToBytes() []byte

ToBytes 将事件转换为SSE格式

type SSEEventDecoder

type SSEEventDecoder struct {
	// contains filtered or unexported fields
}

func (*SSEEventDecoder) Decode

func (dec *SSEEventDecoder) Decode() ([]Event, error)

type TextFilterContactItem

type TextFilterContactItem struct {
	ContactType   string
	ContactString string
	Position      string
}

type TextFilterDetailItem

type TextFilterDetailItem struct {
	RiskLevel string
	RiskCode  string
	Position  string
	Text      string
}

type TextFilterResult

type TextFilterResult struct {
	Code           int32
	Message        string
	RequestId      string
	RiskLevel      string
	RiskCode       string
	SentimentScore float32
	ResultText     string

	Details  []TextFilterDetailItem
	Contacts []TextFilterContactItem
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL