/// <summary> /// Processes requests coming from the web browser /// </summary> public void ProcessRequests() { Socket socket = null; try { socket = listener.AcceptSocket(); Thread t = new Thread(ProcessRequests); lock (this) { threads.Add(t); } t.Start(); if (socket.Connected) { socket.ReceiveTimeout = 200; socket.SendTimeout = 200; socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true); byte[] buf = new byte[2048]; int receivedLen = 0; while ((receivedLen = socket.Receive(buf)) > 0) { string[] requestStrings = Encoding.ASCII.GetString(buf, 0, receivedLen).Split('\r', '\n'); if (requestStrings.Length < 2) { throw new Exception(Properties.Resources.HTTPRequestTooFewStrings); } string[] requestParts = requestStrings[0].Split(' '); if (requestParts.Length != 3) { throw new Exception(Properties.Resources.HTTPRequestNoStringParts); } if (!String.Equals(requestParts[0], "GET")) { throw new Exception(String.Format(Properties.Resources.UnknownHTTPRequestType, requestParts[0])); } string httpVersion = requestParts[2]; string url = requestParts[1]; byte[] response = new byte[0]; string redirectUrl = String.Empty; string mime = "text/html"; if (UrlRequested != null) { UrlRequestedEventArgs urea = new UrlRequestedEventArgs(url); UrlRequested(this, urea); redirectUrl = urea.Redirect ? urea.RedirectUrl(port) : String.Empty; if (urea.Response != null) { response = urea.Response; } mime = urea.MimeType; } SendHeader(httpVersion, response.Length, redirectUrl, socket, mime); socket.Send(response); } lock (this) { threads.Remove(Thread.CurrentThread); } } } catch (Exception) { } finally { try { if (socket != null) { socket.Shutdown(SocketShutdown.Both); socket.Close(0); } } catch (Exception) { } } }
/// <summary> /// Processes requests coming from the web browser /// </summary> public void ProcessRequests() { Socket socket = null; try { socket = listener.AcceptSocket(); Thread t = new Thread(ProcessRequests); lock (this) { threads.Add(t); } t.Start(); if (socket.Connected) { socket.ReceiveTimeout = 200; socket.SendTimeout = 200; socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true); byte[] buf = new byte[2048]; int receivedLen = 0; while ((receivedLen = socket.Receive(buf)) > 0) { string[] requestStrings = Encoding.ASCII.GetString(buf, 0, receivedLen).Split('\r', '\n'); if (requestStrings.Length < 2) { throw new Exception(Properties.Resources.HTTPRequestTooFewStrings); } string[] requestParts = requestStrings[0].Split(' '); if (requestParts.Length != 3) { throw new Exception(Properties.Resources.HTTPRequestNoStringParts); } if (!String.Equals(requestParts[0], "GET")) { throw new Exception(String.Format(Properties.Resources.UnknownHTTPRequestType, requestParts[0])); } string httpVersion = requestParts[2]; string url = requestParts[1]; byte[] response = new byte[0]; string redirectUrl = String.Empty; string mime = "text/html"; if (UrlRequested != null) { UrlRequestedEventArgs urea = new UrlRequestedEventArgs(url); UrlRequested(this, urea); redirectUrl = urea.Redirect ? urea.RedirectUrl(port) : String.Empty; if (urea.Response != null) { response = urea.Response; } mime = urea.MimeType; } SendHeader(httpVersion, response.Length, redirectUrl, socket, mime); socket.Send(response); } lock (this) { threads.Remove(Thread.CurrentThread); } } } catch (Exception) { } finally { try { if (socket != null) { socket.Shutdown(SocketShutdown.Both); socket.Close(0); } } catch (Exception) { } } }