/// <summary> /// Begin processing the request by dispatching a <code>BeginProcessRequest</code> /// invocation to an instance of an IHttpHandler class. If the processing fails /// for any reason, the server responds with HTTP 400 Bad Request. /// </summary> /// <param name="wr"></param> private void ProcessRequest(TcpServerWorkerRequest wr) { try { var context = new HttpContext(wr); var app = _getInstance(context); app.BeginProcessRequest(context, _handlerCompletionCallback, wr); } catch { try { // Send a bad request response if the context cannot // be created for any reason. wr.SendStatus(400, "Bad Request"); wr.SendKnownResponseHeader("Content-Type", "text/html; charset=utf8"); var body = Encoding.ASCII.GetBytes("<html><body>Bad Request</body></html>"); wr.SendResponseFromMemory(body, body.Length); var response = wr.FlushResponse(); SendResponse(response); wr.EndOfRequest(); } finally { } } }
/// <summary> /// Completes the request processing and initiates the process of /// sending a response back to the client. /// </summary> /// <param name="wr"></param> private void FinishRequest(TcpServerWorkerRequest wr) { // The following lines are a temporary stand-in while the rest // of the application processing capabilities are being written. wr.SendStatus(200, "OK"); wr.SendKnownResponseHeader("Content-Type", "text/html; charset=utf8"); var body = Encoding.ASCII.GetBytes("<html><body>Hello, world</body></html>"); wr.SendResponseFromMemory(body, body.Length); var response = wr.FlushResponse(); SendResponse(response); wr.EndOfRequest(); }