github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/transports/fast/header.go (about)

     1  /*
     2   * Copyright 2023 Wang Min Xiang
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   * 	http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   */
    17  
    18  package fast
    19  
    20  import (
    21  	"github.com/aacfactory/fns/commons/bytex"
    22  	"github.com/valyala/fasthttp"
    23  )
    24  
    25  type RequestHeader struct {
    26  	ctx *fasthttp.RequestCtx
    27  }
    28  
    29  func (h RequestHeader) Add(key []byte, value []byte) {
    30  	h.ctx.Request.Header.AddBytesKV(key, value)
    31  }
    32  
    33  func (h RequestHeader) Set(key []byte, value []byte) {
    34  	h.ctx.Request.Header.SetBytesKV(key, value)
    35  }
    36  
    37  func (h RequestHeader) Get(key []byte) []byte {
    38  	return h.ctx.Request.Header.PeekBytes(key)
    39  }
    40  
    41  func (h RequestHeader) Del(key []byte) {
    42  	h.ctx.Request.Header.DelBytes(key)
    43  }
    44  
    45  func (h RequestHeader) Values(key []byte) [][]byte {
    46  	return h.ctx.Request.Header.PeekAll(bytex.ToString(key))
    47  }
    48  
    49  func (h RequestHeader) Foreach(fn func(key []byte, values [][]byte)) {
    50  	if fn == nil {
    51  		return
    52  	}
    53  	keys := make([][]byte, 0)
    54  	h.ctx.Request.Header.VisitAll(func(key, value []byte) {
    55  		keys = append(keys, key)
    56  	})
    57  	for _, key := range keys {
    58  		fn(key, h.Values(key))
    59  	}
    60  }
    61  
    62  func (h RequestHeader) Len() int {
    63  	return h.ctx.Request.Header.Len()
    64  }
    65  
    66  func (h RequestHeader) Reset() {
    67  	h.ctx.Request.Header.Reset()
    68  }
    69  
    70  type ResponseHeader struct {
    71  	*fasthttp.ResponseHeader
    72  }
    73  
    74  func (h *ResponseHeader) Add(key []byte, value []byte) {
    75  	h.ResponseHeader.AddBytesKV(key, value)
    76  }
    77  
    78  func (h *ResponseHeader) Set(key []byte, value []byte) {
    79  	h.ResponseHeader.SetBytesKV(key, value)
    80  }
    81  
    82  func (h *ResponseHeader) Get(key []byte) []byte {
    83  	return h.ResponseHeader.PeekBytes(key)
    84  }
    85  
    86  func (h *ResponseHeader) Del(key []byte) {
    87  	h.ResponseHeader.DelBytes(key)
    88  }
    89  
    90  func (h *ResponseHeader) Values(key []byte) [][]byte {
    91  	return h.ResponseHeader.PeekAll(bytex.ToString(key))
    92  }
    93  
    94  func (h *ResponseHeader) Foreach(fn func(key []byte, values [][]byte)) {
    95  	if fn == nil {
    96  		return
    97  	}
    98  	keys := make([][]byte, 0)
    99  	h.VisitAll(func(key, value []byte) {
   100  		keys = append(keys, key)
   101  	})
   102  	for _, key := range keys {
   103  		fn(key, h.Values(key))
   104  	}
   105  }
   106  
   107  func (h *ResponseHeader) Len() int {
   108  	return h.ResponseHeader.Len()
   109  }
   110  
   111  func (h *ResponseHeader) Reset() {
   112  	h.ResponseHeader.Reset()
   113  }