github.com/Kolosok86/http@v0.1.2/textproto/header_order.go (about)

     1  package textproto
     2  
     3  import (
     4  	"strings"
     5  	"sync"
     6  )
     7  
     8  // HeaderOrder create header order
     9  type HeaderOrder struct {
    10  	mu    sync.RWMutex
    11  	Order []string
    12  }
    13  
    14  func (h *HeaderOrder) Add(key string) {
    15  	h.mu.Lock()
    16  	defer h.mu.Unlock()
    17  
    18  	formatted := h.toLower(key)
    19  
    20  	if h.FindIndex(formatted) != -1 {
    21  		return
    22  	}
    23  
    24  	h.Order = append(h.Order, formatted)
    25  }
    26  
    27  func (h *HeaderOrder) Del(key string) {
    28  	h.mu.Lock()
    29  	defer h.mu.Unlock()
    30  
    31  	formatted := h.toLower(key)
    32  	i := h.FindIndex(formatted)
    33  
    34  	if i == -1 {
    35  		return
    36  	}
    37  
    38  	h.Order = append(h.Order[:i], h.Order[i+1:]...)
    39  }
    40  
    41  func (h *HeaderOrder) FindIndex(key string) int {
    42  	for i, value := range h.Order {
    43  		if value == key {
    44  			return i
    45  		}
    46  	}
    47  
    48  	return -1
    49  }
    50  
    51  func (h *HeaderOrder) toLower(key string) string {
    52  	return strings.ToLower(key)
    53  }