public static Dictionary <string, MultiPartEntry> Parse(Request request)
        {
            var mps         = new Dictionary <string, MultiPartEntry> ();
            var contentType = request.headers.Get("Content-Type");

            if (contentType.Contains("multipart/form-data"))
            {
                var boundary = request.body.Substring(0, request.body.IndexOf("\r\n")) + "\r\n";
                var parts    = request.body.Split(new string[] { boundary }, System.StringSplitOptions.RemoveEmptyEntries);
                foreach (var part in parts)
                {
                    var sep = part.IndexOf("\r\n\r\n");
                    if (sep == -1)
                    {
                        continue;
                    }
                    var headerText = part.Substring(0, sep);
                    var mp         = new MultiPartEntry();
                    mp.headers.Read(headerText);
                    mp.Value = part.Substring(sep);
                    if (mp.headers.Contains("Content-Disposition"))
                    {
                        var s  = mp.headers.Get("Content-Disposition");
                        var nm = new Regex(@"(?<=name\=\"")(.*?)(?=\"")").Match(s);
                        if (nm.Success)
                        {
                            mp.Name = nm.Value.Trim();
                        }
                        var fm = new Regex(@"(?<=filename\=\"")(.*?)(?=\"")").Match(s);
                        if (fm.Success)
                        {
                            mp.Filename = fm.Value.Trim();
                        }
                    }
                    if (mp.Name != null)
                    {
                        mps.Add(mp.Name, mp);
                    }
                }
            }
            return(mps);
        }
Exemple #2
0
        void ServeHTTP(TcpClient tc)
        {
            var stream = tc.GetStream();
            var line   = ReadLine(stream);

            if (line == null)
            {
                return;
            }
            var top = line.Trim().Split(' ');

            if (top.Length != 3)
            {
                return;
            }

            var req = new Request()
            {
                method = top[0], path = top[1], protocol = top[2]
            };

            if (req.path.StartsWith("http://"))
            {
                req.uri = new Uri(req.path);
            }
            else
            {
                req.uri = new Uri("http://" + System.Net.IPAddress.Any + ":" + port + req.path);
            }

            while (true)
            {
                var headerline = ReadLine(stream);
                if (headerline.Length == 0)
                {
                    break;
                }
                req.headers.AddHeaderLine(headerline);
            }


            req.stream = stream;
            if (req.headers.Contains("Content-Length"))
            {
                var count  = int.Parse(req.headers.Get("Content-Length"));
                var bytes  = new byte[count];
                var offset = 0;
                while (count > 0)
                {
                    offset = stream.Read(bytes, offset, count);
                    count -= offset;
                }
                req.body = System.Text.Encoding.UTF8.GetString(bytes);
            }

            if (req.headers.Get("Content-Type").Contains("multipart/form-data"))
            {
                req.formData = MultiPartEntry.Parse(req);
            }

            if (processRequestsInMainThread)
            {
                lock (mainThreadRequests)
                {
                    mainThreadRequests.Enqueue(req);
                }
            }
            else
            {
                var response = new Response();
                ProcessRequest(req, response);
            }
        }