Search code examples
rebolrebol3

Partial read from urls with READ/PART or READ/SEEK


Partial read via HTTP Range header works fine for me:

rebol []
client: open tcp://www.apache.org/

client/awake: func [event /local port] [
    port: event/port
    switch event/type [
        lookup [open port]
        connect [
            write port rejoin [
                {GET / HTTP/1.1} crlf
                {User-Agent: curl/7.26.0} crlf
                {Host: www.apache.org} crlf
                {Accept: */*} crlf
                {Range: bytes=0-9} crlf
                crlf
            ]
        ]
        wrote [read port]
        read [
            probe to-string port/data
            probe length? port/data
            clear port/data
        ]        
    ]
    false
]

wait [client 3]
close client
print "Done"

I think I could use READ/PART to do the same thing:

length? read/part http://www.apache.org/ 10   ;40195
length? read http://www.apache.org/           ;40195

but it does't work, still get all the bytes. The same with READ/SEEK. Why was that? (By the way, it works in Rebol2.)


Solution

  • You can see from the source

    https://github.com/rebol/rebol/blob/master/src/mezz/prot-http.r#L424
    

    that the read actor does not have any refinements defined. This doesn't mean they can't be defined but at present no decision has been made on whether it should be done by using refinements, or by using a query dialect.

    You can see by setting trace/net on, that it's faking it in Rebol2

    >> trace/net on
    >> read/part http://www.apache.org 10
    URL Parse: none none www.apache.org none none none
    Net-log: ["Opening" "tcp" "for" "HTTP"]
    connecting to: www.apache.org
    Net-log: {GET / HTTP/1.0
    Accept: */*
    Connection: close
    User-Agent: REBOL View 2.7.8.3.1
    Host: www.apache.org
    }
    Net-log: "HTTP/1.1 200 OK"
    Net-log: ["low level read of " 2048 "bytes"]
    Net-log: ["low level read of " 2048 "bytes"]
    .. many lines removed
    Net-log: ["low level read of " 2048 "bytes"]
    Net-log: ["low level read of " 2048 "bytes"]
    Net-log: ["low level read of " 2048 "bytes"]
    == "<!DOCTYPE "