1 module hunt.http.client.Http1ClientDecoder; 2 3 import hunt.http.client.Http1ClientConnection; 4 import hunt.http.client.Http2ClientDecoder; 5 6 import hunt.http.codec.http.decode.HttpParser; 7 import hunt.http.codec.websocket.decode.WebSocketDecoder; 8 9 import hunt.net.AbstractConnection; 10 import hunt.net.ConnectionType; 11 import hunt.net.DecoderChain; 12 import hunt.net.Session; 13 14 import hunt.lang.exception; 15 16 import hunt.container.ByteBuffer; 17 18 import hunt.logging; 19 import std.conv; 20 21 22 // import hunt.http.utils.io.BufferUtils.toHeapBuffer; 23 24 class Http1ClientDecoder : DecoderChain { 25 26 private WebSocketDecoder webSocketDecoder; 27 private Http2ClientDecoder http2ClientDecoder; 28 29 this(WebSocketDecoder webSocketDecoder, Http2ClientDecoder http2ClientDecoder) { 30 super(null); 31 this.webSocketDecoder = webSocketDecoder; 32 this.http2ClientDecoder = http2ClientDecoder; 33 } 34 35 override 36 void decode(ByteBuffer buffer, Session session) { 37 ByteBuffer buf = buffer; // toHeapBuffer(buffer); 38 Object attachment = session.getAttachment(); 39 AbstractConnection abstractConnection = cast(AbstractConnection) session.getAttachment(); 40 if(abstractConnection is null) 41 { 42 throw new IllegalStateException("Client connection is null! The actual type is: " 43 ~ typeid(attachment).name); 44 } 45 46 switch (abstractConnection.getConnectionType()) { 47 case ConnectionType.HTTP1: { 48 Http1ClientConnection http1Connection = cast(Http1ClientConnection) abstractConnection; 49 HttpParser parser = http1Connection.getParser(); 50 while (buf.hasRemaining()) { 51 parser.parseNext(buf); 52 if (http1Connection.getUpgradeHttp2Complete()) { 53 http2ClientDecoder.decode(buf, session); 54 break; 55 } else if (http1Connection.getUpgradeWebSocketComplete()) { 56 webSocketDecoder.decode(buf, session); 57 break; 58 } 59 } 60 } 61 break; 62 63 case ConnectionType.HTTP2: { 64 http2ClientDecoder.decode(buf, session); 65 } 66 break; 67 68 case ConnectionType.WEB_SOCKET: { 69 webSocketDecoder.decode(buf, session); 70 } 71 break; 72 73 default: 74 string msg = "client does not support the protocol " ~ abstractConnection.getConnectionType().to!string(); 75 error(msg); 76 throw new IllegalStateException(msg); 77 } 78 } 79 80 }