Android上的Cordova App使用SSL发送multipart / form-data导致Rack应用程序错误:#<eoferror:bad =“”content =“”body =“”>

时间:2015-11-04 22:53:02

标签: android ruby-on-rails cordova ssl puma

我们有一个通过SSL连接到rails服务器的Cordova应用程序。似乎我们最近从Unicorn到Puma的变化导致了一个问题,即Android应用程序在将图像文件发送到服务器时会出现500错误。

有些注意事项:

  • 使用完全相同的代码构建的应用程序的iOS版本完全正常
  • 如果不使用SSL,一切正常
  • 所有其他请求都是SSL并且工作正常,只有在发送时才会发送 multipart / form-data这个问题让人头疼。

我正在努力思考问题可能是什么,甚至在哪里寻找解决方案。我对Puma很新,所以我不知道我是否遗漏了一些重要的东西来处理这个问题。任何想法都将不胜感激。

iOS请求(正常工作)

POST /api/v2/attachments HTTP/1.1
Host: <omitted for security>
Accept: */*
Proxy-Connection: keep-alive
X-Requested-With: XMLHttpRequest
Accept-Encoding: gzip, deflate
If-None-Match: "2146f2b315668b29682ec01973ae4155"
Accept-Language: en-us
Content-Type: multipart/form-data; boundary=+++++org.apache.cordova.formBoundary
Content-Length: 13794
User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13B143 (4954749952)
Connection: keep-alive
Cookie: XSRF-TOKEN=<omitted for security>; _session_id=<omitted for security>

--+++++org.apache.cordova.formBoundary
Content-Disposition: form-data; name="type"

Image
--+++++org.apache.cordova.formBoundary
Content-Disposition: form-data; name="attachment[attachment]"; filename="cdv_photo_003.jpg"
Content-Type: image/jpeg
Content-Length: 13466

iُ  B~,"
#BR$3br 
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz
B   #3Rbr
$4%&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz




""""""''''',,,,,,,,,,


...................................................(<a$GW_ЬUպi&dA+x=R
?)uHv"ԊdcpIސW̚<<Kx~E1xEN]M%>r{Y_Yc<jʥYH_2r[P['6    d#wS+5Bfu

<more Binary data omitted for brevity>


--+++++org.apache.cordova.formBoundary--

等效Android请求(导致错误)

POST /api/v2/attachments HTTP/1.1
Content-Type: multipart/form-data; boundary=+++++
Cookie: _session_id=<omitted for security>; XSRF-TOKEN=<omitted for security>
Transfer-Encoding: chunked
User-Agent: Dalvik/2.1.0 (Linux; U; Android 5.0; SM-G900F Build/LRX21T)
Host: <omitted for security>
Connection: Keep-Alive
Accept-Encoding: gzip

--+++++
Content-Disposition: form-data; name="type"

Image
--+++++
Content-Disposition: form-data; name="attachment[attachment]"; filename="modified.jpg?1446611766814"
Content-Type: image/jpeg

Exif

(1#%(:3=<9387@H\N@DWE78PmQW_bghg>Mqypdx\egc"
#BR$3br 

<more Binary data omitted for brevity>

--+++++--

这些请求存在一些明显的差异,虽然我不称自己是HTTP协议的专家,但没有任何差异在我看来会像Puma那样会导致摔倒。

机架错误跟踪:

2015-11-04T04:36:07.041124+00:00 app[web.1]: 2015-11-04 04:36:07 +0000: Rack app error: #<EOFError: bad content body>
2015-11-04T04:36:07.041131+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/multipart/parser.rb:74:in `block in fast_forward_to_first_boundary'
2015-11-04T04:36:07.041133+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/multipart/parser.rb:72:in `loop'
2015-11-04T04:36:07.041134+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/multipart/parser.rb:72:in `fast_forward_to_first_boundary'
2015-11-04T04:36:07.041152+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/multipart/parser.rb:15:in `parse'
2015-11-04T04:36:07.041153+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/multipart.rb:25:in `parse_multipart'
2015-11-04T04:36:07.041154+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/request.rb:377:in `parse_multipart'
2015-11-04T04:36:07.041154+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/request.rb:203:in `POST'
2015-11-04T04:36:07.041155+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/methodoverride.rb:26:in `method_override'
2015-11-04T04:36:07.041156+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/methodoverride.rb:14:in `call'
2015-11-04T04:36:07.041157+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/newrelic_rpm-3.13.2.302/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
2015-11-04T04:36:07.041157+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/runtime.rb:17:in `call'
2015-11-04T04:36:07.041158+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/newrelic_rpm-3.13.2.302/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
2015-11-04T04:36:07.041158+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/rack-rewrite-1.5.0/lib/rack/rewrite.rb:24:in `call'
2015-11-04T04:36:07.041159+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/newrelic_rpm-3.13.2.302/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
2015-11-04T04:36:07.041159+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.5/lib/active_support/cache/strategy/local_cache.rb:83:in `call'
2015-11-04T04:36:07.041160+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/newrelic_rpm-3.13.2.302/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
2015-11-04T04:36:07.041160+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.5/lib/action_dispatch/middleware/static.rb:64:in `call'
2015-11-04T04:36:07.041161+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/newrelic_rpm-3.13.2.302/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
2015-11-04T04:36:07.041161+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/sendfile.rb:112:in `call'
2015-11-04T04:36:07.041161+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/newrelic_rpm-3.13.2.302/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
2015-11-04T04:36:07.041162+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/railties-4.0.5/lib/rails/engine.rb:511:in `call'
2015-11-04T04:36:07.041162+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/railties-4.0.5/lib/rails/application.rb:97:in `call'
2015-11-04T04:36:07.041163+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/railties-4.0.5/lib/rails/railtie/configurable.rb:30:in `method_missing'
2015-11-04T04:36:07.041163+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/newrelic_rpm-3.13.2.302/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
2015-11-04T04:36:07.041163+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/puma-2.14.0/lib/puma/configuration.rb:78:in `call'
2015-11-04T04:36:07.041164+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/puma-2.14.0/lib/puma/server.rb:541:in `handle_request'
2015-11-04T04:36:07.041164+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/puma-2.14.0/lib/puma/server.rb:388:in `process_client'
2015-11-04T04:36:07.041165+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/puma-2.14.0/lib/puma/server.rb:270:in `block in run'
2015-11-04T04:36:07.041165+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/puma-2.14.0/lib/puma/thread_pool.rb:106:in `call'
2015-11-04T04:36:07.041165+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/puma-2.14.0/lib/puma/thread_pool.rb:106:in `block in spawn_thread'
2015-11-04T04:36:07.523877+00:00 heroku[router]: sock=backend at=error code=H18 desc="Server Request Interrupted" method=POST path="/api/v2/attachments" host=<omitted for security> request_id=a5958dab-8c7c-4121-acc3-54d28be9ad4c fwd="<omitted for security>" dyno=web.1 connect=2ms service=484ms status=503 bytes=154

3 个答案:

答案 0 :(得分:2)

你应该使用补丁Rack::Multipart::Parser.fast_forward_to_first_boundary来添加日志跟踪并确认传递给解析器的内容在到达该函数时是空的(即StringIO引用的@env['rack.input']在多部分解析期间不返回任何数据)。

了解更多信息:rack-throwing-eoferror-bad-content-body

答案 1 :(得分:2)

我无法令人满意地解决这个问题,问题的根源似乎是Puma处理多部分/分块数据的方式。由于我们没有使用最新版本,而且此时升级并不容易,因此我已经黑了&#39;解决方案如下。

这里奇怪的是,它似乎是缺少Content-Length标头导致问题 - 尽管HTTP规范说明分块请求不应该有这个标题。

我认为(尽管我可能错了)SSL实现需要Content-Length,这与chunked请求不提供的要求不一致。

现在的解决方案是禁用来自cordova app的分块数据。我们正在使用cordova-file-transfer-plugin,它允许我们将chunked设置为false。

不理想,但确实有效。 来自https://github.com/apache/cordova-plugin-file-transfer

 options.chunkedMode = false;

答案 2 :(得分:1)

您应该检查服务器日志中有关此500错误的说明