Skip to content

Don't set content type by default #207

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

Merged
merged 2 commits into from
Jun 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions lib/net/http/generic_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@ def finish
def send_request_with_body(sock, ver, path, body)
self.content_length = body.bytesize
delete 'Transfer-Encoding'
supply_default_content_type
write_header sock, ver, path
wait_for_continue sock, ver if sock.continue_timeout
sock.write body
Expand All @@ -271,7 +270,6 @@ def send_request_with_body_stream(sock, ver, path, f)
raise ArgumentError,
"Content-Length not given and Transfer-Encoding is not `chunked'"
end
supply_default_content_type
write_header sock, ver, path
wait_for_continue sock, ver if sock.continue_timeout
if chunked?
Expand Down Expand Up @@ -373,12 +371,6 @@ def flush_buffer(out, buf, chunked_p)
buf.clear
end

def supply_default_content_type
return if content_type()
warn 'net/http: Content-Type did not set; using application/x-www-form-urlencoded', uplevel: 1 if $VERBOSE
set_content_type 'application/x-www-form-urlencoded'
end

##
# Waits up to the continue timeout for a response from the server provided
# we're speaking HTTP 1.1 and are expecting a 100-continue response.
Expand Down Expand Up @@ -411,4 +403,3 @@ def write_header(sock, ver, path)
end

end

14 changes: 5 additions & 9 deletions test/net/http/test_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -494,12 +494,10 @@ def _test_post__no_data(http)

def test_s_post
url = "http://#{config('host')}:#{config('port')}/?q=a"
res = assert_warning(/Content-Type did not set/) do
Net::HTTP.post(
URI.parse(url),
"a=x")
end
assert_equal "application/x-www-form-urlencoded", res["Content-Type"]
res = Net::HTTP.post(
URI.parse(url),
"a=x")
assert_equal "application/octet-stream", res["Content-Type"]
assert_equal "a=x", res.body
assert_equal url, res["X-request-uri"]

Expand Down Expand Up @@ -570,9 +568,7 @@ def test_timeout_during_HTTP_session_write
th = Thread.new do
err = !windows? ? Net::WriteTimeout : Net::ReadTimeout
assert_raise(err) do
assert_warning(/Content-Type did not set/) do
conn.post('/', "a"*50_000_000)
end
conn.post('/', "a"*50_000_000)
end
end
assert th.join(EnvUtil.apply_timeout_scale(10))
Expand Down
13 changes: 10 additions & 3 deletions test/net/http/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ def handle_request(socket)
socket.write "HTTP/1.1 100 Continue\r\n\r\n"
end

# Set default Content-Type if not provided
if !headers['Content-Type'] && (method == 'POST' || method == 'PUT' || method == 'PATCH')
headers['Content-Type'] = 'application/octet-stream'
end

req = Request.new(method, path, headers, socket)
if @procs.key?(req.path) || @procs.key?("#{req.path}/")
proc = @procs[req.path] || @procs["#{req.path}/"]
Expand Down Expand Up @@ -306,16 +311,18 @@ def handle_post(path, headers, socket)
scheme = headers['X-Request-Scheme'] || 'http'
host = @config['host']
port = socket.addr[1]
charset = parse_content_type(headers['Content-Type'])[1]
content_type = headers['Content-Type'] || 'application/octet-stream'
charset = parse_content_type(content_type)[1]
path = "#{scheme}://#{host}:#{port}#{path}"
path = path.encode(charset) if charset
response = "HTTP/1.1 200 OK\r\nContent-Type: #{headers['Content-Type']}\r\nContent-Length: #{body.bytesize}\r\nX-request-uri: #{path}\r\n\r\n#{body}"
response = "HTTP/1.1 200 OK\r\nContent-Type: #{content_type}\r\nContent-Length: #{body.bytesize}\r\nX-request-uri: #{path}\r\n\r\n#{body}"
socket.print(response)
end

def handle_patch(path, headers, socket)
body = socket.read(headers['Content-Length'].to_i)
response = "HTTP/1.1 200 OK\r\nContent-Type: #{headers['Content-Type']}\r\nContent-Length: #{body.bytesize}\r\n\r\n#{body}"
content_type = headers['Content-Type'] || 'application/octet-stream'
response = "HTTP/1.1 200 OK\r\nContent-Type: #{content_type}\r\nContent-Length: #{body.bytesize}\r\n\r\n#{body}"
socket.print(response)
end

Expand Down