forked from ehsanmok/flare
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoding.mojo
More file actions
160 lines (139 loc) Β· 6.49 KB
/
Copy pathencoding.mojo
File metadata and controls
160 lines (139 loc) Β· 6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""Example 10 β HTTP content encoding: gzip and deflate with flare.http.
Demonstrates:
- compress_gzip(data, level) β compress bytes to gzip format
- decompress_gzip(data) β decompress gzip bytes
- decompress_deflate(data) β decompress zlib-wrapped deflate bytes
- decode_content(body, encoding) β dispatch by Content-Encoding header
- Round-trip identity: decompress(compress(data)) == data
- Compressing a JSON payload (simulating a gzip-encoded HTTP response)
- Encoding constants: Encoding.GZIP / DEFLATE / IDENTITY / BR
No network required β all operations are performed in-process.
Run:
pixi run example-encoding
"""
from flare.http import (
Encoding,
compress_gzip,
decompress_gzip,
decompress_deflate,
decode_content,
)
def main() raises:
print("=== flare Example 10: HTTP Content Encoding ===")
print()
# ββ 1. Encoding constants ββββββββββββββββββββββββββββββββββββββββββββββββ
print("ββ 1. Encoding constants ββ")
print(" Encoding.GZIP :", Encoding.GZIP)
print(" Encoding.DEFLATE :", Encoding.DEFLATE)
print(" Encoding.IDENTITY :", Encoding.IDENTITY)
print(" Encoding.BR :", Encoding.BR)
print()
# ββ 2. Gzip round-trip βββββββββββββββββββββββββββββββββββββββββββββββββββ
print("ββ 2. Gzip round-trip ββ")
var original = String("Hello, flare! This is a test of gzip compression.")
var original_bytes = original.as_bytes()
var compressed = compress_gzip(Span[UInt8, _](original_bytes))
print(" original :", len(original_bytes), "bytes")
print(" compressed:", len(compressed), "bytes (gzip)")
var decompressed = decompress_gzip(Span[UInt8, _](compressed))
var restored = String(unsafe_from_utf8=decompressed)
print(" restored :", len(decompressed), "bytes")
print(" match :", restored == original)
print()
# ββ 3. Compression levels ββββββββββββββββββββββββββββββββββββββββββββββββ
print("ββ 3. Compression levels (0=none β¦ 9=max) ββ")
var lorem = String(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
+ "Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
+ "Ut enim ad minim veniam, quis nostrud exercitation ullamco."
)
var lorem_bytes = lorem.as_bytes()
for level in range(0, 10):
var c = compress_gzip(Span[UInt8, _](lorem_bytes), level)
print(" level", level, "β", len(c), "bytes")
print()
# ββ 4. Repeated content compresses well ββββββββββββββββββββββββββββββββββ
print("ββ 4. Highly compressible content ββ")
var rep = String(capacity=1024)
for _ in range(100):
rep += "AAAA"
var rep_bytes = rep.as_bytes()
var rep_compressed = compress_gzip(Span[UInt8, _](rep_bytes))
print(" input :", len(rep_bytes), "bytes (400 Γ 'AAAA')")
print(" output:", len(rep_compressed), "bytes (gzip)")
var ratio = Float64(len(rep_bytes)) / Float64(len(rep_compressed))
print(" ratio :", ratio, "Γ compression")
print()
# ββ 5. JSON payload round-trip ββββββββββββββββββββββββββββββββββββββββββββ
print("ββ 5. JSON payload round-trip ββ")
var json = String(
'{"users": [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]}'
)
var json_bytes = json.as_bytes()
var json_gz = compress_gzip(Span[UInt8, _](json_bytes))
print(" JSON original :", len(json_bytes), "bytes")
print(" JSON gzip :", len(json_gz), "bytes")
var json_back = decompress_gzip(Span[UInt8, _](json_gz))
print(" JSON restored :", String(unsafe_from_utf8=json_back) == json)
print()
# ββ 6. decode_content() dispatch βββββββββββββββββββββββββββββββββββββββββ
print("ββ 6. decode_content() β Content-Encoding dispatch ββ")
var payload = String("the actual HTTP response body")
var body_bytes = payload.as_bytes()
# identity (no decompression)
var body_list_id = List[UInt8](capacity=len(body_bytes))
for b in body_bytes:
body_list_id.append(b)
var decoded_id = decode_content(body_list_id, Encoding.IDENTITY)
print(
" identity β",
String(unsafe_from_utf8=decoded_id),
"(unchanged)",
)
# gzip: compress first, then decode
var gz_bytes = compress_gzip(Span[UInt8, _](body_bytes))
var gz_list = List[UInt8](capacity=len(gz_bytes))
for b in gz_bytes:
gz_list.append(b)
var decoded_gz = decode_content(gz_list, Encoding.GZIP)
print(" gzip β", String(unsafe_from_utf8=decoded_gz))
print()
# ββ 7. Empty input ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
print("ββ 7. Edge cases ββ")
var empty: List[UInt8] = []
var empty_gz = compress_gzip(Span[UInt8, _](empty))
var empty_back = decompress_gzip(Span[UInt8, _](empty_gz))
print(
" empty β compressed:",
len(empty_gz),
"bytes β decompressed:",
len(empty_back),
"bytes",
)
# Single byte
var one: List[UInt8] = [42]
var one_gz = compress_gzip(Span[UInt8, _](one))
var one_back = decompress_gzip(Span[UInt8, _](one_gz))
print(
" [42] β compressed:",
len(one_gz),
"bytes β decompressed:",
Int(one_back[0]),
)
print()
# ββ 8. Error handling: invalid gzip input ββββββββββββββββββββββββββββββββ
print("ββ 8. Error handling ββ")
var garbage: List[UInt8] = [1, 2, 3, 4, 5, 6, 7, 8]
try:
_ = decompress_gzip(Span[UInt8, _](garbage))
print(" ERROR: expected an error for invalid gzip data")
except e:
var msg = String(e)
print(
" β decompress_gzip raised on garbage input:",
String(
unsafe_from_utf8=msg.as_bytes()[: min(40, msg.byte_length())]
),
)
print()
print("=== Example 10 complete ===")