示例#1
0
        private void GetQueryString(string url)
        {
            string[] splittedUrl = url.IndexOf("?") > -1 ?
                                   url.Split('?') : url.Split('#');
            if (splittedUrl.Length <= 1)
            {
                return;
            }

            string allQueries = splittedUrl[1];

            string[] queries = allQueries.IndexOf("&") > -1 ?
                               allQueries.Split('&') : allQueries.Split(';');
            foreach (string query in queries)
            {
                string[] keyAndValues = query.Split('=');
                if (keyAndValues.Length != 2)
                {
                    RestLogger.LogWarning("RestRoute::GetQueryString: {0} does not contain an equal sign", query);
                    continue;
                }

                string   key    = Uri.UnescapeDataString(keyAndValues[0]);
                string[] values = keyAndValues[1].Split(',');
                foreach (string value in values)
                {
                    this.QueryString.Add(key, Uri.UnescapeDataString(value));
                }
            }
        }
示例#2
0
        private void ProcessRequest(HttpListenerContext context)
        {
            string    url   = context.Request.RawUrl;
            RestRoute route = resources.Keys.Where(rt => rt.Matches(context.Request.HttpMethod,
                                                                    url))
                              .FirstOrDefault();

            try
            {
                if (route != null)
                {
                    RestLogger.LogInfo("RestServer::ProcessRequest: " + route + " found");
                    resources[route](new ResourceData(context, route));
                }
                else if (fileResponder.FileExists(url))
                {
                    RestLogger.LogInfo("RestServer::ProcessRequest: file {0} found", url);
                    fileResponder.SendFileResponse(context);
                }
                else
                {
                    RestLogger.LogWarning("RestServer::ProcessRequest: url {0} not found", url);
                    responseWriter.SendNotFound(context.Response);
                }
            }
            catch (Exception ex)
            {
                LogException("ProcessRequest", ex);
                responseWriter.SendInternalServerError(context.Response,
                                                       ex.ToString());
            }
        }
示例#3
0
        public bool FileExists(string url)
        {
            if (!rootExists ||
                string.IsNullOrEmpty(url))
            {
                return(false);
            }

            if (url.Contains(".."))
            {
                RestLogger.LogWarning("FileResponder::FileExists: request with step back{0}{1}",
                                      Environment.NewLine, url);
                return(false);
            }

            string path = GetFullPath(url);

            return(File.Exists(path));
        }