简体   繁体   中英

Ruby: Force open-uri to return IPv4 address

In our Rails app, we have a controller action that opens an external URL, and returns it as JSON for our front end to consume.

Today, I got the following error:

Errno::EAFNOSUPPORT: Address family not supported by protocol - socket(2)

Our devops suggested that this would be the error if the address returned an IPv6 response as well as an IPv4 response. We don't support IPv6, so that could explain it.

Here's the method that fires off the request:

def medline_response
  open("http://apps.nlm.nih.gov/medlineplus/services/mpconnect_service.cfm?mainSearchCriteria.v.cs=#{codesys}&mainSearchCriteria.v.c=#{code}")
end

And indeed, the response from that server's host:

$ host apps.nlm.nih.gov
apps.nlm.nih.gov is an alias for apps.wip.nlm.nih.gov.
apps.wip.nlm.nih.gov has address 130.14.16.117
apps.wip.nlm.nih.gov has IPv6 address 2607:f220:41e:1016::117

The docs for open-uri don't say anything about IP version. Is there an argument you can pass into the open method that forces it to read IPv4, or another way to deal with this?

Edit

When I try to open google from the console, I get this:

[18] pry(main)> require 'open-uri'
=> false
[19] pry(main)> open 'http://google.com'   
=> #<File:/var/folders/hq/7_rgkt4565q728y91dtqzs2r2wxb1y/T/open-uri20130913-50258-1af4zhv>

So it doesn't look like the IPv6 address is an issue there.

Edit 2

$ ruby -v
ruby 1.9.3p385 (2013-02-06 revision 39114) [x86_64-darwin11.4.2]

OS X 10.7.5

Edit 3

It's everyone's favorite kind of error: an intermittent one. Here's what happens when I call read on the open-uri object. I get this same output locally and all 3 of our app servers.

[22] pry(main)> open("http://apps.nlm.nih.gov/medlineplus/services/mpconnect_service.cfm?mainSearchCriteria.v.cs=2.16.840.1.113883.6.103&mainSearchCriteria.v.c=238.4").read
=> "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<feed xml:base=\"http://apps.nlm.nih.gov/medlineplus/services/\" xml:lang=\"en\" xmlns=\"http://www.w3.org/2005/Atom\" xmlns:v3=\"urn:hl7-org:v3\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><title type=\"text\">MedlinePlus Connect</title><subtitle type=\"text\">MedlinePlus Connect results for ICD-9-CM 238.4</subtitle><author><name>U.S. National Library of Medicine</name><uri>http://www.nlm.nih.gov</uri></author><updated type=\"text\">2013-09-13T12:09:48Z</updated><category scheme=\"REDS_MT010001UV\" term=\"MATCHED\"><v3:mainSearchCriteria classCode=\"OBS\" moodCode=\"DEF\" xmlns:v3=\"urn:hl7-org:v3\"><v3:code code=\"KSUBJ\" codeSystem=\"2.16.840.1.113883.5.4\" xmlns:v3=\"urn:hl7-org:v3\"/><v3:value code=\"238.4\" codeSystem=\"2.16.840.1.113883.6.103\" displayName=\"\" xmlns:v3=\"urn:hl7-org:v3\"/></v3:mainSearchCriteria><v3:informationRecipient typeCode=\"IRCP\" xmlns:v3=\"urn:hl7-org:v3\"><v3:patient classCode=\"PAT\" xmlns:v3=\"urn:hl7-org:v3\"/></v3:informationRecipient></category><id></id><entry><title>Bone Marrow Diseases</title><link href=\"http://www.nlm.nih.gov/medlineplus/bonemarrowdiseases.html\" rel=\"alternate\"/><id>tag: nlm.nih.gov, 2013-13-09:/medlineplus/bonemarrowdiseases.html</id><updated>2013-09-13T12:09:48Z</updated><summary type=\"html\">&lt;p&gt;Bone marrow is the spongy tissue inside some of your bones, such as your hip and thigh bones. It contains immature cells, called stem cells. The stem cells can develop into the red blood cells that carry oxygen through your body, the white blood cells that fight infections, and the platelets that help with blood clotting. &lt;/p&gt;&#xd;&#xd;&lt;p&gt;If you have a bone marrow disease, there are problems with the stem cells or how they develop. &lt;a href=\"http://www.nlm.nih.gov/medlineplus/leukemia.html\"&gt;Leukemia&lt;/a&gt; is a cancer in which the bone marrow produces abnormal white blood cells. With &lt;a href=\"http://www.nlm.nih.gov/medlineplus/aplasticanemia.html\"&gt;aplastic anemia&lt;/a&gt;, the bone marrow doesn't make red blood cells. Other diseases, such as &lt;a href=\"http://www.nlm.nih.gov/medlineplus/lymphoma.html\"&gt;lymphoma&lt;/a&gt;, can spread into the bone marrow and affect the production of blood cells.  Other causes of bone marrow disorders include your genetic makeup and environmental factors.&lt;/p&gt; &#xd;&#xd;&lt;p&gt;Symptoms of bone marrow diseases vary. Treatments depend on the disorder and how severe it is. They might involve medicines, blood transfusions or a &lt;a href=\"http://www.nlm.nih.gov/medlineplus/bonemarrowtransplantation.html\"&gt;bone marrow transplant&lt;/a&gt;. &lt;/p&gt;&#xd; &lt;p class=\"NLMrelatedLinks\"&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=\"http://www.nlm.nih.gov/medlineplus/ency/article/003658.htm\"&gt;Bone marrow aspiration&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=\"http://www.nlm.nih.gov/medlineplus/ency/article/003934.htm\"&gt;Bone marrow biopsy&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=\"http://www.nlm.nih.gov/medlineplus/ency/article/003682.htm\"&gt;Bone marrow culture&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=\"http://www.nlm.nih.gov/medlineplus/ency/article/003009.htm\"&gt;Bone marrow transplant&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=\"http://www.nlm.nih.gov/medlineplus/ency/patientinstructions/000010.htm\"&gt;Bone marrow transplant - discharge&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=\"http://www.nlm.nih.gov/medlineplus/ency/article/000589.htm\"&gt;Polycythemia vera&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=\"http://www.nlm.nih.gov/medlineplus/ency/article/000531.htm\"&gt;Primary myelofibrosis&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/p&gt;</summary></entry></feed>  \n\n"

That is, it returns the HTML as a string, like you'd expect.

It seems that you need newer ruby, see ruby bug report

You can "force" open-uri to use ip eg:

require 'open-uri'
require 'socket'

url = "google.com"
ip = IPSocket::getaddress(url)

open("http://#{url}") do |f|
  puts "i'm using url #{url} #{f.base_uri}"
end
open("http://#{ip}") do |f|
  puts "i'm using ip #{ip} #{f.base_uri}"
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM