Class: Maze::TerminatingServer

Inherits:
Object
  • Object
show all
Defined in:
lib/maze/terminating_server.rb

Overview

Receives and terminates network connections without reading any data

Constant Summary collapse

CONTINUE_RESPONSE =
"HTTP/1.1 100 CONTINUE\n\r"
BAD_REQUEST_RESPONSE =
"HTTP/1.1 400 BAD REQUEST\n\r"

Class Method Summary collapse

Class Method Details

.max_received_sizeInteger

The maximum string length to be received before disconnecting

Returns:

  • (Integer)

    The string length, defaults to 1MB



54
55
56
# File 'lib/maze/terminating_server.rb', line 54

def max_received_size
  @max_received_size ||= 1048576
end

.max_received_size=(new_max_size) ⇒ Object

Set the maximum string length to be received before disconnecting

Parameters:

  • new_max_size (Integer)

    The new maximum size



61
62
63
# File 'lib/maze/terminating_server.rb', line 61

def max_received_size=(new_max_size)
  @max_received_size = new_max_size
end

.received_request_countObject

Outputs the amount of times the server has received a connection on the last run



93
94
95
# File 'lib/maze/terminating_server.rb', line 93

def received_request_count
  @received_requests ||= 0
end

.reset_elementsObject

Resets the response string to “400/BAD REQUEST” and the read size to 1MB



80
81
82
83
# File 'lib/maze/terminating_server.rb', line 80

def reset_elements
  @response = BAD_REQUEST_RESPONSE
  @max_received_size = 1048576
end

.responseString

The response string sent to a connected client

Returns:

  • (String)

    The response string, defaults to “400/BAD REQUEST”



68
69
70
# File 'lib/maze/terminating_server.rb', line 68

def response
  @response ||= BAD_REQUEST_RESPONSE
end

.response=(new_response) ⇒ Object

Set the response string to an arbitrary value

Parameters:

  • new_response (String)

    The new response



75
76
77
# File 'lib/maze/terminating_server.rb', line 75

def response=(new_response)
  @response = new_response
end

.running?Boolean

Whether the server thread is running

Returns:

  • (Boolean)

    If the server is running



88
89
90
# File 'lib/maze/terminating_server.rb', line 88

def running?
  @thread&.alive?
end

.startObject

Starts the socket accept loop in a separate thread



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/maze/terminating_server.rb', line 14

def start
  # Only run a single server thread
  return if running?

  attempts = 0
  loop do

    @thread = Thread.new do
      # Reset the received count
      @received_requests = 0

      Socket.tcp_server_loop(Maze.config.null_port) {|socket, _client_addrinfo|
        $logger.info 'Terminating server received request'
        @received_requests += 1
        headers = receive_headers(socket)

        body_length = headers['Content-Length']
        receive_data(socket, body_length) unless body_length.nil?

        end_connection(socket)
      }
    rescue StandardError => e
      $logger.warn "Terminating server error: #{e.message}"
    end

    break if running?

    # Bail out after 3 attempts
    attempts += 1
    raise 'Too many failed attempts to start terminating server' if attempts == 3

    # Failed to start - sleep before retrying
    $logger.info 'Retrying in 3 seconds'
    sleep 1
  end
end

.stopObject

Stops the socket accept loop if alive



98
99
100
101
# File 'lib/maze/terminating_server.rb', line 98

def stop
  @thread&.kill if @thread&.alive?
  @thread = nil
end