Skip to content

Commit

Permalink
[option] Requires member name to be url friendly
Browse files Browse the repository at this point in the history
Member name will be used in such APIs as a path parameter.
  • Loading branch information
zhiyanliu authored and Jack47 committed Dec 26, 2017
1 parent 0902b0e commit a85d230
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
39 changes: 39 additions & 0 deletions src/common/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package common

import (
"fmt"
"regexp"
"strconv"
)

Expand Down Expand Up @@ -144,3 +145,41 @@ func (i *Uint32RangeValue) String() string {
return strconv.FormatUint(uint64(*i.v), 10)
}
}

////

type StringRegexValue struct {
s *string
re *regexp.Regexp
}

func NewStringRegexValue(val string, p *string, r *regexp.Regexp) *StringRegexValue {
if p == nil {
p = new(string)
}
*p = val

return &StringRegexValue{
s: p,
re: r,
}
}

func (s *StringRegexValue) Set(val string) error {
if s.re != nil && !s.re.Match([]byte(val)) {
return fmt.Errorf("invalid pattern, need to match %v", s.re)
}

*s.s = val
return nil
}

func (s *StringRegexValue) Get() interface{} { return *s.s }

func (s *StringRegexValue) String() string {
if s.s == nil {
return ""
}

return *s.s
}
4 changes: 4 additions & 0 deletions src/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package common
import (
"bytes"
"fmt"
"regexp"
"strconv"
"strings"
)
Expand Down Expand Up @@ -214,3 +215,6 @@ func NextNumberPowerOf2(v uint64) uint64 {
v++
return v
}

// safe characters for friendly url, rfc3986 section 2.3
var URL_FRIENDLY_CHARACTERS_REGEX = regexp.MustCompile(`^[A-Za-z0-9\-_\.~]+$`)
7 changes: 2 additions & 5 deletions src/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ import (
plugins_gw "plugins"
)

// safe characters for friendly url, rfc3986 section 2.3
var PIPELINE_PLUGIN_NAME_REGEX = regexp.MustCompile(`^[A-Za-z0-9\-_\.~]+$`)

type PluginAdded func(newPlugin *Plugin)
type PluginDeleted func(deletedPlugin *Plugin)
type PluginUpdated func(updatedPlugin *Plugin, instanceDismissed bool, instanceGen uint64)
Expand Down Expand Up @@ -131,7 +128,7 @@ func (m *Model) AddPlugin(typ string, conf plugins.Config,

pluginName := conf.PluginName()

if !PIPELINE_PLUGIN_NAME_REGEX.Match([]byte(pluginName)) {
if !common.URL_FRIENDLY_CHARACTERS_REGEX.Match([]byte(pluginName)) {
return nil, fmt.Errorf("plugin name %s is invalid", pluginName)
}

Expand Down Expand Up @@ -461,7 +458,7 @@ func (m *Model) DeletePluginUpdatedCallback(name string) {
func (m *Model) AddPipeline(typ string, conf pipelines_gw.Config) (*Pipeline, error) {
pipelineName := conf.PipelineName()

if !PIPELINE_PLUGIN_NAME_REGEX.Match([]byte(pipelineName)) {
if !common.URL_FRIENDLY_CHARACTERS_REGEX.Match([]byte(pipelineName)) {
return nil, fmt.Errorf("pipeline name %s is invalid", pipelineName)
}

Expand Down
4 changes: 3 additions & 1 deletion src/option/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ func init() {
clusterHost := flag.String("cluster_host", "localhost", "specify cluster listen host")
clusterGroup := flag.String("group", "default", "specify cluster group name")
memberMode := flag.String("mode", "read", "specify member mode (read or write)")
memberName := flag.String("name", hostName, "specify member name")
memberName := new(string)
flag.Var(common.NewStringRegexValue(hostName, memberName, common.URL_FRIENDLY_CHARACTERS_REGEX), "name",
"specify member name")
peers := flag.String("peers", "", "specify address list of peer members (separated by comma)")
opLogMaxSeqGapToPull := new(uint16)
flag.Var(common.NewUint16Value(5, opLogMaxSeqGapToPull), "oplog_max_seq_gap_to_pull",
Expand Down

0 comments on commit a85d230

Please sign in to comment.