Skip to content

Commit

Permalink
server.go Simplify default Server name logic (#1467)
Browse files Browse the repository at this point in the history
The serverName atomic.Value field is used as a cache. This is not needed and logic can be simplified.
See related #1458
  • Loading branch information
stokito authored Jan 6, 2023
1 parent 51048b0 commit 434022b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 28 deletions.
40 changes: 15 additions & 25 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,6 @@ type Server struct {
concurrency uint32
concurrencyCh chan struct{}
perIPConnCounter perIPConnCounter
serverName atomic.Value

ctxPool sync.Pool
readerPool sync.Pool
Expand Down Expand Up @@ -2134,10 +2133,7 @@ func (s *Server) serveConn(c net.Conn) (err error) {
return handler(c)
}

var serverName []byte
if !s.NoDefaultServerHeader {
serverName = s.getServerName()
}
serverName := s.getServerName()
connRequestNum := uint64(0)
connID := nextConnID()
connTime := time.Now()
Expand Down Expand Up @@ -2364,8 +2360,8 @@ func (s *Server) serveConn(c net.Conn) (err error) {
// store req.ConnectionClose so even if it was changed inside of handler
connectionClose = s.DisableKeepalive || ctx.Request.Header.ConnectionClose()

if serverName != nil {
ctx.Response.Header.SetServerBytes(serverName)
if serverName != "" {
ctx.Response.Header.SetServer(serverName)
}
ctx.connID = connID
ctx.connRequestNum = connRequestNum
Expand Down Expand Up @@ -2418,8 +2414,8 @@ func (s *Server) serveConn(c net.Conn) (err error) {
ctx.Response.Header.setNonSpecial(strConnection, strKeepAlive)
}

if serverName != nil && len(ctx.Response.Header.Server()) == 0 {
ctx.Response.Header.SetServerBytes(serverName)
if serverName != "" && len(ctx.Response.Header.Server()) == 0 {
ctx.Response.Header.SetServer(serverName)
}

if !hijackNoResponse {
Expand Down Expand Up @@ -2812,29 +2808,23 @@ func (s *Server) releaseCtx(ctx *RequestCtx) {
s.ctxPool.Put(ctx)
}

func (s *Server) getServerName() []byte {
v := s.serverName.Load()
var serverName []byte
if v == nil {
serverName = []byte(s.Name)
if len(serverName) == 0 {
func (s *Server) getServerName() string {
serverName := s.Name
if serverName == "" {
if !s.NoDefaultServerHeader {
serverName = defaultServerName
}
s.serverName.Store(serverName)
} else {
serverName = v.([]byte)
}
return serverName
}

func (s *Server) writeFastError(w io.Writer, statusCode int, msg string) {
w.Write(formatStatusLine(nil, strHTTP11, statusCode, s2b(StatusMessage(statusCode)))) //nolint:errcheck

server := ""
if !s.NoDefaultServerHeader {
server = fmt.Sprintf("Server: %s\r\n", s.getServerName())
server := s.getServerName()
if server != "" {
server = fmt.Sprintf("Server: %s\r\n", server)
}

date := ""
if !s.NoDefaultDate {
serverDateOnce.Do(updateServerDate)
Expand All @@ -2861,16 +2851,16 @@ func defaultErrorHandler(ctx *RequestCtx, err error) {
}
}

func (s *Server) writeErrorResponse(bw *bufio.Writer, ctx *RequestCtx, serverName []byte, err error) *bufio.Writer {
func (s *Server) writeErrorResponse(bw *bufio.Writer, ctx *RequestCtx, serverName string, err error) *bufio.Writer {
errorHandler := defaultErrorHandler
if s.ErrorHandler != nil {
errorHandler = s.ErrorHandler
}

errorHandler(ctx, err)

if serverName != nil {
ctx.Response.Header.SetServerBytes(serverName)
if serverName != "" {
ctx.Response.Header.SetServer(serverName)
}
ctx.SetConnectionClose()
if bw == nil {
Expand Down
4 changes: 2 additions & 2 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,8 @@ func TestServerName(t *testing.T) {
}

resp := getReponse()
if !bytes.Contains(resp, []byte("\r\nServer: "+string(defaultServerName)+"\r\n")) {
t.Fatalf("Unexpected response %q expected Server: "+string(defaultServerName), resp)
if !bytes.Contains(resp, []byte("\r\nServer: "+defaultServerName+"\r\n")) {
t.Fatalf("Unexpected response %q expected Server: "+defaultServerName, resp)
}

// We can't just overwrite s.Name as fasthttp caches the name in an atomic.Value
Expand Down
2 changes: 1 addition & 1 deletion strings.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fasthttp

var (
defaultServerName = []byte("fasthttp")
defaultServerName = "fasthttp"
defaultUserAgent = "fasthttp"
defaultContentType = []byte("text/plain; charset=utf-8")
)
Expand Down

0 comments on commit 434022b

Please # to comment.