customHeader.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package dynamicproxy
  2. /*
  3. CustomHeader.go
  4. This script handle parsing and injecting custom headers
  5. into the dpcore routing logic
  6. */
  7. //SplitInboundOutboundHeaders split user defined headers into upstream and downstream headers
  8. //return upstream header and downstream header key-value pairs
  9. //if the header is expected to be deleted, the value will be set to empty string
  10. func (ept *ProxyEndpoint) SplitInboundOutboundHeaders() ([][]string, [][]string) {
  11. if len(ept.UserDefinedHeaders) == 0 {
  12. //Early return if there are no defined headers
  13. return [][]string{}, [][]string{}
  14. }
  15. //Use pre-allocation for faster performance
  16. upstreamHeaders := make([][]string, len(ept.UserDefinedHeaders))
  17. downstreamHeaders := make([][]string, len(ept.UserDefinedHeaders))
  18. upstreamHeaderCounter := 0
  19. downstreamHeaderCounter := 0
  20. //Sort the headers into upstream or downstream
  21. for _, customHeader := range ept.UserDefinedHeaders {
  22. thisHeaderSet := make([]string, 2)
  23. thisHeaderSet[0] = customHeader.Key
  24. thisHeaderSet[1] = customHeader.Value
  25. if customHeader.IsRemove {
  26. //Prevent invalid config
  27. thisHeaderSet[1] = ""
  28. }
  29. //Assign to slice
  30. if customHeader.Direction == HeaderDirection_ZoraxyToUpstream {
  31. upstreamHeaders[upstreamHeaderCounter] = thisHeaderSet
  32. upstreamHeaderCounter++
  33. } else if customHeader.Direction == HeaderDirection_ZoraxyToDownstream {
  34. downstreamHeaders[downstreamHeaderCounter] = thisHeaderSet
  35. downstreamHeaderCounter++
  36. }
  37. }
  38. return upstreamHeaders, downstreamHeaders
  39. }