Skip to content

Commit

Permalink
add http proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
闫逍旭 committed Feb 17, 2016
1 parent ad01478 commit 780766b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 11 deletions.
Expand Up @@ -3,9 +3,11 @@
import com.shinemo.mpush.api.connection.Connection;
import com.shinemo.mpush.api.protocol.Command;
import com.shinemo.mpush.api.protocol.Packet;
import com.shinemo.mpush.tools.Constants;
import com.shinemo.mpush.tools.MPushUtil;
import io.netty.buffer.ByteBuf;

import java.util.Arrays;
import java.util.Map;

/**
Expand Down Expand Up @@ -42,7 +44,6 @@ public void encode(ByteBuf body) {
}



public String getMethod() {
switch (method) {
case 0:
Expand All @@ -56,4 +57,14 @@ public String getMethod() {
}
return "GET";
}

@Override
public String toString() {
return "HttpRequestMessage{" +
"method=" + method +
", uri='" + uri + '\'' +
", headers=" + headers +
", body=" + (body == null ? "" : new String(body, Constants.UTF_8)) +
'}';
}
}
Expand Up @@ -2,9 +2,11 @@

import com.shinemo.mpush.api.connection.Connection;
import com.shinemo.mpush.api.protocol.Packet;
import com.shinemo.mpush.tools.Constants;
import com.shinemo.mpush.tools.MPushUtil;
import io.netty.buffer.ByteBuf;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -55,4 +57,14 @@ public HttpResponseMessage addHeader(String name, String value) {
this.headers.put(name, value);
return this;
}

@Override
public String toString() {
return "HttpResponseMessage{" +
"statusCode=" + statusCode +
", reasonPhrase='" + reasonPhrase + '\'' +
", headers=" + headers +
", body=" + (body == null ? "" : new String(body, Constants.UTF_8)) +
'}';
}
}
Expand Up @@ -13,6 +13,8 @@
import com.shinemo.mpush.tools.MPushUtil;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetSocketAddress;
import java.net.URI;
Expand All @@ -25,14 +27,15 @@
* Created by ohun on 2016/2/15.
*/
public class HttpProxyHandler extends BaseMessageHandler<HttpRequestMessage> {
private static final Logger LOGGER = LoggerFactory.getLogger(HttpProxyHandler.class);
private final NettyHttpClient httpClient;
private final DnsMapping dnsMapping;

public HttpProxyHandler() {
this.httpClient = new NettyHttpClient();
this.dnsMapping = new DnsMapping();
}

@Override
public HttpRequestMessage decode(Packet packet, Connection connection) {
return new HttpRequestMessage(packet, connection);
Expand All @@ -48,6 +51,7 @@ public void handle(HttpRequestMessage message) {
.setStatusCode(400)
.setReasonPhrase("Bad Request")
.sendRaw();
LOGGER.warn("request url is empty!");
}

uri = doDnsMapping(uri);
Expand All @@ -63,21 +67,22 @@ public void handle(HttpRequestMessage message) {
.setStatusCode(500)
.setReasonPhrase("Internal Server Error")
.sendRaw();
LOGGER.error("send request ex, message=" + message, e);
}
}

private static class DefaultHttpCallback implements HttpCallback {
private final HttpRequestMessage message;
private final HttpRequestMessage request;
private int redirectCount;

private DefaultHttpCallback(HttpRequestMessage message) {
this.message = message;
private DefaultHttpCallback(HttpRequestMessage request) {
this.request = request;
}

@Override
public void onResponse(HttpResponse httpResponse) {
HttpResponseMessage response = HttpResponseMessage
.from(message)
.from(request)
.setStatusCode(httpResponse.status().code())
.setReasonPhrase(httpResponse.status().reasonPhrase().toString());
for (Map.Entry<CharSequence, CharSequence> entry : httpResponse.headers()) {
Expand All @@ -95,33 +100,37 @@ public void onResponse(HttpResponse httpResponse) {
}
}
response.send();
LOGGER.debug("callback success request={}, response={}", request, response);
}

@Override
public void onFailure(int statusCode, String reasonPhrase) {
HttpResponseMessage
.from(message)
.from(request)
.setStatusCode(statusCode)
.setReasonPhrase(reasonPhrase)
.sendRaw();
LOGGER.warn("callback failure request={}, response={}", request, statusCode + ":" + reasonPhrase);
}

@Override
public void onException(Throwable throwable) {
HttpResponseMessage
.from(message)
.from(request)
.setStatusCode(500)
.setReasonPhrase("Internal Server Error")
.sendRaw();
LOGGER.error("callback exception request={}, response={}", request, 500, throwable);
}

@Override
public void onTimeout() {
HttpResponseMessage
.from(message)
.from(request)
.setStatusCode(408)
.setReasonPhrase("Request Timeout")
.sendRaw();
LOGGER.warn("callback timeout request={}, response={}", request, 408);
}

@Override
Expand Down
Expand Up @@ -143,9 +143,9 @@ private class HttpClientHandler extends ChannelHandlerAdapter {

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
LOGGER.error("http client caught an error,", cause);
RequestInfo info = ctx.channel().attr(key).get();
LOGGER.error("http client caught an error, info=" + info, cause);
try {
RequestInfo info = ctx.channel().attr(key).get();
if (info.cancel()) {
info.callback.onException(cause);
}
Expand Down
Expand Up @@ -46,4 +46,14 @@ public void run(Timeout timeout) throws Exception {
public boolean cancel() {
return cancelled.compareAndSet(false, true);
}

@Override
public String toString() {
return "RequestInfo{" +
"cancelled=" + cancelled +
", request=" + request +
", host='" + host + '\'' +
", timeout=" + timeout +
'}';
}
}

0 comments on commit 780766b

Please sign in to comment.