use of com.firenio.codec.http11.ClientHttpFrame in project baseio by generallycloud.
the class TestSimpleHttpClient method main.
@Test
public void main() throws Exception {
Options.setEnableEpoll(true);
Waiter<String> w = new Waiter<>();
ChannelConnector context = new ChannelConnector("firenio.com", 443);
SslContext sslContext = SslContextBuilder.forClient(true).build();
context.addProtocolCodec(new ClientHttpCodec());
context.addProtocolCodec(new WebSocketCodec());
context.setIoEventHandle(new IoEventHandle() {
@Override
public void accept(Channel ch, Frame frame) {
ClientHttpFrame res = (ClientHttpFrame) frame;
System.out.println();
System.out.println(new String(res.getBytesContent()));
System.out.println();
Util.close(context);
w.call(new String(res.getBytesContent()), null);
}
});
context.addChannelEventListener(new LoggerChannelOpenListener());
context.setSslContext(sslContext);
long start = Util.now();
Channel ch = context.connect(3000);
ch.writeAndFlush(new ClientHttpFrame("/test?p=2222"));
w.await(3000);
System.out.println(Util.past(start));
Assert.assertEquals("yes server already accept your message :) {p=2222}", w.getResponse());
}
use of com.firenio.codec.http11.ClientHttpFrame in project baseio by generallycloud.
the class TestSimpleHttpClient2 method main.
// telnet 192.168.133.134 8080
public static void main(String[] args) throws Exception {
String host = "www.baidu.com";
String url = "/plaintext";
host = "firenio.com";
host = "127.0.0.1";
// host = "fe80::a793:9577:4396:8ca6";
// host = "www.baidu.com";
// host = "api.weixin.qq.com";
// host = "192.168.1.103";
int port = 1443;
port = 8300;
// port = 443;
ChannelConnector context = new ChannelConnector(host, port);
context.addProtocolCodec(new ClientHttpCodec());
context.addProtocolCodec(new WebSocketCodec());
context.setIoEventHandle(new IoEventHandle() {
@Override
public void accept(Channel ch, Frame frame) throws Exception {
ClientHttpFrame res = (ClientHttpFrame) frame;
System.out.println();
System.out.println(new String(res.getBytesContent()));
System.out.println();
// Util.close(context);
}
});
context.addChannelEventListener(new ChannelEventListenerAdapter() {
@Override
public void channelOpened(Channel ch) throws Exception {
throw new IOException();
}
});
context.addChannelEventListener(new LoggerChannelOpenListener());
if (port == 1443 || port == 443) {
context.setSslContext(SslContextBuilder.forClient(true).build());
}
Channel ch = context.connect(999999);
ClientHttpFrame f = new ClientHttpFrame(url, HttpMethod.GET);
// f.setRequestHeader(HttpHeader.Host, host);
// f.setContent("abc123".readBytes());
ch.write(f);
ch.writeAndFlush(f);
}
use of com.firenio.codec.http11.ClientHttpFrame in project baseio by generallycloud.
the class HttpProxyServer method strtup.
public synchronized void strtup(NioEventLoopGroup group, int port) throws Exception {
if (context != null && context.isActive()) {
return;
}
IoEventHandle eventHandle = new IoEventHandle() {
@Override
public void accept(Channel ch_src, Frame frame) throws Exception {
final HttpFrame f = (HttpFrame) frame;
if (f.getMethod() == HttpMethod.CONNECT) {
ch_src.writeAndFlush(CONNECT_RES_BUF.duplicate());
HttpProxyAttr s = HttpProxyAttr.get(ch_src);
String[] arr = f.getHost().split(":");
s.host = arr[0];
s.port = Integer.parseInt(arr[1]);
s.handshakeFinished = true;
} else {
String host = f.getHost();
String[] arr = host.split(":");
int port = 80;
if (arr.length == 2) {
port = Integer.parseInt(arr[1]);
}
if (f.getRequestHeaders().remove(HttpHeader.Proxy_Connection.getId()) == null) {
return;
}
NioEventLoop el = ch_src.getEventLoop();
ChannelConnector context = new ChannelConnector(el, arr[0], port);
context.addProtocolCodec(new ClientHttpCodec());
context.setIoEventHandle(new IoEventHandle() {
@Override
public void accept(Channel ch, Frame frame) throws Exception {
ClientHttpFrame res = (ClientHttpFrame) frame;
IntObjectMap<String> hs = res.getResponse_headers();
for (hs.scan(); hs.hasNext(); ) {
String v = hs.getValue();
if (v == null) {
continue;
}
if (hs.getKey() == HttpHeader.Content_Length.getId() || hs.getKey() == HttpHeader.Connection.getId() || hs.getKey() == HttpHeader.Transfer_Encoding.getId() || hs.getKey() == HttpHeader.Content_Encoding.getId()) {
continue;
}
f.setResponseHeader(hs.getKey(), v.getBytes());
}
if (res.getContent() != null) {
f.setContent(res.getContent());
} else if (res.isChunked()) {
f.setString("not support chunked now.", ch);
}
ch_src.writeAndFlush(f);
ch.close();
}
});
String url = parseRequestURL(f.getRequestURL());
context.setPrintConfig(false);
context.addChannelEventListener(new LoggerChannelOpenListener());
context.connect((ch, ex) -> {
if (ex == null) {
ClientHttpFrame req = new ClientHttpFrame(url, f.getMethod());
req.setRequestHeaders(f.getRequestHeaders());
req.getRequestHeaders().remove(HttpHeader.Proxy_Connection.getId());
if (f.getMethod() == HttpMethod.POST) {
req.setContent(f.getContent());
}
try {
ch.writeAndFlush(req);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
};
context = new ChannelAcceptor(group, 8088);
context.addProtocolCodec(new HttpProxyCodec());
context.setIoEventHandle(eventHandle);
context.addChannelEventListener(new LoggerChannelOpenListener());
context.bind();
}
use of com.firenio.codec.http11.ClientHttpFrame in project baseio by generallycloud.
the class TestSimpleWebSocketClient method main.
public static void main(String[] args) throws Exception {
IoEventHandle eventHandleAdaptor = new IoEventHandle() {
@Override
public void accept(Channel ch, Frame frame) throws Exception {
if (frame instanceof ClientHttpFrame) {
ClientHttpFrame f = (ClientHttpFrame) frame;
if (f.updateWebSocketProtocol(ch)) {
WebSocketFrame f2 = new WebSocketFrame();
Map<String, String> map = new HashMap<>();
map.put("action", "add-user");
map.put("username", "火星人" + Util.randomUUID());
f2.setString(JSON.toJSONString(map), ch);
ch.writeAndFlush(f2);
}
System.out.println(f.getResponse_headers());
} else {
WebSocketFrame f = (WebSocketFrame) frame;
System.out.println(f.getStringContent());
}
}
};
String host = "firenio.com";
int port = 443;
NioEventLoopGroup g = new NioEventLoopGroup();
g.setEnableMemoryPool(false);
ChannelConnector context = new ChannelConnector(g, host, 443);
// context.setExecutorGroup(new ExecutorEventLoopGroup());
context.setIoEventHandle(eventHandleAdaptor);
context.addProtocolCodec(new ClientHttpCodec());
context.addProtocolCodec(new WebSocketCodec());
context.addChannelEventListener(new LoggerChannelOpenListener());
context.setSslContext(SslContextBuilder.forClient(true).build());
Channel ch = context.connect();
String url = "/web-socket-chat";
HttpFrame frame = new WsUpgradeRequestFrame(url);
frame.setRequestHeader(HttpHeader.Host, host + port);
frame.setRequestHeader(HttpHeader.Pragma, "no-cache");
frame.setRequestHeader(HttpHeader.Cache_Control, "no-cache");
frame.setRequestHeader(HttpHeader.User_Agent, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36");
frame.setRequestHeader(HttpHeader.Accept_Encoding, "gzip, deflate, sdch");
frame.setRequestHeader(HttpHeader.Accept_Language, "zh-CN,zh;q=0.8");
ch.writeAndFlush(frame);
Util.sleep(100);
WebSocketFrame f2 = new WebSocketFrame();
Map<String, String> map = new HashMap<>();
map.put("action", "new-message");
map.put("message", TestUtil.newString(1024 * 8));
f2.setString(JSON.toJSONString(map), ch);
ch.writeAndFlush(f2);
Util.sleep(999999999);
Util.close(context);
}
Aggregations