1 module hunt.http.codec.http.stream.AbstractHttpHandler;
2 
3 import hunt.http.codec.http.stream.Http2Configuration;
4 import hunt.http.codec.http.stream.AbstractHttpConnection;
5 
6 // import hunt.http.codec.websocket.stream.impl.WebSocketConnectionImpl;
7 import hunt.net.Handler;
8 import hunt.net.Session;
9 
10 import hunt.lang.exception;
11 import hunt.logging;
12 
13 import std.exception;
14 
15 abstract class AbstractHttpHandler : Handler {
16 
17     protected Http2Configuration config;
18 
19     this(Http2Configuration config) {
20         this.config = config;
21     }
22 
23     override
24     void messageReceived(Session session, Object message) {
25         implementationMissing(false);
26     }
27 
28     override
29     void exceptionCaught(Session session, Exception t) {
30         try {
31             errorf("HTTP handler exception: %s", t.toString());
32             Object attachment = session.getAttachment();
33             if (attachment is null) {
34                 return;
35             }
36             
37             AbstractHttpConnection httpConnection = cast(AbstractHttpConnection) attachment;
38             if (httpConnection !is null ) {
39                 try {
40                     httpConnection.notifyException(t);
41                 } catch (Exception e) {
42                     errorf("The http connection exception listener error: %s", e.message);
43                 }
44             } 
45             // else if (typeid(attachment) == typeid(WebSocketConnectionImpl)) {
46             //     try {
47             //         WebSocketConnectionImpl webSocketConnection = cast(WebSocketConnectionImpl) attachment;
48             //         webSocketConnection.notifyException(t);
49             //     } catch (Exception e) {
50             //         errorf("The websocket connection exception listener error", e);
51             //     }
52             // }
53         } finally {
54             session.close();
55         }
56     }
57 
58     override
59     void sessionClosed(Session session) {
60         version(HUNT_DEBUG) 
61             tracef("The HTTP handler received the session %s closed event.", session.getSessionId());
62         Object attachment = session.getAttachment();
63         if (attachment is null) {
64             return;
65         }
66         if (typeid(attachment) == typeid(AbstractHttpConnection)) {
67             try {
68                 AbstractHttpConnection httpConnection = cast(AbstractHttpConnection) attachment;
69                 httpConnection.notifyClose();
70             } catch (Exception e) {
71                 errorf("The http2 connection close exception", e);
72             }
73         } 
74         // else if (typeid(attachment) == typeid(WebSocketConnectionImpl)) {
75         //     try {
76         //         WebSocketConnectionImpl webSocketConnection = cast(WebSocketConnectionImpl) attachment;
77         //         webSocketConnection.notifyClose();
78         //     } catch (Exception e) {
79         //         errorf("The websocket connection close exception", e);
80         //     }
81         // }
82     }
83 
84 }