Skip to content

Commit ba6a3c7

Browse files
authored
Use new io-lib. (#119)
1 parent ae287f7 commit ba6a3c7

12 files changed

+308
-232
lines changed

src/chunked.toit

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,30 @@
22
// Use of this source code is governed by an MIT-style license that can be
33
// found in the LICENSE file.
44
5-
import reader
6-
import writer
5+
import io
76

87
import .connection
98

10-
// This is an adapter that converts a chunked stream (RFC 2616) to a stream of
11-
// just the payload bytes. It takes a BufferedReader as a constructor argument,
12-
// and acts like a Socket or TlsSocket, having one method, called read, which
13-
// returns ByteArrays. End of stream is indicated with a null return value
14-
// from read.
15-
class ChunkedReader implements reader.Reader:
9+
/**
10+
This is an adapter that converts a chunked stream (RFC 2616) to a stream of
11+
just the payload bytes. It takes an $io.Reader as a constructor argument,
12+
and acts like an $io.Reader.
13+
14+
Deprecated for public use. Use the type $io.Reader instead.
15+
This class will be made private in the future.
16+
*/
17+
class ChunkedReader extends ChunkedReader_:
18+
constructor connection/Connection reader/io.Reader:
19+
super connection reader
20+
21+
/**
22+
This is an adapter that converts a chunked stream (RFC 2616) to a stream of
23+
just the payload bytes. It takes an $io.Reader as a constructor argument,
24+
and acts like an $io.Reader.
25+
*/
26+
class ChunkedReader_ extends io.Reader:
1627
connection_/Connection? := null
17-
reader_/reader.BufferedReader? := ?
28+
reader_/io.Reader? := ?
1829
left_in_chunk_ := 0 // How much more raw data we are waiting for before the next size line.
1930
2031
constructor .connection_ .reader_:
@@ -23,26 +34,29 @@ class ChunkedReader implements reader.Reader:
2334
Returns the underlying reader, which may have buffered up data.
2435
2536
The ChunkedReader is unusable after a called to $detach_reader.
37+
38+
Deprecated.
2639
*/
27-
detach_reader -> reader.BufferedReader:
40+
// TODO(florian): remove already now?
41+
detach_reader -> io.Reader:
2842
r := reader_
2943
reader_ = null
3044
return r
3145

32-
read -> ByteArray?:
46+
read_ -> ByteArray?:
3347
while true:
3448
if not connection_:
3549
return null
3650
if left_in_chunk_ > 0:
3751
result := reader_.read --max_size=left_in_chunk_
38-
if not result: throw reader.UNEXPECTED_END_OF_READER_EXCEPTION
52+
if not result: throw io.Reader.UNEXPECTED_END_OF_READER
3953
left_in_chunk_ -= result.size
4054
if left_in_chunk_ == 0:
4155
expect_ '\r'
4256
expect_ '\n'
4357
return result
4458

45-
raw_length := reader_.read_bytes_until '\r'
59+
raw_length := reader_.read_bytes_up_to '\r'
4660
expect_ '\n'
4761

4862
left_in_chunk_ = int.parse raw_length --radix=16
@@ -55,34 +69,42 @@ class ChunkedReader implements reader.Reader:
5569
connection_ = null
5670

5771
expect_ byte/int:
58-
b := reader_.byte 0
72+
b := reader_.peek_byte 0
5973
if b != byte: throw "PROTOCOL_ERROR"
6074
reader_.skip 1
6175

62-
class ChunkedWriter implements BodyWriter:
76+
/**
77+
Deprecated for public use. Use the type $io.CloseableWriter instead.
78+
This class will be made private in the future.
79+
*/
80+
class ChunkedWriter extends ChunkedWriter_:
81+
constructor connection/Connection writer/io.Writer:
82+
super connection writer
83+
84+
class ChunkedWriter_ extends io.CloseableWriter:
6385
static CRLF_ ::= "\r\n"
6486

6587
connection_/Connection? := null
66-
writer_/writer.Writer
88+
writer_/io.Writer
89+
90+
constructor .connection_ .writer_:
6791

6892
// We don't know the amount of data ahead of time, so it may already be done.
69-
is_done -> bool:
93+
is_done_ -> bool:
7094
return true
7195

72-
constructor .connection_ .writer_:
73-
74-
write data -> int:
75-
size := data.size
96+
try_write_ data/io.Data from/int to/int -> int:
97+
size := to - from
7698
if size == 0: return 0
7799
write_header_ size
78-
writer_.write data // Always writes all data.
100+
writer_.write data from to // Always writes all data.
79101
// Once we've sent the data, the other side might conclude that
80102
// they have gotten everything they need, so we don't want to throw
81103
// an exception on writing the final CRLF.
82104
catch: writer_.write CRLF_
83105
return size
84106

85-
close -> none:
107+
close_ -> none:
86108
if not connection_: return
87109
// Once we've sent the last chunk, the remaining transmitted information
88110
// is redundant, so we don't want to throw exceptions if the other side

src/client.toit

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
// Use of this source code is governed by an MIT-style license that can be
33
// found in the LICENSE file.
44
5-
import bytes
65
import encoding.json
76
import encoding.url
7+
import io
88
import net
99
import net.tcp
10-
import reader
1110
import tls
1211

1312
import .connection
@@ -207,6 +206,8 @@ class Client:
207206
- suffixing the $host parameter with ":port", for example `localhost:8080`.
208207
209208
If neither is specified then the default port is used.
209+
210+
Deprecated. Use $(new_request method --host) instead.
210211
*/
211212
new_request method/string host/string --port/int?=null path/string --headers/Headers?=null -> RequestOutgoing:
212213
parsed := ParsedUri_.private_
@@ -493,7 +494,7 @@ class Client:
493494
response := null
494495
try_to_reuse_ parsed: | connection |
495496
request := connection.new_request POST parsed.path headers
496-
request.body = bytes.Reader data
497+
request.body = io.Reader data
497498
response = request.send
498499

499500
if follow_redirects and is_regular_redirect_ response.status_code:
@@ -606,7 +607,7 @@ class Client:
606607
return post_form_ map parsed --headers=headers --follow_redirects=follow_redirects
607608

608609
url_encode_ map/Map -> ByteArray:
609-
buffer := bytes.Buffer
610+
buffer := io.Buffer
610611
first := true
611612
map.do: | key value |
612613
if key is not string: throw "WRONG_OBJECT_TYPE"
@@ -680,7 +681,7 @@ class Client:
680681
ensure_connection_ location/ParsedUri_ -> bool:
681682
if connection_ and connection_.is_open_:
682683
if location.can_reuse_connection connection_.location_:
683-
connection_.drain // Remove any remnants of previous requests.
684+
connection_.drain_ // Remove any remnants of previous requests.
684685
return true
685686
// Hostname etc. didn't match so we need a new connection.
686687
connection_.close

0 commit comments

Comments
 (0)