-
-
Save LCHCAPITALHUMAIN/94e9644d0cda12b14a9ed370a508fc62 to your computer and use it in GitHub Desktop.
Ratpack HTTP Proxy
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
import org.slf4j.Logger | |
import org.slf4j.LoggerFactory | |
import ratpack.handling.Context | |
import ratpack.handling.Handler | |
import ratpack.http.client.HttpClient | |
import ratpack.http.client.StreamedResponse | |
import ratpack.http.MutableHeaders | |
import ratpack.http.client.RequestSpec | |
import static ratpack.groovy.Groovy.ratpack | |
final Logger log = LoggerFactory.getLogger(Ratpack.class); | |
class BlockedHostHandler implements Handler { | |
def blockedHosts = ['www.asos.co.uk'] | |
@Override | |
void handle(Context context) throws Exception { | |
if (blockedHosts.contains(context.request.headers.host)) { | |
context.redirect(302, "http://ratpack-foaas.herokuapp.com/you/no $context.request.headers.host allowed/the Boss") | |
} | |
context.next() | |
} | |
} | |
ratpack { | |
handlers { | |
handler new BlockedHostHandler() | |
handler { HttpClient httpClient -> | |
log.info("Proxy to: $request.rawUri") | |
httpClient.streamRequest(new URI(request.rawUri)) { RequestSpec spec -> | |
spec.headers.copy(request.headers) | |
} then { StreamedResponse responseStream -> | |
responseStream.send(response) { MutableHeaders headers -> | |
def via = '1.1 Ratpack Proxy' | |
via = headers.get('Via') ? via + ', ' + headers.get('Via') : via | |
headers.set('Via', via) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment