ncr / rack-proxy

A request/response rewriting HTTP proxy. A Rack app.
MIT License
269 stars 94 forks source link

Problems with proxying chunked encoding from rails server #50

Open nhemsley opened 9 years ago

nhemsley commented 9 years ago

I am having a problem with proxying json with chunked encoding in the header.

It appears that the body is never sent. I have no idea how to fix this, and cant be bothered finding out what chunked encoding is (streaming..?). A quick look at it, this https://github.com/rack/rack/issues/396 indicates that the body needs to be yielded, but I dont have time to have a look into this.

a quick fix for this is to remove the header:

in call(env) assuming a ret var

ret[1].delete 'transfer-encoding' if ret.length > 1

Here is the output from wget -S (save headers) from the proxy server, and the original server:

proxied:

HTTP/1.1 200 OK Date: Wed, 04 Feb 2015 02:00:22 GMT Status: 200 OK Connection: close X-Frame-Options: SAMEORIGIN X-Xss-Protection: 1; mode=block X-Content-Type-Options: nosniff X-Ua-Compatible: chrome=1 Content-Type: application/json; charset=utf-8 Etag: "208b8efe53ef1664e9c91c244afc42e1" Cache-Control: max-age=0, private, must-revalidate X-Request-Id: e06a103d-00a8-41bf-80ce-f094546654db X-Runtime: 0.025867 Transfer-Encoding: chunked Server: WEBrick/1.3.1 (Ruby/2.0.0/2014-11-13)

unproxied: HTTP/1.1 200 OK Date: Wed, 04 Feb 2015 02:00:52 GMT Status: 200 OK Connection: close X-Frame-Options: SAMEORIGIN X-XSS-Protection: 1; mode=block X-Content-Type-Options: nosniff X-UA-Compatible: chrome=1 Content-Type: application/json; charset=utf-8 ETag: "208b8efe53ef1664e9c91c244afc42e1" Cache-Control: max-age=0, private, must-revalidate X-Request-Id: d2ad9565-7648-4e10-9387-83052b069cca X-Runtime: 0.029753 Transfer-Encoding: chunked

[{"id":1,"created_at":"2015-02-03T08:15:01.000Z","updated_at":"2015-02-03T08:15:01.000Z","job_date":"2015-02-03T00:00:00.000Z","address":null,"total":null,"client_id":1,"extras":null,"comments":null,"private_comments":null,"job_date_end":"2015-02-04T00:00:00.000Z","client":{"id":1,"created_at":"2015-02-03T08:15:01.000Z","updated_at":"2015-02-03T08:15:01.000Z","name":"Elna Ernser","date":null,"address":"992 Harber Field","work_phone":null,"home_phone":null,"mob_phone":null,"fax":null,"phones":null,"email":null},"groups":[]},{"id":2,"created_at":"2015-02-03T08:15:01.000Z","updated_at":"2015-02-03T08:15:01.000Z","job_date":"2015-02-03T00:00:00.000Z","address":null,"total":null,"client_id":2,"extras":null,"comments":null,"private_comments":null,"job_date_end":"2015-02-04T00:00:00.000Z","client":{"id":2,"created_at":"2015-02-03T08:15:01.000Z","updated_at":"2015-02-03T08:15:01.000Z","name":"Ewell Fritsch","date":null,"address":"9854 Courtney Keys","work_phone":null,"home_phone":null,"mob_phone":null,"fax":null,"phones":null,"email":null},"groups":[]},{"id":3,"created_at":"2015-02-03T08:15:01.000Z","updated_at":"2015-02-03T08:15:01.000Z","job_date":"2015-02-03T00:00:00.000Z","address":null,"total":null,"client_id":3,"extras":null,"comments":null,"private_comments":null,"job_date_end":"2015-02-04T00:00:00.000Z","client":{"id":3,"created_at":"2015-02-03T08:15:01.000Z","updated_at":"2015-02-03T08:15:01.000Z","name":"Vincenza Block DDS","date":null,"address":"3278 Dickens Parks","work_phone":null,"home_phone":null,"mob_phone":null,"fax":null,"phones":null,"email":null},"groups":[]},{"id":4,"created_at":"2015-02-03T08:15:01.000Z","updated_at":"2015-02-03T08:15:01.000Z","job_date":"2015-02-03T00:00:00.000Z","address":null,"total":null,"client_id":4,"extras":null,"comments":null,"private_comments":null,"job_date_end":"2015-02-04T00:00:00.000Z","client":{"id":4,"created_at":"2015-02-03T08:15:01.000Z","updated_at":"2015-02-03T08:15:01.000Z","name":"Elena Turcotte V","date":null,"address":"9891 Schowalter Manor","work_phone":null,"home_phone":null,"mob_phone":null,"fax":null,"phones":null,"email":null},"groups":[]},{"id":5,"created_at":"2015-02-03T08:15:01.000Z","updated_at":"2015-02-03T08:15:01.000Z","job_date":"2015-02-03T00:00:00.000Z","address":null,"total":null,"client_id":5,"extras":null,"comments":null,"private_comments":null,"job_date_end":"2015-02-04T00:00:00.000Z","client":{"id":5,"created_at":"2015-02-03T08:15:01.000Z","updated_at":"2015-02-03T08:15:01.000Z","name":"Mr. Retha O'Connell","date":null,"address":"12506 Hermann Pine","work_phone":null,"home_phone":null,"mob_phone":null,"fax":null,"phones":null,"email":null},"groups":[]}]

westoque commented 9 years ago

This also happened to me. I got a bad Rack::Lint error for the "content-length" header because it seems that my server is returning chunked response. I fixed this by forcing the proxy to use @streaming = true.

def call(env)
  @streaming = true
   perform_request(env)
end
tirumaraiselvan commented 8 years ago

I solved this issue with

    def call(env)
      @streaming = true
      super
    end

    def rewrite_response(triplet)
      status, headers, body = triplet
      headers.delete "transfer-encoding"
      triplet
    end
steookk commented 7 years ago

@tirumaraiselvan actually defining @streaming = true is not necessary, at least in my case rewrite_response does the job