<commit_before># frozen-string-literal: true

$LOAD_PATH << File.expand_path("../../lib", __FILE__)
require "plum"
require "openssl"
require "socket"
require "cgi"

begin
  require "sslkeylog/autotrace"
rescue LoadError
end

def log(con, stream, s)
  prefix = "[%02x;%02x] " % [con, stream]
  if s.is_a?(Enumerable)
    puts s.map { |a| prefix + a.to_s }.join("\n")
  else
    puts prefix + s.to_s
  end
end

ctx = OpenSSL::SSL::SSLContext.new
ctx.ssl_version = :TLSv1_2
ctx.alpn_select_cb = -> protocols {
  raise "Client does not support HTTP/2: #{protocols}" unless protocols.include?("h2")
  "h2"
}
if ctx.respond_to?(:tmp_ecdh_callback) && !ctx.respond_to?(:set_ecdh_curves)
  ctx.tmp_ecdh_callback = -> (sock, ise, keyl) {
    OpenSSL::PKey::EC.new("prime256v1")
  }
end
ctx.cert = OpenSSL::X509::Certificate.new File.read(".crt.local")
ctx.key = OpenSSL::PKey::RSA.new File.read(".key.local")
tcp_server = TCPServer.new("0.0.0.0", 40443)
ssl_server = OpenSSL::SSL::SSLServer.new(tcp_server, ctx)

loop do
  begin
    sock = ssl_server.accept
    id = sock.io.fileno
    puts "#{id}: accept! #{sock.cipher.inspect}"
  rescue
    STDERR.puts $!
    next
  end

  plum = Plum::ServerConnection.new(sock.method(:write))

  plum.on(:frame) do |frame|
    log(id, frame.stream_id, "recv: #{frame.inspect}")<commit_msg>examples: static_server: use test certificate in test/<commit_after># frozen-string-literal: true

$LOAD_PATH << File.expand_path("../../lib", __FILE__)
require "plum"
require "openssl"
require "socket"
require "cgi"

begin
  require "sslkeylog/autotrace"
rescue LoadError
end

def log(con, stream, s)
  prefix = "[%02x;%02x] " % [con, stream]
  if s.is_a?(Enumerable)
    puts s.map { |a| prefix + a.to_s }.join("\n")
  else
    puts prefix + s.to_s
  end
end

ctx = OpenSSL::SSL::SSLContext.new
ctx.ssl_version = :TLSv1_2
ctx.alpn_select_cb = -> protocols {
  raise "Client does not support HTTP/2: #{protocols}" unless protocols.include?("h2")
  "h2"
}
if ctx.respond_to?(:tmp_ecdh_callback) && !ctx.respond_to?(:set_ecdh_curves)
  ctx.tmp_ecdh_callback = -> (sock, ise, keyl) {
    OpenSSL::PKey::EC.new("prime256v1")
  }
end
ctx.cert = OpenSSL::X509::Certificate.new File.read(File.expand_path("../../test/server.crt", __FILE__))
ctx.key = OpenSSL::PKey::RSA.new File.read(File.expand_path("../../test/server.key", __FILE__))
tcp_server = TCPServer.new("0.0.0.0", 40443)
ssl_server = OpenSSL::SSL::SSLServer.new(tcp_server, ctx)

loop do
  begin
    sock = ssl_server.accept
    id = sock.io.fileno
    puts "#{id}: accept! #{sock.cipher.inspect}"
  rescue
    STDERR.puts $!
    next
  end

  plum = Plum::ServerConnection.new(sock.method(:write))

  plum.on(:frame) do |frame|
    log(id, frame.stream_id, "recv: #{frame.inspect}")
{'fraction_non_alphanumeric': 0.171563293434893, 'fraction_numerical': 0.018135654697134566, 'mean_word_length': 4.223484848484849, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '5539959', 'n_tokens_mistral': 1103, 'n_tokens_neox': 1077, 'n_words': 213}