Exemple #1
0
 public override void handleGETRequest(RequestHandler p, string url)
 {
     try
     {
         string path = router.CheckRoutes(url, p);
         if (path != null)
         {
             // check if it has parameters from get post
             if (path.Contains('?'))
             {
                 // parses the get values
                 Header header = new ResponseHeader();
                 Dictionary<string, string> data = ParseGetData(url);
                 byte[] bytes = WritePost(data, path.Split('?')[0], p.stream);
                 string extension = GetFileExtensionFromString(path);
                 header.SetHeader("ContentLength", bytes.Length.ToString());
                 header.SetHeader("ContentType", ext.extensions.Where(x => x.ext == extension).FirstOrDefault().content + "; charset=UTF-8 ");
                 SendContentHandler.SendHeader(header, p.stream);
                 SendContentHandler.SendContent(bytes, p.stream);
             }
             else
                 WritePost(p.stream, path);
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine("File not Found");
         errorHandler.SendErrorPage(p.stream, 404);
     }
     m_ServerSemaphore.Release();
 }
 public void GetFormValues(Stream stream, string url)
 {
     Header header = new ResponseHeader();
     string json = JSONParser.SerializeJSON(settings);
     byte[] response = Encoding.ASCII.GetBytes(json);
     header.SetHeader("ContentType", "text/html; charset=UTF-8 ");
     header.SetHeader("ContentLength", response.Length.ToString());
     SendContentHandler.SendHeader(header, stream);
     stream.Write(response, 0, response.Length);
 }
Exemple #3
0
 public override void handlePOSTRequest(RequestHandler p, StreamReader inputData, string url)
 {
     // parses the post data form the request
     Header header = new ResponseHeader();
     string path = router.CheckRoutes(url, p);
     Dictionary<string, string> data = ParsePostData(inputData);
     byte[] bytes = WritePost(data, path, p.stream);
     string extension = GetFileExtensionFromString(url);
     header.SetHeader("ContentLength", bytes.Length.ToString());
     header.SetHeader("ContentType", ext.extensions.Where(x => x.ext == extension).FirstOrDefault().content + "; charset=UTF-8 ");
     SendContentHandler.SendHeader(header, p.stream);
     SendContentHandler.SendContent(bytes, p.stream);
     m_ServerSemaphore.Release();
 }
        public void doLogout(Stream stream,StreamReader inputData)
        {
            Dictionary<string, string> data = ParsePostData(inputData);
            SessionManager.deleteSession(data.ElementAt(0).Value);

            Header header = new ResponseHeader();
            var jSerializer = new JavaScriptSerializer();
            string json = jSerializer.Serialize(new { Success = true });
            header.SetHeader("ContentType", "text/html");
            byte[] response = Encoding.ASCII.GetBytes(json);
            header.SetHeader("ContentLength", response.Length.ToString());
            SendContentHandler.SendHeader(header, stream);
            stream.Write(response, 0, response.Length);
        }
Exemple #5
0
 // writes content for just a request
 private void WritePost(Stream stream, string path)
 {
     Header header = new ResponseHeader();
     const int chunkSize = 1024;
     using (var file = File.OpenRead(path))
     {
         string extension = Path.GetExtension(file.Name).Replace(".", "");
         header.SetHeader("ContentLength", file.Length.ToString());
         header.SetHeader("ContentType", ext.extensions.Where(x => x.ext == extension).FirstOrDefault().content + "; charset=UTF-8 ");
         SendContentHandler.SendHeader(header, stream);
         int bytesRead;
         var buffer = new byte[chunkSize];
         while ((bytesRead = file.Read(buffer, 0, buffer.Length)) > 0)
         {
             SendContentHandler.SendChunk(bytesRead, buffer, stream);
         }
     }
 }
 public void GetLoginAuthentication(Stream stream, bool authentication, string user)
 {
     Header header = new ResponseHeader();
     var jSerializer = new JavaScriptSerializer();
     string json = "";
     if (authentication.Equals(true))
     {
         string sessionId = SessionManager.getSessionId(user);
         json = jSerializer.Serialize(new { Authentication = authentication, SessionID = sessionId });
     }
     else
         json = jSerializer.Serialize(new { Authentication = authentication });
     header.SetHeader("ContentType", "text/html");
     byte[] response = Encoding.ASCII.GetBytes(json);
     header.SetHeader("ContentLength", response.Length.ToString());
     SendContentHandler.SendHeader(header, stream);
     stream.Write(response, 0, response.Length);
 }
 public void WritePost(Stream stream, string path)
 {
     Header header = new ResponseHeader();
     string sResponse = "";
     int iTotBytes = 0;
     FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
     BinaryReader reader = new BinaryReader(fs);
     byte[] bytes = new byte[fs.Length];
     int read;
     while ((read = reader.Read(bytes, 0, bytes.Length)) != 0)
     {
         sResponse = sResponse + Encoding.ASCII.GetString(bytes, 0, read);
         iTotBytes = iTotBytes + read;
     }
     reader.Close();
     fs.Close();
     string extension = GetFileExtensionFromString(path);
     header.SetHeader("ContentLength", bytes.Length.ToString());
     header.SetHeader("ContentType", ext.extensions.Where(x => x.ext == extension).FirstOrDefault().content + "; charset=UTF-8 ");
     SendContentHandler.SendHeader(header, stream);
     stream.Write(bytes, 0, bytes.Length);
 }
 public void OpenLogFile(Stream stream)
 {
     Header header = new ResponseHeader();
     StringBuilder sb = new StringBuilder();
     using (StreamReader sr = new StreamReader(Statics.Root + @"/Logfiles/ControlServer.log.txt"))
     {
         String line;
         while ((line = sr.ReadLine()) != null)
         {
             sb.AppendLine(line + "<br />");
         }
     }
     byte[] response = Encoding.ASCII.GetBytes(sb.ToString());
     header.SetHeader("ContentType", "text/html");
     header.SetHeader("ContentLength", response.Length.ToString());
     SendContentHandler.SendHeader(header, stream);
     stream.Write(response, 0, response.Length);
 }