1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2026-01-03 15:55:20 -05:00

Some code improvements

* Rewrite empty string checks more idiomatically.
* Change strings.ToLower comparisons to strings.EqualFold.
* Rewrite switch statement with only one case as if.
This commit is contained in:
Kirill Motkov
2019-06-14 16:47:28 +03:00
parent 4c93d36d49
commit 0401a91ef4
26 changed files with 38 additions and 39 deletions

View File

@@ -120,11 +120,11 @@ Start:
}
defaultPort := net.Port(80)
if strings.ToLower(request.URL.Scheme) == "https" {
if strings.EqualFold(request.URL.Scheme, "https") {
defaultPort = net.Port(443)
}
host := request.Host
if len(host) == 0 {
if host == "" {
host = request.URL.Host
}
dest, err := http_proto.ParseHost(host, defaultPort)
@@ -137,7 +137,7 @@ Start:
Status: log.AccessAccepted,
})
if strings.ToUpper(request.Method) == "CONNECT" {
if strings.EqualFold(request.Method, "CONNECT") {
return s.handleConnect(ctx, request, reader, conn, dest, dispatcher)
}
@@ -211,7 +211,7 @@ func (s *Server) handleConnect(ctx context.Context, request *http.Request, reade
var errWaitAnother = newError("keep alive")
func (s *Server) handlePlainHTTP(ctx context.Context, request *http.Request, writer io.Writer, dest net.Destination, dispatcher routing.Dispatcher) error {
if !s.config.AllowTransparent && len(request.URL.Host) <= 0 {
if !s.config.AllowTransparent && request.URL.Host == "" {
// RFC 2068 (HTTP/1.1) requires URL to be absolute URL in HTTP proxy.
response := &http.Response{
Status: "Bad Request",
@@ -235,7 +235,7 @@ func (s *Server) handlePlainHTTP(ctx context.Context, request *http.Request, wri
http_proto.RemoveHopByHopHeaders(request.Header)
// Prevent UA from being set to golang's default ones
if len(request.Header.Get("User-Agent")) == 0 {
if request.Header.Get("User-Agent") == "" {
request.Header.Set("User-Agent", "")
}

View File

@@ -167,7 +167,7 @@ func (h *Handler) AddUser(ctx context.Context, user *protocol.MemoryUser) error
}
func (h *Handler) RemoveUser(ctx context.Context, email string) error {
if len(email) == 0 {
if email == "" {
return newError("Email must not be empty.")
}
if !h.usersByEmail.Remove(email) {

View File

@@ -149,7 +149,7 @@ func (v *TimedUserValidator) Remove(email string) bool {
email = strings.ToLower(email)
idx := -1
for i, u := range v.users {
if strings.ToLower(u.user.Email) == email {
if strings.EqualFold(u.user.Email, email) {
idx = i
break
}