def handler(self): eol = '\r\n' response = '' sep = '' # TODO: figure out the max possible response line, and set the default # remaining to that amount. check for sep or that amount of data... # its a bit of a sanity check, as this could be attacked. # while not sep: response += (yield None) response, sep, data = response.partition(eol) checkError(response) response = response.split(' ') word = response.pop(0) resp = self.lookup.get(word, None) # sanity checks if not resp: errstr = "Response was: %s %s" % (word, ' '.join(response)) elif len(response) != len(resp.args): errstr = "Response %s had wrong # args, got %s (expected %s)" errstr %= (word, response, resp.args) else: # all good errstr = '' if errstr: raise errors.UnexpectedResponse(errstr) reply = dict(izip(resp.args, imap(intit, response))) reply['state'] = str(resp) if not resp.hasData: self.remaining = 0 yield reply return self.remaining = (reply['bytes'] + 2) - len(data) while self.remaining > 0: newdata = (yield None) self.remaining -= len(newdata) data += newdata if not data.endswith(eol) or not (len(data) == reply['bytes']+2): raise errors.ExpectedCrlf('Data not properly sent from server') reply['data'] = resp.parsefunc(data.rstrip(eol)) yield reply return
def handler(self): eol = '\r\n' response = '' sep = '' # TODO: figure out the max possible response line, and set the default # remaining to that amount. check for sep or that amount of data... # its a bit of a sanity check, as this could be attacked. # while not sep: response += (yield None) response, sep, data = response.partition(eol) checkError(response) response = response.split(' ') word = response.pop(0) resp = self.lookup.get(word, None) # sanity checks if not resp: errstr = "Response was: %s %s" % (word, ' '.join(response)) elif len(response) != len(resp.args): errstr = "Response %s had wrong # args, got %s (expected %s)" errstr %= (word, response, args) else: # all good errstr = '' if errstr: raise errors.UnexpectedResponse(errstr) reply = dict(izip(resp.args, imap(intit, response))) reply['state'] = str(resp) if not resp.hasData: self.remaining = 0 yield reply return self.remaining = (reply['bytes'] + 2) - len(data) while self.remaining > 0: newdata = (yield None) self.remaining -= len(newdata) data += newdata if not data.endswith(eol) or not (len(data) == reply['bytes']+2): raise errors.ExpectedCrlf('Data not properly sent from server') reply['data'] = resp.parsefunc(data.rstrip(eol)) yield reply return
def handler(self): eol = '\r\n' response = '' sep = '' while not sep: response += (yield None) response, sep, data = response.partition(eol) checkError(response) response = response.split(' ') word = response.pop(0) args, state = self.lookup.get(word, ([],'')) # sanity checks if not state: errstr = "Repsonse was: %s %s" % (word, ' '.join(response)) elif len(response) != len(args): errstr = "Repsonse %s had wrong # args, got %s (expected %s)" errstr %= (word, response, args) else: # all good errstr = '' if errstr: raise errors.UnexpectedResponse(errstr) reply = dict(itertools.izip(args, itertools.imap(intit, response))) reply['state'] = state if not self.has_data: self.remaining = 0 yield reply return self.remaining = (reply['bytes'] + 2) - len(data) while self.remaining > 0: newdata = (yield None) self.remaining -= len(newdata) data += newdata if not data.endswith(eol) or not (len(data) == reply['bytes']+2): raise errors.ExpectedCrlf('Data not properly sent from server') reply['data'] = self.parse(data.rstrip(eol)) yield reply return