Example #1
0
        async Task HandleClientConnections(HttpListener listener)
        {
            var context = await listener.GetContextAsync().ConfigureAwait(false);

            var action = GetSiteAction(ref context);

            var method   = (context.Request.HttpMethod == "POST") ? HTTPMethod.POST : HTTPMethod.GET;
            var url      = context.Request.Url.PathAndQuery.Split('?');
            var path     = GetContentType((url[0] == "/") ? "/index.html" : url[0], ref context);
            var formdata = GetPostData((url.Length > 1 && method == HTTPMethod.GET) ?
                                       string.Format("?{0}", url[1]) : path, ref context, method);

            var target = GetRequestTarget(ref path, ref context);

            var provider = GetRequestedProvider(ref path);
            var reqType  = GetRequestType(ref context, ref provider);

            switch (target)
            {
            case RequestTarget.Site:
            case RequestTarget.Provider:
                var directEvArgs = new DataReceivedEventArgs();
                directEvArgs.Action      = action;
                directEvArgs.Context     = context;
                directEvArgs.Method      = method;
                directEvArgs.UserAgent   = context.Request.Headers["UAgent"];
                directEvArgs.Params      = formdata;
                directEvArgs.Provider    = provider;
                directEvArgs.Path        = path.Split('?')[0];
                directEvArgs.RequestType = reqType;
                directEvArgs.Target      = target;

                DataReceived?.Invoke(this, directEvArgs);
                break;

            case RequestTarget.File:
                var filePath = GetContentType(Filesystem.Combine(docroot, path), ref context);
                var data     = SendErrorDocument(404, string.Format("Not Found ({0})!", filePath), ref context);

                using (var fs = new Filesystem(docroot))
                {
                    if (fs.Exists(filePath))
                    {
                        Array.Clear(data, 0, data.Length);
                        data = fs.Read(filePath).Result;

                        context.Response.StatusCode        = 200;
                        context.Response.StatusDescription = "OK";
                    }
                }

                Send(ref data, ref context);
                break;

            default:
                break;
            }

            await HandleClientConnections(listener).ConfigureAwait(false);
        }
Example #2
0
        public SQLDatabase(string database)
        {
            if (string.IsNullOrEmpty(database))
            {
                return;
            }

            var dataBase = Filesystem.Combine(Environment.CurrentDirectory, database);

            sqlConn = new SQLiteConnection(string.Format("Data Source={0};Version=3;", dataBase));

            Open();
        }
Example #3
0
        public static async Task <Dictionary <string, string> > GetPostData(Filesystem fs,
                                                                            string path, HttpListenerContext context, HTTPMethod method)
        {
            var formdata = new Dictionary <string, string>();

            switch (method)
            {
            case HTTPMethod.POST:
                var encoding = context.Request.ContentEncoding;
                var ctype    = context.Request.ContentType;
                var line     = string.Empty;

                using (var reader = new StreamReader(context.Request.InputStream, encoding))
                    line = reader.ReadToEnd();

                if (string.IsNullOrEmpty(line))
                {
                    return(formdata);
                }

                if (!string.IsNullOrEmpty(ctype))
                {
                    if (ctype.Split(';')[0] != "application/x-www-form-urlencoded")
                    {
                        var boundary = ctype.Split('=')[1];

                        if (string.IsNullOrEmpty(line))
                        {
                            return(null);
                        }

                        var start = line.IndexOf(boundary) + (boundary.Length + 2);
                        var end   = line.LastIndexOf(boundary) + boundary.Length;
                        line = line.Substring(start, end - start);
                        var formparts = new List <string>();

                        while (line.Contains(boundary))
                        {
                            if (line.StartsWith("Content-Disposition:"))
                            {
                                start = line.IndexOf("Content-Disposition: form-data;") +
                                        "Content-Disposition: form-data;".Length;

                                end = line.IndexOf(boundary);
                                formparts.Add(line.Substring(start, end - start).TrimStart());
                                line = line.Remove(0, end);
                            }

                            if (line.StartsWith(boundary))
                            {
                                if (line.Length > boundary.Length + 2)
                                {
                                    line = line.Remove(0, boundary.Length + 2);
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }

                        foreach (var item in formparts)
                        {
                            if (item.Contains("filename=\""))
                            {
                                var posttag = item.Substring(0, item.IndexOf(";"));
                                if (string.IsNullOrEmpty(posttag))
                                {
                                    continue;
                                }

                                var data = item;
                                start = data.IndexOf("filename=\"") + "filename=\"".Length;
                                data  = data.Remove(0, start);
                                end   = data.IndexOf("\"");

                                var filename = data.Substring(0, end);
                                if (string.IsNullOrEmpty(filename))
                                {
                                    continue;
                                }

                                if (filename.Contains("\\") || filename.Contains("/"))
                                {
                                    var parts = filename.Split(filename.Contains("\\") ? '\\' : '/');
                                    filename = parts[parts.Length - 1];
                                }

                                start = data.IndexOf("Content-Type: ");
                                data  = data.Remove(0, start);
                                end   = data.IndexOf("\r\n");

                                var cType = data.Substring(0, end + 2);
                                data = data.Remove(0, end + 2);

                                var filedata = context.Request.ContentEncoding
                                               .GetBytes(data.Substring(2, data.IndexOf("\r\n--")));

                                if (filedata.Length != 0)
                                {
                                    var uploadpath = Filesystem.Combine(path, filename);

                                    try
                                    {
                                        await fs.Write(uploadpath, filedata);

                                        if (!formdata.ContainsKey(posttag))
                                        {
                                            formdata.Add(posttag, uploadpath);
                                        }
                                    }
                                    catch
                                    {
                                        continue;
                                    }
                                }
#if DEBUG
                                else
                                {
                                    throw new Exception(string.Format("Die hochgeladene Datei \"{0}\", enthält keine Daten (leer?)!", filename));
                                }
#endif
                            }
                            else
                            {
                                var x = item.Replace("\r\n--", string.Empty).Replace("name=\"",
                                                                                     string.Empty).Replace("\"", string.Empty).Replace("\r\n\r\n", "|").Split('|');
                                x[0] = x[0].Replace(" file", string.Empty);

                                if (!formdata.ContainsKey(x[0]))
                                {
                                    formdata.Add(x[0].Replace("\"", string.Empty), x[1].Replace("\"", string.Empty));
                                }
                            }
                        }

                        formparts.Clear();
                        formparts = null;
                    }
                    else
                    {
                        var tmp = line.Split('&');
                        for (var i = 0; i < tmp.Length; i++)
                        {
                            if (tmp[i].Contains("="))
                            {
                                var p = tmp[i].Split('=');
                                if (!formdata.ContainsKey(p[0]))
                                {
                                    formdata.Add(p[0].Replace("\"", string.Empty), HttpUtility.UrlDecode(p[1]).Replace("\"", string.Empty));
                                }
                            }
                        }
                    }
                }

                break;

            case HTTPMethod.GET:
                if (path.Contains("?") && path.Contains("&") && path.Contains("="))
                {
                    var get_params = HttpUtility.UrlDecode(path).Split('?')[1].Split('&');
                    for (var i = 0; i < get_params.Length; i++)
                    {
                        if (get_params[i].Contains("="))
                        {
                            var p = get_params[i].Split('=');
                            if (!formdata.ContainsKey(p[0]))
                            {
                                formdata.Add(p[0], p[1]);
                            }
                        }
                    }
                }

                break;

            default:
                break;
            }

            return(formdata);
        }