public Client(TcpClient Client) { string Request = ""; byte[] Buffer = new byte[1024]; int Count; while ((Count = Client.GetStream().Read(Buffer, 0, Buffer.Length)) > 0) { Request += Encoding.ASCII.GetString(Buffer, 0, Count); if (Request.IndexOf("\r\n\r\n") >= 0 || Request.Length > 4096) { break; } } Responce response = new Responce(); if (Request == "") { response = SendError(400); return; } Request ReqClient = parsing(Request); response = ProcessRequest(ReqClient, new DigestAuthentication()); Response(response, Client); return; }
void Response2(Responce response, TcpClient Client, FileStream FS) { string Str = Bilder(response); byte[] Buffer1 = Encoding.ASCII.GetBytes(Str); Client.GetStream().Write(Buffer1, 0, Buffer1.Length); Client.Close(); }
string Bilder(Responce response) { string str = response.Version + ' ' + response.Status + '\n'; foreach (var i in response.Headers) { str += i.Key + ": " + i.Value + '\n'; } str += '\n' + response.Body; return(str); }
private Responce SendError(int Code) { string CodeStr = Code.ToString() + " " + ((HttpStatusCode)Code).ToString(); string Html = "<html><body><h1>" + CodeStr + "</h1></body></html>"; Responce response = new Responce(); response.Version = "HTTP/1.1"; response.Status = CodeStr; response.Headers = new Dictionary <string, string>(); response.Headers.Add("Content-type:", "text/html"); response.Headers.Add("Content-Length:", Html.Length.ToString()); response.Body = Html; return(response); }
public Responce GetFailedResponce() { int Code = 401; string CodeStr = Code.ToString() + " " + ((HttpStatusCode)Code).ToString(); string Html = "<html><body><h1>" + CodeStr + "</h1></body></html>"; Responce response = new Responce(); response.Version = "HTTP/1.1"; response.Status = CodeStr; response.Headers = new Dictionary <string, string>(); response.Headers.Add("WWW-Authenticate", "Digest realm=\"MyServer\", nonce = \"dcd98b7102dd2f0e8b11d0f600bfb0c093\", opaque = \"5ccc069c403ebaf9f0171e9517f40e41\""); response.Headers.Add("Content-type", "text/html"); response.Headers.Add("Content-Length", Html.Length.ToString()); response.Body = Html; return(response); }
public Responce GetFailedResponce() { int Code = 401; string CodeStr = Code.ToString() + " " + ((HttpStatusCode)Code).ToString(); string Html = "<html><body><h1>" + CodeStr + "</h1></body></html>"; Responce response = new Responce(); response.Version = "HTTP/1.1"; response.Status = CodeStr; response.Headers = new Dictionary <string, string>(); response.Headers.Add("WWW-Authenticate", "Basic realm=\"My Server\""); response.Headers.Add("Content-type", "text/html"); response.Headers.Add("Content-Length", Html.Length.ToString()); response.Body = Html; return(response); }
void Response(Responce response, TcpClient Client) { string Str = Bilder(response); byte[] Buffer = Encoding.ASCII.GetBytes(Str); Client.GetStream().Write(Buffer, 0, Buffer.Length); if (response.FilePath != null) { FileStream FS = new FileStream(response.FilePath, FileMode.Open, FileAccess.Read, FileShare.Read); while (FS.Position < FS.Length) { // Читаем данные из файла var Count = FS.Read(Buffer, 0, Buffer.Length); // И передаем их клиенту Client.GetStream().Write(Buffer, 0, Count); } FS.Close(); } Client.Close(); }
public Responce ProcessRequest(Request request, IAuthorization authorizator) { if (!authorizator.CheckIdentification(request)) { return(authorizator.GetFailedResponce()); } else { if (!authorizator.CheckAuthorization(request)) { return(SendError(403)); } } string RequestUri = request.Path; RequestUri = Uri.UnescapeDataString(RequestUri); if (RequestUri.IndexOf("..") >= 0) { return(SendError(400)); } if (RequestUri.EndsWith("/")) { RequestUri += "index.html"; } string FilePath = Environment.CurrentDirectory + RequestUri; if (!File.Exists(FilePath)) { Console.WriteLine(FilePath); return(SendError(404)); } string Extension = RequestUri.Substring(RequestUri.LastIndexOf('.')); string ContentType = ""; switch (Extension) { case ".htm": case ".html": ContentType = "text/html"; break; case ".css": ContentType = "text/stylesheet"; break; case ".js": ContentType = "text/javascript"; break; case ".jpg": ContentType = "image/jpeg"; break; case ".jpeg": case ".png": case ".gif": ContentType = "image/" + Extension.Substring(1); break; default: if (Extension.Length > 1) { ContentType = "application/" + Extension.Substring(1); } else { ContentType = "application/unknown"; } break; } // Открываем файл, страхуясь на случай ошибки FileStream FS; try { FS = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read); } catch (Exception) { // Если случилась ошибка, посылаем клиенту ошибку 500 return(SendError(500)); } Responce response = new Responce(); response.Version = "HTTP/1.1"; response.Status = "200 OK"; response.Headers = new Dictionary <string, string>(); response.Headers.Add("Content-type:", ContentType); response.Headers.Add("Content-Length:", FS.Length.ToString()); response.FilePath = FilePath; FS.Close(); return(response); }