Sha256: a2dbbf580acb69ae977eb80a32caa43e324987f0f8da3e4c0e7f0ee05bfd63c1

Contents?: true

Size: 1.88 KB

Versions: 56

Compression:

Stored size: 1.88 KB

Contents

require 'openssl'
require 'socket'
require 'thread'

# NOTE: This code is garbage.  It probably has deadlocks, it might leak
# threads, and otherwise cause problems in a real system.  It's really only
# intended for testing HTTParty.
class SSLTestServer
  attr_accessor :ctx    # SSLContext object
  attr_reader :port

  def initialize(options={})
    @ctx             = OpenSSL::SSL::SSLContext.new
    @ctx.cert        = OpenSSL::X509::Certificate.new(options[:cert])
    @ctx.key         = OpenSSL::PKey::RSA.new(options[:rsa_key])
    @ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE   # Don't verify client certificate
    @port            = options[:port] || 0
    @thread          = nil
    @stopping_mutex  = Mutex.new
    @stopping        = false
  end

  def start
    @raw_server = TCPServer.new(@port)

    if @port == 0
      @port = Socket::getnameinfo(@raw_server.getsockname, Socket::NI_NUMERICHOST|Socket::NI_NUMERICSERV)[1].to_i
    end

    @ssl_server = OpenSSL::SSL::SSLServer.new(@raw_server, @ctx)

    @stopping_mutex.synchronize{
      return if @stopping
      @thread = Thread.new{ thread_main }
    }

    nil
  end

  def stop
    @stopping_mutex.synchronize{
      return if @stopping
      @stopping = true
    }
    @thread.join
  end

  private

    def thread_main
      until @stopping_mutex.synchronize{ @stopping }
        (rr,_,_) = select([@ssl_server.to_io], nil, nil, 0.1)

        next unless rr && rr.include?(@ssl_server.to_io)

        socket = @ssl_server.accept

        Thread.new{
          header = []

          until (line = socket.readline).rstrip.empty?
            header << line
          end

          response =<<EOF
HTTP/1.1 200 OK
Connection: close
Content-Type: application/json; charset=UTF-8

{"success":true}
EOF

          socket.write(response.gsub(/\r\n/n, "\n").gsub(/\n/n, "\r\n"))
          socket.close
      }
      end

      @ssl_server.close
    end
end

Version data entries

56 entries across 55 versions & 8 rubygems

Version Path
alloy_sdk-0.1.0 vendor/bundle/ruby/2.6.0/gems/httparty-0.13.1/spec/support/ssl_test_server.rb
alloy_sdk-0.1.0 vendor/bundle/ruby/3.0.0/gems/httparty-0.13.1/spec/support/ssl_test_server.rb
httsoiree-0.13.3 spec/support/ssl_test_server.rb
allegro_api_client-0.0.9 gems/ruby/2.1.0/gems/httparty-0.13.1/spec/support/ssl_test_server.rb
httparty-0.13.3 spec/support/ssl_test_server.rb
allegro_api_client-0.0.8 gems/ruby/2.1.0/gems/httparty-0.13.1/spec/support/ssl_test_server.rb
allegro_api_client-0.0.7 gems/ruby/2.1.0/gems/httparty-0.13.1/spec/support/ssl_test_server.rb
httparty-0.13.2 spec/support/ssl_test_server.rb
asana2flowdock-1.0.0 vendor/bundle/ruby/1.9.1/gems/httparty-0.13.1/spec/support/ssl_test_server.rb
allegro_api_client-0.0.6 gems/ruby/2.1.0/gems/httparty-0.13.1/spec/support/ssl_test_server.rb
allegro_api_client-0.0.5 gems/ruby/2.1.0/gems/httparty-0.13.1/spec/support/ssl_test_server.rb
allegro_api_client-0.0.4 gems/ruby/2.1.0/gems/httparty-0.13.1/spec/support/ssl_test_server.rb
allegro_api_client-0.0.3 gems/ruby/2.1.0/gems/httparty-0.13.1/spec/support/ssl_test_server.rb
allegro_api_client-0.0.2 gems/ruby/2.1.0/gems/httparty-0.13.1/spec/support/ssl_test_server.rb
allegro_api_client-0.0.1 gems/ruby/2.1.0/gems/httparty-0.13.1/spec/support/ssl_test_server.rb
httsoiree-0.13.1.1 spec/support/ssl_test_server.rb
httsoiree-0.13.1 spec/support/ssl_test_server.rb
httparty-enterprise-edition-0.13.1.1 spec/support/ssl_test_server.rb
httparty-enterprise-edition-0.13.1 spec/support/ssl_test_server.rb
httparty-0.13.1 spec/support/ssl_test_server.rb