remove hardcode instead of http status value (#1411)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by 田欧 · committed by Bo-Yi Wu · parent 1f59bad · Browse files · 6 files·+15 added−13 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+15 added−13 removed
- other:+15 added−13 removed
Provenance
- Commit
6c6d97b- Tree
a72b6b0- Author
田欧 <thinkerou@gmail.com>- Committer
Bo-Yi Wu <appleboy.tw@gmail.com>- Parent
1f59bad
6 files changed+15 added−13 removed
modified:auth.go+2 added-1 removed
5 unmodified lines6 7 import (8 "crypto/subtle"9 "encoding/base64"10+ "net/http"11 "strconv"12 )13 14 // AuthUserKey is the cookie name for user credential in basic auth.36 unmodified lines51 user, found := pairs.searchCredential(c.requestHeader("Authorization"))52 if !found {53 // Credentials doesn't match, we return 401 and abort handlers chain.54 c.Header("WWW-Authenticate", realm)54- c.AbortWithStatus(401)55+ c.AbortWithStatus(http.StatusUnauthorized)56 return57 }58 59 // The user credentials was found, set user's id to key AuthUserKey in this context, the user's id can be read later using37 unmodified linesmodified:context.go+3 added-3 removed
472 unmodified lines473 // It will abort the request with HTTP 400 if any error ocurrs.474 // See the binding package.475 func (c *Context) MustBindWith(obj interface{}, b binding.Binding) (err error) {476 if err = c.ShouldBindWith(obj, b); err != nil {477- c.AbortWithError(400, err).SetType(ErrorTypeBind)477+ c.AbortWithError(http.StatusBadRequest, err).SetType(ErrorTypeBind)478 }479 480 return481 }106 unmodified lines588 func bodyAllowedForStatus(status int) bool {589 switch {590 case status >= 100 && status <= 199:591 return false592- case status == 204:592+ case status == http.StatusNoContent:593 return false594- case status == 304:594+ case status == http.StatusNotModified:595 return false596 }597 return true598 }262 unmodified linesmodified:gin.go+6 added-6 removed
376 unmodified lines377 for _, tree := range engine.trees {378 if tree.method != httpMethod {379 if handlers, _, _ := tree.root.getValue(path, nil, unescape); handlers != nil {380 c.handlers = engine.allNoMethod381- serveError(c, 405, default405Body)381+ serveError(c, http.StatusMethodNotAllowed, default405Body)382 return383 }384 }385 }386 }387 c.handlers = engine.allNoRoute388- serveError(c, 404, default404Body)388+ serveError(c, http.StatusNotFound, default404Body)389 }390 391 var mimePlain = []string{MIMEPlain}392 12 unmodified lines405 406 func redirectTrailingSlash(c *Context) {407 req := c.Request408 path := req.URL.Path409- code := 301 // Permanent redirect, request with GET method409+ code := http.StatusMovedPermanently // Permanent redirect, request with GET method410 if req.Method != "GET" {411- code = 307411+ code = http.StatusTemporaryRedirect412 }413 414 if length := len(path); length > 1 && path[length-1] == '/' {415 req.URL.Path = path[:length-1]13 unmodified lines429 cleanPath(path),430 trailingSlash,431 )432 if found {433- code := 301 // Permanent redirect, request with GET method433+ code := http.StatusMovedPermanently // Permanent redirect, request with GET method434 if req.Method != "GET" {435- code = 307435+ code = http.StatusTemporaryRedirect436 }437 req.URL.Path = string(fixedPath)438 debugPrint("redirecting request %d: %s --> %s", code, path, req.URL.String())439 http.Redirect(c.Writer, req, req.URL.String(), code)5 unmodified linesmodified:recovery.go+2 added-1 removed
8 unmodified lines9 "fmt"10 "io"11 "io/ioutil"12 "log"13+ "net/http"14 "net/http/httputil"15 "runtime"16 "time"17 )23 unmodified lines41 stack := stack(3)42 httprequest, _ := httputil.DumpRequest(c.Request, false)43 logger.Printf("[Recovery] %s panic recovered:\n%s\n%s\n%s%s", timeFormat(time.Now()), string(httprequest), err, stack, reset)44 }44- c.AbortWithStatus(500)45+ c.AbortWithStatus(http.StatusInternalServerError)46 }47 }()48 c.Next()49 }67 unmodified linesmodified:response_writer.go+1 added-1 removed
11 unmodified lines12 )13 14 const (15 noWritten = -116- defaultStatus = 20016+ defaultStatus = http.StatusOK17 )18 19 type responseWriterBase interface {20 http.ResponseWriter94 unmodified linesmodified:routergroup.go+1 added-1 removed
182 unmodified lines183 fileServer := http.StripPrefix(absolutePath, http.FileServer(fs))184 _, nolisting := fs.(*onlyfilesFS)185 return func(c *Context) {186 if nolisting {187- c.Writer.WriteHeader(404)187+ c.Writer.WriteHeader(http.StatusNotFound)188 }189 fileServer.ServeHTTP(c.Writer, c.Request)190 }191 }22 unmodified lines