コード例 #1
0
ファイル: QueryValues.cs プロジェクト: matteofabbri/Grimoire
 public QueryValues this[string index]
 {
     get
     {
         if (!Fields.ContainsKey(index))
         {
             Fields[index] = new QueryValues();
         }
         return(Fields[index]);
     }
     set { Fields[index] = value; }
 }
コード例 #2
0
ファイル: Context.cs プロジェクト: matteofabbri/Grimoire
        public Context(HttpListenerContext c)
        {
            stopwatch = new Stopwatch();
            stopwatch.Start();

            HttpListernerContext = c;
            Request  = new HttpRequest(c.Request);
            Response = new HttpResponse(this, c.Response);

            Values = new QueryValues();
            Files  = new HttpFileCollection();
            IsOpen = true;

            var sess = c.Request.Cookies["session"];

            if (sess != null)
            {
                SessionId = sess.Value;
            }
            else
            {
                SessionId = Security.UID() + Security.UID() + Security.UID();
            }

            Response.ContentType = "text/html";
            Response.Headers["Access-Control-Allow-Origin"] = "*";


            //FIXME: valutare qualcosa di ancora piu sicuro
            sess         = new Cookie("session", SessionId, "/");
            sess.Expires = DateTime.Now + TimeSpan.FromDays(1);

            c.Response.Cookies.Add(sess);

            #region GET PARAMETERS
            if (!string.IsNullOrEmpty(c.Request.Url.Query))
            {
                var parts = (c.Request.Url.Query[0] == '?' ? c.Request.Url.Query.Substring(1) : c.Request.Url.Query).Split(new[] { '&' }, StringSplitOptions.RemoveEmptyEntries);

                foreach (string s in parts)
                {
                    string[] val   = s.Split(new[] { '=' });
                    int      count = val.Length;
                    string   k     = HttpUtility.UrlDecode(val[0]);

                    switch (count)
                    {
                    case 2:
                        string v = HttpUtility.UrlDecode(val[1]);
                        Values.Add(k, v);
                        break;

                    case 1:
                        Values.Add(k, "true");
                        break;
                    }
                }
            }
            #endregion

            #region read request
            if (HttpListernerContext.Request.HttpMethod == "GET" || HttpListernerContext.Request.ContentLength64 <= 0)
            {
                return;
            }

            if (HttpListernerContext.Request.Headers["Content-Type"].StartsWith("application/x-www-form-urlencoded"))
            {
                //FIXME: Aggiungere MAX-POST-SIZE

                var r = new byte[HttpListernerContext.Request.ContentLength64];
                HttpListernerContext.Request.InputStream.Read(r, 0, r.Length);
                string str = HttpListernerContext.Request.ContentEncoding.GetString(r);

                foreach (string s in str.Split('&'))
                {
                    string[] val   = s.Split(new[] { '=' });
                    int      count = val.Length;
                    string   k     = HttpUtility.UrlDecode(val[0]);

                    switch (count)
                    {
                    case 2:
                        string v = HttpUtility.UrlDecode(val[1]);
                        Values.Add(k, v);
                        break;

                    case 1:
                        Values.Add(k, "true");
                        break;
                    }
                }
            }
            else if (HttpListernerContext.Request.Headers["Content-Type"].StartsWith("multipart/form-data; boundary="))
            {
                var b = HttpListernerContext.Request.Headers["Content-Type"].Substring("multipart/form-data; boundary=".Length);

                var parser = new MultipartFormDataParser(HttpListernerContext.Request.InputStream, b, HttpListernerContext.Request.ContentEncoding);
                parser.Parameters.ForEach(x => Values.Add(x.Key, x.Value.Data));
                parser.Files.ForEach(x => Files.Add(x));
            }
            #endregion
        }