Exemplo n.º 1
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());
            }
        }
Exemplo n.º 2
0
 public RestClient(string url, NetworkCredential credentialsOrNull)
 {
     this.Cookies = new CookieContainer();
     url          = SanitizeUrl(url);
     this.root    = url;
     RestLogger.LogInfo("Client root {0}", this.root);
     this.credentialsOrNull = credentialsOrNull;
 }
Exemplo n.º 3
0
 public void Start()
 {
     try
     {
         contextQueue = new ProducerConsumerQueue
                        <HttpListenerContext>(ProcessRequest, config.NumWorkerThreads);
         RestLogger.LogInfo("RestServer starting, {0}", config.BaseUrl);
         listener.Prefixes.Add(config.BaseUrl);
         IsListening = true;
         listener.Start();
         listenerThread.Start();
     }
     catch (Exception ex)
     {
         IsListening = false;
         LogException("Start", ex);
         throw;
     }
 }
Exemplo n.º 4
0
        public FileResponder(string root, ResponseWriter responseWriter)
        {
            if (!string.IsNullOrEmpty(root))
            {
                if (responseWriter == null)
                {
                    throw new ArgumentNullException("responseWriter");
                }
                this.responseWriter = responseWriter;

                root = Path.GetFullPath(root);
                if (!Directory.Exists(root))
                {
                    RestLogger.LogInfo("FileResponder: creating directory " + root);
                    Directory.CreateDirectory(root);
                }
                RestLogger.LogInfo("FileResponder: web root " + root);
                this.rootExists = true;
                this.root       = root;
            }
        }