lib/archival/helper_server.rb in archival-0.0.8 vs lib/archival/helper_server.rb in archival-0.0.9

- old
+ new

@@ -42,20 +42,27 @@ private MAGIC_GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11' def handle_request(client, req, method, path) - if method == 'GET' && path.start_with?('/js/') - # For static paths, just serve the files they refer to. + if method == 'GET' && path.start_with?('/js/archival-helper.js') + # For this special file, serve it from the helper dir http_response(client, type: 'application/javascript') do - serve_static(client, path) + serve_static(client, path, @helper_dir) end client.close elsif (matches = req.match(/^Sec-WebSocket-Key: (\S+)/)) websocket_key = matches[1] # puts "Websocket handshake detected with key: #{websocket_key}" connect_socket(client, websocket_key) + elsif method == 'GET' + # For static paths, just serve the files they refer to. + # TODO: mime type should be inferred from file type + http_response(client, type: 'application/javascript') do + serve_static(client, path) + end + client.close else client.close end end @@ -97,12 +104,13 @@ fin = first_byte & 0b10000000 opcode = first_byte & 0b00001111 # Our server only supports single-frame, text messages. # Raise an exception if the client tries to send anything else. - raise "We don't support continuations" unless fin - raise 'We only support opcode 1' unless opcode == 1 + raise 'Archival dev server does not support continuations' unless fin + # Some browsers send this regardless, so ignore it to keep the noise down. + return unless opcode == 1 second_byte = @socket.getbyte is_masked = second_byte & 0b10000000 payload_size = second_byte & 0b01111111 @@ -137,20 +145,21 @@ output = [0b10000001, message.size, message] @socket.write output.pack("CCA#{message.size}") end - def serve_static(client, path) - buffer = File.open(File.join(@helper_dir, path)).read + def serve_static(client, path, base = @build_dir) + buffer = File.open(File.join(base, path)).read buffer.sub! '$PORT', @port.to_s client.print buffer end def http_response(client, config) status = config[:status] ||= 200 type = config[:type] ||= 'text/html' client.print "HTTP/1.1 #{status}\r\n" client.print "Content-Type: #{type}\r\n" + client.print "Access-Control-Allow-Origin: *\r\n" client.print "\r\n" yield end end end