-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* draft proxy * fix proxy * Decompress response before recording & remove header on forwarding to requester Signed-off-by: Alexander Ignition <izh.sever@gmail.com> * Update AppConfiguration * Update ProxyMiddleware * Update Loggers.swift Add MultiplexLogHandler * Add text proxy * Update README.md * Update Proxy example in README.md Co-authored-by: Anton Glezman <a.glezman@redmadrobot.com> Co-authored-by: Vasily Fedorov <vasily.fedorov@alibaba-inc.com> Co-authored-by: Anton Glezman <a.glezman@redmadrobot.com>
- Loading branch information
1 parent
787b830
commit b590424
Showing
14 changed files
with
333 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
Packages/CatbirdApp/Sources/CatbirdApp/Common/HTTPClientSupport.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import Vapor | ||
|
||
extension Request { | ||
/// Send HTTP request. | ||
/// | ||
/// - Parameter configure: client request configuration function. | ||
/// - Returns: Server response. | ||
func send(configure: ((inout ClientRequest) -> Void)? = nil) -> EventLoopFuture<Response> { | ||
return body | ||
.collect(max: nil) | ||
.flatMap { (bytesBuffer: ByteBuffer?) -> EventLoopFuture<Response> in | ||
var clientRequest = self.clientRequest(body: bytesBuffer) | ||
configure?(&clientRequest) | ||
return self.client.send(clientRequest).map { (clientResponse: ClientResponse) -> Response in | ||
clientResponse.response(version: self.version) | ||
} | ||
} | ||
} | ||
|
||
/// Convert to HTTP client request. | ||
private func clientRequest(body: ByteBuffer?) -> ClientRequest { | ||
var headers = self.headers | ||
if let host = headers.first(name: "Host") { | ||
headers.replaceOrAdd(name: "X-Forwarded-Host", value: host) | ||
headers.remove(name: "Host") | ||
} | ||
return ClientRequest(method: method, url: url, headers: headers, body: body) | ||
} | ||
} | ||
|
||
extension HTTPHeaders { | ||
fileprivate var contentLength: Int? { | ||
first(name: "Content-Length").flatMap { Int($0) } | ||
} | ||
} | ||
|
||
extension ClientResponse { | ||
/// Convert to Server Response. | ||
fileprivate func response(version: HTTPVersion) -> Response { | ||
let body = body.map { Response.Body(buffer: $0) } ?? .empty | ||
return Response(status: status, version: version, headers: headers, body: body) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
Packages/CatbirdApp/Sources/CatbirdApp/Middlewares/ProxyMiddleware.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import Vapor | ||
|
||
final class ProxyMiddleware: Middleware { | ||
|
||
func respond(to request: Request, chainingTo next: Responder) -> EventLoopFuture<Response> { | ||
if request.url.host == nil { | ||
request.logger.info("Proxy break \(request.method) \(request.url)") | ||
return next.respond(to: request) | ||
} | ||
return next.respond(to: request).notFound { | ||
var url = request.url | ||
if url.scheme == nil { | ||
url.scheme = url.port == 443 ? "https" : "http" | ||
} | ||
|
||
request.logger.info("Proxy \(request.method) \(url), scheme \(url.scheme ?? "<nil>")") | ||
|
||
// Send request to real host | ||
return request.send { | ||
$0.url = url | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.