Beispiel #1
0
        public override void handle(Request req)
        {
            FileStream fs;
            string     uri = "";

            byte[] buffer = new byte[BUFFER_SIZE];


            try {
                // get file
                if (isFile)
                {
                    fs  = new FileStream(rootPath, FileMode.Open);
                    uri = rootPath;
                }
                else
                {
                    uri = (prefix != "") ? req.URL.Substring(prefix.Length): req.URI;
                    uri = WebUtility.UrlDecode(uri);
                    uri = uri.Replace('/', '\\');
                    Console.WriteLine("PATH: {0}", rootPath + uri);
                    if (File.Exists(rootPath + uri))
                    {
                        fs = new FileStream(rootPath + uri, FileMode.Open);
                    }
                    else
                    {
                        Console.WriteLine("Error: {0}", rootPath + uri);
                        req.StatusCode        = 400;
                        req.StatusDescription = "File " + uri + " not found.";
                        req.ContentType       = MIME.TXT;
                        req.WriteString("400 File " + uri + " not found.");
                        return;
                    }
                }
                // write header
                //req.AcceptRange = true;
                req.ContentType       = MIME.GetTypeFromFilen(uri);
                req.SendChunked       = true;
                req.StatusCode        = 200;
                req.StatusDescription = "OK";
                int len = (int)fs.Length;
                req.ContentLength = len;
                int start = req.Range();
                fs.Seek(start, SeekOrigin.Begin);
                // write data
                int i;
                while (len > 0)
                {
                    i = fs.Read(buffer, 0, BUFFER_SIZE);
                    req.Write(buffer, i);
                    len -= i;
                }
                fs.Close();
            } catch (Exception e) {
                Console.WriteLine(e);
                req.StatusCode        = 400;
                req.StatusDescription = "File " + uri + " not found.";
            }
        }
Beispiel #2
0
 public override void handle(Pink.Request req)
 {
     if (req.URL == "/schild/students.json")
     {
         req_students(req, Pink.MIME.JSON, -1);
     }
     else if (req.URL == "/schild/faculty.json")
     {
         req_faculty(req, Pink.MIME.JSON, -1);
     }
     else if (req.URL == "/schild/classes.json")
     {
         req_classes(req, Pink.MIME.JSON, -1);
     }
     else if (req.URL == "/schild/courses.json")
     {
         req_courses(req, Pink.MIME.JSON, -1);
     }
     else if (req.URL == "/schild/allocation.json")
     {
         req_allocation(req, Pink.MIME.JSON, -1);
     }
     else if (req.URL == "/schild/s2/courses.json")
     {
         req_courses_s2(req, Pink.MIME.JSON, -1);
     }
     else if (req.URL == "/schild/s2/allocation.json")
     {
         req_allocation_s2(req, Pink.MIME.JSON, -1);
     }
     else if (req.URL == "/schild/students.csv")
     {
         req_students(req, Pink.MIME.CSV, -1);
     }
     else if (req.URL == "/schild/faculty.csv")
     {
         req_faculty(req, Pink.MIME.CSV, -1);
     }
     else if (req.URL == "/schild/classes.csv")
     {
         req_classes(req, Pink.MIME.CSV, -1);
     }
     else if (req.URL == "/schild/courses.csv")
     {
         req_courses(req, Pink.MIME.CSV, -1);
     }
     else if (req.URL == "/schild/allocation.csv")
     {
         req_allocation(req, Pink.MIME.CSV, -1);
     }
     else if (req.URL == "/schild/s2/courses.csv")
     {
         req_courses_s2(req, Pink.MIME.CSV, -1);
     }
     else if (req.URL == "/schild/s2/allocation.csv")
     {
         req_allocation_s2(req, Pink.MIME.CSV, -1);
     }
     else
     {
         req.StatusCode  = 200;
         req.ContentType = Pink.MIME.JSON;
         req.WriteString(string.Format("{{\"success\":true,\"url\":\"{0}\",\"info\":\"default json test response\"}}", req.URL));
     }
 }
Beispiel #3
0
        public override void handle(Pink.Request req)
        {
            int r = DB.Execute("UPDATE Pages SET Title='" + req.GetPostQuery("title") + "', Body='" + req.GetPostQuery("body").Replace("'", "''") +
                               "', Author='" + req.GetPostQuery("author") + "' WHERE ID=" + req.GetPostQuery("ID") + ";", 10);

            Console.WriteLine("Update: {0}", r);
            if (r != 1) // try insert
            {
                r = DB.Execute("INSERT INTO Pages (Title, Body, Author) VALUES ('" + req.GetPostQuery("title") + "', '" + req.GetPostQuery("body").Replace("'", "''") + "','" + req.GetPostQuery("author") + "');", 10);
                Console.WriteLine("Insert: {0}", r);
            }


            req.StatusCode = 200;
            req.WriteString("<HTML><HEAD><TITLE>Upload PinkServer ... </TITLE></HEAD><BODY>");
            req.WriteString(string.Format("<h1>Pink Server</h1><i>{0}</i><br>", DateTime.Now));
            req.WriteString(string.Format("<p>encoding: {0}</p>", req.ContentType));
            req.WriteString(string.Format("<p>length: {0}</p>", req.ContentLength));
            req.WriteString(string.Format("<hr>"));
            req.WriteString(req.GetPostQuery("title"));
            req.WriteString(string.Format("<hr>"));
            req.WriteString(req.GetPostQuery("author"));
            req.WriteString(string.Format("<hr>"));
            req.WriteString(req.GetPostQuery("body"));
            req.WriteString(string.Format("<hr>"));
            req.WriteString("</BODY></HTML>");
        }