Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date is added to HTTPResponse headers #37

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions Sources/PerfectHTTPServer/HTTP11/HTTP11Response.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import PerfectNet
import PerfectThread
import PerfectHTTP
import PerfectLib

class HTTP11Response: HTTPResponse {
var status = HTTPResponseStatus.ok
Expand Down Expand Up @@ -149,21 +150,27 @@ class HTTP11Response: HTTPResponse {
}
}

func pushHeaders(callback: @escaping (Bool) -> ()) {
wroteHeaders = true
if isKeepAlive {
addHeader(.connection, value: "keep-alive")
func pushHeaders(callback: @escaping (Bool) -> ()) {
wroteHeaders = true
if isKeepAlive {
addHeader(.connection, value: "keep-alive")
}
if isStreaming {
addHeader(.transferEncoding, value: "chunked")
} else if !contentLengthSet {
addHeader(.contentLength, value: "\(bodyBytes.count)")
}
var posixTime = timeval()
gettimeofday(&posixTime, nil)
let timeOfDay = Double((posixTime.tv_sec * 1000) + (Int(posixTime.tv_usec)/1000))
if let formattedDate = try? formatDate(timeOfDay, format: "%a, %d-%b-%Y %T GMT") {
setHeader(.date, value: formattedDate)
}
if isStreaming {
addHeader(.transferEncoding, value: "chunked")
} else if !contentLengthSet {
addHeader(.contentLength, value: "\(bodyBytes.count)")
}
if let filters = self.filters {
return filterHeaders(allFilters: filters, callback: callback)
}
finishPushHeaders(callback: callback)
}
}

func filterHeaders(allFilters: IndexingIterator<[[HTTPResponseFilter]]>, callback: @escaping (Bool) -> ()) {
var allFilters = allFilters
Expand Down
12 changes: 12 additions & 0 deletions Sources/PerfectHTTPServer/HTTP2/HTTP2Response.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
//===----------------------------------------------------------------------===//
//

#if os(Linux)
import SwiftGlibc
#else
import Darwin
#endif

import PerfectLib
import PerfectHTTP
import PerfectThread
Expand Down Expand Up @@ -97,6 +103,12 @@ final class HTTP2Response: HTTPResponse {
guard h2Request.streamState != .closed else {
return callback(false)
}
var posixTime = timeval()
gettimeofday(&posixTime, nil)
let timeOfDay = Double((posixTime.tv_sec * 1000) + (Int(posixTime.tv_usec)/1000))
if let formattedDate = try? formatDate(timeOfDay, format: "%a, %d-%b-%Y %T GMT") {
setHeader(.date, value: formattedDate)
}
if let filters = self.filters {
filterHeaders(allFilters: filters, callback: callback)
} else {
Expand Down