Http_open and repositioning the stream

When using seek/4 on a stream obtained via http_open I get weird behaviour.

If I use read_string to just read a bit from it, I’m able to rewind it and read again:

So:

http_open("https://www.swi-prolog.org/", S, []), read_string(S, 9, Str), seek(S, 0, bof, _), read_string(S, 9, Str).

unifies. However, if I read the stream until the end, it throws the No permission to reposition stream exception:

http_open("https://www.swi-prolog.org/", S, []), read_string(S, _, Str), seek(S, 0, bof, _), read_string(S, _, Str).

Is there some kind of flag set that disables repositioning once eof is reached and can I change this behaviour?

HTTP streams are sockets and not repositionable. You can be lucky if the seek is inside the current buffer. Possibly peek_string/3 can solve your problem?

I think peek_string/3 could work, but it can’t be used to peek until the end, that is the length must be supplied/known beforehand. Right now I am working around this by using read_string/3 to read the raw response, then reopening it with open_string/2 and passing that stream to json parser.

There is no limit on how long you can peek. It does cost memory though. If the input is really long it is probably best to first send to a file.