protected void WebsiteLife() { try { while (Listener.IsListening) { HttpListenerContext context = Listener.GetContext(); Task.Factory.StartNew(() => { Stream output = context.Response.OutputStream; byte[] buffer; // GET Body Stream inputstream = context.Request.InputStream; // GET // GET Cookies List <PanCookie> cookies = new List <PanCookie>(); foreach (Cookie c in context.Request.Cookies) { cookies.Add(new PanCookie(c.Name, c.Value, c.Path, c.Expires)); } // GET Address and Data string[] address = context.Request.Url.AbsolutePath .Split("/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); Dictionary <string, string> data = new Dictionary <string, string>(); if (context.Request.Url.Query.Length > 1) { string[] data_str = context.Request.Url.Query.Remove(0, 1) .Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries); foreach (string kv in data_str) { string[] kv_splitted = kv.Split('='); string key = kv_splitted[0]; string val = kv_splitted[1]; data.Add(key, val); } } // GET Method string method = context.Request.HttpMethod; PanRequest request = new PanRequest(address, method, data, inputstream, cookies); PanResponse response = onRequest.Invoke(request); // SET Text buffer = System.Text.Encoding.UTF8.GetBytes(response.ResponseText); // SET Code int code = response.Code; context.Response.StatusCode = code; // SET Cookies foreach (PanCookie c in response.Cookies) { string cookie = ""; cookie += (c.Name + "=" + (c.Value == null ? "" : c.Value)); if (c.Expires != null) { cookie += ("; Expires=" + c.Expires.ToString()); } cookie += ("; Path=" + c.Path); context.Response.Headers.Add("Set-Cookie", cookie); } context.Response.ContentLength64 = buffer.Length; output.Write(buffer, 0, buffer.Length); output.Close(); context.Response.Close(); }); } } catch (ThreadAbortException ex) { // } catch (Exception ex) { throw new WebsiteException(ex); } }
protected void WebsiteLife() { try { while (Listener.IsListening) { HttpListenerContext context = Listener.GetContext(); Task.Factory.StartNew(() => { Stream output = context.Response.OutputStream; // GET Cookies List <PanCookie> cookies = new List <PanCookie>(); foreach (Cookie c in context.Request.Cookies) { cookies.Add(new PanCookie(c.Name, c.Value, c.Path, c.Expires)); } // GET Headers Dictionary <string, string[]> headers = new Dictionary <string, string[]>(); System.Collections.Specialized.NameValueCollection cheaders = context.Request.Headers; foreach (string key in cheaders.AllKeys) { string current_key = key; string[] currentvalues = cheaders.GetValues(current_key); } // GET Data string url = context.Request.RawUrl; // Url string method = context.Request.HttpMethod; // Method Stream inputStream = new MemoryStream(); // Body bool hasEntityBody = context.Request.HasEntityBody; // Has Entity Body if (hasEntityBody) { context.Request.InputStream.CopyTo(inputStream); inputStream.Position = 0; } string[] acceptTypes = context.Request.AcceptTypes; // Accept Types Encoding contentEncoding = context.Request.ContentEncoding; // Content Encoding string contentType = context.Request.ContentType; // Content Type bool isLocal = context.Request.IsLocal; // Is Local string userAgent = context.Request.UserAgent; // User Agent string[] userLanguages = context.Request.UserLanguages; // User Languages IPEndPoint remoteEndPoint = context.Request.RemoteEndPoint; // User IP string userIP = remoteEndPoint.Address.ToString(); PanRequest request = new PanRequest(method, url, inputStream, cookies, hasEntityBody, acceptTypes, contentEncoding, contentType, headers, isLocal, userAgent, userLanguages, userIP); PanResponse response = onRequest.Invoke(request); // SET Code int code = response.Code; context.Response.StatusCode = code; // SET context.Response.ContentType = response.MIME; // SET Cookies if (response.Cookies == null) { response.Cookies = new List <PanCookie>(); } foreach (PanCookie c in response.Cookies) { string cookie = ""; cookie += (c.Name + "=" + (c.Value == null ? "" : c.Value)); if (c.Expires != null) { cookie += ("; Expires=" + c.Expires.ToString()); } cookie += ("; Path=" + c.Path); context.Response.Headers.Add("Set-Cookie", cookie); } response.OutputStream.CopyTo(output); response.OutputStream.Close(); response.OutputStream.Dispose(); output.Close(); context.Response.Close(); }); } } catch (ThreadAbortException ex) { // } catch (Exception ex) { throw new WebsiteException(ex); } }
static public PanResponse OnRequest(PanRequest request) { var cookies = new List <PanCookie>(); Console.WriteLine(request.Url + " " + request.IsLocal.ToString() + " " + request.UserIP); if (request.Address.Length < 1) { return(PanResponse.ReturnContent("Emtry page", Encoding.UTF8)); } else { switch (request.Address[0]) { case "favicon.ico": return(PanResponse.ReturnFile(@"E:\PROJECTS\PanWebsite\Website2\favicon.ico", Encoding.UTF8)); break; case "content": return(PanResponse.ReturnContent("Content", Encoding.UTF8)); break; case "html": return(PanResponse.ReturnHtml(@"E:\PROJECTS\PanWebsite\Website2\index.html", Encoding.UTF8)); break; case "code": return(PanResponse.ReturnCode(500)); break; case "file": return(PanResponse.ReturnFile(@"E:\PROJECTS\PanWebsite\Website2\image.jpg", Encoding.UTF8)); break; case "json": return(PanResponse.ReturnJson(new { a = 5, s = "fff", b = true })); break; case "upload": if (request.Address.Length == 1) { return(PanResponse.ReturnHtml(@"E:\PROJECTS\PanWebsite\Website2\upload.html", Encoding.UTF8)); break; } else if (request.Address[1] == "api") { if (request.HasEntityBody) { var d = request.MutlipartFormData; foreach (var item in d) { if (item.Filename != "") { FileStream fs = File.Open(Path.Combine(@"E:\PROJECTS\PanWebsite\Website2\downloads", item.Filename), FileMode.Create, FileAccess.ReadWrite); item.Data.Position = 0; fs.Position = 0; item.Data.CopyTo(fs); fs.Close(); fs.Dispose(); } } return(PanResponse.ReturnCode(200)); } else { return(PanResponse.ReturnCode(500)); } } else { return(PanResponse.ReturnCode(500)); break; } break; default: return(PanResponse.ReturnCode(404)); break; } } }