Method: NicInfo::Main#get

Defined in:
lib/nicinfo/nicinfo_main.rb

#get(url, try, expect_rdap = true) ⇒ Object

Do an HTTP GET with the path.



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/nicinfo/nicinfo_main.rb', line 316

def get url, try, expect_rdap = true

  data = @cache.get(url)
  if data == nil

    @config.logger.trace("Issuing GET for " + url)
    uri = URI.parse( URI::encode( url ) )
    req = Net::HTTP::Get.new(uri.request_uri)
    req["User-Agent"] = NicInfo::VERSION_LABEL
    req["Accept"] = NicInfo::RDAP_CONTENT_TYPE + ", " + NicInfo::JSON_CONTENT_TYPE
    req["Connection"] = "close"
    http = Net::HTTP.new( uri.host, uri.port )
    if uri.scheme == "https"
      http.use_ssl=true
      http.verify_mode=OpenSSL::SSL::VERIFY_NONE
    end

    begin
      res = http.start do |http_req|
        http_req.request(req)
      end
    rescue OpenSSL::SSL::SSLError => e
      if @config.config[ NicInfo::SECURITY ][ NicInfo::TRY_INSECURE ]
        @config.logger.mesg( "Secure connection failed. Trying insecure connection." )
        uri.scheme = "http"
        return get( uri.to_s, try, expect_rdap )
      else
        raise e
      end
    end

    case res
      when Net::HTTPSuccess
        content_type = res[ "content-type" ].downcase
        if expect_rdap
          unless content_type.include?(NicInfo::RDAP_CONTENT_TYPE) or content_type.include?(NicInfo::JSON_CONTENT_TYPE)
            raise Net::HTTPServerException.new("Bad Content Type", res)
          end
          if content_type.include? NicInfo::JSON_CONTENT_TYPE
            @config.conf_msgs << "Server responded with non-RDAP content type but it is JSON"
          end
        end
        data = res.body
        @cache.create_or_update(url, data)
      else
        if res.code == "301" or res.code == "302" or res.code == "303" or res.code == "307" or res.code == "308"
          res.error! if try >= 5
          location = res["location"]
          return get( location, try + 1, expect_rdap)
        end
        res.error!
    end #end case

  end #end if

  return data

end