Skip to content
Myr

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

6 files+15 added−13 removed

modified:auth.go+2 added-1 removed
5 unmodified lines
67import (8 "crypto/subtle"9 "encoding/base64"10 "net/http"11 "strconv"12)1314// AuthUserKey is the cookie name for user credential in basic auth.
36 unmodified lines
51 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 }5859 // The user credentials was found, set user's id to key AuthUserKey in this context, the user's id can be read later using
37 unmodified lines
modified:context.go+3 added-3 removed
472 unmodified lines
473// It will abort the request with HTTP 400 if any error ocurrs.474// See the binding package.475func (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 }479480 return481}
106 unmodified lines
588func 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 lines
modified:gin.go+6 added-6 removed
376 unmodified lines
377 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}390391var mimePlain = []string{MIMEPlain}392
12 unmodified lines
405406func 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 }413414 if length := len(path); length > 1 && path[length-1] == '/' {415 req.URL.Path = path[:length-1]
13 unmodified lines
429 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 lines
modified:recovery.go+2 added-1 removed
8 unmodified lines
9 "fmt"10 "io"11 "io/ioutil"12 "log"13 "net/http"14 "net/http/httputil"15 "runtime"16 "time"17)
23 unmodified lines
41 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 lines
modified:response_writer.go+1 added-1 removed
11 unmodified lines
12)1314const (15 noWritten = -116 defaultStatus = 20016 defaultStatus = http.StatusOK17)1819type responseWriterBase interface {20 http.ResponseWriter
94 unmodified lines
modified:routergroup.go+1 added-1 removed
182 unmodified lines
183 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