Skip to content

class HeaderUtilities

cangjie
public class HeaderUtilities

功能:提供 HTTP 标头解析和格式化辅助函数。

成员概览

成员功能
let bytesUnit表示字节范围标头使用的单位名称。
func isQuoted(String)检查字符串是否为带引号的 quoted-string。
func removeQuotes(String)去除字符串首尾的双引号。
func unescapeAsQuotedString(String)对 quoted-string 执行去引号和反转义。
func escapeAsQuotedString(String)将字符串转义为 quoted-string。
func tryParseNonNegativeInt64(String)尝试将字符串解析为非负 Int64。
func formatDate(Int64)将 Unix epoch 秒格式化为 RFC 7231 IMF-fixdate 字符串。
func tryStringToDate(String, Int64)尝试将 HTTP-date 字符串解析为时间戳。

let bytesUnit

cangjie
public static let bytesUnit: String = "bytes"

功能:表示字节范围标头使用的单位名称。

类型:String

func isQuoted(String)

cangjie
public static func isQuoted(value: String): Bool

功能:检查字符串是否为带引号的 quoted-string。

参数:

  • value: String - 要检查的字符串。

返回值:

  • Bool - 如果字符串是 quoted-string 则返回 true,否则返回 false。

func removeQuotes(String)

cangjie
public static func removeQuotes(value: String): String

功能:去除字符串首尾的双引号。

参数:

  • value: String - 要处理的字符串。

返回值:

  • String - 去除首尾双引号后的字符串。

func unescapeAsQuotedString(String)

cangjie
public static func unescapeAsQuotedString(value: String): String

功能:对 quoted-string 执行去引号和反转义。

参数:

  • value: String - 要处理的 quoted-string。

返回值:

  • String - 处理后的普通字符串。

func escapeAsQuotedString(String)

cangjie
public static func escapeAsQuotedString(value: String): String

功能:将字符串转义为 quoted-string。

参数:

  • value: String - 要转义的字符串。

返回值:

  • String - 转义后的 quoted-string。

异常:

  • Exception - 当输入包含非法控制字符时抛出。

func tryParseNonNegativeInt64(String)

cangjie
public static func tryParseNonNegativeInt64(input: String): ?Int64

功能:尝试将字符串解析为非负 Int64。

参数:

  • input: String - 要解析的字符串。

返回值:

  • ?Int64 - 解析成功时返回非负整数,否则返回空。

func formatDate(Int64)

cangjie
public static func formatDate(epochSeconds: Int64): String

功能:将 Unix epoch 秒格式化为 RFC 7231 IMF-fixdate 字符串。

参数:

  • epochSeconds: Int64 - Unix epoch 秒值。

返回值:

  • String - RFC 7231 IMF-fixdate 格式的日期字符串。

func tryStringToDate(String, Int64)

cangjie
public static func tryStringToDate(input: String, startIndex: Int64): ?(Int64, Int64)

功能:尝试将 HTTP-date 字符串解析为时间戳。

参数:

  • input: String - 要解析的 HTTP-date 字符串。
  • startIndex: Int64 - 开始解析的位置。

返回值:

  • ?(Int64, Int64) - 解析成功时返回消耗长度和 Unix epoch 秒,否则返回空。