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.http.HttpConnection;
10 import hunt.http.HttpConnection;
11 import hunt.http.HttpConnection;
12 import hunt.net.codec.Decoder;
13 import hunt.net.Connection;
14 
15 import hunt.Exceptions;
16 
17 import hunt.io.ByteBuffer;
18 
19 import hunt.logging;
20 import std.conv;
21 
22 
23 // import hunt.http.utils.io.BufferUtils.toHeapBuffer;
24 
25 class Http1ClientDecoder : DecoderChain {
26 
27     private WebSocketDecoder webSocketDecoder;
28     private Http2ClientDecoder http2ClientDecoder;
29 
30     this(WebSocketDecoder webSocketDecoder, Http2ClientDecoder http2ClientDecoder) {
31         super(null);
32         this.webSocketDecoder = webSocketDecoder;
33         this.http2ClientDecoder = http2ClientDecoder;
34     }
35 
36     override
37     void decode(ByteBuffer buffer, Connection session) {
38         ByteBuffer buf = buffer; // toHeapBuffer(buffer);
39         Object attachment = session.getAttribute(HttpConnection.NAME); // session.getAttachment();
40 
41         AbstractHttpConnection abstractConnection = cast(AbstractHttpConnection) attachment;
42         if(abstractConnection is null)
43         {
44             throw new IllegalStateException("Client connection is null! The actual type is: " 
45                 ~ typeid(attachment).name);
46         }
47 
48         switch (abstractConnection.getConnectionType()) {
49             case HttpConnectionType.HTTP1: {
50                 Http1ClientConnection http1Connection = cast(Http1ClientConnection) abstractConnection;
51                 HttpParser parser = http1Connection.getParser();
52                 while (buf.hasRemaining()) {
53                     version(HUNT_HTTP_DEBUG) tracef("parsing buffer: %s", buf.toString());
54                     parser.parseNext(buf);
55                     if (http1Connection.getUpgradeHttp2Complete()) {
56                         http2ClientDecoder.decode(buf, session);
57                         break;
58                     } else if (http1Connection.getUpgradeWebSocketComplete()) {
59                         webSocketDecoder.decode(buf, session);
60                         break;
61                     }
62                 }
63             }
64             break;
65 
66             case HttpConnectionType.HTTP2: {
67                 http2ClientDecoder.decode(buf, session);
68             }
69             break;
70 
71             case HttpConnectionType.WEB_SOCKET: {
72                 webSocketDecoder.decode(buf, session);
73             }
74             break;
75 
76             default:
77                 string msg = "client does not support the protocol " ~ abstractConnection.getConnectionType().to!string();
78                 error(msg);
79                 throw new IllegalStateException(msg);
80         }
81     }
82 
83 }