示例#1
0
        public void ProcessRequest(Request request)
        {
            string workDir = Directory.GetCurrentDirectory();//aktuelles verzeichnis bestimmen
            byte[] responseBody;

            if (request.Url.Path == "/")
                workDir = Path.Combine(workDir, "index.html");
            else
                workDir = workDir + request.Url.Path;//vollen Pfad zur angeforderten Datei zusammensetzen. Annahme, dass
            //Datei im selben Verzeichnis wie die Exec liegt.
            Response resp = new Response(request, request._socket);
            if (File.Exists(workDir)) //prüfen ob datei existiert bzw. im angegebenen pfad ist
            {
                using (var fs = File.OpenRead(workDir))
                {
                    long count = fs.Length;

                    responseBody = new byte[count];

                    fs.Read(responseBody, 0, (int)count);

                    resp.sendResponse(request.HttpVersion, getMIMEType(request.Url.Extension), (int)count, " 200 OK", responseBody);

                }
            }
            else //wenn nicht, error message an den browser senden
            {
                string errorMessage = "<h2> Error: File not found! </h2>";
                resp.sendResponse(request.HttpVersion, "", errorMessage.Length, " 404 NOT FOUND", Encoding.ASCII.GetBytes(errorMessage));

            }
        }
示例#2
0
文件: Router.cs 项目: lnsp/box
        /// <summary>
        /// Handles the given request and routes it to the fitting handler.
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public Response Handle(Request req)
        {
            Response res = new Response();
            // Checks if the method is implemented (routed)
            if (!HandledRoutes.ContainsKey(req.Method))
            {
                res.Header.StatusCode = "501 Not Implemented";
                res.Header.ContentType = "text/plain; encoding=utf-8";
                res.Write("501 Not Implemented\n");
                return res;
            }

            List<KeyValuePair<string, Route>> possibleRoutes = HandledRoutes[req.Method];
            foreach (KeyValuePair<string, Route> r in possibleRoutes)
            {
                if (Matches(r.Key, req.Path))
                {
                    try
                    {
                        if (r.Value(req, res))
                            return res;
                    }
                    catch (Exception e)
                    {
                        res.Header.StatusCode = "500 Internal Server Error";
                        res.Header.ContentType = "text/plain; encoding=utf-8";
                        res.ClearContent();
                        res.Write("500 Internal Server Error\n");
                        res.Write($"Route {r.Key} threw error: {e.Message}\n");
                        return res;
                    }
                }
            }
            return res;
        }