public override bool SendResponse(HttpStatusCode acode, CHttpRequest arequest) { bool result = false; arequest.Response.StatusCode = (int)acode; try { StreamWriter writer = new StreamWriter(arequest.Response.OutputStream); writer.WriteLine(text); writer.Close(); } finally { try { arequest.Response.Close(); result = true; } catch (Exception e) { arequest.Server.ServerLog.LogWarn("Got close response error " + e.Message + " for request " + arequest.RequestId); } } return(result); }
private bool ValidateSession(CHttpRequest arequest) { Cookie sessionCookie = arequest.Request.Cookies["sessionId"]; CHttpSession sessionObject = null; if ((sessionCookie == null) || ((sessionCookie != null) && (sessionCookie.Value == ""))) { sessionObject = arequest.Server.ServerSessions.GetSession(""); arequest.Response.Cookies.Add(new Cookie("sessionId", sessionObject.SessionId)); } else { sessionObject = arequest.Server.ServerSessions.GetSession(sessionCookie.Value); } if (sessionObject != null) { try { sessionObject.TouchTime = DateTime.Now; arequest.Session = sessionObject; CHttpResponse response = new CHttpTextResponse("Hello with session id " + sessionObject.SessionId + " touchTime " + sessionObject.TouchTime.ToString()); response.SendResponse(HttpStatusCode.OK, arequest); } finally { arequest.Session.IsUsed = false; } } else { arequest.Response.Cookies.Add(new Cookie("sessionId", "")); arequest.SendServerExpired(arequest); } return(true); }
private void ProcessRequest(Object stateInfo) { CHttpRequest request = (CHttpRequest)stateInfo; bool processed = false; int counter = 0; try { while ((!processed) && (counter <= handlerTypes.Length - 1)) { CHttpHandler handler = (CHttpHandler)CreateHandler(handlerTypes[counter]); processed = handler.ProcessRequest(request); if (processed) { serverLog.LogInfo("Request " + request.RequestId + " HTTP " + request.Request.HttpMethod + " " + request.Request.Url + " was handled by " + handler.ToString()); } counter++; } if (!processed) { CHttpResponse response = new CHttpTextResponse("Sorry, request not handled"); response.SendResponse(HttpStatusCode.OK, request); serverLog.LogWarn("Request " + request.RequestId + " was not handled by any handler"); } } catch (Exception e) { serverLog.LogError("Got error while processing " + request.RequestId + " " + e.Message); request.SendServerError(request); } }
public override bool SendResponse(HttpStatusCode acode, CHttpRequest arequest) { bool result = false; arequest.Response.StatusCode = (int)acode; if (File.Exists(fileName)) { try { FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); BinaryReader responseReader = new BinaryReader(fileStream); long bytesLeft = fileStream.Length; byte[] byteBuffer = new byte[8192]; bool finished = false; int bytesRead; try { while ((!finished) && (arequest.Server.IsRunning)) { bytesRead = responseReader.Read(byteBuffer, 0, 8192); if (bytesRead > 0) { try { arequest.Response.OutputStream.Write(byteBuffer, 0, bytesRead); } catch (Exception e) { finished = true; arequest.Server.ServerLog.LogWarn("Got response write error " + e.Message + " for request " + arequest.RequestId); } } else { finished = true; } } } finally { fileStream.Close(); try { arequest.Response.Close(); result = true; } catch (Exception e) { arequest.Server.ServerLog.LogWarn("Got close response error " + e.Message + " for request " + arequest.RequestId); } } } catch (Exception e) { arequest.Server.ServerLog.LogWarn("Got processing error " + e.Message + " for request " + arequest.RequestId); } } return(result); }
public override bool ProcessRequest(CHttpRequest arequest) { if (arequest.Request.HttpMethod.ToUpper() == "GET") { string fileName = rootDirectory + arequest.Request.Url.LocalPath.Replace("/", "\\"); CHttpFileResponse response = new CHttpFileResponse(fileName); return(response.SendResponse(HttpStatusCode.OK, arequest)); } else { return(false); } }
public override bool ProcessRequest(CHttpRequest arequest) { Uri reqUri = arequest.Request.Url; if (reqUri.PathAndQuery.StartsWith("/cmanager?")) { if (ValidateSession(arequest)) { return(true); } else { return(true); } } else { return(false); } }
private void AcceptNewClient(IAsyncResult iar) { if (serverListener.IsListening) { try { HttpListenerContext context = serverListener.EndGetContext(iar); CHttpRequest request = new CHttpRequest(this, context); try { ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessRequest), request); } catch (Exception e) { serverLog.LogError("Cant queue request " + context.Request.RequestTraceIdentifier.ToString("N") + " " + e.Message); request.SendServerOverloaded(request); } } catch (Exception e) { serverLog.LogError("Accept client error " + e.Message); } } }
public abstract bool SendResponse(HttpStatusCode acode, CHttpRequest arequest);
public void SendServerError(CHttpRequest arequest) { CHttpResponse response = new CHttpTextResponse("Server error"); response.SendResponse(HttpStatusCode.InternalServerError, arequest); }
public void SendServerExpired(CHttpRequest arequest) { CHttpResponse response = new CHttpTextResponse("Session expired"); response.SendResponse(HttpStatusCode.Forbidden, arequest); }
public void SendServerOverloaded(CHttpRequest arequest) { CHttpResponse response = new CHttpTextResponse("Sever overloaded"); response.SendResponse(HttpStatusCode.ServiceUnavailable, arequest); }
abstract public bool ProcessRequest(CHttpRequest arequest);