public bool ProcessConnection(IMyTcpClient client) { if (client == null) { return(false); } IRequestContext httpRequest = GetRequestInformationFromConnection(client); try { //if couldnt parse return error message if (httpRequest != null) { PrintConnectionDetails(httpRequest); } else { client.Close(); return(false); } // evoke endpoint for requested resource int statusCode = EndPointApi.InvokeEndPoint(httpRequest.HTTPVerb, httpRequest.MessageEndPoint, httpRequest); if (statusCode == -1) { client.Close(); return(false); } } catch (SocketException) { //couldnt write to stream client.Close(); return(false); } catch (ArgumentNullException) { // null was given as argument client.Close(); return(false); } catch (Exception e) when(e.Message == "NotAValidEndpoint") { // ResourceEndpoint was not found in Endpoint Api httpRequest.ReponseHandler.SendDefaultStatus(httpRequest.Stream, "404"); client.Close(); return(false); } catch (Exception e) when(e.Message == "NotAValidVerbForEndpoint") { // HTTP verb not registered for that resource endpoint httpRequest.ReponseHandler.SendDefaultStatus(httpRequest.Stream, "501"); client.Close(); return(false); } Console.WriteLine("Thread finished and Client closed"); client.Close(); return(true); }
public RequestContext(IMyTcpClient Client) { this.Client = Client; this.Stream = Client.GetStream(); ReponseHandler = new HTTPResponseWrapper(); Headers = new Dictionary <string, string>(); HTTPVerb = ""; HttpProtokoll = ""; MessageEndPoint = ""; Headers.Clear(); PayLoad = ""; }
public void HandleClient(Object obj) { IMyTcpClient client = (IMyTcpClient)obj; StreamReader reader = new StreamReader(client.GetStream()); String msg = ""; while (reader.Peek() != -1) { msg += (char)reader.Read(); } Console.WriteLine("Request: \n" + msg); this.Request = Request.GetRequest(msg); this.Response = Response.From(this.Request); this.Response.Post(client.GetStream()); client.Close(); }
protected IRequestContext GetRequestInformationFromConnection(IMyTcpClient client) { //creates RequestContext and parses the stream into it if (client == null) { return(null); } try { IRequestContext requestInformation = new RequestContext(client); if (requestInformation.Parse()) { return(requestInformation); } else { return(null); } } catch { return(null); } }