Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/http: Error adds newline to message #50186

Closed
YetAnotherRussian opened this issue Dec 15, 2021 · 5 comments
Closed

net/http: Error adds newline to message #50186

YetAnotherRussian opened this issue Dec 15, 2021 · 5 comments

Comments

@YetAnotherRussian
Copy link

YetAnotherRussian commented Dec 15, 2021

What version of Go are you using (go version)?

go1.17.5 windows/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

Microsoft Windows [Version 6.3.9600]

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\DMusin\AppData\Local\go-build
set GOENV=C:\Users\DMusin\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Go\bin\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Go\bin
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.17.5
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Temp\go-b
uild1689227927=/tmp/go-build -gno-record-gcc-switches

What did you do?

I use a small program to test GET requests to API:

package main

import (
"fmt"
"log"
"net"
"net/http"
"strings"
//"encoding/json"
)

func process(w http.ResponseWriter, r *http.Request) {

switch r.Method {
case "GET":
	key := r.Header.Get("API-key")
		if key != "123-345-678" {
		fmt.Println("The request was missing a valid API key!")
		http.Error(w, "", http.StatusForbidden)
		return
		}
    http.ServeFile(w, r, "list.html")
case "POST":
    fmt.Fprintln(w, "Method not implemented yet.")
	return
default:
    fmt.Fprintln(w, "Method not supported!")
}

}

func GetLocalIP() string {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
log.Fatal(err)
}
defer conn.Close()

return strings.Split(conn.LocalAddr().String(), ":")[0]

}

func main() {
fmt.Println("Server is starting...")
http.HandleFunc("/", process)
localIp := GetLocalIP() + ":12345"
fmt.Println("Bound to " + localIp)
log.Fatal(http.ListenAndServe(localIp, nil))
}


What did you expect to see?

http.Error(w, "", http.StatusForbidden) - expected an empty message here
http.Error(w, "Wrong API key!", http.StatusForbidden) - expected "Wrong API key!" message

What did you see instead?

http.Error(w, "", http.StatusForbidden) - "\n" message here
http.Error(w, "Wrong API key!", http.StatusForbidden) - "Wrong API key!\n" message here

From Chrome:
image

From Visual Studio:
image

@davecheney
Copy link
Contributor

@YetAnotherRussian
Copy link
Author

Yep, but I'm unsure that's a correct way. If it is, then I'll have to fix that @ client side apps.

@davecheney
Copy link
Contributor

What is the problem that the new line causes?

@YetAnotherRussian
Copy link
Author

Line break increases height of any UI element which is used to show that message (user side). Even if it's you who form these messages, you have to forcibly use Trim() (or any of it's equivalent from any lang) @ client side. Then you do the same for logging. If you concat these strings, you go even further. My vision is that if the string is empty, it should be empty, but not a line break/whitespace(s) etc.

@seankhliao seankhliao changed the title Extra line break in func Error(w, error, code) - error string net/http: Error adds newline to message Dec 15, 2021
@seankhliao
Copy link
Member

Duplicate of #24530

@seankhliao seankhliao marked this as a duplicate of #24530 Dec 15, 2021
@golang golang locked and limited conversation to collaborators Dec 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants