Search in sources :

Example 11 with HttpFuture

use of com.generallycloud.baseio.codec.http11.future.HttpFuture in project baseio by generallycloud.

the class TestWebSocketRumpetrollServlet method doAccept.

@Override
protected void doAccept(HttpSession session, HttpFuture future) throws Exception {
    future.updateWebSocketProtocol();
    session.flush(future);
    msgAdapter.addClient(getAddress(session.getIoSession()), session.getIoSession());
    SocketSession ioSession = session.getIoSession();
    JSONObject o = new JSONObject();
    o.put("type", "welcome");
    o.put("id", ioSession.getSessionId());
    WebSocketFuture f = new WebSocketFutureImpl(ioSession.getContext());
    f.write(o.toJSONString());
    session.flush(f);
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) SocketSession(com.generallycloud.baseio.component.SocketSession) WebSocketFutureImpl(com.generallycloud.baseio.codec.http11.future.WebSocketFutureImpl) WebSocketFuture(com.generallycloud.baseio.codec.http11.future.WebSocketFuture)

Example 12 with HttpFuture

use of com.generallycloud.baseio.codec.http11.future.HttpFuture in project baseio by generallycloud.

the class DefaultHttpSessionManager method getHttpSession.

@Override
public HttpSession getHttpSession(HttpContext context, SocketSession ioSession, HttpFuture future) {
    String sessionId = future.getCookie(COOKIE_NAME_SESSIONID);
    if (StringUtil.isNullOrBlank(sessionId)) {
        DefaultHttpSession session = new DefaultHttpSession(context, ioSession);
        sessionId = session.getSessionId();
        Cookie cookie = new Cookie(COOKIE_NAME_SESSIONID, sessionId);
        future.addCookie(cookie);
        this.sessions.put(sessionId, session);
        return session;
    }
    HttpSession session = sessions.get(sessionId);
    if (session == null) {
        session = new DefaultHttpSession(context, ioSession, sessionId);
        this.sessions.put(sessionId, session);
        return session;
    }
    if (!session.isValidate()) {
        sessions.remove(sessionId);
        CloseUtil.close(session.getIoSession());
        return getHttpSession(context, ioSession, future);
    }
    session.active(ioSession);
    return session;
}
Also used : Cookie(com.generallycloud.baseio.codec.http11.future.Cookie)

Example 13 with HttpFuture

use of com.generallycloud.baseio.codec.http11.future.HttpFuture in project baseio by generallycloud.

the class ClientHTTPProtocolEncoder method encode.

@Override
public void encode(SocketChannel channel, ChannelFuture future) throws IOException {
    ByteBufAllocator allocator = channel.getByteBufAllocator();
    HttpFuture f = (HttpFuture) future;
    ByteBuf buf = allocator.allocate(256);
    buf.put(f.getMethod().getBytes());
    buf.putByte(SPACE);
    buf.put(getRequestURI(f).getBytes());
    buf.put(PROTOCOL);
    writeHeaders(f, buf);
    List<Cookie> cookieList = f.getCookieList();
    if (cookieList != null && cookieList.size() > 0) {
        buf.put(COOKIE);
        for (Cookie c : cookieList) {
            writeBuf(buf, c.getName().getBytes());
            writeBuf(buf, COLON);
            writeBuf(buf, c.getValue().getBytes());
            writeBuf(buf, SEMICOLON);
        }
        buf.position(buf.position() - 1);
    }
    buf.put(RN);
    future.setByteBuf(buf.flip());
}
Also used : Cookie(com.generallycloud.baseio.codec.http11.future.Cookie) ByteBufAllocator(com.generallycloud.baseio.buffer.ByteBufAllocator) ByteBuf(com.generallycloud.baseio.buffer.ByteBuf) HttpFuture(com.generallycloud.baseio.codec.http11.future.HttpFuture)

Example 14 with HttpFuture

use of com.generallycloud.baseio.codec.http11.future.HttpFuture in project baseio by generallycloud.

the class TestSimpleHttpClient method main.

public static void main(String[] args) throws Exception {
    HttpIOEventHandle eventHandleAdaptor = new HttpIOEventHandle();
    // ServerConfiguration c = new ServerConfiguration("localhost",80);
    ServerConfiguration c = new ServerConfiguration("generallycloud.com", 443);
    SocketChannelContext context = new NioSocketChannelContext(c);
    SocketChannelConnector connector = new SocketChannelConnector(context);
    SslContext sslContext = SSLUtil.initClient(true);
    context.setProtocolFactory(new ClientHTTPProtocolFactory());
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.setSslContext(sslContext);
    SocketSession session = connector.connect();
    HttpClient client = new HttpClient(session);
    HttpFuture future = new ClientHttpFuture(context, "/test-show-memory");
    HttpFuture res = client.request(future, 10000);
    System.out.println();
    System.out.println(new String(res.getBodyContent()));
    System.out.println();
    CloseUtil.close(connector);
}
Also used : SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) ClientHTTPProtocolFactory(com.generallycloud.baseio.codec.http11.ClientHTTPProtocolFactory) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) ClientHttpFuture(com.generallycloud.baseio.codec.http11.future.ClientHttpFuture) HttpFuture(com.generallycloud.baseio.codec.http11.future.HttpFuture) HttpIOEventHandle(com.generallycloud.baseio.codec.http11.HttpIOEventHandle) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) ClientHttpFuture(com.generallycloud.baseio.codec.http11.future.ClientHttpFuture) SocketSession(com.generallycloud.baseio.component.SocketSession) HttpClient(com.generallycloud.baseio.codec.http11.HttpClient) SslContext(com.generallycloud.baseio.component.ssl.SslContext)

Aggregations

HttpFuture (com.generallycloud.baseio.codec.http11.future.HttpFuture)11 SocketSession (com.generallycloud.baseio.component.SocketSession)6 ClientHttpFuture (com.generallycloud.baseio.codec.http11.future.ClientHttpFuture)4 WebSocketFuture (com.generallycloud.baseio.codec.http11.future.WebSocketFuture)4 LoggerSocketSEListener (com.generallycloud.baseio.component.LoggerSocketSEListener)4 SocketChannelContext (com.generallycloud.baseio.component.SocketChannelContext)4 ServerConfiguration (com.generallycloud.baseio.configuration.ServerConfiguration)4 JSONObject (com.alibaba.fastjson.JSONObject)3 ClientHTTPProtocolFactory (com.generallycloud.baseio.codec.http11.ClientHTTPProtocolFactory)3 Cookie (com.generallycloud.baseio.codec.http11.future.Cookie)3 NioSocketChannelContext (com.generallycloud.baseio.component.NioSocketChannelContext)3 SocketChannelConnector (com.generallycloud.baseio.connector.SocketChannelConnector)3 HttpClient (com.generallycloud.baseio.codec.http11.HttpClient)2 HttpIOEventHandle (com.generallycloud.baseio.codec.http11.HttpIOEventHandle)2 WebSocketFutureImpl (com.generallycloud.baseio.codec.http11.future.WebSocketFutureImpl)2 IoEventHandleAdaptor (com.generallycloud.baseio.component.IoEventHandleAdaptor)2 Waiter (com.generallycloud.baseio.concurrent.Waiter)2 Future (com.generallycloud.baseio.protocol.Future)2 TimeoutException (com.generallycloud.baseio.TimeoutException)1 SocketChannelAcceptor (com.generallycloud.baseio.acceptor.SocketChannelAcceptor)1