Skip to content

Commit 08aa999

Browse files
committed
FEAT: support for read/part (using Range request with read/part/binary)
fixes: Oldes/Rebol-issues#2434
1 parent fa8968a commit 08aa999

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/mezz/prot-http.reb

+15-5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ REBOL [
3333
0.3.2 25-Feb-2020 "Oldes" "FIX: Properly handling chunked data"
3434
0.3.3 25-Feb-2020 "Oldes" "FEAT: support for read/binary and write/binary to force raw data result"
3535
0.3.4 26-Feb-2020 "Oldes" "FIX: limit input data according Content-Length (#issues/2386)"
36+
0.3.5 26-Oct-2020 "Oldes" "FEAT: support for read/part (using Range request with read/part/binary)"
3637
]
3738
]
3839

@@ -264,7 +265,7 @@ do-request: func [
264265
Accept-charset: "utf-8"
265266
Accept-Encoding: "gzip,deflate"
266267
Host: either not find [80 443] spec/port-id [
267-
rejoin [form spec/host #":" spec/port-id]
268+
ajoin [spec/host #":" spec/port-id]
268269
][
269270
form spec/host
270271
]
@@ -471,11 +472,9 @@ http-response-headers: construct [
471472
Last-Modified:
472473
]
473474

474-
do-redirect: func [port [port!] new-uri [url! string! file!] /local spec state][
475+
do-redirect: func [port [port!] new-uri [url! string! file!] /local spec state headers][
475476
spec: port/spec
476477
state: port/state
477-
478-
clear spec/headers
479478
port/data: none
480479

481480
new-uri: as url! new-uri
@@ -514,11 +513,16 @@ do-redirect: func [port [port!] new-uri [url! string! file!] /local spec state][
514513
return throw-http-error port {Redirect to a protocol different from HTTP or HTTPS not supported}
515514
]
516515

517-
;we need to reset tcp connection here before doing a redirect
516+
; store original request headers
517+
headers: spec/headers
518+
; we need to reset tcp connection here before doing a redirect
518519
close port/state/connection
519520
port/spec: spec: new-uri
520521
port/state: none
521522
open port
523+
; restore original request headers
524+
port/spec/headers: headers
525+
port
522526
]
523527

524528
check-data: func [port /local headers res data available out chunk-size pos trailer state conn][
@@ -683,9 +687,14 @@ sys/make-scheme [
683687
read: func [
684688
port [port!]
685689
/binary
690+
/part length [number!]
686691
/local result
687692
][
688693
sys/log/debug 'HTTP "READ"
694+
unless port/state [open port port/state/close?: yes]
695+
if all [part binary length > 0] [
696+
append port/spec/headers compose [Range: (join "bytes=0-" (to integer! length) - 1)]
697+
]
689698
either any-function? :port/awake [
690699
unless open? port [cause-error 'Access 'not-open port/spec/ref]
691700
if port/state/state <> 'ready [throw-http-error "Port not ready"]
@@ -694,6 +703,7 @@ sys/make-scheme [
694703
][
695704
result: sync-op port []
696705
unless binary [decode-result result]
706+
if all [part result/2] [ clear skip result/2 length ]
697707
result/2
698708
]
699709
]

src/tests/units/port-test.r3

+4
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ if system/platform = 'Windows [
154154
;@@ https://github.com/Oldes/Rebol3/issues/14
155155
--assert exists? http://httpbin.org/
156156
--assert not exists? http://httpbin.org/not-exists
157+
--test-- "read/part"
158+
;@@ https://github.com/Oldes/Rebol-issues/issues/2434
159+
--assert "<!DOCTYPE" = read/part http://httpbin.org/ 9
160+
--assert #{89504E47} = read/binary/part http://avatars-04.gitter.im/gh/uv/4/oldes 4
157161
===end-group===
158162

159163

0 commit comments

Comments
 (0)