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: Server doesn't send a 501 (Not implemented) on unknown Transfer-Encodings #30710

Open
odeke-em opened this Issue Mar 10, 2019 · 1 comment

Comments

Projects
None yet
3 participants
@odeke-em
Copy link
Member

odeke-em commented Mar 10, 2019

Our net/http server implementation doesn't return a 501 (Not implemented) for unknown transfer-encodings.

I just noticed this issue while working on the CL for #29162. RFC 7230 Section 3.3.1 https://tools.ietf.org/html/rfc7230#section-3.3.1 says
" A server that receives a request message with a transfer coding it
does not understand SHOULD respond with 501 (Not Implemented)."

Screen Shot 2019-03-09 at 9 01 20 PM

and I can verify this with https://play.golang.org/p/ybUoNsIZEWw or inlined below

package main

import (
	"bufio"
	"bytes"
	"compress/gzip"
	"fmt"
	"log"
	"net"
	"net/http"
	"net/http/httptest"
	"net/http/httputil"
	"net/url"
)

func main() {
	log.SetFlags(0)
	cst := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Aloha, World1"))
	}))
	defer cst.Close()

	ux, err := url.Parse(cst.URL)
	if err != nil {
		log.Fatalf("Failed to parse server URL: %v", err)
	}

	buf := new(bytes.Buffer)
	gzw := gzip.NewWriter(buf)
	n, _ := gzw.Write([]byte("This is that"))
	gzw.Close()

	reqPayloads := []string{
		fmt.Sprintf("POST / HTTP/1.1\r\nHost: localhost\r\nTransfer-Encoding: chunked\r\n\r\n%02x\r\n%s\r\n0\r\n\r\n", n, buf.String()),

		// The unknown transfer-encoding.
		fmt.Sprintf("POST / HTTP/1.1\r\nHost: localhost\r\nTransfer-Encoding: fugazi, chunked\r\n\r\n%02x\r\n%s\r\n0\r\n\r\n", n, buf.String()),
	}

	for _, reqBody := range reqPayloads {
		resBlob, err := makeReq(ux.Host, []byte(reqBody))
		log.Printf("******\nResBlob:\n%s\nErr: %v\n*****\n\n", resBlob, err)
	}
}

func makeReq(host string, reqBody []byte) ([]byte, error) {
	conn, err := net.Dial("tcp", host)
	if err != nil {
		return nil, err
	}
	defer conn.Close()
	if _, err := conn.Write(reqBody); err != nil {
		return nil, err
	}

	brRes := bufio.NewReader(conn)
	res, err := http.ReadResponse(brRes, nil)
	if err != nil {
		return nil, err
	}
	return httputil.DumpResponse(res, true)
}

which prints

******
ResBlob:
HTTP/1.1 200 OK
Connection: close
Content-Length: 13
Content-Type: text/plain; charset=utf-8
Date: Tue, 10 Nov 2009 23:00:00 GMT

Aloha, World1
Err: <nil>
*****

******
ResBlob:
HTTP/1.1 400 Bad Request
Connection: close
Content-Type: text/plain; charset=utf-8

400 Bad Request
Err: <nil>
*****

but the last should ideally be

******
ResBlob:
HTTP/1.1 501 Not Implemented
Connection: close
Content-Type: text/plain; charset=utf-8

501 Not Implemented
Err: <nil>
*****

/cc @bradfitz

@odeke-em odeke-em added this to the Go1.13 milestone Mar 10, 2019

@gopherbot

This comment has been minimized.

Copy link

gopherbot commented Mar 11, 2019

Change https://golang.org/cl/167017 mentions this issue: net/http: spec compliance for unsupported transfer-encodings

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.