Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/rex/proto/nuuo/response.rb
Views: 11704
# -*- coding:binary -*-12module Rex3module Proto4module Nuuo5class Response67module ParseCode8Completed = 19Partial = 210Error = 311end1213module ParseState14ProcessingHeader = 115ProcessingBody = 216Completed = 317end1819attr_accessor :headers20attr_accessor :body21attr_accessor :protocol22attr_accessor :status_code23attr_accessor :message24attr_accessor :bufq25attr_accessor :state2627def initialize(buf=nil)28self.state = ParseState::ProcessingHeader29self.headers = {}30self.body = ''31self.protocol = nil32self.status_code = nil33self.message = nil34self.bufq = ''35parse(buf) if buf36end3738def to_s39s = ''40return unless self.protocol41s << self.protocol42s << " #{self.status_code}" if self.status_code43s << " #{self.message}" if self.message44s << "\r\n"4546self.headers.each do |k,v|47s << "#{k}: #{v}\r\n"48end4950s << "\r\n#{self.body}"51end5253# returns state of parsing54def parse(buf)55self.bufq << buf5657if self.state == ParseState::ProcessingHeader58parse_header59end6061if self.state == ParseState::ProcessingBody62if self.body_bytes_left == 063self.state = ParseState::Completed64else65parse_body66end67end6869(self.state == ParseState::Completed) ? ParseCode::Completed : ParseCode::Partial70end7172protected73attr_accessor :body_bytes_left7475def parse_header76head,body = self.bufq.split("\r\n\r\n", 2)77return nil unless body7879get_headers(head)80self.bufq = body || ''81self.body_bytes_left = 08283if self.headers['Content-Length']84self.body_bytes_left = self.headers['Content-Length'].to_i85end8687self.state = ParseState::ProcessingBody88end8990def parse_body91return if self.bufq.length == 092if self.body_bytes_left >= 093part = self.bufq.slice!(0, self.body_bytes_left)94self.body << part95self.body_bytes_left -= part.length96else97self.body_bytes_left = 098end99100if self.body_bytes_left == 0101self.state = ParseState::Completed102end103end104105def get_headers(head)106head.each_line.with_index do |l, i|107if i == 0108self.protocol,self.status_code,self.message = l.split(' ', 3)109self.status_code = self.status_code.to_i if self.status_code110next111end112k,v = l.split(':', 2)113self.headers[k] = v.strip114end115end116117end118end119end120end121122123