Changeset 326 for binary-improvements/MapRendering/Web/Web.cs
- Timestamp:
- Sep 4, 2018, 2:33:52 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/MapRendering/Web/Web.cs
r325 r326 107 107 ); 108 108 109 connectionHandler = new ConnectionHandler ( this);109 connectionHandler = new ConnectionHandler (); 110 110 111 111 _listener.Prefixes.Add (string.Format ("http://*:{0}/", webPort + 2)); … … 149 149 150 150 private void HandleRequest (IAsyncResult result) { 151 if (_listener.IsListening) { 152 Interlocked.Increment (ref handlingCount); 153 Interlocked.Increment (ref currentHandlers); 151 if (!_listener.IsListening) { 152 return; 153 } 154 155 Interlocked.Increment (ref handlingCount); 156 Interlocked.Increment (ref currentHandlers); 154 157 155 158 // MicroStopwatch msw = new MicroStopwatch (); 156 HttpListenerContext ctx = _listener.EndGetContext (result); 157 _listener.BeginGetContext (HandleRequest, _listener); 158 try { 159 HttpListenerRequest request = ctx.Request; 160 HttpListenerResponse response = ctx.Response; 161 response.SendChunked = false; 162 163 response.ProtocolVersion = new Version ("1.1"); 164 165 WebConnection conn; 166 int permissionLevel = DoAuthentication (request, out conn); 167 168 169 //Log.Out ("Login status: conn!=null: {0}, permissionlevel: {1}", conn != null, permissionLevel); 170 171 172 if (conn != null) { 173 Cookie cookie = new Cookie ("sid", conn.SessionID, "/"); 174 cookie.Expired = false; 175 cookie.Expires = new DateTime (2020, 1, 1); 176 cookie.HttpOnly = true; 177 cookie.Secure = false; 178 response.AppendCookie (cookie); 179 } 180 181 // No game yet -> fail request 182 if (GameManager.Instance.World == null) { 183 response.StatusCode = (int) HttpStatusCode.ServiceUnavailable; 184 return; 185 } 186 187 if (request.Url.AbsolutePath.Length < 2) { 188 handlers ["/index.htm"].HandleRequest (request, response, conn, permissionLevel); 189 return; 190 } else { 191 foreach (KeyValuePair<string, PathHandler> kvp in handlers) { 192 if (request.Url.AbsolutePath.StartsWith (kvp.Key)) { 193 if (!kvp.Value.IsAuthorizedForHandler (conn, permissionLevel)) { 194 response.StatusCode = (int) HttpStatusCode.Forbidden; 195 if (conn != null) { 196 //Log.Out ("Web.HandleRequest: user '{0}' not allowed to access '{1}'", conn.SteamID, kvp.Value.ModuleName); 197 } 198 } else { 199 kvp.Value.HandleRequest (request, response, conn, permissionLevel); 159 HttpListenerContext ctx = _listener.EndGetContext (result); 160 _listener.BeginGetContext (HandleRequest, _listener); 161 try { 162 HttpListenerRequest request = ctx.Request; 163 HttpListenerResponse response = ctx.Response; 164 response.SendChunked = false; 165 166 response.ProtocolVersion = new Version ("1.1"); 167 168 WebConnection conn; 169 int permissionLevel = DoAuthentication (request, out conn); 170 171 172 //Log.Out ("Login status: conn!=null: {0}, permissionlevel: {1}", conn != null, permissionLevel); 173 174 175 if (conn != null) { 176 Cookie cookie = new Cookie ("sid", conn.SessionID, "/"); 177 cookie.Expired = false; 178 cookie.Expires = new DateTime (2020, 1, 1); 179 cookie.HttpOnly = true; 180 cookie.Secure = false; 181 response.AppendCookie (cookie); 182 } 183 184 // No game yet -> fail request 185 if (GameManager.Instance.World == null) { 186 response.StatusCode = (int) HttpStatusCode.ServiceUnavailable; 187 return; 188 } 189 190 if (request.Url.AbsolutePath.Length < 2) { 191 handlers ["/index.htm"].HandleRequest (request, response, conn, permissionLevel); 192 return; 193 } else { 194 foreach (KeyValuePair<string, PathHandler> kvp in handlers) { 195 if (request.Url.AbsolutePath.StartsWith (kvp.Key)) { 196 if (!kvp.Value.IsAuthorizedForHandler (conn, permissionLevel)) { 197 response.StatusCode = (int) HttpStatusCode.Forbidden; 198 if (conn != null) { 199 //Log.Out ("Web.HandleRequest: user '{0}' not allowed to access '{1}'", conn.SteamID, kvp.Value.ModuleName); 200 200 } 201 202 return;201 } else { 202 kvp.Value.HandleRequest (request, response, conn, permissionLevel); 203 203 } 204 205 return; 204 206 } 205 207 } 206 207 // Not really relevant for non-debugging purposes: 208 //Log.Out ("Error in Web.HandleRequest(): No handler found for path \"" + request.Url.AbsolutePath + "\""); 209 response.StatusCode = (int) HttpStatusCode.NotFound; 210 } catch (IOException e) { 211 if (e.InnerException is SocketException) { 212 Log.Out ("Error in Web.HandleRequest(): Remote host closed connection: " + 213 e.InnerException.Message); 214 } else { 215 Log.Out ("Error (IO) in Web.HandleRequest(): " + e); 216 } 217 } catch (Exception e) { 218 Log.Out ("Error in Web.HandleRequest(): " + e); 219 } finally { 220 if (ctx != null && !ctx.Response.SendChunked) { 221 ctx.Response.Close (); 222 } 208 } 209 210 // Not really relevant for non-debugging purposes: 211 //Log.Out ("Error in Web.HandleRequest(): No handler found for path \"" + request.Url.AbsolutePath + "\""); 212 response.StatusCode = (int) HttpStatusCode.NotFound; 213 } catch (IOException e) { 214 if (e.InnerException is SocketException) { 215 Log.Out ("Error in Web.HandleRequest(): Remote host closed connection: " + 216 e.InnerException.Message); 217 } else { 218 Log.Out ("Error (IO) in Web.HandleRequest(): " + e); 219 } 220 } catch (Exception e) { 221 Log.Out ("Error in Web.HandleRequest(): " + e); 222 } finally { 223 if (ctx != null && !ctx.Response.SendChunked) { 224 ctx.Response.Close (); 225 } 223 226 224 227 // msw.Stop (); 225 228 // totalHandlingTime += msw.ElapsedMicroseconds; 226 229 // Log.Out ("Web.HandleRequest(): Took {0} µs", msw.ElapsedMicroseconds); 227 Interlocked.Decrement (ref currentHandlers); 228 } 230 Interlocked.Decrement (ref currentHandlers); 229 231 } 230 232 }
Note:
See TracChangeset
for help on using the changeset viewer.