public HttpContext( WebServer ownerServer, int recvBufferSize, int sendBufferSize) { this.EnableWebSocket = true; this.ownerServer = ownerServer; //each recvSendArgs is created for this connection session only *** //---------------------------------------------------------------------------------------------------------- KeepAlive = true; //set buffer for newly created saArgs ownerServer.SetBufferFor(this.recvSendArgs = new SocketAsyncEventArgs()); recvIO = new RecvIO(recvSendArgs, recvSendArgs.Offset, recvBufferSize, HandleReceive); sendIO = new SendIO(recvSendArgs, recvSendArgs.Offset + recvBufferSize, sendBufferSize, HandleSend); //---------------------------------------------------------------------------------------------------------- httpReq = new HttpRequest(this); httpResp = new HttpResponse(this, sendIO); //common(shared) event listener*** recvSendArgs.Completed += (object sender, SocketAsyncEventArgs e) => { switch (e.LastOperation) { case SocketAsyncOperation.Receive: recvIO.ProcessReceivedData(); break; case SocketAsyncOperation.Send: sendIO.ProcessWaitingData(); break; default: throw new ArgumentException("The last operation completed on the socket was not a receive or send"); } }; }
public void HandleRequest(HttpRequest req, HttpResponse resp) { //string rootFolder = @"C:\apache2\htdocs\"; //string absFile = rootFolder + "\\" + req.Url; //if (File.Exists(absFile)) //{ // byte[] buffer = File.ReadAllBytes(absFile); // resp.AllowCrossOriginPolicy = crossOriginPolicy; // switch (Path.GetExtension(absFile)) // { // case ".jpg": // resp.ContentType = WebResponseContentType.ImageJpeg; // break; // case ".png": // resp.ContentType = WebResponseContentType.ImagePng; // break; // case ".php": // case ".html": // resp.ContentType = WebResponseContentType.TextHtml; // break; // case ".js": // resp.ContentType = WebResponseContentType.TextJavascript; // break; // case ".css": // resp.ContentType = WebResponseContentType.TextCss; // break; // } // resp.End(buffer); //} //else //{ // resp.End("something wrong"); //} switch (req.Url) { case "/": { resp.TransferEncoding = ResponseTransferEncoding.Chunked; resp.End("hello!"); } break; case "/websocket": { resp.ContentType = WebResponseContentType.TextHtml; resp.End(html); } break; case "/version": { resp.End("1.0"); } break; case "/cross": { resp.AllowCrossOriginPolicy = crossOriginPolicy; resp.End("ok"); } break; default: { resp.End(""); } break; } }