Skip to content

Remove Content-Length: 0 from GET request #487

@zt2

Description

@zt2

When I find a GET request, there is always a Content-Length: 0 field in the HTTP header like

GET / HTTP/1.1
Connection: close
Host: localhost:8080
User-Agent: http.rb/3.3.0
Content-Length: 0

It happened in lib/http/request/writer.rb add_body_type_headers:

      # Adds the headers to the header array for the given request body we are working
      # with
      def add_body_type_headers
        return if @headers[Headers::CONTENT_LENGTH] || chunked?

        @request_header << "#{Headers::CONTENT_LENGTH}: #{@body.size}"
      end

In this case, I think the header Content-Length should only be included when you are sending a message-body which body size > 0.

This behavior trigger some bugs, for example:

require 'http'

res = HTTP.get 'https://registry.npmjs.org'
puts res.to_s

will cause an error:

[ztz@localhost tmp]$ ruby http.rb
No frontdoor hosts available

by using typhoeus:

require 'typhoeus'

res = Typhoeus.get 'https://registry.npmjs.org'
puts res.to_s

will output:

[ztz@localhost tmp]$ ruby typhoeus.rb 
{"db_name":"registry","doc_count":922224,"doc_del_count":342,"update_seq":11768451,"purge_seq":0,"compact_running":false,"disk_size":8918630986,"data_size":6431280597,"instance_start_time":"1528907394828029","disk_format_version":6,"committed_update_seq":11768449}

You can test by using commands below:

curl -I -H "Content-Length: 0" https://registry.npmjs.org
curl -I https://registry.npmjs.org

What you expected to happen:

GET request without Content-Length: 0

GET / HTTP/1.1
Connection: close
Host: localhost:8080
User-Agent: http.rb/3.3.0

What actually happened:

GET request with Content-Length: 0

GET / HTTP/1.1
Connection: close
Host: localhost:8080
User-Agent: http.rb/3.3.0
Content-Length: 0

Version of gem or commit ref you are using:
http 3.3.0

Version of ruby you are using:
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions