Skip to content

Commit 934410c

Browse files
authored
Merge pull request #207 from ruby/removed-default-content-type
Don't set content type by default
2 parents b652fa5 + fc5870d commit 934410c

File tree

3 files changed

+15
-21
lines changed

3 files changed

+15
-21
lines changed

lib/net/http/generic_request.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,6 @@ def finish
260260
def send_request_with_body(sock, ver, path, body)
261261
self.content_length = body.bytesize
262262
delete 'Transfer-Encoding'
263-
supply_default_content_type
264263
write_header sock, ver, path
265264
wait_for_continue sock, ver if sock.continue_timeout
266265
sock.write body
@@ -271,7 +270,6 @@ def send_request_with_body_stream(sock, ver, path, f)
271270
raise ArgumentError,
272271
"Content-Length not given and Transfer-Encoding is not `chunked'"
273272
end
274-
supply_default_content_type
275273
write_header sock, ver, path
276274
wait_for_continue sock, ver if sock.continue_timeout
277275
if chunked?
@@ -373,12 +371,6 @@ def flush_buffer(out, buf, chunked_p)
373371
buf.clear
374372
end
375373

376-
def supply_default_content_type
377-
return if content_type()
378-
warn 'net/http: Content-Type did not set; using application/x-www-form-urlencoded', uplevel: 1 if $VERBOSE
379-
set_content_type 'application/x-www-form-urlencoded'
380-
end
381-
382374
##
383375
# Waits up to the continue timeout for a response from the server provided
384376
# we're speaking HTTP 1.1 and are expecting a 100-continue response.
@@ -411,4 +403,3 @@ def write_header(sock, ver, path)
411403
end
412404

413405
end
414-

test/net/http/test_http.rb

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -494,12 +494,10 @@ def _test_post__no_data(http)
494494

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

@@ -570,9 +568,7 @@ def test_timeout_during_HTTP_session_write
570568
th = Thread.new do
571569
err = !windows? ? Net::WriteTimeout : Net::ReadTimeout
572570
assert_raise(err) do
573-
assert_warning(/Content-Type did not set/) do
574-
conn.post('/', "a"*50_000_000)
575-
end
571+
conn.post('/', "a"*50_000_000)
576572
end
577573
end
578574
assert th.join(EnvUtil.apply_timeout_scale(10))

test/net/http/utils.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ def handle_request(socket)
7171
socket.write "HTTP/1.1 100 Continue\r\n\r\n"
7272
end
7373

74+
# Set default Content-Type if not provided
75+
if !headers['Content-Type'] && (method == 'POST' || method == 'PUT' || method == 'PATCH')
76+
headers['Content-Type'] = 'application/octet-stream'
77+
end
78+
7479
req = Request.new(method, path, headers, socket)
7580
if @procs.key?(req.path) || @procs.key?("#{req.path}/")
7681
proc = @procs[req.path] || @procs["#{req.path}/"]
@@ -306,16 +311,18 @@ def handle_post(path, headers, socket)
306311
scheme = headers['X-Request-Scheme'] || 'http'
307312
host = @config['host']
308313
port = socket.addr[1]
309-
charset = parse_content_type(headers['Content-Type'])[1]
314+
content_type = headers['Content-Type'] || 'application/octet-stream'
315+
charset = parse_content_type(content_type)[1]
310316
path = "#{scheme}://#{host}:#{port}#{path}"
311317
path = path.encode(charset) if charset
312-
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}"
318+
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}"
313319
socket.print(response)
314320
end
315321

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

0 commit comments

Comments
 (0)