Module: WebSocket::HttpStaticFileServerHandlerInstanceMethods

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

Overview

The HttpStaticFileServerHandlerInstanceMethods module

Constant Summary collapse

FORWARD_SLASH_BEFORE_EOL_PATTERN =
%r{/$}
URI_FORWARD_SLASH_TEMPLATE =
'%<uri>s/'.freeze

Instance Method Summary collapse

Instance Method Details

#exceptionCaught(ctx, cause) ⇒ Object

rubocop: enable Metrics/AbcSize rubocop: enable Metrics/CyclomaticComplexity rubocop: enable Metrics/MethodLength rubocop: enable Metrics/PerceivedComplexity



151
152
153
154
155
# File 'lib/websocket/http_static_file_server_handler_instance_methods.rb', line 151

def exceptionCaught(ctx, cause)
  cause.printStackTrace()
  return unless ctx.channel().isActive()
  send_error(ctx, HttpResponseStatus::INTERNAL_SERVER_ERROR)
end

#messageReceived(ctx, request) ⇒ Object

rubocop: disable Metrics/AbcSize rubocop: disable Metrics/CyclomaticComplexity rubocop: disable Metrics/MethodLength rubocop: disable Metrics/PerceivedComplexity



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/websocket/http_static_file_server_handler_instance_methods.rb', line 46

def messageReceived(ctx, request)
  return if %r{^#{options[:web_socket_path]}$}.match?(request.uri)

  unless request.decoderResult().isSuccess()
    send_error(ctx, HttpResponseStatus::BAD_REQUEST)
    return
  end

  unless request.method() == HttpMethod::GET
    send_error(ctx, HttpResponseStatus::METHOD_NOT_ALLOWED)
    return
  end

  uri = request.uri
  path = sanitize_uri(uri)
  if path.nil?
    send_error(ctx, HttpResponseStatus::FORBIDDEN)
    return
  end

  unless File.exist? path
    send_error(ctx, HttpResponseStatus::NOT_FOUND)
    return
  end

  if File.directory? path
    if FORWARD_SLASH_BEFORE_EOL_PATTERN.match?(uri)
      send_listing(ctx, path)
    else
      send_redirect(ctx, format(URI_FORWARD_SLASH_TEMPLATE, uri: uri))
    end
    return
  end

  unless File.exist? path
    send_error(ctx, HttpResponseStatus::FORBIDDEN)
    return
  end

  # Cache Validation
  modified_since = request.headers().get(HttpHeaderNames::IF_MODIFIED_SINCE)
  if !modified_since.nil? && !modified_since.empty?
    file_last_modified = File.mtime(path).to_s
    # Only compare up to the second because the format of the timestamp
    # sent to the client does not include milliseconds
    modified_since_seconds = DateTime.parse(modified_since).to_time.to_i
    file_last_modified_seconds = DateTime.parse(file_last_modified).to_time.to_i

    if modified_since_seconds == file_last_modified_seconds
      send_not_modified(ctx, file_last_modified_seconds)
      return
    end
  end

  raf = nil
  begin
    raf = RandomAccessFile.new(path, 'r')
  rescue StandardError => _e
    send_error(ctx, HttpResponseStatus::NOT_FOUND)
    return
  end
  file_length = raf.length

  response = DefaultHttpResponse.new(HttpVersion::HTTP_1_1, HttpResponseStatus::OK)
  HttpUtil.setContentLength(response, file_length)
  content_type_header(response, guess_content_type(path))
  date_and_cache_headers(response, path)
  keep_alive_header(response) if HttpUtil.isKeepAlive(request)

  # Write the initial line and the header.
  ctx.write(response)

  send_file_future = nil
  last_content_future = nil
  progressive_promise = ctx.newProgressivePromise()

  # Write the content.
  if ctx.pipeline().get(SslHandler.java_class)
    # SSL enabled - cannot use zero-copy file transfer.
    chunked_file = ChunkedFile.new(raf, 0, file_length, 8192)
    chunked_input = HttpChunkedInput.new(chunked_file)
    send_file_future = ctx.writeAndFlush(chunked_input, progressive_promise)
    # HttpChunkedInput will write the end marker (LastHttpContent) for us.
    last_content_future = send_file_future
  else
    # SSL not enabled - can use zero-copy file transfer.
    file_region = DefaultFileRegion.new(raf.channel, 0, file_length)
    send_file_future = ctx.write(file_region, progressive_promise)
    # Write the end marker.
    last_content_future = ctx.writeAndFlush(LastHttpContent::EMPTY_LAST_CONTENT)
  end

  send_file_future.addListener(FileServerChannelProgressiveFutureListener.new)

  # Decide whether to close the connection or not.
  return if HttpUtil.isKeepAlive(request)

  # Close the connection when the whole content is written out.
  last_content_future.addListener(ChannelFutureListener::CLOSE)
end