Пример #1
0
        public void PerformGet <T>(HttpListenerContext context, GetResourceByQuery <T> getResource)
        {
            if (_verify != null && !_verify(context.Request))
            {
                WriteError(context.Response, HttpStatusCode.Unauthorized, "Authorization failed");
                return;
            }

            string query = context.Request.Url.Query;

            if (ParseQuery(query, out Dictionary <string, string> queryDictionary) &&
                getResource.Invoke(queryDictionary, out IEnumerable <T> resources))
            {
                WriteToResponse(context.Response, resources);
                return;
            }

            WriteError(context.Response, HttpStatusCode.BadRequest, "bad query parameters");
        }
Пример #2
0
        public void PerformGet <T>(HttpListenerContext context, GetResourceByQuery <T> getResource)
        {
            if (_verify != null && !_verify(context.Request))
            {
                WriteError(context.Response, HttpStatusCode.Unauthorized, "Authorization failed");
                return;
            }

            string query = context.Request.Url.Query;
            Dictionary <string, string> queryDictionary = null;

            if (!string.IsNullOrEmpty(query))
            {
                query           = query.Substring(1);
                queryDictionary = new Dictionary <string, string>();

                string[] pairs = query.Split('&');
                foreach (var pair in pairs)
                {
                    var splited = pair.Split('=');
                    if (splited.Length != 2)
                    {
                        WriteError(context.Response, HttpStatusCode.BadRequest, "bad query parameters");
                        return;
                    }
                    queryDictionary.Add(splited[0], splited[1]);
                }
            }

            if (getResource.Invoke(queryDictionary, out IEnumerable <T> resources))
            {
                WriteToResponse(context.Response, resources);
                return;
            }

            WriteError(context.Response, HttpStatusCode.BadRequest, "bad query parameters");
        }