Module: Pray::Serve

Defined in:
lib/pray/serve.rb

Constant Summary collapse

DEFAULT_MAX_CONNECTIONS =
8

Class Method Summary collapse

Class Method Details

.content_type_for(path) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/pray/serve.rb', line 104

def content_type_for(path)
  case File.extname(path)
  when ".json" then "application/json"
  when ".praypkg" then "application/octet-stream"
  else "text/plain"
  end
end

.dispatch_request(root, method, path, body = "") ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/pray/serve.rb', line 73

def dispatch_request(root, method, path, body = "")
  path = path.split("?", 2).first

  case [method, path]
  when ["GET", "/.well-known/pray-federation.json"]
    return ServeFederation.discovery_response(root)
  when ["GET", "/v1/sync/index"]
    return ServeFederation.index_response(root)
  when ["POST", "/v1/confessions"]
    return ServeFederation.append_confession(root, body)
  end

  if method == "GET" && path.start_with?("/v1/sync/package/")
    return ServeFederation.package_response(root, path)
  end

  return not_found unless method == "GET"

  if path == "/"
    return html_response("<h1>Pray distribution</h1><p>Root: #{root}</p>")
  end

  file_path = PathSafety.join_under_root(root, path.delete_prefix("/"))
  return not_found unless file_path
  return not_found unless File.file?(file_path)

  content_type = content_type_for(file_path)
  file_body = File.binread(file_path)
  ok_response(content_type, file_body)
end

.handle_connection(root, socket) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/pray/serve.rb', line 48

def handle_connection(root, socket)
  request_line = socket.gets
  return unless request_line

  method, path, = request_line.split
  headers = read_headers(socket)
  body_length = headers["content-length"].to_i
  body = body_length.positive? ? socket.read(body_length) : ""

  response = dispatch_request(root, method, path, body)
  socket.print(response)
end

.html_response(body) ⇒ Object



116
117
118
# File 'lib/pray/serve.rb', line 116

def html_response(body)
  ok_response("text/html", body)
end

.not_foundObject



120
121
122
123
# File 'lib/pray/serve.rb', line 120

def not_found
  body = "not found"
  "HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\nContent-Length: #{body.bytesize}\r\nConnection: close\r\n\r\n#{body}"
end

.ok_response(content_type, body) ⇒ Object



112
113
114
# File 'lib/pray/serve.rb', line 112

def ok_response(content_type, body)
  "HTTP/1.1 200 OK\r\nContent-Type: #{content_type}\r\nContent-Length: #{body.bytesize}\r\nConnection: close\r\n\r\n#{body}"
end

.read_headers(socket) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/pray/serve.rb', line 61

def read_headers(socket)
  headers = {}
  loop do
    line = socket.gets
    break if line.nil? || line.strip.empty?

    name, value = line.split(":", 2)
    headers[name.strip.downcase] = value.strip if name && value
  end
  headers
end

.run_server(root:, host: "127.0.0.1", port: 7429, max_connections: DEFAULT_MAX_CONNECTIONS) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/pray/serve.rb', line 16

def run_server(root:, host: "127.0.0.1", port: 7429, max_connections: DEFAULT_MAX_CONNECTIONS)
  root = File.expand_path(root)
  server = TCPServer.new(host, port)
  connection_slots = SizedQueue.new(max_connections)
  max_connections.times { connection_slots << true }
  puts "Serving #{root} on http://#{host}:#{port}"

  loop do
    socket = server.accept
    begin
      connection_slots.pop(true)
    rescue ThreadError
      socket.print(service_unavailable)
      socket.close
      next
    end

    Thread.new { serve_connection(root, socket, connection_slots) }
  rescue Interrupt
    break
  end
ensure
  server&.close
end

.serve_connection(root, socket, connection_slots) ⇒ Object



41
42
43
44
45
46
# File 'lib/pray/serve.rb', line 41

def serve_connection(root, socket, connection_slots)
  handle_connection(root, socket)
ensure
  connection_slots << true unless connection_slots.closed?
  socket.close unless socket.closed?
end

.service_unavailableObject



125
126
127
128
# File 'lib/pray/serve.rb', line 125

def service_unavailable
  body = "too many connections"
  "HTTP/1.1 503 Service Unavailable\r\nContent-Type: text/plain\r\nContent-Length: #{body.bytesize}\r\nConnection: close\r\n\r\n#{body}"
end