示例#1
0
        private static void onGet(HttpRequestEventArgs eventArgs)
        {
            var request  = eventArgs.Request;
              var response = eventArgs.Response;
              var content  = getContent(request.RawUrl);
              if (content != null)
              {
            response.WriteContent(content);
            return;
              }

              response.StatusCode = (int)HttpStatusCode.NotFound;
        }
 /// Iterate over all responders to find one that works
 public void DispatchRequest(object sender, HttpRequestEventArgs request)
 {
     foreach (var responder in responderChain.Reverse<IHTTPRequestResponder>())
     {
         try {
             if (responder.process(request.Request, request.Response))
             {
                 return;
             }
         } catch (Exception ex) {
             PluginLogger.print("Caught exception in web handlers: " + ex.ToString());
         }
     }
     // If here, we had no responder.
     request.Response.StatusCode = (int)HttpStatusCode.NotFound;
 }
        private void acceptHttpRequest(HttpListenerContext context)
        {
            var args   = new HttpRequestEventArgs(context);
            var method = context.Request.HttpMethod;

            if (method == "GET")
            {
                if (OnGet != null)
                {
                    OnGet(this, args);
                    return;
                }
            }
            else if (method == "HEAD")
            {
                if (OnHead != null)
                {
                    OnHead(this, args);
                    return;
                }
            }
            else if (method == "POST")
            {
                if (OnPost != null)
                {
                    OnPost(this, args);
                    return;
                }
            }
            else if (method == "PUT")
            {
                if (OnPut != null)
                {
                    OnPut(this, args);
                    return;
                }
            }
            else if (method == "DELETE")
            {
                if (OnDelete != null)
                {
                    OnDelete(this, args);
                    return;
                }
            }
            else if (method == "OPTIONS")
            {
                if (OnOptions != null)
                {
                    OnOptions(this, args);
                    return;
                }
            }
            else if (method == "TRACE")
            {
                if (OnTrace != null)
                {
                    OnTrace(this, args);
                    return;
                }
            }
            else if (method == "CONNECT")
            {
                if (OnConnect != null)
                {
                    OnConnect(this, args);
                    return;
                }
            }
            else if (method == "PATCH")
            {
                if (OnPatch != null)
                {
                    OnPatch(this, args);
                    return;
                }
            }

            context.Response.StatusCode = (int)HttpStatusCode.NotImplemented;
        }
示例#4
0
        private void onRequest(HttpListenerContext context)
        {
            var req       = context.Request;
            var res       = context.Response;
            var eventArgs = new HttpRequestEventArgs(context);

            if (req.HttpMethod == "GET" && !OnGet.IsNull())
            {
                OnGet(this, eventArgs);
                return;
            }

            if (req.HttpMethod == "HEAD" && !OnHead.IsNull())
            {
                OnHead(this, eventArgs);
                return;
            }

            if (req.HttpMethod == "POST" && !OnPost.IsNull())
            {
                OnPost(this, eventArgs);
                return;
            }

            if (req.HttpMethod == "PUT" && !OnPut.IsNull())
            {
                OnPut(this, eventArgs);
                return;
            }

            if (req.HttpMethod == "DELETE" && !OnDelete.IsNull())
            {
                OnDelete(this, eventArgs);
                return;
            }

            if (req.HttpMethod == "OPTIONS" && !OnOptions.IsNull())
            {
                OnOptions(this, eventArgs);
                return;
            }

            if (req.HttpMethod == "TRACE" && !OnTrace.IsNull())
            {
                OnTrace(this, eventArgs);
                return;
            }

            if (req.HttpMethod == "CONNECT" && !OnConnect.IsNull())
            {
                OnConnect(this, eventArgs);
                return;
            }

            if (req.HttpMethod == "PATCH" && !OnPatch.IsNull())
            {
                OnPatch(this, eventArgs);
                return;
            }

            res.StatusCode = (int)HttpStatusCode.NotImplemented;
        }
    private void OnGet(object sender, HttpRequestEventArgs e) {
      var req = e.Request;
      var res = e.Response;
      if(req.RemoteEndPoint==null) {
        res.StatusCode=(int)HttpStatusCode.NotAcceptable;
        return;
      }
      System.Net.IPEndPoint remoteEndPoint = req.RemoteEndPoint;
      {
        System.Net.IPAddress remIP;
        if(req.Headers.Contains("X-Real-IP") && System.Net.IPAddress.TryParse(req.Headers["X-Real-IP"], out remIP)) {
          remoteEndPoint=new System.Net.IPEndPoint(remIP, remoteEndPoint.Port);
        }
      }
      string path = req.Url.LocalPath == "/" ? "/index.html" : req.Url.LocalPath;
      string client;
      client=remoteEndPoint.Address.ToString();

      try {
        Tuple<Stream, string> rsc;
        HttpStatusCode statusCode;
        if(_resources.TryGetValue(path.Substring(1), out rsc)) {
          string et;
          if(req.Headers.Contains("If-None-Match") && (et=req.Headers["If-None-Match"])==rsc.Item2) {
            res.Headers.Add("ETag", rsc.Item2);
            statusCode=HttpStatusCode.NotModified;
            res.StatusCode=(int)statusCode;
            res.WriteContent(Encoding.UTF8.GetBytes("Not Modified"));
          } else {
            res.Headers.Add("ETag", rsc.Item2);
            res.ContentType=Ext2ContentType(Path.GetExtension(path));
            rsc.Item1.Position=0;
            rsc.Item1.CopyTo(res.OutputStream);
            res.ContentLength64=rsc.Item1.Length;
            statusCode=HttpStatusCode.OK;
          }
        } else {
          FileInfo f = new FileInfo(Path.Combine(_srv.RootPath, path.Substring(1)));
          if(f.Exists) {
            string eTag=f.LastWriteTimeUtc.Ticks.ToString("X8")+"-"+f.Length.ToString("X4");
            string et;
            if(req.Headers.Contains("If-None-Match") && (et=req.Headers["If-None-Match"])==eTag) {
              res.Headers.Add("ETag", eTag);
              statusCode=HttpStatusCode.NotModified;
              res.StatusCode=(int)statusCode;
              res.WriteContent(Encoding.UTF8.GetBytes("Not Modified"));
            } else {
              res.Headers.Add("ETag", eTag);
              res.ContentType=Ext2ContentType(f.Extension);
              using(var fs=f.OpenRead()) {
                fs.CopyTo(res.OutputStream);
                res.ContentLength64=fs.Length;
              }
              statusCode=HttpStatusCode.OK;
            }
          } else {
            statusCode=HttpStatusCode.NotFound;
            res.StatusCode = (int)statusCode;
            res.WriteContent(Encoding.UTF8.GetBytes("404 Not found"));
          }
        }
        if(true) {
          Log.Debug("{0} [{1}]{2} - {3}", client, req.HttpMethod, req.RawUrl, statusCode.ToString());
        }
      }
      catch(Exception ex) {
        if(true) {
          Log.Debug("{0} [{1}]{2} - {3}", client, req.HttpMethod, req.RawUrl, ex.Message);
        }
      }
    }
示例#6
0
        private void acceptHttpRequest(HttpListenerContext context)
        {
            HttpRequestEventArgs e = new HttpRequestEventArgs(context);
            string httpMethod      = context.Request.HttpMethod;

            if (httpMethod == "GET")
            {
                if (this.OnGet != null)
                {
                    this.OnGet(this, e);
                    return;
                }
            }
            else if (httpMethod == "HEAD")
            {
                if (this.OnHead != null)
                {
                    this.OnHead(this, e);
                    return;
                }
            }
            else if (httpMethod == "POST")
            {
                if (this.OnPost != null)
                {
                    this.OnPost(this, e);
                    return;
                }
            }
            else if (httpMethod == "PUT")
            {
                if (this.OnPut != null)
                {
                    this.OnPut(this, e);
                    return;
                }
            }
            else if (httpMethod == "DELETE")
            {
                if (this.OnDelete != null)
                {
                    this.OnDelete(this, e);
                    return;
                }
            }
            else if (httpMethod == "OPTIONS")
            {
                if (this.OnOptions != null)
                {
                    this.OnOptions(this, e);
                    return;
                }
            }
            else if (httpMethod == "TRACE")
            {
                if (this.OnTrace != null)
                {
                    this.OnTrace(this, e);
                    return;
                }
            }
            else if (httpMethod == "CONNECT")
            {
                if (this.OnConnect != null)
                {
                    this.OnConnect(this, e);
                    return;
                }
            }
            else if (httpMethod == "PATCH" && this.OnPatch != null)
            {
                this.OnPatch(this, e);
                return;
            }
            context.Response.StatusCode = 501;
        }
 void _httpsv_OnRequest(object sender, HttpRequestEventArgs e)
 {
     Task.Factory.StartNew(() => InitTask(e.Context));
 }
示例#8
0
        private void OnGet(object sender, HttpRequestEventArgs e)
        {
            var req = e.Request;
              var res = e.Response;
              if(req.RemoteEndPoint==null) {
            res.StatusCode=(int)HttpStatusCode.NotAcceptable;
            return;
              }
              if(req.HttpMethod!="GET") {
            res.StatusCode=(int)HttpStatusCode.MethodNotAllowed;
            return;
              }
              System.Net.IPEndPoint remoteEndPoint = req.RemoteEndPoint;
              {
            System.Net.IPAddress remIP;
            if(req.Headers.Contains("X-Real-IP") && System.Net.IPAddress.TryParse(req.Headers["X-Real-IP"], out remIP)) {
              remoteEndPoint=new System.Net.IPEndPoint(remIP, remoteEndPoint.Port);
            }
              }
              string path=req.RawUrl=="/"?"/index.html":req.RawUrl;
              string client;
              Session ses;
              if(req.Cookies["sessionId"]!=null) {
            ses=Session.Get(req.Cookies["sessionId"].Value, remoteEndPoint, false);
              } else {
            ses=null;
              }

              if(ses!=null && ses.owner!=null) {
            client=ses.owner.name;
              } else {
            client=remoteEndPoint.Address.ToString();
              }

              try {
            FileInfo f = new FileInfo(Path.Combine(_sv.RootPath, path.Substring(1)));
            if(f.Exists) {
              string eTag=f.LastWriteTimeUtc.Ticks.ToString("X8")+"-"+f.Length.ToString("X4");
              string et;
              if(req.Headers.Contains("If-None-Match") && (et=req.Headers["If-None-Match"])==eTag) {
            res.Headers.Add("ETag", eTag);
            res.StatusCode=(int)HttpStatusCode.NotModified;
            res.WriteContent(Encoding.UTF8.GetBytes("Not Modified"));
              } else {
            byte[] content;
            if((content =_sv.GetFile(path))!=null) {
              res.Headers.Add("ETag", eTag);
              res.ContentType=Ext2ContentType(f.Extension);
              res.WriteContent(content);
            } else {
              res.StatusCode=(int)HttpStatusCode.InternalServerError;
              res.WriteContent(Encoding.UTF8.GetBytes("Content is broken"));
            }
              }
            } else {
              res.StatusCode = (int)HttpStatusCode.NotFound;
              res.WriteContent(Encoding.UTF8.GetBytes("404 Not found"));
            }
            //if(_verbose.value) {
              Log.Debug("{0} [{1}]{2} - {3}", client, req.HttpMethod, req.RawUrl, ((HttpStatusCode)res.StatusCode).ToString());
            //}
              }
              catch(Exception ex) {
            //if(_verbose.value) {
              Log.Debug("{0} [{1}]{2} - {3}", client, req.HttpMethod, req.RawUrl, ex.Message);
            //}
              }
        }