Browse code

Remove Trailing Whitespace in User-Agent

After removed, the User-Agent shows in log like this:

[debug] http.go:160 https://index.docker.io/v1/repositories/busybox/images --
HEADERS: map[User-Agent:[docker/0.11.1-dev go/go1.2.2 git-commit/8887e00-dirty kernel/3.14.3-n1 os/linux arch/amd64]]

The code also moved all validation work into validVersion,
to keep the main logic as clean.

Docker-DCO-1.1-Signed-off-by: Derek <crq@kernel.org> (github: crquan)

Derek authored on 2014/05/17 08:46:22
Showing 1 changed files
... ...
@@ -1,7 +1,6 @@
1 1
 package utils
2 2
 
3 3
 import (
4
-	"bytes"
5 4
 	"io"
6 5
 	"net/http"
7 6
 	"strings"
... ...
@@ -15,11 +14,13 @@ type VersionInfo interface {
15 15
 }
16 16
 
17 17
 func validVersion(version VersionInfo) bool {
18
-	stopChars := " \t\r\n/"
19
-	if strings.ContainsAny(version.Name(), stopChars) {
18
+	const stopChars = " \t\r\n/"
19
+	name := version.Name()
20
+	vers := version.Version()
21
+	if len(name) == 0 || strings.ContainsAny(name, stopChars) {
20 22
 		return false
21 23
 	}
22
-	if strings.ContainsAny(version.Version(), stopChars) {
24
+	if len(vers) == 0 || strings.ContainsAny(vers, stopChars) {
23 25
 		return false
24 26
 	}
25 27
 	return true
... ...
@@ -36,27 +37,18 @@ func appendVersions(base string, versions ...VersionInfo) string {
36 36
 		return base
37 37
 	}
38 38
 
39
-	var buf bytes.Buffer
39
+	verstrs := make([]string, 0, 1+len(versions))
40 40
 	if len(base) > 0 {
41
-		buf.Write([]byte(base))
41
+		verstrs = append(verstrs, base)
42 42
 	}
43 43
 
44 44
 	for _, v := range versions {
45
-		name := []byte(v.Name())
46
-		version := []byte(v.Version())
47
-
48
-		if len(name) == 0 || len(version) == 0 {
49
-			continue
50
-		}
51 45
 		if !validVersion(v) {
52 46
 			continue
53 47
 		}
54
-		buf.Write([]byte(v.Name()))
55
-		buf.Write([]byte("/"))
56
-		buf.Write([]byte(v.Version()))
57
-		buf.Write([]byte(" "))
48
+		verstrs = append(verstrs, v.Name()+"/"+v.Version())
58 49
 	}
59
-	return buf.String()
50
+	return strings.Join(verstrs, " ")
60 51
 }
61 52
 
62 53
 // HTTPRequestDecorator is used to change an instance of