Skip to content

Commit

Permalink
Moved the response header output to be before the error check
Browse files Browse the repository at this point in the history
Fixes #40
  • Loading branch information
stevenklassen8376 committed Jun 2, 2022
1 parent 5971a14 commit 0280d1c
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions Sources/CrestLib/Operation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ final class ResponseDelegate: HTTPClientResponseDelegate {
var needNewline = false

func didReceiveHead(task: HTTPClient.Task<Void>, _ head: HTTPResponseHead) -> EventLoopFuture<Void> {
outputResponseHeaders(head)
if head.status != .ok {
error = OperationError.httpError(head.status)
} else {
Expand All @@ -136,28 +137,6 @@ final class ResponseDelegate: HTTPClientResponseDelegate {
prettyPrintIsPossible = true
}
}
if Configuration.shared.showResponseHeaders {
print("Headers:")
print(" \(head.version) \(head.status.code) \(head.status)".uppercased())
var maxLen = 0
for header in head.headers {
let len = header.name.count
if len > maxLen && len <= 25 {
maxLen = len
}
}
if maxLen > 25 {
maxLen = 25
}
for header in head.headers {
var name = header.name + ":"
if name.count < maxLen+1 {
name = name.padding(toLength: maxLen+1, withPad: " ", startingAt: 0)
}
print(" \(name) \(header.value)")
}
print("Content:")
}
}
return task.eventLoop.makeSucceededVoidFuture()
}
Expand Down Expand Up @@ -196,6 +175,31 @@ final class ResponseDelegate: HTTPClientResponseDelegate {
|| contentType.starts(with: "text/xml")
|| contentType.starts(with: "application/json")
}

func outputResponseHeaders(_ head: HTTPResponseHead) {
if Configuration.shared.showResponseHeaders {
print("Headers:")
print(" \(head.version) \(head.status.code) \(head.status)".uppercased())
var maxLen = 0
for header in head.headers {
let len = header.name.count
if len > maxLen && len <= 25 {
maxLen = len
}
}
if maxLen > 25 {
maxLen = 25
}
for header in head.headers {
var name = header.name + ":"
if name.count < maxLen+1 {
name = name.padding(toLength: maxLen+1, withPad: " ", startingAt: 0)
}
print(" \(name) \(header.value)")
}
print("Content:")
}
}
}


Expand Down

0 comments on commit 0280d1c

Please sign in to comment.