Skip to content

Commit

Permalink
Set response Server header to Server.Name if it is empty. This proper…
Browse files Browse the repository at this point in the history
…ly handles #118
  • Loading branch information
valyala committed Jun 17, 2016
1 parent 83a24c3 commit d42167f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
4 changes: 4 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,10 @@ func (s *Server) serveConn(c net.Conn) error {
ctx.Response.Header.SetCanonical(strConnection, strKeepAlive)
}

if len(ctx.Response.Header.Server()) == 0 {
ctx.Response.Header.SetServerBytes(serverName)
}

if bw == nil {
bw = acquireWriter(ctx)
}
Expand Down
14 changes: 10 additions & 4 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ func TestServerResponseServerHeader(t *testing.T) {
} else {
ctx.WriteString("OK")
}

// make sure the server name is sent to the client after ctx.Response.Reset()
ctx.NotFound()
},
Name: serverName,
}
Expand Down Expand Up @@ -89,11 +92,14 @@ func TestServerResponseServerHeader(t *testing.T) {
t.Fatalf("unexpected error: %s", err)
}

if resp.StatusCode() != StatusOK {
t.Fatalf("unexpected status code: %d. Expecting %d", resp.StatusCode(), StatusOK)
if resp.StatusCode() != StatusNotFound {
t.Fatalf("unexpected status code: %d. Expecting %d", resp.StatusCode(), StatusNotFound)
}
if string(resp.Body()) != "OK" {
t.Fatalf("unexpected body: %q. Expecting %q", resp.Body(), "OK")
if string(resp.Body()) != "404 Page not found" {
t.Fatalf("unexpected body: %q. Expecting %q", resp.Body(), "404 Page not found")
}
if string(resp.Header.Server()) != serverName {
t.Fatalf("unexpected server header: %q. Expecting %q", resp.Header.Server(), serverName)
}
if err = c.Close(); err != nil {
t.Fatalf("unexpected error: %s", err)
Expand Down

0 comments on commit d42167f

Please sign in to comment.