Class: Maze::TerminatingServer
- Inherits:
-
Object
- Object
- Maze::TerminatingServer
- 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
-
.max_received_size ⇒ Integer
The maximum string length to be received before disconnecting.
-
.max_received_size=(new_max_size) ⇒ Object
Set the maximum string length to be received before disconnecting.
-
.received_request_count ⇒ Object
Outputs the amount of times the server has received a connection on the last run.
-
.reset_elements ⇒ Object
Resets the response string to “400/BAD REQUEST” and the read size to 1MB.
-
.response ⇒ String
The response string sent to a connected client.
-
.response=(new_response) ⇒ Object
Set the response string to an arbitrary value.
-
.running? ⇒ Boolean
Whether the server thread is running.
-
.start ⇒ Object
Starts the socket accept loop in a separate thread.
-
.stop ⇒ Object
Stops the socket accept loop if alive.
Class Method Details
.max_received_size ⇒ Integer
The maximum string length to be received before disconnecting
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
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_count ⇒ Object
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_elements ⇒ Object
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 |
.response ⇒ String
The response string sent to a connected client
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
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
88 89 90 |
# File 'lib/maze/terminating_server.rb', line 88 def running? @thread&.alive? end |
.start ⇒ Object
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.}" 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 |
.stop ⇒ Object
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 |