Class: Rex::Proto::Nuuo::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/rex/proto/nuuo/response.rb

Defined Under Namespace

Modules: ParseCode, ParseState

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buf = nil) ⇒ Response

Returns a new instance of Response.


28
29
30
31
32
33
34
35
36
37
# File 'lib/rex/proto/nuuo/response.rb', line 28

def initialize(buf=nil)
  self.state = ParseState::ProcessingHeader
  self.headers = {}
  self.body = ''
  self.protocol = nil
  self.status_code = nil
  self.message = nil
  self.bufq = ''
  parse(buf) if buf
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body


21
22
23
# File 'lib/rex/proto/nuuo/response.rb', line 21

def body
  @body
end

#body_bytes_leftObject (protected)

Returns the value of attribute body_bytes_left


74
75
76
# File 'lib/rex/proto/nuuo/response.rb', line 74

def body_bytes_left
  @body_bytes_left
end

#bufqObject

Returns the value of attribute bufq


25
26
27
# File 'lib/rex/proto/nuuo/response.rb', line 25

def bufq
  @bufq
end

#headersObject

Returns the value of attribute headers


20
21
22
# File 'lib/rex/proto/nuuo/response.rb', line 20

def headers
  @headers
end

#messageObject

Returns the value of attribute message


24
25
26
# File 'lib/rex/proto/nuuo/response.rb', line 24

def message
  @message
end

#protocolObject

Returns the value of attribute protocol


22
23
24
# File 'lib/rex/proto/nuuo/response.rb', line 22

def protocol
  @protocol
end

#stateObject

Returns the value of attribute state


26
27
28
# File 'lib/rex/proto/nuuo/response.rb', line 26

def state
  @state
end

#status_codeObject

Returns the value of attribute status_code


23
24
25
# File 'lib/rex/proto/nuuo/response.rb', line 23

def status_code
  @status_code
end

Instance Method Details

#get_headers(head) ⇒ Object (protected)


106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rex/proto/nuuo/response.rb', line 106

def get_headers(head)
  head.each_line.with_index do |l, i|
    if i == 0
      self.protocol,self.status_code,self.message = l.split(' ', 3)
      self.status_code = self.status_code.to_i if self.status_code
      next
    end
    k,v = l.split(':', 2)
    self.headers[k] = v.strip
  end
end

#parse(buf) ⇒ Object

returns state of parsing


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rex/proto/nuuo/response.rb', line 55

def parse(buf)
  self.bufq << buf

  if self.state == ParseState::ProcessingHeader
    parse_header
  end

  if self.state == ParseState::ProcessingBody
    if self.body_bytes_left == 0
      self.state = ParseState::Completed
    else
      parse_body
    end
  end

  (self.state == ParseState::Completed) ? ParseCode::Completed : ParseCode::Partial
end

#parse_bodyObject (protected)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rex/proto/nuuo/response.rb', line 91

def parse_body
  return if self.bufq.length == 0
  if self.body_bytes_left >= 0
    part = self.bufq.slice!(0, self.body_bytes_left)
    self.body << part
    self.body_bytes_left -= part.length
  else
    self.body_bytes_left = 0
  end

  if self.body_bytes_left == 0
    self.state = ParseState::Completed
  end
end

#parse_headerObject (protected)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rex/proto/nuuo/response.rb', line 76

def parse_header
  head,body = self.bufq.split("\r\n\r\n", 2)
  return nil unless body

  get_headers(head)
  self.bufq = body || ''
  self.body_bytes_left = 0

  if self.headers['Content-Length']
    self.body_bytes_left = self.headers['Content-Length'].to_i
  end

  self.state = ParseState::ProcessingBody
end

#to_sObject


39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rex/proto/nuuo/response.rb', line 39

def to_s
  s = ''
  return unless self.protocol
  s << self.protocol
  s << " #{self.status_code}" if self.status_code
  s << " #{self.message}" if self.message
  s << "\r\n"

  self.headers.each do |k,v|
    s << "#{k}: #{v}\r\n"
  end

  s << "\r\n#{self.body}"
end