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

update of LittleProxy 0.5.3 to work with Netty 3.10.4.Final #223

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
<version>3.6.5.Final</version>
<version>3.10.4.Final</version>
<scope>compile</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void addHandler(final ProxyAuthorizationHandler pah) {

public boolean handleProxyAuthorization(final HttpRequest request,
final ChannelHandlerContext ctx) {
if (!request.containsHeader(HttpHeaders.Names.PROXY_AUTHORIZATION)) {
if (!request.headers().contains(HttpHeaders.Names.PROXY_AUTHORIZATION)) {
if (!handlers.isEmpty()) {
rejectRequest(ctx);
return false;
Expand All @@ -41,7 +41,7 @@ public boolean handleProxyAuthorization(final HttpRequest request,
}

final List<String> values =
request.getHeaders(HttpHeaders.Names.PROXY_AUTHORIZATION);
request.headers().getAll(HttpHeaders.Names.PROXY_AUTHORIZATION);
final String fullValue = values.iterator().next();
final String value =
StringUtils.substringAfter(fullValue, "Basic ").trim();
Expand All @@ -64,9 +64,9 @@ public boolean handleProxyAuthorization(final HttpRequest request,
log.info("Got proxy authorization!");
// We need to remove the header before sending the request on.
final String authentication =
request.getHeader(HttpHeaders.Names.PROXY_AUTHORIZATION);
request.headers().get(HttpHeaders.Names.PROXY_AUTHORIZATION);
log.info(authentication);
request.removeHeader(HttpHeaders.Names.PROXY_AUTHORIZATION);
request.headers().remove(HttpHeaders.Names.PROXY_AUTHORIZATION);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,26 +208,26 @@ private boolean isCacheable(final HttpRequest httpRequest,
}

// Don't use the cache if the request has cookies -- security violation.
if (httpResponse.containsHeader(HttpHeaders.Names.SET_COOKIE)) {
if (httpResponse.headers().contains(HttpHeaders.Names.SET_COOKIE)) {
log.info("Response contains set cookie header");
return false;
}
if (httpResponse.containsHeader(HttpHeaders.Names.SET_COOKIE2)) {
if (httpResponse.headers().contains(HttpHeaders.Names.SET_COOKIE2)) {
log.info("Response contains set cookie2 header");
return false;
}

/*
if (httpRequest.containsHeader(HttpHeaders.Names.COOKIE)) {
if (httpRequest.headers().contains(HttpHeaders.Names.COOKIE)) {
log.info("Request contains Cookie header");
return false;
}
*/

final List<String> responseControl =
httpResponse.getHeaders(HttpHeaders.Names.CACHE_CONTROL);
httpResponse.headers().getAll(HttpHeaders.Names.CACHE_CONTROL);
final List<String> requestControl =
httpRequest.getHeaders(HttpHeaders.Names.CACHE_CONTROL);
httpRequest.headers().getAll(HttpHeaders.Names.CACHE_CONTROL);
final Set<String> cacheControl = new HashSet<String>();
cacheControl.addAll(requestControl);
cacheControl.addAll(responseControl);
Expand Down Expand Up @@ -261,15 +261,15 @@ private boolean isCacheable(final HttpRequest httpRequest,
}

final String responsePragma =
httpResponse.getHeader(HttpHeaders.Names.PRAGMA);
httpResponse.headers().get(HttpHeaders.Names.PRAGMA);
if (StringUtils.isNotBlank(responsePragma) &&
responsePragma.contains(HttpHeaders.Values.NO_CACHE)) {
log.info("Not caching with response pragma no cache");
return false;
}

final String requestPragma =
httpRequest.getHeader(HttpHeaders.Names.PRAGMA);
httpRequest.headers().get(HttpHeaders.Names.PRAGMA);
if (StringUtils.isNotBlank(requestPragma) &&
requestPragma.contains(HttpHeaders.Values.NO_CACHE)) {
log.info("Not caching with request pragma no cache");
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/org/littleshoot/proxy/HttpRelayingHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,16 @@ public void messageReceived(final ChannelHandlerContext ctx,
final HttpResponse response;

// Double check the Transfer-Encoding, since it gets tricky.
final String te = hr.getHeader(HttpHeaders.Names.TRANSFER_ENCODING);
final String te = hr.headers().get(HttpHeaders.Names.TRANSFER_ENCODING);
if (StringUtils.isNotBlank(te) &&
te.equalsIgnoreCase(HttpHeaders.Values.CHUNKED)) {
if (hr.getProtocolVersion() != HttpVersion.HTTP_1_1) {
log.warn("Fixing HTTP version.");
response = ProxyUtils.copyMutableResponseFields(hr,
new DefaultHttpResponse(HttpVersion.HTTP_1_1, hr.getStatus()));
if (!response.containsHeader(HttpHeaders.Names.TRANSFER_ENCODING)) {
if (!response.headers().contains(HttpHeaders.Names.TRANSFER_ENCODING)) {
log.debug("Adding chunked encoding header");
response.addHeader(HttpHeaders.Names.TRANSFER_ENCODING,
response.headers().add(HttpHeaders.Names.TRANSFER_ENCODING,
HttpHeaders.Values.CHUNKED);
}
}
Expand Down Expand Up @@ -340,11 +340,11 @@ public void channelConnected(final ChannelHandlerContext ctx,
}

private boolean closeEndsResponseBody(final HttpResponse res) {
final String cl = res.getHeader(HttpHeaders.Names.CONTENT_LENGTH);
final String cl = res.headers().get(HttpHeaders.Names.CONTENT_LENGTH);
if (StringUtils.isNotBlank(cl)) {
return false;
}
final String te = res.getHeader(HttpHeaders.Names.TRANSFER_ENCODING);
final String te = res.headers().get(HttpHeaders.Names.TRANSFER_ENCODING);
if (StringUtils.isNotBlank(te) &&
te.equalsIgnoreCase(HttpHeaders.Values.CHUNKED)) {
return false;
Expand Down Expand Up @@ -386,13 +386,13 @@ private boolean shouldCloseBrowserConnection(final HttpRequest req,
// Switch the de-facto standard "Proxy-Connection" header to
// "Connection" when we pass it along to the remote host.
final String proxyConnectionKey = "Proxy-Connection";
if (req.containsHeader(proxyConnectionKey)) {
final String header = req.getHeader(proxyConnectionKey);
req.removeHeader(proxyConnectionKey);
if (req.headers().contains(proxyConnectionKey)) {
final String header = req.headers().get(proxyConnectionKey);
req.headers().remove(proxyConnectionKey);
if (req.getProtocolVersion() == HttpVersion.HTTP_1_1) {
log.debug("Switching Proxy-Connection to Connection for " +
"analyzing request for close");
req.setHeader("Connection", header);
req.headers().set("Connection", header);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/littleshoot/proxy/HttpRequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ private void processRequest(final ChannelHandlerContext ctx,
hostAndPort = ProxyUtils.parseHostAndPort(request);
if (StringUtils.isBlank(hostAndPort)) {
final List<String> hosts =
request.getHeaders(HttpHeaders.Names.HOST);
request.headers().getAll(HttpHeaders.Names.HOST);
if (hosts != null && !hosts.isEmpty()) {
hostAndPort = hosts.get(0);
} else {
Expand Down Expand Up @@ -550,11 +550,11 @@ private void badGateway(final HttpRequest request,
final HttpResponse response =
new DefaultHttpResponse(HttpVersion.HTTP_1_1,
HttpResponseStatus.BAD_GATEWAY);
response.setHeader(HttpHeaders.Names.CONNECTION, "close");
response.headers().set(HttpHeaders.Names.CONNECTION, "close");
final String body = "Bad Gateway: "+request.getUri();
response.setContent(ChannelBuffers.copiedBuffer(body,
Charset.forName("UTF-8")));
response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, body.length());
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, body.length());
inboundChannel.write(response);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void channelIdle(final ChannelHandlerContext ctx,
for (HttpRequest unansweredRequest : unansweredHttpRequests) {
// Go through each unanswered request and concat the info
message.append(unansweredRequest.getUri());
String referrer = unansweredRequest.getHeader("Referer");
String referrer = unansweredRequest.headers().get("Referer");
if (!StringUtils.isBlank(referrer)) {
// Capture the referrer so that slow resources can be tracked
// to a page
Expand Down
48 changes: 24 additions & 24 deletions src/main/java/org/littleshoot/proxy/ProxyUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public static String stripHost(final String uri) {
* @return The cache URI.
*/
public static String cacheUri(final HttpRequest httpRequest) {
final String host = httpRequest.getHeader(HttpHeaders.Names.HOST);
final String host = httpRequest.headers().get(HttpHeaders.Names.HOST);
final String uri = httpRequest.getUri();
final String path;
if (HTTP_PREFIX.matcher(uri).matches()) {
Expand Down Expand Up @@ -236,10 +236,10 @@ public static String httpDate() {
public static HttpResponse copyMutableResponseFields(
final HttpResponse original, final HttpResponse copy) {

final Collection<String> headerNames = original.getHeaderNames();
final Collection<String> headerNames = original.headers().names();
for (final String name : headerNames) {
final List<String> values = original.getHeaders(name);
copy.setHeader(name, values);
final List<String> values = original.headers().getAll(name);
copy.headers().set(name, values);
}
copy.setContent(original.getContent());
if (original.isChunked()) {
Expand Down Expand Up @@ -295,9 +295,9 @@ public static void printHeaders(final HttpMessage msg) {
final String status = msg.getProtocolVersion().toString();
LOG.debug(status);
final StringBuilder sb = new StringBuilder();
final Set<String> headerNames = msg.getHeaderNames();
final Set<String> headerNames = msg.headers().names();
for (final String name : headerNames) {
final String value = msg.getHeader(name);
final String value = msg.headers().get(name);
sb.append(name);
sb.append(": ");
sb.append(value);
Expand All @@ -313,7 +313,7 @@ public static void printHeaders(final HttpMessage msg) {
* @param name The name of the header to print.
*/
public static void printHeader(final HttpMessage msg, final String name) {
final String value = msg.getHeader(name);
final String value = msg.headers().get(name);
LOG.debug(name + ": "+value);
}

Expand Down Expand Up @@ -387,7 +387,7 @@ public static String parseHostAndPort(final String uri) {
}

public static String parseHost(final HttpRequest request) {
final String host = request.getHeader(HttpHeaders.Names.HOST);
final String host = request.headers().get(HttpHeaders.Names.HOST);
if (StringUtils.isNotBlank(host)) {
return host;
}
Expand Down Expand Up @@ -474,11 +474,11 @@ public static HttpRequest copyHttpRequest(final HttpRequest original,
LOG.debug("Request copy method: {}", copy.getMethod());
copyHeaders(original, copy);

final String ae = copy.getHeader(HttpHeaders.Names.ACCEPT_ENCODING);
final String ae = copy.headers().get(HttpHeaders.Names.ACCEPT_ENCODING);
if (StringUtils.isNotBlank(ae)) {
// Remove sdch from encodings we accept since we can't decode it.
final String noSdch = ae.replace(",sdch", "").replace("sdch", "");
copy.setHeader(HttpHeaders.Names.ACCEPT_ENCODING, noSdch);
copy.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, noSdch);
LOG.debug("Removed sdch and inserted: {}", noSdch);
}

Expand All @@ -487,10 +487,10 @@ public static HttpRequest copyHttpRequest(final HttpRequest original,
// largely undocumented but seems to be what most browsers and servers
// expect.
final String proxyConnectionKey = "Proxy-Connection";
if (copy.containsHeader(proxyConnectionKey)) {
final String header = copy.getHeader(proxyConnectionKey);
copy.removeHeader(proxyConnectionKey);
copy.setHeader("Connection", header);
if (copy.headers().contains(proxyConnectionKey)) {
final String header = copy.headers().get(proxyConnectionKey);
copy.headers().remove(proxyConnectionKey);
copy.headers().set("Connection", header);
}

ProxyUtils.addVia(copy);
Expand All @@ -499,11 +499,11 @@ public static HttpRequest copyHttpRequest(final HttpRequest original,

private static void copyHeaders(final HttpMessage original,
final HttpMessage copy) {
final Set<String> headerNames = original.getHeaderNames();
final Set<String> headerNames = original.headers().names();
for (final String name : headerNames) {
if (!hopByHopHeaders.contains(name.toLowerCase())) {
final List<String> values = original.getHeaders(name);
copy.setHeader(name, values);
final List<String> values = original.headers().getAll(name);
copy.headers().set(name, values);
}
}
}
Expand All @@ -515,10 +515,10 @@ private static void copyHeaders(final HttpMessage original,
* @param msg The message to strip headers from.
*/
public static void stripHopByHopHeaders(final HttpMessage msg) {
final Set<String> headerNames = msg.getHeaderNames();
final Set<String> headerNames = msg.headers().names();
for (final String name : headerNames) {
if (hopByHopHeaders.contains(name.toLowerCase())) {
msg.removeHeader(name);
msg.headers().remove(name);
}
}
}
Expand Down Expand Up @@ -548,14 +548,14 @@ public static void addVia(final HttpMessage msg) {
sb.append(".");
sb.append(hostName);
final List<String> vias;
if (msg.containsHeader(HttpHeaders.Names.VIA)) {
vias = msg.getHeaders(HttpHeaders.Names.VIA);
if (msg.headers().contains(HttpHeaders.Names.VIA)) {
vias = msg.headers().getAll(HttpHeaders.Names.VIA);
vias.add(sb.toString());
}
else {
vias = Arrays.asList(sb.toString());
}
msg.setHeader(HttpHeaders.Names.VIA, vias);
msg.headers().set(HttpHeaders.Names.VIA, vias);
}

/**
Expand All @@ -571,12 +571,12 @@ public static Charset detectCharset(HttpResponse http) {

Charset headerCharset = CharsetUtil.ISO_8859_1; // Default charset for detection is latin-1

if (http.getHeader("Content-Type") != null) { // If has Content-Type header, try to detect charset from it
if (http.headers().get("Content-Type") != null) { // If has Content-Type header, try to detect charset from it

String header_pattern = "^\\s*?.*?\\s*?charset\\s*?=\\s*?(.*?)$"; // How to find charset in header

Pattern pattern = Pattern.compile(header_pattern, Pattern.CASE_INSENSITIVE); // Set Pattern Matcher to
Matcher matcher = pattern.matcher(http.getHeader("Content-Type")); // find charset in header
Matcher matcher = pattern.matcher(http.headers().get("Content-Type")); // find charset in header

if (matcher.find()) { // If there is a charset definition

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private RegexHttpRequestFilter(final String hostRegex,

public void filter(final HttpRequest httpRequest) {
if (filterHosts) {
final List<String> hosts = httpRequest.getHeaders("Host");
final List<String> hosts = httpRequest.headers().getAll("Host");
if (hosts != null) {
if (!hosts.isEmpty()) {
final String host = hosts.get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class DefaultProxyCacheManagerTest {
"http://www.littleshoot.org");
final HttpResponse httpResponse =
new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
httpResponse.setHeader(HttpHeaders.Names.CACHE_CONTROL, HttpHeaders.Values.PUBLIC);
httpResponse.headers().set(HttpHeaders.Names.CACHE_CONTROL, HttpHeaders.Values.PUBLIC);
final class PubEncoder extends HttpResponseEncoder {
public Object pubEncode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
return encode(ctx, channel, msg);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/littleshoot/proxy/HttpProxyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ private void readCrLf(final InputStream is) throws IOException {
crlf[1] = (byte) lf;
final ChannelBuffer buf = ChannelBuffers.wrappedBuffer(crlf);
throw new Error("Did not get expected CRLF!! Instead got hex: "+
ChannelBuffers.hexDump(buf)+" and str: "+buf.toString("US-ASCII"));
ChannelBuffers.hexDump(buf)+" and str: "+buf.toString(java.nio.charset.Charset.forName("US-ASCII")));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private void doNotMatch(String uri) throws URISyntaxException {

private HttpRequest createRequest(String url) {
final HttpRequest request = new DefaultHttpRequest(HTTP_1_0, GET, ProxyUtils.stripHost(url));
request.setHeader("Host", ProxyUtils.parseHost(url));
request.headers().set("Host", ProxyUtils.parseHost(url));
return request;
}

Expand Down