Skip to content

How can I support unsafe URIs? #2112

@srabraham

Description

@srabraham

Hi there,
We're in a silly situation with our web services. Some of our clients call our APIs with unsafe (unencoded) URIs. For example, we'll get something like

GET /?a=This is gross HTTP/1.1 rather than something sane like
GET /?a=This%20is%20better HTTP/1.1

We were able to make that work in our fasthttp-based server, up until this PR #1710. Before then, header.go looked backward from the end of the line for the last " " to find the HTTP version. That meant it was fine for the URI to contain spaces. Now, the code finds the HTTP version by seeking the second " " in the line, so if your URI contains a space, the code will error out.

The new behavior from that PR may be more in line with the RFCs, so I'm not asking you to backtrack. I am wondering how you'd suggest we handle this absurd situation though, assuming I'm stuck with my clients sending dirty URIs.

As an example of what I'm talking about, here's a test that passes before that PR (at a8cb5d5), but fails after that PR (at master). Thanks!

// insert this test into repo root's header_test.go
func TestRequestHeaderUnencodedParam(t *testing.T) {
	t.Parallel()

	s := "GET /?a=This has unescaped spaces HTTP/1.1\r\n" +
		"\r\n"
	var h RequestHeader
	br := bufio.NewReader(bytes.NewBufferString(s))
	if err := h.Read(br); err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if string(h.requestURI) != "/?a=This has unescaped spaces" {
		t.Fatalf("unexpected request URI: %q. Expecting %q", h.requestURI, "/?a=This has unescaped spaces")
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions