1 module server;
2 
3 import std.stdio;
4 
5 import hunt.http.codec.http.frame;
6 import hunt.http.codec.http.stream;
7 // import hunt.http.HttpVersion;
8 import hunt.http.server;
9 
10 import hunt.util.Common;
11 import hunt.collection;
12 import hunt.logging;
13 import hunt.util.Common;
14 
15 import core.time;
16 
17 
18 void main(string[] args)
19 {
20 	HttpServerOptions serverOptions = new HttpServerOptions();
21 	serverOptions.setSecureConnectionEnabled(false);
22 	serverOptions.setFlowControlStrategy("simple");
23 	serverOptions.getTcpConfiguration().setIdleTimeout(60.seconds);
24 	serverOptions.setProtocol(HttpVersion.HTTP_2.toString());
25 	serverOptions.setHost("0.0.0.0");
26 	serverOptions.setPort(8080);
27 
28 	Map!(int, int) settings = new HashMap!(int, int)();
29 	settings.put(SettingsFrame.HEADER_TABLE_SIZE, serverOptions.getMaxDynamicTableSize());
30 	settings.put(SettingsFrame.INITIAL_WINDOW_SIZE, serverOptions.getInitialStreamSendWindow());
31 
32 	HttpServer server = new HttpServer(serverOptions, new class ServerSessionListener {
33 
34 		override
35 		Map!(int, int) onPreface(Session session) {
36 			infof("server received preface: %s", session);
37 			return settings;
38 		}
39 
40 		override
41 		StreamListener onNewStream(Stream stream, HeadersFrame frame) {
42 			infof("server created new stream: %d", stream.getId());
43 			infof("server created new stream headers: %s", frame.getMetaData().toString());
44 
45 			return new class StreamListener {
46 
47 				override
48 				void onHeaders(Stream stream, HeadersFrame frame) {
49 					infof("server received headers: %s", frame.getMetaData());
50 				}
51 
52 				override
53 				StreamListener onPush(Stream stream, PushPromiseFrame frame) {
54 					return null;
55 				}
56 
57 				override
58 				void onData(Stream stream, DataFrame frame, Callback callback) {
59 					infof("server received data %s, %s", BufferUtils.toString(frame.getData()), frame);
60 					callback.succeeded();
61 				}
62 
63 				void onReset(Stream stream, ResetFrame frame, Callback callback) {
64 					try {
65 						onReset(stream, frame);
66 						callback.succeeded();
67 					} catch (Exception x) {
68 						callback.failed(x);
69 					}
70 				}
71 
72 				override
73 				void onReset(Stream stream, ResetFrame frame) {
74 					infof("server reseted: %s | %s", stream, frame);
75 				}
76 
77 				override
78 				bool onIdleTimeout(Stream stream, Exception x) {
79 					infof("idle timeout", x);
80 					return true;
81 				}
82 
83 				override string toString() {
84 					return super.toString();
85 				}
86 
87 			};
88 		}
89 
90 		override
91 		void onSettings(Session session, SettingsFrame frame) {
92 			infof("server received settings: %s", frame);
93 		}
94 
95 		override
96 		void onPing(Session session, PingFrame frame) {
97 		}
98 
99 		override
100 		void onReset(Session session, ResetFrame frame) {
101 			infof("server reset " ~ frame.toString());
102 		}
103 
104 		override
105 		void onClose(Session session, GoAwayFrame frame) {
106 			infof("server closed " ~ frame.toString());
107 		}
108 
109 		override
110 		void onFailure(Session session, Exception failure) {
111 			errorf("server failure, %s", failure, session);
112 		}
113 
114 		void onClose(Session session, GoAwayFrame frame, Callback callback)
115 		{
116 			try
117 			{
118 				onClose(session, frame);
119 				callback.succeeded();
120 			}
121 			catch (Exception x)
122 			{
123 				callback.failed(x);
124 			}
125 		}
126 
127 		void onFailure(Session session, Exception failure, Callback callback)
128 		{
129 			try
130 			{
131 				onFailure(session, failure);
132 				callback.succeeded();
133 			}
134 			catch (Exception x)
135 			{
136 				callback.failed(x);
137 			}
138 		}
139 
140 		override
141 		void onAccept(Session session) {
142 		}
143 
144 		override
145 		bool onIdleTimeout(Session session) {
146 			return false;
147 		}
148 	}, new ServerHttpHandlerAdapter(serverOptions), null);
149 
150 	server.start();
151 }