def get(self, socket, request): name = "" type = "" for header in request.header_data: print(header) if isinstance(header, headers.Name): name = header.decode().strip(b"\x00") print("Receiving request for %s" % name) elif isinstance(header, headers.Type): type = header.decode().strip(b"\x00") print("Type %s" % type) path = os.path.abspath(os.path.join(self.directory, name)) if os.path.isdir(path) or type == "x-obex/folder-listing": if path.startswith(self.directory): s = gen_folder_listing(path) print(s) response = responses.Success() response_headers = [ headers.Name(name.encode("utf8")), headers.Length(len(s)), headers.Body(s.encode("utf8")) ] self.send_response(socket, response, response_headers) else: self._reject(socket) else: self._reject(socket)
def gen_body_headers(data, csize=65500): """Generate a list of body headers (to encapsulate large data)""" hdrs = [] i = 0 while i < len(data): chunk = data[i:i + csize] if len(data) - i > csize: hdrs.append(headers.Body(chunk)) else: hdrs.append(headers.End_Of_Body(chunk)) i += csize return hdrs
def _put(self, name, file_data, header_list=()): header_list = [headers.Name(name), headers.Length(len(file_data))] + list(header_list) max_length = self.remote_info.max_packet_length request = requests.Put() response = self._send_headers(request, header_list, max_length) yield response if not isinstance(response, responses.Continue): return # Send the file data. # The optimum size is the maximum packet length accepted by the # remote device minus three bytes for the header ID and length # minus three bytes for the request. optimum_size = max_length - 3 - 3 i = 0 while i < len(file_data): data = file_data[i:i + optimum_size] i += len(data) if i < len(file_data): request = requests.Put() request.add_header(headers.Body(data, False), max_length) self.socket.sendall(request.encode()) response = self.response_handler.decode(self.socket) yield response if not isinstance(response, responses.Continue): return else: request = requests.Put_Final() request.add_header(headers.End_Of_Body(data, False), max_length) self.socket.sendall(request.encode()) response = self.response_handler.decode(self.socket) yield response if not isinstance(response, responses.Success): return