Module: Wpxf::Net::HttpServer

Includes:
Wpxf
Included in:
WordPress::Xss
Defined in:
lib/wpxf/net/http_server.rb

Overview

Provides basic, single threaded HTTP server functionality.

Instance Method Summary collapse

Instance Method Details

#http_server_bind_addressString

Returns the address the HTTP server is bound to.

Returns:

  • (String)

    the address the HTTP server is bound to.



33
34
35
# File 'lib/wpxf/net/http_server.rb', line 33

def http_server_bind_address
  normalized_option_value('http_server_bind_address')
end

#http_server_bind_portInteger

Returns the port the HTTP server is listening on.

Returns:

  • (Integer)

    the port the HTTP server is listening on.



38
39
40
# File 'lib/wpxf/net/http_server.rb', line 38

def http_server_bind_port
  normalized_option_value('http_server_bind_port')
end

#http_server_threadThread

Returns thread that the server runs on when in non-blocking mode.

Returns:

  • (Thread)

    thread that the server runs on when in non-blocking mode.



98
99
100
# File 'lib/wpxf/net/http_server.rb', line 98

def http_server_thread
  @http_server_thread
end

#initializeObject

Initialize a new instance of Wpxf::Net::HttpServer.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/wpxf/net/http_server.rb', line 11

def initialize
  super

  register_options([
    StringOption.new(
      name: 'http_server_bind_address',
      desc: 'Address to bind the HTTP server to',
      default: '0.0.0.0',
      required: true
    ),
    PortOption.new(
      name: 'http_server_bind_port',
      desc: 'Port for the HTTP server to listen on',
      default: 80,
      required: true
    )
  ])

  @http_server_kill_switch = false
end

#js_ajax_downloadString

Returns the AJAX download helper script.

Returns:

  • (String)

    the AJAX download helper script.



54
55
56
# File 'lib/wpxf/net/http_server.rb', line 54

def js_ajax_download
  File.read(File.join(Wpxf.data_directory, 'js', 'ajax_download.js'))
end

#js_ajax_postString

Returns the AJAX post helper script.

Returns:

  • (String)

    the AJAX post helper script.



59
60
61
# File 'lib/wpxf/net/http_server.rb', line 59

def js_ajax_post
  File.read(File.join(Wpxf.data_directory, 'js', 'ajax_post.js'))
end

#js_postString

Returns the JS post helper script.

Returns:

  • (String)

    the JS post helper script.



64
65
66
# File 'lib/wpxf/net/http_server.rb', line 64

def js_post
  File.read(File.join(Wpxf.data_directory, 'js', 'post.js'))
end

#on_http_request(path, params, headers) ⇒ String, Hash

Invoked when a HTTP request is made to the server.

Parameters:

  • path (String)

    the path requested.

  • params (Hash)

    the query string parameters.

  • headers (Hash)

    the HTTP headers.

Returns:

  • (String, Hash)

    if a string is returned, it will be used as the response body to send to the client. If a hash is returned, it should contain the keys:

    • :body - the body text of the response.

    • :type - the MIME type of the response.

    • :headers - an array of header strings.



51
# File 'lib/wpxf/net/http_server.rb', line 51

def on_http_request(path, params, headers) end

#start_http_server(non_block = false) ⇒ Object

Start the HTTP server.

Parameters:

  • non_block (Boolean) (defaults to: false)

    run the server in non-blocking mode.



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/wpxf/net/http_server.rb', line 70

def start_http_server(non_block = false)
  @tcp_server = TCPServer.new(http_server_bind_address, http_server_bind_port)
  emit_info "Started HTTP server on #{http_server_bind_address}:"\
            "#{http_server_bind_port}"

  if non_block
    @http_server_thread = Thread.new do
      _http_server_loop
    end
  else
    _http_server_loop
  end
end

#stop_http_serverObject

Stop the HTTP server after it has finished processing the current request.



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/wpxf/net/http_server.rb', line 85

def stop_http_server
  return false if @is_stopping
  @is_stopping = true

  emit_info 'Stopping HTTP server...', true
  @http_server_kill_switch = true
  @http_server_thread&.exit
  @tcp_server.close if !@tcp_server.nil? && !@tcp_server.closed?
  emit_info 'HTTP server stopped'
  @is_stopping = false
end