# Serve http behavior when the size is unknown?

**URL:** <https://forum.rclone.org/t/serve-http-behavior-when-the-size-is-unknown/21319>\
**Category:** Dev Discussions\
**Created:** [December 26, 2020, 3:42pm UTC](https://forum.rclone.org/t/serve-http-behavior-when-the-size-is-unknown/21319 "2020-12-26T15:42:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![poundifdef](https://forum.rclone.org/letter_avatar_proxy/v4/letter/p/e5b9ba/32.png) [@poundifdef](https://forum.rclone.org/u/poundifdef)\
**Post date:** [December 26, 2020, 3:42pm UTC](https://forum.rclone.org/t/serve-http-behavior-when-the-size-is-unknown/21319/1 "2020-12-26T15:42:53Z")

</div>

Hello! I am currently writing a new backend for myself, and in this case, the size of the file that I want to download is unknown. In order to notate this, I've created my implementation of `DirEntry.Size()` to return -1:

```auto
func (o *Object) Size() int64 {
	return -1
}

```

For many operations, such as `rclone cat`, this is not a problem. However, for the `serve http` command, the `Content-Length` header is always set to 0, causing the browser to not download the file.

This is a little bit curious: I actually tried removing all instances where we are setting the `Content-Length` header in the `httplib` package, and yet the header is still being set!

Thus, my questions are:

- Is it expected behavior that `serve http` requires a Content-Length header?
- Is it a supported behavior for the remote object size to be unknown (if so, is there an example remote which I could look at to see how it is being handled?)
- What would be the best way to handle this case?

If it helps, I am running:

```auto
rclone v1.54.0-DEV
- os/arch: darwin/amd64
- go version: go1.14.6

```

Thanks so much!

---

<div class="post-metadata">

**Author:** ![ncw](https://forum.rclone.org/user_avatar/forum.rclone.org/ncw/32/18_2.png) [@ncw](https://forum.rclone.org/u/ncw)\
**Post date:** [December 26, 2020, 9:06pm UTC](https://forum.rclone.org/t/serve-http-behavior-when-the-size-is-unknown/21319/2 "2020-12-26T21:06:16Z")

</div>

> [@poundifdef](#):
>
> Hello! I am currently writing a new backend for myself, and in this case, the size of the file that I want to download is unknown. In order to notate this, I've created my implementation of `DirEntry.Size()` to return -1:

That is correct

> [@poundifdef](#):
>
> This is a little bit curious: I actually tried removing all instances where we are setting the `Content-Length` header in the `httplib` package, and yet the header is still being set!

Hmm... that looks like a bug... Try this

[v1.54.0-beta.4978.045aff9e5.fix-serve-http-unknown-len](https://beta.rclone.org/branch/fix-serve-http-unknown-len/v1.54.0-beta.4978.045aff9e5.fix-serve-http-unknown-len/) on branch [fix-serve-http-unknown-len](https://github.com/rclone/rclone/tree/fix-serve-http-unknown-len) (uploaded in 15-30 mins)

Apparently `http.ServeContent` can't cope with unknown length files.

---

<div class="post-metadata">

**Author:** ![poundifdef](https://forum.rclone.org/letter_avatar_proxy/v4/letter/p/e5b9ba/32.png) [@poundifdef](https://forum.rclone.org/u/poundifdef)\
**Post date:** [December 27, 2020, 12:05am UTC](https://forum.rclone.org/t/serve-http-behavior-when-the-size-is-unknown/21319/3 "2020-12-27T00:05:44Z")

</div>

The updated code works! Interestingly, the `Content-Length` header is still being set (on a small file, 13 bytes), however I have not investigated the implementation of the ResponseWriter.

Relatedly, I also see a similar error when running with cmount:

When I run this:

```auto
go run -tags cmount github.com/rclone/rclone cmount sk: ./mnt/

```

then the files show as 0 bytes, and have a length of 0 when I open them. I'm not sure whether this is a requirement for mounting on MacOS, but the code implies that cmount is intended able to accommodate this case (maybe not on my platform.)

Let me know if I can run other tests? I can try your branch with a more comprehensive set of files with larger sizes.

---

<div class="post-metadata">

**Author:** ![ncw](https://forum.rclone.org/user_avatar/forum.rclone.org/ncw/32/18_2.png) [@ncw](https://forum.rclone.org/u/ncw)\
**Post date:** [December 27, 2020, 10:04pm UTC](https://forum.rclone.org/t/serve-http-behavior-when-the-size-is-unknown/21319/4 "2020-12-27T22:04:04Z")

</div>

> [@poundifdef](#):
>
> The updated code works! Interestingly, the `Content-Length` header is still being set (on a small file, 13 bytes), however I have not investigated the implementation of the ResponseWriter.

It sets the content length on files it knows the length of.

I merged that fix into master.

> [@poundifdef](#):
>
> Relatedly, I also see a similar error when running with cmount:

File systems really like to know the length of files!

There is a special workaround for linux which makes it work - it should work for macOS according to the commit message. It depends on how you open the file - try using `cat > otherfile` on it.

> <https://github.com/rclone/rclone/commit/1222b78ec402ca955b02dbaf295a04fe0b1e66bf>
>
> This means that on Linux and OSX at least reading a google doc from a
> mount will… behave sensibly.

If you want to unknown length files with a more established backend, try a google doc on google drive, or the google photos backend.

---

<div class="post-metadata">

**Author:** ![poundifdef](https://forum.rclone.org/letter_avatar_proxy/v4/letter/p/e5b9ba/32.png) [@poundifdef](https://forum.rclone.org/u/poundifdef)\
**Post date:** [December 28, 2020, 5:53pm UTC](https://forum.rclone.org/t/serve-http-behavior-when-the-size-is-unknown/21319/5 "2020-12-28T17:53:44Z")

</div>

Awesome, thank you for fixing and merging HTTP! And also for the explanatory material for cmount. (I am reminded of Linus' [colorful remarks](https://yarchive.net/comp/linux/o_direct.html) about O\_DIRECT 🙂

---

<div class="post-metadata">

**Author:** ![ncw](https://forum.rclone.org/user_avatar/forum.rclone.org/ncw/32/18_2.png) [@ncw](https://forum.rclone.org/u/ncw)\
**Post date:** [December 29, 2020, 12:38pm UTC](https://forum.rclone.org/t/serve-http-behavior-when-the-size-is-unknown/21319/6 "2020-12-29T12:38:48Z")

</div>

He he!

Linus doesn't like O\_DIRECT does he!

> The thing that has always disturbed me about O\_DIRECT is that the whole  
> interface is just stupid, and was probably designed by a deranged monkey  
> on some serious mind-controlling substances

---

<div class="post-metadata">

**Author:** ![system](https://forum.rclone.org/uploads/default/original/2X/3/39ad3235f6ffec76ccd0c81616bd55697b4c300d.png) [@system](https://forum.rclone.org/u/system)\
**Post date:** [February 28, 2021, 8:38am UTC](https://forum.rclone.org/t/serve-http-behavior-when-the-size-is-unknown/21319/7 "2021-02-28T08:38:51Z")

</div>

Closed on February 28, 2021, 8:38am UTC

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
