@@ -733,15 +733,28 @@ sys/make-scheme [
733
733
actor: [
734
734
read : func [
735
735
port [port! ]
736
- /binary
737
- /part length [number! ]
738
- /lines
736
+ /part {Partial read a given number of units (source relative)}
737
+ length [integer! ]
738
+ /seek {Read from a specific position (source relative)}
739
+ index [integer! ] "zero-based!"
740
+ /string {Convert UTF and line terminators to standard text string}
741
+ /binary {Preserves contents exactly}
742
+ /lines {Convert to block of strings (implies /string)}
739
743
/local result
740
744
] [
741
745
sys/log/debug 'HTTP "READ"
746
+ if lines [
747
+ if binary [cause-error 'Script 'bad-refine /binary ]
748
+ seek: part: none
749
+ ]
750
+ if all [string binary] [cause-error 'Script 'bad-refines none]
751
+
742
752
unless port/state [open port port/state/close?: yes]
743
- if all [part binary length > 0 ] [
744
- append port/spec/headers compose [Range: (join "bytes=0-" (to integer! length) - 1 )]
753
+
754
+ if all [any [part seek] not string] [
755
+ either seek [ binary: true assert [index >= 0 ]][ index: 0 ]
756
+ length: either part [ assert [length > 0 ] length + index][ none ]
757
+ put port/spec/headers quote Range: ajoin ["bytes=" index #"-" any [all [length length - 1 ] "" ]]
745
758
]
746
759
either any-function? : port/awake [
747
760
unless open? port [cause-error 'Access 'not-open port/spec/ref ]
@@ -750,11 +763,19 @@ sys/make-scheme [
750
763
do-request port
751
764
][
752
765
result: sync-op port []
753
- unless binary [decode-result result]
754
- if result/2 [
766
+ either binary [
767
+ unless find result/1 'Accept-Ranges [
768
+ case /all [
769
+ seek [ result/2: at result/2 index if part [length: length - index]]
770
+ part [ clear skip result/2 length]
771
+ ]
772
+ ]
773
+ ][
774
+ decode-result result
755
775
case /all [
756
- lines [ result/2: split-lines result/2 ]
757
- part [ clear skip result/2 length ]
776
+ lines [ result/2: split-lines result/2 ]
777
+ index [ result/2: skip result/2 index ]
778
+ length [ clear skip result/2 length]
758
779
]
759
780
]
760
781
result/2
0 commit comments