Skip to content

Commit 9a6ba18

Browse files
committed
make format
1 parent 0a6394a commit 9a6ba18

File tree

5 files changed

+941
-941
lines changed

5 files changed

+941
-941
lines changed

cookie.go

Lines changed: 129 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -5,153 +5,153 @@
55
package web
66

77
import (
8-
"bytes"
9-
"fmt"
10-
"http"
11-
"io"
12-
"os"
13-
"sort"
14-
"strings"
15-
"time"
8+
"bytes"
9+
"fmt"
10+
"http"
11+
"io"
12+
"os"
13+
"sort"
14+
"strings"
15+
"time"
1616
)
1717

1818
// writeSetCookies writes the wire representation of the set-cookies
1919
// to w. Each cookie is written on a separate "Set-Cookie: " line.
2020
// This choice is made because HTTP parsers tend to have a limit on
2121
// line-length, so it seems safer to place cookies on separate lines.
2222
func writeSetCookies(w io.Writer, kk []*http.Cookie) os.Error {
23-
if kk == nil {
24-
return nil
25-
}
26-
lines := make([]string, 0, len(kk))
27-
var b bytes.Buffer
28-
for _, c := range kk {
29-
b.Reset()
30-
// TODO(petar): c.Value (below) should be unquoted if it is recognized as quoted
31-
fmt.Fprintf(&b, "%s=%s", http.CanonicalHeaderKey(c.Name), c.Value)
32-
if len(c.Path) > 0 {
33-
fmt.Fprintf(&b, "; Path=%s", http.URLEscape(c.Path))
34-
}
35-
if len(c.Domain) > 0 {
36-
fmt.Fprintf(&b, "; Domain=%s", http.URLEscape(c.Domain))
37-
}
38-
if len(c.Expires.Zone) > 0 {
39-
fmt.Fprintf(&b, "; Expires=%s", c.Expires.Format(time.RFC1123))
40-
}
41-
if c.MaxAge >= 0 {
42-
fmt.Fprintf(&b, "; Max-Age=%d", c.MaxAge)
43-
}
44-
if c.HttpOnly {
45-
fmt.Fprintf(&b, "; HttpOnly")
46-
}
47-
if c.Secure {
48-
fmt.Fprintf(&b, "; Secure")
49-
}
50-
lines = append(lines, "Set-Cookie: "+b.String()+"\r\n")
51-
}
52-
sort.SortStrings(lines)
53-
for _, l := range lines {
54-
if _, err := io.WriteString(w, l); err != nil {
55-
return err
56-
}
57-
}
58-
return nil
23+
if kk == nil {
24+
return nil
25+
}
26+
lines := make([]string, 0, len(kk))
27+
var b bytes.Buffer
28+
for _, c := range kk {
29+
b.Reset()
30+
// TODO(petar): c.Value (below) should be unquoted if it is recognized as quoted
31+
fmt.Fprintf(&b, "%s=%s", http.CanonicalHeaderKey(c.Name), c.Value)
32+
if len(c.Path) > 0 {
33+
fmt.Fprintf(&b, "; Path=%s", http.URLEscape(c.Path))
34+
}
35+
if len(c.Domain) > 0 {
36+
fmt.Fprintf(&b, "; Domain=%s", http.URLEscape(c.Domain))
37+
}
38+
if len(c.Expires.Zone) > 0 {
39+
fmt.Fprintf(&b, "; Expires=%s", c.Expires.Format(time.RFC1123))
40+
}
41+
if c.MaxAge >= 0 {
42+
fmt.Fprintf(&b, "; Max-Age=%d", c.MaxAge)
43+
}
44+
if c.HttpOnly {
45+
fmt.Fprintf(&b, "; HttpOnly")
46+
}
47+
if c.Secure {
48+
fmt.Fprintf(&b, "; Secure")
49+
}
50+
lines = append(lines, "Set-Cookie: "+b.String()+"\r\n")
51+
}
52+
sort.SortStrings(lines)
53+
for _, l := range lines {
54+
if _, err := io.WriteString(w, l); err != nil {
55+
return err
56+
}
57+
}
58+
return nil
5959
}
6060

6161
// writeCookies writes the wire representation of the cookies
6262
// to w. Each cookie is written on a separate "Cookie: " line.
6363
// This choice is made because HTTP parsers tend to have a limit on
6464
// line-length, so it seems safer to place cookies on separate lines.
6565
func writeCookies(w io.Writer, kk []*http.Cookie) os.Error {
66-
lines := make([]string, 0, len(kk))
67-
var b bytes.Buffer
68-
for _, c := range kk {
69-
b.Reset()
70-
n := c.Name
71-
// TODO(petar): c.Value (below) should be unquoted if it is recognized as quoted
72-
fmt.Fprintf(&b, "%s=%s", http.CanonicalHeaderKey(n), c.Value)
73-
if len(c.Path) > 0 {
74-
fmt.Fprintf(&b, "; $Path=%s", http.URLEscape(c.Path))
75-
}
76-
if len(c.Domain) > 0 {
77-
fmt.Fprintf(&b, "; $Domain=%s", http.URLEscape(c.Domain))
78-
}
79-
if c.HttpOnly {
80-
fmt.Fprintf(&b, "; $HttpOnly")
81-
}
82-
lines = append(lines, "Cookie: "+b.String()+"\r\n")
83-
}
84-
sort.SortStrings(lines)
85-
for _, l := range lines {
86-
if _, err := io.WriteString(w, l); err != nil {
87-
return err
88-
}
89-
}
90-
return nil
66+
lines := make([]string, 0, len(kk))
67+
var b bytes.Buffer
68+
for _, c := range kk {
69+
b.Reset()
70+
n := c.Name
71+
// TODO(petar): c.Value (below) should be unquoted if it is recognized as quoted
72+
fmt.Fprintf(&b, "%s=%s", http.CanonicalHeaderKey(n), c.Value)
73+
if len(c.Path) > 0 {
74+
fmt.Fprintf(&b, "; $Path=%s", http.URLEscape(c.Path))
75+
}
76+
if len(c.Domain) > 0 {
77+
fmt.Fprintf(&b, "; $Domain=%s", http.URLEscape(c.Domain))
78+
}
79+
if c.HttpOnly {
80+
fmt.Fprintf(&b, "; $HttpOnly")
81+
}
82+
lines = append(lines, "Cookie: "+b.String()+"\r\n")
83+
}
84+
sort.SortStrings(lines)
85+
for _, l := range lines {
86+
if _, err := io.WriteString(w, l); err != nil {
87+
return err
88+
}
89+
}
90+
return nil
9191
}
9292

9393
// readCookies parses all "Cookie" values from
9494
// the header h, removes the successfully parsed values from the
9595
// "Cookie" key in h and returns the parsed Cookies.
9696
func readCookies(h http.Header) []*http.Cookie {
97-
cookies := []*http.Cookie{}
98-
lines, ok := h["Cookie"]
99-
if !ok {
100-
return cookies
101-
}
102-
unparsedLines := []string{}
103-
for _, line := range lines {
104-
parts := strings.Split(strings.TrimSpace(line), ";", -1)
105-
if len(parts) == 1 && parts[0] == "" {
106-
continue
107-
}
108-
// Per-line attributes
109-
var lineCookies = make(map[string]string)
110-
var path string
111-
var domain string
112-
var httponly bool
113-
for i := 0; i < len(parts); i++ {
114-
parts[i] = strings.TrimSpace(parts[i])
115-
if len(parts[i]) == 0 {
116-
continue
117-
}
118-
attr, val := parts[i], ""
119-
var err os.Error
120-
if j := strings.Index(attr, "="); j >= 0 {
121-
attr, val = attr[:j], attr[j+1:]
122-
val, err = http.URLUnescape(val)
123-
if err != nil {
124-
continue
125-
}
126-
}
127-
switch strings.ToLower(attr) {
128-
case "$httponly":
129-
httponly = true
130-
case "$domain":
131-
domain = val
132-
// TODO: Add domain parsing
133-
case "$path":
134-
path = val
135-
// TODO: Add path parsing
136-
default:
137-
lineCookies[attr] = val
138-
}
139-
}
140-
if len(lineCookies) == 0 {
141-
unparsedLines = append(unparsedLines, line)
142-
}
143-
for n, v := range lineCookies {
144-
cookies = append(cookies, &http.Cookie{
145-
Name: n,
146-
Value: v,
147-
Path: path,
148-
Domain: domain,
149-
HttpOnly: httponly,
150-
MaxAge: -1,
151-
Raw: line,
152-
})
153-
}
154-
}
155-
h["Cookie"] = unparsedLines, len(unparsedLines) > 0
156-
return cookies
97+
cookies := []*http.Cookie{}
98+
lines, ok := h["Cookie"]
99+
if !ok {
100+
return cookies
101+
}
102+
unparsedLines := []string{}
103+
for _, line := range lines {
104+
parts := strings.Split(strings.TrimSpace(line), ";", -1)
105+
if len(parts) == 1 && parts[0] == "" {
106+
continue
107+
}
108+
// Per-line attributes
109+
var lineCookies = make(map[string]string)
110+
var path string
111+
var domain string
112+
var httponly bool
113+
for i := 0; i < len(parts); i++ {
114+
parts[i] = strings.TrimSpace(parts[i])
115+
if len(parts[i]) == 0 {
116+
continue
117+
}
118+
attr, val := parts[i], ""
119+
var err os.Error
120+
if j := strings.Index(attr, "="); j >= 0 {
121+
attr, val = attr[:j], attr[j+1:]
122+
val, err = http.URLUnescape(val)
123+
if err != nil {
124+
continue
125+
}
126+
}
127+
switch strings.ToLower(attr) {
128+
case "$httponly":
129+
httponly = true
130+
case "$domain":
131+
domain = val
132+
// TODO: Add domain parsing
133+
case "$path":
134+
path = val
135+
// TODO: Add path parsing
136+
default:
137+
lineCookies[attr] = val
138+
}
139+
}
140+
if len(lineCookies) == 0 {
141+
unparsedLines = append(unparsedLines, line)
142+
}
143+
for n, v := range lineCookies {
144+
cookies = append(cookies, &http.Cookie{
145+
Name: n,
146+
Value: v,
147+
Path: path,
148+
Domain: domain,
149+
HttpOnly: httponly,
150+
MaxAge: -1,
151+
Raw: line,
152+
})
153+
}
154+
}
155+
h["Cookie"] = unparsedLines, len(unparsedLines) > 0
156+
return cookies
157157
}

examples/logger.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
package main
22

33
import (
4-
"log"
5-
"os"
6-
"web"
4+
"log"
5+
"os"
6+
"web"
77
)
88

99
func hello(val string) string { return "hello " + val }
1010

1111
func main() {
12-
f, err := os.Create("server.log")
13-
if err != nil {
14-
println(err.String())
15-
return
16-
}
17-
logger := log.New(f, "", log.Ldate|log.Ltime)
18-
web.Get("/(.*)", hello)
19-
web.SetLogger(logger)
20-
web.Run("0.0.0.0:9999")
12+
f, err := os.Create("server.log")
13+
if err != nil {
14+
println(err.String())
15+
return
16+
}
17+
logger := log.New(f, "", log.Ldate|log.Ltime)
18+
web.Get("/(.*)", hello)
19+
web.SetLogger(logger)
20+
web.Run("0.0.0.0:9999")
2121
}

0 commit comments

Comments
 (0)