<commit_before>require 'net/http'

class Webmention
  class UnexpectedResponse < StandardError; end

  def deliver(source, target)
    uri = discover(target)

    request = Net::HTTP::Post.new(uri.request_uri, 'User-Agent' => user_agent)
    request.set_form_data(source: source, target: target)
    client(uri).request(request)
  end

  def discover(url)
    head, final_url = fetch(url, :head)
    endpoint = discover_from_page(head)

    unless endpoint
      page, final_url = fetch(url, :get)
      endpoint = discover_from_page(page)
    end

    return URI.join(final_url, endpoint) if endpoint
  end

  def discover_from_page(page)
    endpoint = endpoint_from_headers(page)

    return endpoint if endpoint
    return unless page.body

    document = Nokogiri::HTML(page.body)
    attribute_selector = '[rel~="webmention"][href]'
    document.css("link#{attribute_selector}, a#{attribute_selector}").first.try(:[], :href)
  end

  def fetch(url, method = :get, redirect_limit: 10)
    uri = URI.parse(url)
    (1 + redirect_limit).times do
      response = simple_request(uri, method)
<commit_msg>Fixed webmention delivery when there is no webmention endpoint<commit_after>require 'net/http'

class Webmention
  class UnexpectedResponse < StandardError; end

  def deliver(source, target)
    uri = discover(target)

    if uri
      request = Net::HTTP::Post.new(uri.request_uri, 'User-Agent' => user_agent)
      request.set_form_data(source: source, target: target)
      client(uri).request(request)
    end
  end

  def discover(url)
    head, final_url = fetch(url, :head)
    endpoint = discover_from_page(head)

    unless endpoint
      page, final_url = fetch(url, :get)
      endpoint = discover_from_page(page)
    end

    return URI.join(final_url, endpoint) if endpoint
  end

  def discover_from_page(page)
    endpoint = endpoint_from_headers(page)

    return endpoint if endpoint
    return unless page.body

    document = Nokogiri::HTML(page.body)
    attribute_selector = '[rel~="webmention"][href]'
    document.css("link#{attribute_selector}, a#{attribute_selector}").first.try(:[], :href)
  end

  def fetch(url, method = :get, redirect_limit: 10)
    uri = URI.parse(url)
    (1 + redirect_limit).times do
      response = simple_request(uri, method)

{'fraction_non_alphanumeric': 0.1111111111111111, 'fraction_numerical': 0.0026246719160104987, 'mean_word_length': 3.907725321888412, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1265904', 'n_tokens_mistral': 730, 'n_tokens_neox': 711, 'n_words': 169}