Skip to content

Commit ac6dde4

Browse files
author
Michael Hoisie
committed
Use new mime package for content-types
Run the new gofmt
1 parent a344644 commit ac6dde4

File tree

4 files changed

+20
-30
lines changed

4 files changed

+20
-30
lines changed

request.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func (r *Request) parseParams() (err os.Error) {
161161
}
162162
parts := bytes.Split(b, strings.Bytes("--"+boundary+"--\r\n"), 0)
163163
parts = bytes.Split(parts[0], strings.Bytes("--"+boundary+"\r\n"), 0)
164-
for _, data := range (parts) {
164+
for _, data := range parts {
165165
if len(data) < 2 {
166166
continue
167167
}
@@ -186,7 +186,7 @@ func (r *Request) parseParams() (err os.Error) {
186186
v := strings.TrimSpace(header[1])
187187
if n == "Content-Disposition" {
188188
cdparts := strings.Split(v, ";", 0)
189-
for _, cdparam := range (cdparts[1:]) {
189+
for _, cdparam := range cdparts[1:] {
190190
split := strings.Split(cdparam, "=", 2)
191191
pname := strings.TrimSpace(split[0])
192192
pval := strings.TrimSpace(split[1])
@@ -236,10 +236,10 @@ func (r *Request) parseCookies() (err os.Error) {
236236

237237
r.Cookies = make(map[string]string)
238238

239-
for k, v := range (r.Headers) {
239+
for k, v := range r.Headers {
240240
if k == "Cookie" {
241241
cookies := strings.Split(v, ";", 0)
242-
for _, cookie := range (cookies) {
242+
for _, cookie := range cookies {
243243
cookie = strings.TrimSpace(cookie)
244244
parts := strings.Split(cookie, "=", 2)
245245
if len(parts) != 2 {
@@ -259,8 +259,8 @@ func (r *Request) GetParam(name string) string {
259259
return ""
260260
}
261261
params, ok := r.Params[name]
262-
if !ok || len(params) == 0 {
263-
return ""
262+
if !ok || len(params) == 0 {
263+
return ""
264264
}
265265
return params[0]
266266
}

servefile.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,12 @@ package web
22

33
import (
44
"io"
5+
"mime"
56
"os"
67
"path"
78
"utf8"
89
)
910

10-
var contentByExt = map[string]string{
11-
".css": "text/css",
12-
".gif": "image/gif",
13-
".html": "text/html; charset=utf-8",
14-
".htm": "text/html; charset=utf-8",
15-
".jpg": "image/jpeg",
16-
".js": "application/x-javascript",
17-
".pdf": "application/pdf",
18-
".png": "image/png",
19-
}
20-
2111
func isText(b []byte) bool {
2212
for len(b) > 0 && utf8.FullRune(b) {
2313
rune, size := utf8.DecodeRune(b)
@@ -53,7 +43,7 @@ func serveFile(ctx *Context, name string) {
5343
defer f.Close()
5444
ext := path.Ext(name)
5545

56-
if ctype, ok := contentByExt[ext]; ok {
46+
if ctype := mime.TypeByExtension(ext); ctype != "" {
5747
ctx.SetHeader("Content-Type", ctype, true)
5848
} else {
5949
// read first chunk to decide between utf-8 text and binary

web.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ func SetStaticDir(dir string) os.Error {
400400

401401
func Urlencode(data map[string]string) string {
402402
var buf bytes.Buffer
403-
for k, v := range (data) {
403+
for k, v := range data {
404404
buf.WriteString(http.URLEscape(k))
405405
buf.WriteByte('=')
406406
buf.WriteString(http.URLEscape(v))

web_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func buildTestResponse(buf *bytes.Buffer) *testResponse {
5353
statusParts := strings.Split(headers[0], " ", 3)
5454
response.statusCode, _ = strconv.Atoi(statusParts[1])
5555

56-
for _, h := range (headers[1:]) {
56+
for _, h := range headers[1:] {
5757
split := strings.Split(h, ":", 2)
5858
name := strings.TrimSpace(split[0])
5959
value := strings.TrimSpace(split[1])
@@ -193,7 +193,7 @@ func buildTestRequest(method string, path string, body string, headers map[strin
193193
}
194194

195195
func TestRouting(t *testing.T) {
196-
for _, test := range (tests) {
196+
for _, test := range tests {
197197
resp := getTestResponse(test.method, test.path, test.body, make(map[string]string))
198198

199199
if resp.statusCode != test.expectedStatus {
@@ -213,7 +213,7 @@ func TestRouting(t *testing.T) {
213213
}
214214

215215
func TestHead(t *testing.T) {
216-
for _, test := range (tests) {
216+
for _, test := range tests {
217217

218218
if test.method != "GET" {
219219
continue
@@ -252,7 +252,7 @@ func TestHead(t *testing.T) {
252252

253253
func buildScgiFields(fields map[string]string, buf *bytes.Buffer) []byte {
254254

255-
for k, v := range (fields) {
255+
for k, v := range fields {
256256
buf.WriteString(k)
257257
buf.Write([]byte{0})
258258
buf.WriteString(v)
@@ -304,7 +304,7 @@ func buildTestScgiRequest(method string, path string, body string, headers map[s
304304
}
305305

306306
func TestScgi(t *testing.T) {
307-
for _, test := range (tests) {
307+
for _, test := range tests {
308308
req := buildTestScgiRequest(test.method, test.path, test.body, make(map[string]string))
309309
var output bytes.Buffer
310310
nb := tcpBuffer{input: req, output: &output}
@@ -322,7 +322,7 @@ func TestScgi(t *testing.T) {
322322
}
323323

324324
func TestScgiHead(t *testing.T) {
325-
for _, test := range (tests) {
325+
for _, test := range tests {
326326

327327
if test.method != "GET" {
328328
continue
@@ -400,7 +400,7 @@ func buildTestFcgiRequest(method string, path string, bodychunks []string, heade
400400
fcgiHeaders := make(map[string]string)
401401

402402
bodylength := 0
403-
for _, s := range (bodychunks) {
403+
for _, s := range bodychunks {
404404
bodylength += len(s)
405405
}
406406

@@ -421,7 +421,7 @@ func buildTestFcgiRequest(method string, path string, bodychunks []string, heade
421421
req.Write(newFcgiRecord(fcgiBeginRequest, 0, make([]byte, 8)))
422422

423423
var buf bytes.Buffer
424-
for k, v := range (fcgiHeaders) {
424+
for k, v := range fcgiHeaders {
425425
kv := buildFcgiKeyValue(k, v)
426426
buf.Write(kv)
427427
}
@@ -433,7 +433,7 @@ func buildTestFcgiRequest(method string, path string, bodychunks []string, heade
433433
req.Write(newFcgiRecord(fcgiParams, 0, []byte{}))
434434

435435
//send the body
436-
for _, s := range (bodychunks) {
436+
for _, s := range bodychunks {
437437
if len(s) > 0 {
438438
req.Write(newFcgiRecord(fcgiStdin, 0, strings.Bytes(s)))
439439
}
@@ -472,7 +472,7 @@ func getFcgiOutput(br *bytes.Buffer) *bytes.Buffer {
472472
}
473473

474474
func TestFcgi(t *testing.T) {
475-
for _, test := range (tests) {
475+
for _, test := range tests {
476476
req := buildTestFcgiRequest(test.method, test.path, []string{test.body}, make(map[string]string))
477477
var output bytes.Buffer
478478
nb := tcpBuffer{input: req, output: &output}
@@ -491,7 +491,7 @@ func TestFcgi(t *testing.T) {
491491
}
492492

493493
func TestFcgiHead(t *testing.T) {
494-
for _, test := range (tests) {
494+
for _, test := range tests {
495495

496496
if test.method != "GET" {
497497
continue

0 commit comments

Comments
 (0)