endpoints.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package dynamicproxy
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "strings"
  6. )
  7. /*
  8. Endpoint Functions
  9. */
  10. // Get virtual directory handler from given URI
  11. func (ep *ProxyEndpoint) GetVirtualDirectoryHandlerFromRequestURI(requestURI string) *VirtualDirectoryEndpoint {
  12. for _, vdir := range ep.VirtualDirectories {
  13. if strings.HasPrefix(requestURI, vdir.MatchingPath) {
  14. return vdir
  15. }
  16. }
  17. return nil
  18. }
  19. // Get virtual directory handler by matching path (exact match required)
  20. func (ep *ProxyEndpoint) GetVirtualDirectoryRuleByMatchingPath(matchingPath string) *VirtualDirectoryEndpoint {
  21. for _, vdir := range ep.VirtualDirectories {
  22. if vdir.MatchingPath == matchingPath {
  23. return vdir
  24. }
  25. }
  26. return nil
  27. }
  28. // Delete a vdir rule by its matching path
  29. func (ep *ProxyEndpoint) RemoveVirtualDirectoryRuleByMatchingPath(matchingPath string) error {
  30. entryFound := false
  31. newVirtualDirectoryList := []*VirtualDirectoryEndpoint{}
  32. for _, vdir := range ep.VirtualDirectories {
  33. if vdir.MatchingPath == matchingPath {
  34. entryFound = true
  35. } else {
  36. newVirtualDirectoryList = append(newVirtualDirectoryList, vdir)
  37. }
  38. }
  39. if entryFound {
  40. //Update the list of vdirs
  41. ep.VirtualDirectories = newVirtualDirectoryList
  42. return nil
  43. }
  44. return errors.New("target virtual directory routing rule not found")
  45. }
  46. // Delete a vdir rule by its matching path
  47. func (ep *ProxyEndpoint) AddVirtualDirectoryRule(vdir *VirtualDirectoryEndpoint) (*ProxyEndpoint, error) {
  48. //Check for matching path duplicate
  49. if ep.GetVirtualDirectoryRuleByMatchingPath(vdir.MatchingPath) != nil {
  50. return nil, errors.New("rule with same matching path already exists")
  51. }
  52. //Append it to the list of virtual directory
  53. ep.VirtualDirectories = append(ep.VirtualDirectories, vdir)
  54. //Prepare to replace the current routing rule
  55. parentRouter := ep.parent
  56. readyRoutingRule, err := parentRouter.PrepareProxyRoute(ep)
  57. if err != nil {
  58. return nil, err
  59. }
  60. if ep.ProxyType == ProxyType_Root {
  61. parentRouter.Root = readyRoutingRule
  62. } else if ep.ProxyType == ProxyType_Host {
  63. ep.Remove()
  64. parentRouter.AddProxyRouteToRuntime(readyRoutingRule)
  65. } else {
  66. return nil, errors.New("unsupported proxy type")
  67. }
  68. return readyRoutingRule, nil
  69. }
  70. // Create a deep clone object of the proxy endpoint
  71. // Note the returned object is not activated. Call to prepare function before pushing into runtime
  72. func (ep *ProxyEndpoint) Clone() *ProxyEndpoint {
  73. clonedProxyEndpoint := ProxyEndpoint{}
  74. js, _ := json.Marshal(ep)
  75. json.Unmarshal(js, &clonedProxyEndpoint)
  76. return &clonedProxyEndpoint
  77. }