Module: WebSocket::ResponseHelpers

Included in:
HttpStaticFileServerHandler
Defined in:
lib/websocket/response_helpers.rb

Overview

The ResponseHelpers helpers

Constant Summary collapse

NON_SPACES_BEFORE_EOL_PATTERN =
%r{([^\s]+)$}

Instance Method Summary collapse

Instance Method Details

#index_listing(path) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/websocket/response_helpers.rb', line 31

def index_listing(path)
  results = `ls -la #{path}`.strip.split("\n")
  results.shift
  index = results.collect do |s|
    s.gsub(NON_SPACES_BEFORE_EOL_PATTERN) do |resource_name|
      %(<a href="#{resource_name}">#{resource_name}</a>)
    end
  end.join '<br />'
  "<html><pre>#{index}</pre></html>"
end

#send_error(ctx, status) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/websocket/response_helpers.rb', line 64

def send_error(ctx, status)
  message = Unpooled.copiedBuffer("#{status}\r\n", CharsetUtil::UTF_8)
  response = DefaultFullHttpResponse.new(HttpVersion::HTTP_1_1, status, message)
  response.headers().set(HttpHeaderNames::CONTENT_TYPE, WebSocket::HtmlContentType)

  # Close the connection as soon as the error message is sent.
  ctx.writeAndFlush(response).addListener(ChannelFutureListener::CLOSE)
end

#send_listing(ctx, path) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/websocket/response_helpers.rb', line 42

def send_listing(ctx, path)
  response = DefaultFullHttpResponse.new(HttpVersion::HTTP_1_1, HttpResponseStatus::OK)
  response.headers().set(HttpHeaderNames::CONTENT_TYPE, WebSocket::HtmlContentType)

  html = index_listing(path)

  buffer = Unpooled.copiedBuffer(html, WebSocket::Encoding)
  response.content().writeBytes(buffer)
  buffer.release()

  # Close the connection as soon as the error message is sent.
  ctx.writeAndFlush(response).addListener(ChannelFutureListener::CLOSE)
end

#send_not_modified(ctx, date) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/websocket/response_helpers.rb', line 73

def send_not_modified(ctx, date)
  response = DefaultFullHttpResponse.new(HttpVersion::HTTP_1_1, HttpResponseStatus::NOT_MODIFIED)
  date_header(response, date)

  # Close the connection as soon as the error message is sent.
  ctx.writeAndFlush(response).addListener(ChannelFutureListener::CLOSE)
end

#send_redirect(ctx, redirect_to_uri) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/websocket/response_helpers.rb', line 56

def send_redirect(ctx, redirect_to_uri)
  response = DefaultFullHttpResponse.new(HttpVersion::HTTP_1_1, HttpResponseStatus::FOUND)
  response.headers().set(HttpHeaderNames::LOCATION, redirect_to_uri)

  # Close the connection as soon as the error message is sent.
  ctx.writeAndFlush(response).addListener(ChannelFutureListener::CLOSE)
end