Skip to content

Commit 57a8ff3

Browse files
committed
Merge branch 'develop'
2 parents 65fd4b9 + 37d6758 commit 57a8ff3

12 files changed

+478
-168
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ let package = Package(
1717

1818
dependencies: [
1919
.package(url: "https://github.com/Macro-swift/Macro.git",
20-
from: "1.0.4"),
20+
from: "1.0.10"),
2121
.package(url: "https://github.com/AlwaysRightInstitute/mustache.git",
2222
from: "1.0.2")
2323
],

Sources/connect/BodyParser.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ public extension BodyParserBody {
247247
guard let value = self[key] else { return nil }
248248
switch value { // TBD
249249
case let v as Int : return v
250-
case let v as Int64 : return Int(v)
250+
case let v as Int64 : return Int(clamping: v)
251251
case let v as String : return Int(v)
252252
case let v as Double : return Int(v)
253253
default: return nil

Sources/connect/Connect.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Noze.io / Macro
44
//
55
// Created by Helge Heß on 5/3/16.
6-
// Copyright © 2016-2020 ZeeZide GmbH. All rights reserved.
6+
// Copyright © 2016-2025 ZeeZide GmbH. All rights reserved.
77
//
88

99
import class http.Server
@@ -194,6 +194,7 @@ public extension Connect {
194194
}
195195

196196
import func MacroCore.__dirname
197+
// TBD: can we make those internal? They produce ambiguities.
197198

198199
#if swift(>=5.3)
199200
@inlinable

Sources/connect/Cookies.swift

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import let MacroCore.console
1010
import class http.IncomingMessage
1111
import class http.ServerResponse
1212

13+
1314
// "Set-Cookie:" Name "=" Value *( ";" Attribute)
1415
// "Cookie:" Name "=" Value *( ";" Name "=" Value)
1516
//
@@ -21,11 +22,12 @@ import class http.ServerResponse
2122
/// Module and object at the same time
2223
///
2324
/// Usage:
24-
///
25-
/// let cookies = Cookies(req, res)
26-
///
27-
/// cookies.set("theAnswer", "42") // set a cookie
28-
/// if let answer = cookies.get("theAnswer") // get a cookie
25+
/// ```swift
26+
/// let cookies = Cookies(req, res)
27+
///
28+
/// cookies.set("theAnswer", "42") // set a cookie
29+
/// if let answer = cookies.get("theAnswer") // get a cookie
30+
/// ```
2931
///
3032
public struct Cookies {
3133

@@ -37,24 +39,33 @@ public struct Cookies {
3739
self.res = res
3840

3941
// request values we care about
40-
self.cookies = req.extractStringCookieDictionary()
42+
self.cookies = req.extractStringCookieDictionary()
4143
}
4244

4345

4446
// get/set funcs
4547

48+
@inlinable
4649
public func get(_ name: String) -> String? {
4750
return cookies[name]
4851
}
4952

53+
@inlinable
5054
public func set(cookie c: Cookie) {
51-
guard res != nil else {
55+
guard let res = res else {
5256
console.warn("attempt to set cookie, but got no response object!")
5357
return
5458
}
55-
res!.setHeader("Set-Cookie", c.description)
59+
res.setHeader("Set-Cookie", c.httpHeaderValue)
60+
}
61+
62+
@inlinable
63+
public func set(_ cookie: Cookie) {
64+
// TODO: convenience, deprecate one
65+
set(cookie: cookie)
5666
}
5767

68+
@inlinable
5869
public func set(_ name: String, _ value: String,
5970
path : String? = "/",
6071
httpOnly : Bool = true,
@@ -73,12 +84,14 @@ public struct Cookies {
7384
set(cookie: cookie)
7485
}
7586

87+
@inlinable
7688
public func reset(_ name: String) {
7789
set(cookie: Cookie(name: name, maxAge: 0))
7890
}
7991

8092
// subscript
8193

94+
@inlinable
8295
public subscript(name : String) -> String? {
8396
set {
8497
if let newValue = newValue {
@@ -88,13 +101,12 @@ public struct Cookies {
88101
console.error("attempt to set nil-value cookie: \(name), ignoring.")
89102
}
90103
}
91-
get {
92-
return get(name)
93-
}
104+
get { return get(name) }
94105
}
95106
}
96107

97108
extension Cookies: CustomStringConvertible {
109+
98110
public var description: String {
99111
var ms = "<Cookies:"
100112
if cookies.isEmpty {
@@ -122,13 +134,17 @@ extension Cookies: CustomStringConvertible {
122134
}
123135
}
124136

137+
// Expose the type under the "cookies" name for naming compat.
125138
public let cookies = Cookies.self
126139

127140
// MARK: - Internals
128141

142+
// TBD(2025-08-26): The new shared Foundation might have a proper Cookie parser?
143+
129144
import struct Foundation.Date
130145

131146
public struct Cookie {
147+
132148
public let name : String
133149
public var value : String
134150
public var path : String?
@@ -263,8 +279,9 @@ extension String {
263279
}
264280
}
265281

282+
/// "Cookie: a=10; b=20" => [ "a=10", "b=20" ]
266283
private func splitCookieFields(headerValue v: String) -> [ String ] {
267-
return v.splitAndTrim(splitchar: 59) // semicolon
284+
return v.splitAndTrim(splitchar: 59) // semicolon (multiple cookies in line)
268285
}
269286

270287
private extension IncomingMessage {

Sources/express/BasicAuth.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
// Macro
44
//
55
// Created by Helge Heß on 6/3/16.
6-
// Copyright © 2020-2023 ZeeZide GmbH. All rights reserved.
6+
// Copyright © 2020-2025 ZeeZide GmbH. All rights reserved.
77
//
88

99
#if canImport(Foundation)
1010
import Foundation
1111
#endif
1212
import http
1313
import protocol MacroCore.EnvironmentKey
14+
import struct MacroCore.EnvironmentValues
1415

1516
public enum BasicAuthModule {}
1617
public typealias expressBasicAuth = BasicAuthModule
@@ -245,6 +246,14 @@ private enum BasicAuthUserKey: EnvironmentKey {
245246
static let loggingKey = "user"
246247
}
247248

249+
public extension EnvironmentValues {
250+
251+
var authenticatedBasicAuthUser : String {
252+
get { return self[BasicAuthUserKey.self] }
253+
set { return self[BasicAuthUserKey.self] = newValue }
254+
}
255+
}
256+
248257
public extension IncomingMessage {
249258

250259
/**

0 commit comments

Comments
 (0)