Skip to content

Commit 104fb93

Browse files
committed
add support for gzipped text resources
1 parent 8398f81 commit 104fb93

34 files changed

+196
-64
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,7 @@ let package:Package = .init(
543543
.target(name: "GitHubAPI"),
544544
.target(name: "UnidocLinker"),
545545
.target(name: "UnixTime"),
546+
.product(name: "LZ77", package: "swift-png"),
546547
.product(name: "MongoDB", package: "swift-mongodb"),
547548
]),
548549

@@ -625,7 +626,6 @@ let package:Package = .init(
625626
.target(name: "SwiftinitAssets"),
626627
.target(name: "SwiftinitPages"),
627628
.target(name: "SwiftinitPlugins"),
628-
.product(name: "LZ77", package: "swift-png"),
629629
]),
630630

631631

Sources/HTTP/HTTP.Resource.Content.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,21 @@ extension HTTP.Resource
55
@frozen public
66
enum Content:Equatable, Sendable
77
{
8-
case binary([UInt8])
8+
case binary(ArraySlice<UInt8>)
99
case buffer(ByteBuffer)
1010
case string(String)
1111
case length(Int)
1212
}
1313
}
1414
extension HTTP.Resource.Content
15+
{
16+
@inlinable public static
17+
func binary(_ bytes:[UInt8]) -> Self
18+
{
19+
.binary(bytes[...])
20+
}
21+
}
22+
extension HTTP.Resource.Content
1523
{
1624
/// The size of the content to be transferred, in bytes. Unlike ``length``, this property
1725
/// is zero if the content is a ``length(_:)`` cache result.

Sources/HTTP/HTTP.Resource.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,21 @@ extension HTTP
1717
public
1818
var type:MediaType
1919
public
20+
var gzip:Bool
21+
public
2022
var hash:MD5?
2123

2224
@inlinable public
23-
init(headers:Headers = .init(), content:Content, type:MediaType, hash:MD5? = nil)
25+
init(headers:Headers = .init(),
26+
content:Content,
27+
type:MediaType,
28+
gzip:Bool,
29+
hash:MD5? = nil)
2430
{
2531
self.headers = headers
2632
self.content = content
2733
self.type = type
34+
self.gzip = gzip
2835
self.hash = hash
2936
}
3037
}
@@ -34,7 +41,9 @@ extension HTTP.Resource:ExpressibleByStringLiteral, ExpressibleByStringInterpola
3441
@inlinable public
3542
init(stringLiteral:String)
3643
{
37-
self.init(content: .string(stringLiteral), type: .text(.plain, charset: .utf8))
44+
self.init(content: .string(stringLiteral),
45+
type: .text(.plain, charset: .utf8),
46+
gzip: false)
3847
}
3948
}
4049
extension HTTP.Resource

Sources/HTTPServer/Channels/HTTP.ServerMessage.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ extension HTTP.ServerMessage
4747
copying: .init(
4848
headers: .init(),
4949
content: .string(Authority.redact(error: error)),
50-
type: .text(.plain, charset: .utf8)),
50+
type: .text(.plain, charset: .utf8),
51+
gzip: false),
5152
using: allocator)
5253
}
5354
}
@@ -119,5 +120,9 @@ extension HTTP.ServerMessage
119120
self.headers.add(name: "access-control-allow-origin", value: "*")
120121
self.headers.add(name: "content-length", value: "\(length)")
121122
self.headers.add(name: "content-type", value: "\(resource.type)")
123+
if resource.gzip
124+
{
125+
self.headers.add(name: "content-encoding", value: "gzip")
126+
}
122127
}
123128
}

Sources/JSONAST/JSON.EscapeCode.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ extension JSON.EscapeCode
3131
}
3232

3333
@inlinable internal static
34-
func += (utf8:inout [UInt8], self:Self)
34+
func += (utf8:inout ArraySlice<UInt8>, self:Self)
3535
{
3636
utf8.append(0x5C) // '\'
3737
switch self

Sources/JSONAST/JSON.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
struct JSON:Sendable
33
{
44
public
5-
var utf8:[UInt8]
5+
var utf8:ArraySlice<UInt8>
66

77
@inlinable public
8-
init(utf8:[UInt8])
8+
init(utf8:ArraySlice<UInt8>)
99
{
1010
self.utf8 = utf8
1111
}

Sources/PackageMetadataTests/Main.PackageResolved.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ extension Main.PackageResolved:TestBattery
102102
tests.do
103103
{
104104
let filepath:FilePath = "Package.resolved"
105-
let json:JSON = .init(utf8: try filepath.read())
105+
let json:JSON = .init(utf8: try filepath.read()[...])
106106
let _:SPM.DependencyResolutions = try json.decode()
107107
}
108108
}

Sources/SwiftinitPages/Surfaces/Swiftinit.MediaEndpoint.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,26 @@ extension Swiftinit.MediaEndpoint
2929
}
3030

3131
let content:HTTP.Resource.Content
32-
switch output.utf8
32+
let gzipped:Bool
33+
switch output.text
3334
{
34-
case .binary(let utf8): content = .binary(utf8)
35-
case .length(let bytes): content = .length(bytes)
35+
case .inline(.gzip(let gzip)):
36+
content = .binary(gzip)
37+
gzipped = true
38+
39+
case .inline(.utf8(let utf8)):
40+
content = .binary(utf8)
41+
gzipped = false
42+
43+
case .length(let bytes):
44+
content = .length(bytes)
45+
gzipped = false // ???
3646
}
3747

3848
return .ok(.init(
3949
content: content,
4050
type: self.type,
51+
gzip: gzipped,
4152
hash: output.hash))
4253
}
4354
}

Sources/SwiftinitPages/Surfaces/Swiftinit.SitemapEndpoint.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ extension Swiftinit.SitemapEndpoint:HTTP.ServerEndpoint
6262

6363
return .ok(.init(content: .string(string),
6464
type: .text(.plain, charset: .utf8),
65+
gzip: false,
6566
hash: output.sitemap.hash))
6667
}
6768
}

Sources/SwiftinitPages/Surfaces/Swiftinit.VertexEndpoint.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ extension Swiftinit.VertexEndpoint where Self:HTTP.ServerEndpoint
4848
{
4949
return .notFound(.init(
5050
content: .string("Snapshot not found."),
51-
type: .text(.plain, charset: .utf8)))
51+
type: .text(.plain, charset: .utf8),
52+
gzip: false))
5253
}
5354

5455
guard

0 commit comments

Comments
 (0)