Repository: vapor/vapor
File: Sources/Vapor/Engine/EngineClient.swift (Swift)

Code before:
                
                if req.http.uri.path.last == "/" {
                    req.http.uri.path += path
                } else {
                    var components = req.http.uri.path.split(separator: "/")
                    components.removeLast()
                    req.http.uri.path = components.joined(separator: "/") + "/" + path
                }
            }
        }
        
        return self.respond(to: req, redirecting: redirecting - 1)
    }
    
    /// Processes an HTTP esponse and acts upon redirects accordingly
    private func response(
        from httpRes: HTTPResponse,
        for req: Request,
        redirecting: Int
    ) throws -> Future<Response> {
        if httpRes.status.code >= 300 && httpRes.status.code < 400 {
            switch httpRes.status.code {
            case 301, 307, 308:
                return try redirect(httpRes, for: req, redirecting: redirecting)
            case 302, 303:
                req.http.method = .get
                return try redirect(httpRes, for: req, redirecting: redirecting)
            default: break
            }
        }
        
        let res = req.makeResponse()
        res.http = httpRes
        return Future(res)
    }

    /// Responds to a Request using TLS client.
    private func tlsRespond(to req: Request, redirecting: Int) throws -> Future<Response> {
        let tcpSocket = try TCPSocket(isNonBlocking: true)
        let tcpClient = try TCPClient(socket: tcpSocket)
        var settings = TLSClientSettings()
        let hostname = try req.http.uri.requireHostname()
        settings.peerDomainName = hostname
        #if os(macOS)
            let tlsClient = try AppleTLSClient(tcp: tcpClient, using: settings)
        #else
            let tlsClient = try OpenSSLClient(tcp: tcpClient, using: settings)
        #endif
        try tlsClient.connect(hostname: hostname, port: req.http.uri.port ?? 443)
        let client = HTTPClient(
            stream: tlsClient.socket.stream(on: self.container),

Review comment (suggestion):
When transforming to GET, make sure to remove any request body and Content-Type header.

Code after:
        from httpRes: HTTPResponse,
        for req: Request,
        maxRedirects redirects: Int
    ) throws -> Future<Response> {
        if httpRes.status.code >= 300 && httpRes.status.code < 400 {
            switch httpRes.status.code {
            case 301, 307, 308:
                return try redirect(httpRes, for: req, maxRedirects: redirects)
            case 302, 303:
                req.http.method = .get
                req.http.body = HTTPBody()
                req.http.mediaType = nil
                return try redirect(httpRes, for: req, maxRedirects: redirects)
            default: break
            }
        }
        
        let res = req.makeResponse()
        res.http = httpRes
        return Future(res)
    }

    /// Responds to a Request using TLS client.
    private func tlsRespond(to req: Request, maxRedirects redirects: Int) throws -> Future<Response> {
        let tcpSocket = try TCPSocket(isNonBlocking: true)
        let tcpClient = try TCPClient(socket: tcpSocket)
        var settings = TLSClientSettings()
        let hostname = try req.http.uri.requireHostname()
        settings.peerDomainName = hostname
        #if os(macOS)
            let tlsClient = try AppleTLSClient(tcp: tcpClient, using: settings)
        #else
            let tlsClient = try OpenSSLClient(tcp: tcpClient, using: settings)
        #endif
        try tlsClient.connect(hostname: hostname, port: req.http.uri.port ?? 443)
        let client = HTTPClient(
            stream: tlsClient.socket.stream(on: self.container),
            on: self.container
        )
        req.http.headers[.host] = hostname
        return client.send(req.http).flatMap(to: Response.self) { httpRes in
            tlsClient.close()
            
            return try self.response(from: httpRes, for: req, maxRedirects: redirects)
        }
    }

    /// Responds to a Request using TCP client.
    private func plaintextRespond(to req: Request, maxRedirects redirects: Int) throws -> Future<Response> {
        let tcpSocket = try TCPSocket(isNonBlocking: true)
        let tcpClient = try TCPClient(socket: tcpSocket)