# Buggy code
def render_PUT(self, request):
    """
    .. http:put:: /downloads

    A PUT request to this endpoint will start a download from a provided URI. This URI can either represent a file
    location, a magnet link or a HTTP(S) url.
    - anon_hops: the number of hops for the anonymous download. 0 hops is equivalent to a plain download
    - safe_seeding: whether the seeding of the download should be anonymous or not (0 = off, 1 = on)
    - destination: the download destination path of the torrent
    - torrent: the URI of the torrent file that should be downloaded. This parameter is required.

        **Example request**:

            .. sourcecode:: none

                curl -X PUT http://localhost:8085/downloads
                --data "anon_hops=2&safe_seeding=1&destination=/my/dest/on/disk/&uri=file:/home/me/test.torrent

        **Example response**:

            .. sourcecode:: javascript

                {"started": True, "infohash": "4344503b7e797ebf31582327a5baae35b11bda01"}
    """
    parameters = http.parse_qs(request.content.read(), 1)

    if "uri" not in parameters or len(parameters["uri"]) == 0:
        request.setResponseCode(http.BAD_REQUEST)
        return json.twisted_dumps({"error": "uri parameter missing"})

    download_config, error = DownloadsEndpoint.create_dconfig_from_params(parameters)
    if error:
        request.setResponseCode(http.BAD_REQUEST)
        return json.twisted_dumps({"error": error})

    def download_added(download):
        request.write(
            json.twisted_dumps(
                {
                    "started": True,
                    "infohash": hexlify(download.get_def().get_infohash()),
                }
            )
        )
        request.finish()

    def on_error(error):
        request.setResponseCode(http.INTERNAL_SERVER_ERROR)
        request.write(
            json.twisted_dumps({"error": unichar_string(error.getErrorMessage())})
        )
        request.finish()

    uri = parameters["uri"][0]
    if uri.startswith("file:"):
        if uri.endswith(".mdblob"):
            filename = url2pathname(
                uri[5:].encode("utf-8") if isinstance(uri, six.text_type) else uri[5:]
            )
            try:
                payload = ChannelMetadataPayload.from_file(filename)
            except IOError:
                request.setResponseCode(http.BAD_REQUEST)
                return json.twisted_dumps({"error": "file not found"})
            except InvalidSignatureException:
                request.setResponseCode(http.BAD_REQUEST)
                return json.twisted_dumps({"error": "Metadata has invalid signature"})

            with db_session:
                result = self.session.lm.mds.process_payload(payload)
                if result:
                    node, status = result[0]
                    if status == UNKNOWN_CHANNEL or (
                        status == UPDATED_OUR_VERSION
                        and node.metadata_type == CHANNEL_TORRENT
                    ):
                        node.subscribed = True
                        return json.twisted_dumps(
                            {"started": True, "infohash": hexlify(node.infohash)}
                        )
                return json.twisted_dumps({"error": "Already subscribed"})
        else:
            download_uri = "file:%s" % url2pathname(uri[5:]).decode("utf-8")
    else:
        download_uri = unquote_plus(cast_to_unicode_utf8(uri))
    download_deferred = self.session.start_download_from_uri(
        download_uri, download_config
    )
    download_deferred.addCallback(download_added)
    download_deferred.addErrback(on_error)

    return NOT_DONE_YET


# Traceback (RuntimeError)
Traceback (most recent call last):
File "TriblerGUI\core_manager.py", line 118, in on_received_state
RuntimeError: [Failure instance: Traceback: <type 'exceptions.ValueError'>: The filename, directory name, or volume label syntax is incorrect
run_tribler.py:85:start_tribler
Tribler\Core\Session.py:432:start
lib\site-packages\twisted\internet\defer.py:321:addCallback
lib\site-packages\twisted\internet\defer.py:310:addCallbacks
--- <exception caught here> ---
lib\site-packages\twisted\internet\defer.py:653:_runCallbacks
Tribler\Core\Session.py:429:load_checkpoint
Tribler\Core\Session.py:384:load_checkpoint
Tribler\Core\APIImplementation\LaunchManyCore.py:570:load_checkpoint
Tribler\Core\APIImplementation\LaunchManyCore.py:567:do_load_checkpoint
Tribler\Core\APIImplementation\LaunchManyCore.py:599:resume_download
Tribler\Core\TorrentDef.py:115:load_from_dict
Tribler\Core\TorrentDef.py:68:__init__
]

# Fixed code
def render_PUT(self, request):
    """
    .. http:put:: /downloads

    A PUT request to this endpoint will start a download from a provided URI. This URI can either represent a file
    location, a magnet link or a HTTP(S) url.
    - anon_hops: the number of hops for the anonymous download. 0 hops is equivalent to a plain download
    - safe_seeding: whether the seeding of the download should be anonymous or not (0 = off, 1 = on)
    - destination: the download destination path of the torrent
    - torrent: the URI of the torrent file that should be downloaded. This parameter is required.

        **Example request**:

            .. sourcecode:: none

                curl -X PUT http://localhost:8085/downloads
                --data "anon_hops=2&safe_seeding=1&destination=/my/dest/on/disk/&uri=file:/home/me/test.torrent

        **Example response**:

            .. sourcecode:: javascript

                {"started": True, "infohash": "4344503b7e797ebf31582327a5baae35b11bda01"}
    """
    parameters = http.parse_qs(request.content.read(), 1)

    if "uri" not in parameters or len(parameters["uri"]) == 0:
        request.setResponseCode(http.BAD_REQUEST)
        return json.twisted_dumps({"error": "uri parameter missing"})

    download_config, error = DownloadsEndpoint.create_dconfig_from_params(parameters)
    if error:
        request.setResponseCode(http.BAD_REQUEST)
        return json.twisted_dumps({"error": error})

    def download_added(download):
        request.write(
            json.twisted_dumps(
                {
                    "started": True,
                    "infohash": hexlify(download.get_def().get_infohash()),
                }
            )
        )
        request.finish()

    def on_error(error):
        request.setResponseCode(http.INTERNAL_SERVER_ERROR)
        request.write(
            json.twisted_dumps({"error": unichar_string(error.getErrorMessage())})
        )
        request.finish()

    uri = ensure_unicode(parameters["uri"][0], "utf-8")
    if uri.startswith("file:"):
        filename = url2pathname(uri[5:])
        if uri.endswith(".mdblob"):
            try:
                payload = ChannelMetadataPayload.from_file(filename)
            except IOError:
                request.setResponseCode(http.BAD_REQUEST)
                return json.twisted_dumps({"error": "file not found"})
            except InvalidSignatureException:
                request.setResponseCode(http.BAD_REQUEST)
                return json.twisted_dumps({"error": "Metadata has invalid signature"})

            with db_session:
                result = self.session.lm.mds.process_payload(payload)
                if result:
                    node, status = result[0]
                    if status == UNKNOWN_CHANNEL or (
                        status == UPDATED_OUR_VERSION
                        and node.metadata_type == CHANNEL_TORRENT
                    ):
                        node.subscribed = True
                        return json.twisted_dumps(
                            {"started": True, "infohash": hexlify(node.infohash)}
                        )
                return json.twisted_dumps({"error": "Already subscribed"})
        else:
            download_uri = "file:%s" % filename
    else:
        download_uri = unquote_plus(uri)
    download_deferred = self.session.start_download_from_uri(
        download_uri, download_config
    )
    download_deferred.addCallback(download_added)
    download_deferred.addErrback(on_error)

    return NOT_DONE_YET