URL not found for on-demand attachment download


#1

Hello,

I’m having an issue downloading attachments on-demand using the Couchbase Lite mobile framework (iOS, version 1.4.1). Currently I have it set up so that all attachments download automatically via the pull replicator (which works fine), but as some documents have many or large attachments, I want to optimize it so that they are downloaded as needed.

To do this, I’m just calling downloadAttachment: on the pull replicator with the appropriate attachment fetched from the document. The Progress object it returns has the completedUnitCount and totalUnitCount both at -1, and no progress is made.

I turned on verbose logging and found this:

11:38:01.206‖ ChangeTracker: CBLSocketChangeTracker[0x6080001d8420 data]: Started... <https://xxxx.xxx.xxxxxxxx.xxx/data/_changes?feed=normal&heartbeat=300000&style=all_docs&since=844856:844804&filter=sync_gateway/bychannel>
11:38:01.206‖ ChangeTracker: CBLSocketChangeTracker[0x6080001d8420 data]: Event 1 on <__NSCFInputStream: 0x608000117c40>
11:38:01.325‖ RemoteRequest: CBLAttachmentDownloader[GET https://xxxx.xxx.xxxxxxxx.xxx/data/YmKd0-1ofOmK5x_JbKdqRgNh/BG::50d279db-b070-4a3b-981b-80382c82fa0d/BG::50d279db-b070-4a3b-981b-80382c82fa0d?rev=3-0e3578f5de948a920a222d412e8bb0ef]: Got response, status 404
11:38:01.325‖ RemoteRequest: CBLRemoteSession done with CBLAttachmentDownloader[GET https://xxxx.xxx.xxxxxxxx.xxx/data/YmKd0-1ofOmK5x_JbKdqRgNh/BG::50d279db-b070-4a3b-981b-80382c82fa0d/BG::50d279db-b070-4a3b-981b-80382c82fa0d?rev=3-0e3578f5de948a920a222d412e8bb0ef]
11:38:01.325‖ RemoteRequest: CBLAttachmentDownloader[GET https://xxxx.xxx.xxxxxxxx.xxx/data/YmKd0-1ofOmK5x_JbKdqRgNh/BG::50d279db-b070-4a3b-981b-80382c82fa0d/BG::50d279db-b070-4a3b-981b-80382c82fa0d?rev=3-0e3578f5de948a920a222d412e8bb0ef]: JSON error message is: {"error":"not_found","reason":"unknown URL"}
11:38:01.326‖ CBLAttachmentDownloader[GET https://xxxx.xxx.xxxxxxxx.xxx/data/YmKd0-1ofOmK5x_JbKdqRgNh/BG::50d279db-b070-4a3b-981b-80382c82fa0d/BG::50d279db-b070-4a3b-981b-80382c82fa0d?rev=3-0e3578f5de948a920a222d412e8bb0ef]: Got error CBLHTTP[404, "unknown URL", <https://xxxx.xxx.xxxxxxxx.xxx/data/YmKd0-1ofOmK5x_JbKdqRgNh/BG::50d279db-b070-4a3b-981b-80382c82fa0d/BG::50d279db-b070-4a3b-981b-80382c82fa0d?rev=3-0e3578f5de948a920a222d412e8bb0ef>]

Looking at the URL, it has the attachment ID twice, so I tried removing one of them, and made a GET request to it, and that returned a 401 so at least it found it.
i.e. Change
https://xxxx.xxx.xxxxxxxx.xxx/data/YmKd0-1ofOmK5x_JbKdqRgNh/BG::50d279db-b070-4a3b-981b-80382c82fa0d/BG::50d279db-b070-4a3b-981b-80382c82fa0d?rev=3-0e3578f5de948a920a222d412e8bb0ef
to
https://xxxx.xxx.xxxxxxxx.xxx/data/YmKd0-1ofOmK5x_JbKdqRgNh/BG::50d279db-b070-4a3b-981b-80382c82fa0d?rev=3-0e3578f5de948a920a222d412e8bb0ef

Does anyone know where or how this URL is set? Would this occur in the cloud backend, or is this URL constructed by the Couchbase Lite framework?

Thanks.


#2

That looks like a bug in Couchbase Lite. Just to confirm: the document ID is YmKd0-1ofOmK5x_JbKdqRgNh, and the attachment name is BG::50d279db-b070-4a3b-981b-80382c82fa0d?

Looking at the URL, it has the attachment ID twice, so I tried removing one of them, and made a GET request to it, and that returned a 401 so at least it found it.

The 401 status just means you need to provide credentials to access that URL; it says nothing about whether the resource exists. But you’re correct that the second form (with the attachment name only once) is the correct one.

Take a look at the issue tracker and see if there’s a report of this … if not, please file one. IIRC we’re still working on a 1.4.x bug-fix release.


#3

The document ID is actually YmKd0-1ofOmK5x_JbKdqRgNh/BG::50d279db-b070-4a3b-981b-80382c82fa0d, and the attachment name is BG::50d279db-b070-4a3b-981b-80382c82fa0d, so I think I just spotted the problem. It looks like it’s because the document ID has a forward slash in it (we do that for namespacing purposes for our various document types).

Is there any way to override the URL that Couchbase uses when downloading an attachment?

Oh, and the reason I was getting a 401 when testing the second form of the URL is that I was just sending a GET request using CURL without any credentials. I just wanted to see if I could get another response besides 404.


#4

Ouch. Yes, looks like the docID isn’t being URL-escaped properly; this can be tricky since the default Foundation calls for that leave slashes alone.

Is there any way to override the URL that Couchbase uses when downloading an attachment?

Only by changing the source code…


#5

No worries. Thanks for the help! At least I know what’s causing it now. :slight_smile: