Tag: HTTP

Padrino + Thin Web Server – uninitialized constant Thin::HttpParser (NameError)

New server, new thin, new error while executing:

Padrino/0.9.19 has taken the stage production on 3001
Thin web server (v1.2.8 codename Black Keys)
Maximum connections set to 1024
Listening on localhost:3001, CTRL+C to stop
`initialize': uninitialized constant Thin::HttpParser (NameError)
/thin-1.2.8/lib/thin/connection.rb:35:in `new'
/thin-1.2.8/lib/thin/connection.rb:35:in `post_init'
/eventmachine-0.12.10/lib/em/connection.rb:45:in `new'
/eventmachine-0.12.10/lib/em/connection.rb:36:in `instance_eval'
/eventmachine-0.12.10/lib/em/connection.rb:36:in `new'
/eventmachine-0.12.10/lib/eventmachine.rb:1430:in `event_callback'

Error occurred because there were some old (incompatible) native C extensions after OS upgrade. To fix this just reinstall Thin:

gem uninstall thin
gem install thin

If Thin version changed, you'll also need to run:

bundle install

Ruby – Sprawdzanie dostępności linku ze znakami z UTF-8

Przed kilkoma dniami natrafiłem na pewien problem. Mianowicie open-uri i metoda open nie działają dla znaków z utf-8 (czyli między innymi dla stron gdzie fragment URLa ma "ogonki"). W związku z czym, taki fragment kodu będzie zawsze zwracał fałsz, mimo tego, że pod adresem istnieje strona:

# coding: utf-8
# Wesja rzecz jasna uproszczona - bez obsługi timeoutów itd
url = 'http://www.nina.gov.pl/nina/czołówka'
io_thing = open(url)
correct = io_thing.status[0] == '200'

Zasadniczo bez obsługi błędów będziemy dostawać taki oto wyjątek:

 bad URI(is not URI?): www.nina.gov.pl/nina/czołówka (URI::InvalidURIError)

Rozwiązanie tego problemu może nie jest zbyt eleganckie ale działa:

# coding: utf-8
require 'open-uri'
require 'net/http'

url = 'http://www.nina.gov.pl/nina/czołówka'
url = URI.parse(URI.encode(url))

correct = false
level = 0
while(!correct && level < 10) do
  requst = Net::HTTP.new(url.host, url.port)
  if url.to_s.include?('https')
    requst.use_ssl = true
    requst.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  
  requst.start do |http|
    http = http.head(url.request_uri)
    if http.code == "200"
      correct = true
    else
      url = URI.parse(URI.encode(http.header['location'] ))
    end
    level += 1
  end
end

http.head nie podąża za przekierowaniami więc musimy robić to sami.

Copyright © 2024 Closer to Code

Theme by Anders NorenUp ↑