Skip to content

handle_empty_content! causes problems with Go's http.Client #1787

@catatsuy

Description

@catatsuy

refs: #1603

Go's http.client's automatic redirection uses the same Content-Type for GET. This request is invalid in rack 2.2.

I don't know if the Go implementation is wrong or the rack implementation is wrong. But because it is used in the actual implementation, could you please consider an option to disable it?

For example, you can see it in the following code.

require 'sinatra'

post '/file_upload' do
  redirect '/redirect', 302
end

get '/redirect' do
  return 'abcd'
end
package main

import (
	"bytes"
	"io"
	"log"
	"mime/multipart"
	"net/http"
	"os"
)

func main() {
	hClient := &http.Client{
		CheckRedirect: func(req *http.Request, via []*http.Request) error {
			return nil
			// If you enable the following code, you will not get the error.
			// return http.ErrUseLastResponse
		},
	}

	file, err := os.Open("test.png")
	if err != nil {
		log.Fatal(err)
	}
	defer file.Close()

	body := &bytes.Buffer{}
	writer := multipart.NewWriter(body)
	part, err := writer.CreateFormFile("image", "upload.png")
	if err != nil {
		log.Fatal(err)
	}

	_, err = io.Copy(part, file)
	if err != nil {
		log.Fatal(err)
	}

	contentType := writer.FormDataContentType()

	err = writer.Close()
	if err != nil {
		log.Fatal(err)
	}

	req, err := http.NewRequest(http.MethodPost, "http://localhost:4567/file_upload", body)
	if err != nil {
		log.Fatal(err)
	}

	req.Header.Set("Content-Type", contentType)

	res, err := hClient.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	res.Body.Close()
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions