@@ -11,13 +11,13 @@ REBOL [
11
11
}
12
12
Name: 'http
13
13
Type: 'module
14
- Version: 0.3.2
14
+ Version: 0.3.3
15
+ Date: 25-Feb-2020
15
16
File: %prot-http.r
16
17
Purpose: {
17
18
This program defines the HTTP protocol scheme for REBOL 3.
18
19
}
19
20
Author: ["Gabriele Santilli" "Richard Smolak" "Oldes" ]
20
- Date: 02-Apr-2019
21
21
History: [
22
22
0.1.1 22-Jun-2007 "Gabriele Santilli" "Version used in R3-Alpha"
23
23
0.1.4 26-Nov-2012 "Richard Smolak" "Version from Atronix's fork"
@@ -31,10 +31,11 @@ REBOL [
31
31
0.3.0 06-Jul-2019 "Oldes" "FIX: Error handling revisited and improved dealing with chunked data"
32
32
0.3.1 13-Feb-2020 "Oldes" "FEAT: Possible auto conversion to text if found charset specification in content-type"
33
33
0.3.2 25-Feb-2020 "Oldes" "FIX: Properly handling chunked data"
34
+ 0.3.3 25-Feb-2020 "Oldes" "FEAT: support for read/binary and write/binary to force raw data result"
34
35
]
35
36
]
36
37
37
- sync-op : func [ port body /local state encoding content-type code-page tmp] [
38
+ sync-op : func [ port body /local header state encoding content-type code-page tmp] [
38
39
unless port/state [open port port/state/close?: yes]
39
40
state: port/state
40
41
state/awake: :read-sync-awake
@@ -49,7 +50,7 @@ sync-op: func [port body /local state encoding content-type code-page tmp][
49
50
throw-http-error port make error! [
50
51
type: 'Access
51
52
id: 'no-connect
52
- arg1: port/spec/ref
53
+ arg1: port/spec/ref
53
54
arg2: 'timeout
54
55
]
55
56
exit
@@ -99,26 +100,16 @@ sync-op: func [port body /local state encoding content-type code-page tmp][
99
100
]
100
101
]
101
102
102
- if all [
103
- content-type: select port/state/info/headers 'Content-Type
104
- any [
105
- ; consider content to be a text if charset specification is included
106
- parse content-type [to #";" thru "charset=" copy code-page to end]
107
- ; or when it is without charset, but of type text/*
108
- parse content-type ["text/" to end]
109
- ]
110
- ][
111
- unless code-page [code-page: "utf-8" ]
112
- sys/log/info 'HTTP ["Trying to decode from code-page:^[ [m" code-page]
113
- if string? tmp: try [iconv body code-page][ body: tmp ]
114
- ]
103
+ header: copy port/state/info/headers
115
104
116
105
if state/close? [
117
106
sys/log/more 'HTTP ["Closing port for:^[ [m" port/spec/ref ]
118
107
close port
119
108
]
120
- body
109
+
110
+ reduce [header body]
121
111
]
112
+
122
113
read-sync-awake : func [ event [event! ] /local error] [
123
114
sys/log/debug 'HTTP ["Read-sync-awake:" event/type ]
124
115
switch /default event/type [
@@ -551,7 +542,7 @@ check-data: func [port /local headers res data available out chunk-size pos trai
551
542
conn: state/connection
552
543
res: false
553
544
554
- sys/log/more 'HTTP ["Check-data; bytes:^[ [m" length? conn/data " ^[ [36mfrom: ^[ [m" state /read-pos ]
545
+ sys/log/more 'HTTP ["Check-data; bytes:^[ [m" length? conn/data ]
555
546
556
547
case [
557
548
headers/transfer-encoding = "chunked" [
@@ -649,6 +640,27 @@ check-data: func [port /local headers res data available out chunk-size pos trai
649
640
]
650
641
res
651
642
]
643
+
644
+ decode-result : func [
645
+ result [block! ] {[header body]}
646
+ /local body content-type code-page
647
+ ] [
648
+ if all [
649
+ content-type: select result/1 'Content-Type
650
+ any [
651
+ ; consider content to be a text if charset specification is included
652
+ parse content-type [to #";" thru "charset=" copy code-page to end]
653
+ ; or when it is without charset, but of type text/*
654
+ parse content-type [["text/" | "application/json" ] to end]
655
+ ]
656
+ ][
657
+ unless code-page [code-page: "utf-8" ]
658
+ sys/log/info 'HTTP ["Trying to decode from code-page:^[ [m" code-page]
659
+ try [result/2: iconv result/2 code-page]
660
+ ]
661
+ result
662
+ ]
663
+
652
664
hex-digits: charset "1234567890abcdefABCDEF"
653
665
sys/make-scheme [
654
666
name: 'http
@@ -668,7 +680,8 @@ sys/make-scheme [
668
680
actor: [
669
681
read : func [
670
682
port [port! ]
671
- /binary
683
+ /binary
684
+ /local result
672
685
] [
673
686
sys/log/debug 'HTTP "READ"
674
687
either any-function? : port/awake [
@@ -677,12 +690,16 @@ sys/make-scheme [
677
690
port/state/awake: : port/awake
678
691
do-request port
679
692
][
680
- sync-op port []
693
+ result: sync-op port []
694
+ unless binary [decode-result result]
695
+ result/2
681
696
]
682
697
]
683
698
write : func [
684
699
port [port! ]
685
700
value
701
+ /binary
702
+ /local result
686
703
] [
687
704
sys/log/debug 'HTTP "WRITE"
688
705
;?? port
@@ -696,7 +713,9 @@ sys/make-scheme [
696
713
parse-write-dialect port value
697
714
do-request port
698
715
][
699
- sync-op port [parse-write-dialect port value]
716
+ result: sync-op port [parse-write-dialect port value]
717
+ unless binary [decode-result result]
718
+ result/2
700
719
]
701
720
]
702
721
open : func [
@@ -711,12 +730,12 @@ sys/make-scheme [
711
730
connection:
712
731
error: none
713
732
close?: no
733
+ binary?: no
714
734
info: make port/scheme/info [type: 'url]
715
735
awake: : port/awake
716
736
redirects: 0
717
737
chunk: none
718
738
chunk-size: none
719
- read-pos: 0
720
739
]
721
740
;? port/state/info
722
741
port/state/connection: conn: make port! compose [
0 commit comments