1 module hunt.http.codec.CommonEncoder;
2 
3 import hunt.http.HttpConnection;
4 
5 import hunt.io.ByteBuffer;
6 import hunt.Exceptions;
7 import hunt.logging;
8 import hunt.net.codec.Encoder;
9 import hunt.net.Connection;
10 import hunt.net.Exceptions;
11 import hunt.net.secure.SecureSession;
12 import hunt.util.Common;
13 
14 import std.format;
15 
16 /**
17  * 
18  */
19 class CommonEncoder : EncoderChain {
20 
21     override void encode(Object message, Connection session) {
22 
23         ConnectionState connState = session.getState();
24         version(HUNT_HTTP_DEBUG_MORE) {
25             infof("ConnectionState: %s", connState);        
26         }
27 
28         auto messageTypeInfo = typeid(message);
29         version(HUNT_HTTP_DEBUG_MORE) {
30             tracef("encoding... message: %s", messageTypeInfo.name);
31         }
32 
33         if(connState == ConnectionState.Secured) {
34             SecureSession secureSession = cast(SecureSession) session.getAttribute(SecureSession.NAME);
35             ByteBuffer messageBuffer = cast(ByteBuffer) message;
36             if(messageBuffer !is null) {
37                 // connection.encrypt(messageBuffer);
38                 secureSession.write(messageBuffer, Callback.NOOP);
39             } else {
40 
41                 throw new IllegalArgumentException("the encoder object type error: " ~ messageTypeInfo.toString());
42                 // ByteBufferOutputEntry entry = cast(ByteBufferOutputEntry) message;
43                 // if (entry !is null)  {
44                 //     implementationMissing(false);
45                 //     // connection.encrypt(cast(ByteBufferOutputEntry) message);
46                 // // } else if (message instanceof ByteBufferArrayOutputEntry) {
47                 // //     connection.encrypt(cast(ByteBufferArrayOutputEntry) message);
48                 // } else {
49                 //     throw new IllegalArgumentException("the encoder object type error: " ~ messageTypeInfo.toString());
50                 // }
51             }
52         } else if(connState == ConnectionState.Opened || connState == ConnectionState.Ready) {
53             ByteBuffer messageBuffer = cast(ByteBuffer) message;
54             if(messageBuffer !is null) {
55                 session.write(messageBuffer); // , Callback.NOOP
56             } else { 
57             //     ByteBufferOutputEntry entry = cast(ByteBufferOutputEntry) message;
58             //     if (entry !is null) {
59             //         session.write(entry.getData()); // , entry.getCallback()
60             // // } else if (message instanceof ByteBufferArrayOutputEntry) {
61             // //     session.write(cast(ByteBufferArrayOutputEntry) message);
62             //    } else {
63             //         throw new IllegalArgumentException("the encoder object type error: " ~ messageTypeInfo.toString());
64             //     }
65                 throw new IllegalArgumentException("the encoder object type error: " ~ messageTypeInfo.toString());
66             } 
67         } else if(connState == ConnectionState.Closed) {
68             string msg = format("connection [id=%d] closed.", session.getId());
69             debug warningf(msg);
70             throw new Exception(msg);
71         } else {
72             string msg = format("Unhandled connection [id=%d] state: %s", session.getId(), connState);
73             debug warningf(msg);
74             throw new Exception(msg);
75         }
76     }
77 }