Class: Wildnet::Server::HttpResponse

Inherits:
DefaultHttpResponse
  • Object
show all
Defined in:
lib/wildnet-server/http_response.rb

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ HttpResponse

Returns a new instance of HttpResponse.



30
31
32
33
34
35
36
37
# File 'lib/wildnet-server/http_response.rb', line 30

def initialize context
  super(HttpVersion::HTTP_1_1, HttpResponseStatus.valueOf(200))
  @buffer       = ChannelBuffers.dynamicBuffer
  @context      = context
  @future       = nil
  @headers_sent = false
  @closed       = false
end

Instance Method Details

#<<(data) ⇒ Object



68
69
70
# File 'lib/wildnet-server/http_response.rb', line 68

def << data
  @buffer.writeBytes(data.to_s.to_java_bytes)
end

#[](name) ⇒ Object



47
48
49
# File 'lib/wildnet-server/http_response.rb', line 47

def [] name
  getHeader(name)
end

#[]=(name, value) ⇒ Object



51
52
53
# File 'lib/wildnet-server/http_response.rb', line 51

def []= name, value
  setHeader(name, value)
end

#close(proc = nil) ⇒ Object



82
83
84
85
# File 'lib/wildnet-server/http_response.rb', line 82

def close proc = nil
  @future.addListener(Finisher.new(proc)) if proc
  @future.addListener(ChannelFutureListener::CLOSE)
end

#flushObject



72
73
74
75
76
77
78
79
80
# File 'lib/wildnet-server/http_response.rb', line 72

def flush
  unless @headers_sent
    @future = @context.channel.write(self)
  end
  if @buffer.writerIndex > 0
    @future = @context.channel.write(@buffer)
    @buffer = ChannelBuffers.dynamicBuffer
  end
end

#send_file(path) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/wildnet-server/http_response.rb', line 55

def send_file path
  if getHeader("Content-Type") == "wildnet/detect"
    ct = ::MIME::Types.type_for(path).first
    self["Content-Type"] =  "text/plain"
    self["Content-Type"] =  ct.content_type if ct
  end
  raf = RandomAccessFile.new(path, "r")
  region = DefaultFileRegion.new(raf.channel, 0, raf.length)
  HttpHeaders.setContentLength(self, raf.length)
  @future = @context.channel.write(self)
  @future = @context.channel.write(region)
end

#statusObject



39
40
41
# File 'lib/wildnet-server/http_response.rb', line 39

def status
  getStatus().getCode()
end

#status=(code) ⇒ Object



43
44
45
# File 'lib/wildnet-server/http_response.rb', line 43

def status= code
  setStatus(HttpResponseStatus.valueOf(code))
end