def get(self, socket, request): name = '' mimetype = b'' for header in request.header_data: print(header) if isinstance(header, headers.Name): name = header.decode().strip('\x00') print("Receiving request for %s" % name) elif isinstance(header, headers.Type): mimetype = header.decode().strip(b'\x00') print("Type %s" % mimetype) path = os.path.abspath(os.path.join(self.cur_directory, name)) if not path.startswith(self.directory): self._reject(socket) return if os.path.isdir(path) or mimetype == b'x-bt/vcard-listing': try: listing = open(path + "/listing.xml", 'rb') except IOError: sys.stderr.write("failed to open listing for %s" % path) self._reject(socket) return s = listing.read() listing.close() response = responses.Success() response_headers = [headers.Name(name), headers.Length(len(s))] + \ gen_body_headers(s, self._max_length() - 50) self.send_response(socket, response, response_headers) elif os.path.isfile(path): try: fd = open(path, 'rb') except IOError: sys.stderr.write("failed to open vcard %s" % path) self._reject(socket) return s = fd.read() fd.close() response = responses.Success() response_headers = [headers.Name(name), headers.Length(len(s))] + \ gen_body_headers(s, self._max_length() - 50) self.send_response(socket, response, response_headers) else: self._reject(socket)
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 put(self, socket, request): name = "" length = 0 body = b'' mimetype = b'' print("\nput") while True: for header in request.header_data: if isinstance(header, headers.Name): name = header.decode() print("Receiving", name) elif isinstance(header, headers.Length): length = header.decode() print("Length", length) elif isinstance(header, headers.Body): body += header.decode() elif isinstance(header, headers.End_Of_Body): body += header.decode() elif isinstance(header, headers.Type): mimetype = header.decode().strip(b'\x00') print("Type %s" % mimetype) if request.is_final(): break # Ask for more data. self.send_response(socket, responses.Continue()) # Get the next part of the data. request = self.request_handler.decode(socket) resp_headers = [] if mimetype == b'x-bt/MAP-event-report': print("MAP event", body) elif mimetype == b'x-bt/MAP-NotificationRegistration': print("MAP register for notifications") elif mimetype == b'x-bt/messageStatus': print("set message status") elif mimetype == b'x-bt/message': name = name.strip('\x00') name = os.path.split(name)[-1] path = os.path.join(self.cur_directory, name) path = os.path.join(path, gen_handle()) path = os.path.abspath(path) print("Push message", repr(path)) open(path, "wb").write(body) resp_headers.append(headers.Name(name)) elif mimetype == b'x-bt/MAP-messageUpdate': print("MAP inbox update requested") self.send_response(socket, responses.Success(), resp_headers)
def delete(self, name, header_list=()): """delete(self, name, header_list = ()) Requests the deletion of the file with the specified name from the current directory. Raises an OBEXError with the response if there is an error. """ header_list = [headers.Name(name)] + list(header_list) max_length = self.remote_info.max_packet_length request = requests.Put_Final() response = self._send_headers(request, header_list, max_length) if not isinstance(response, responses.Success): raise OBEXError(response)
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
def setpath(self, name="", create_dir=False, to_parent=False, header_list=()): """setpath(self, name = "", create_dir = False, to_parent = False, header_list = ()) Requests a change to the server's current directory for the session to the directory with the specified name. Raises an exception with the response if the response was unexpected. This method is also used to perform other actions, such as navigating to the parent directory (set to_parent to True) and creating a new directory (set create_dir to True). Additional headers can be sent by passing a sequence as the header_list keyword argument. These will be sent after the name information. """ header_list = list(header_list) if name is not None: header_list = [headers.Name(name)] + header_list max_length = self.remote_info.max_packet_length flags = 0 if not create_dir: flags |= requests.Set_Path.DontCreateDir if to_parent: flags |= requests.Set_Path.NavigateToParent request = requests.Set_Path((flags, 0)) response = self._send_headers(request, header_list, max_length) if not isinstance(response, responses.Success): raise OBEXError(response)
def _get(self, name=None, header_list=()): header_list = list(header_list) if name is not None: header_list = [headers.Name(name)] + header_list max_length = self.remote_info.max_packet_length request = requests.Get() response = self._send_headers(request, header_list, max_length) yield response if not (isinstance(response, responses.Continue) or isinstance(response, responses.Success)): return # Retrieve the file data. file_data = [] request = requests.Get_Final() while isinstance(response, responses.Continue): self.socket.sendall(request.encode()) response = self.response_handler.decode(self.socket) yield response