Example #1
0
        private void _AddHandler(WebHandleTypes ht, Action <string, object, object> httpRequestHandler, stIPFilter ipFilter, string path, string dir)
        {
            this._Check();

            if (httpRequestHandler == null)
            {
                throw new ArgumentNullException(Properties.Resources.httpCallbackNull);
            }
            if (string.IsNullOrWhiteSpace(path))
            {
                path = httpRequestHandler.ToString();
            }

            path = ((!path.StartsWith("/")) ? "/" + path : path);
            path = ((!path.EndsWith("/")) ? path + "/" : path);

            stWebResourceLocator resLocator = new stWebResourceLocator()
            {
                IPFilter    = ((ipFilter == null) ? new stIPFilter() : ipFilter),
                ReqCallBack = httpRequestHandler,
                HandleTypes = ht
            };

            if (!this._ResourceLocator.ContainsKey(path))
            {
                this._ResourceLocator.Add(path, resLocator);
            }
            else
            {
                this._ResourceLocator[path] = resLocator;
            }
            if (!this._listener.Prefixes.Contains(path))
            {
                try
                {
                    this._listener.Prefixes.Add(
                        string.Format(
                            @"{0}{1}",
                            this._SrvUri,
                            path
                            )
                        );
                }
                catch (Exception e)
                {
                    throw new ArgumentException(
                              string.Format(
                                  Properties.Resources.httpListenerExceptionAddResource,
                                  e.Message
                                  )
                              );
                }
            }
            if (
                ((this._isConcat) || (this._isMinify)) &&
                (!string.IsNullOrWhiteSpace(dir))
                )
            {
                path = path.Substring(1, (path.Length - 2));
                path = Path.Combine(dir, path);

                if (Directory.Exists(path))
                {
                    stBootStrap minify = null;

                    try
                    {
                        minify = new stBootStrap(this._isConcat, this._isMinify, this._iLog);
                        if (this._WatchChange)
                        {
                            minify.Minify(path, this._AddContentChangeWatch);
                        }
                        else
                        {
                            minify.Minify(path, null);
                        }
                    }
                    catch (Exception e)
                    {
                        throw new ArgumentException(e.Message);
                    }
                    finally
                    {
                        if (minify != null)
                        {
                            minify.Dispose();
                        }
                    }
                }
            }
        }
Example #2
0
        private void _OnRequest(object ctx)
        {
            bool isRequestFound         = false;
            HttpListenerContext context = ctx as HttpListenerContext;

            context.Response.Headers.Add(@"Server", this._httpUa);
            context.Response.ProtocolVersion = new Version("1.1");

            try
            {
                int            statusCode = 0;
                string         respTxt    = String.Empty;
                WebHandleTypes wht        = WebHandleTypes.None;

                foreach (KeyValuePair <string, stWebResourceLocator> pair in this._ResourceLocator)
                {
                    if (context.Request.RawUrl.StartsWith(pair.Key))
                    {
                        stWebResourceLocator rl = pair.Value as stWebResourceLocator;
                        if (rl.IPFilter != null)
                        {
                            if (rl.IPFilter.IpFilterCheck(
                                    stWebServerUtil.HttpUtil.GetHttpClientIP(context.Request))
                                )
                            {
                                wht        = rl.HandleTypes;
                                respTxt    = HttpStatusCode.Forbidden.ToString();
                                statusCode = (int)HttpStatusCode.Forbidden;
                                break;
                            }
                        }
                        rl.ReqCallBack(context.Request.RawUrl, context, this._userData);
                        statusCode     = (int)HttpStatusCode.OK;
                        isRequestFound = true;
                        break;
                    }
                }
                if (!isRequestFound)
                {
                    respTxt = ((string.IsNullOrWhiteSpace(respTxt)) ? context.Request.RawUrl : respTxt);
                    context.Response.StatusCode = ((statusCode > 0) ? statusCode : (int)HttpStatusCode.BadRequest);

                    if (this._badReq != null)
                    {
                        this._badReq(
                            respTxt,
                            context.Request,
                            context.Response,
                            this._userData
                            );
                    }
                    else
                    {
                        switch (wht)
                        {
                        default:
                        case WebHandleTypes.None:
                        case WebHandleTypes.TemplateWebRequest:
                        case WebHandleTypes.FileWebRequest:
                        {
                            this.BadRequestHtml(
                                respTxt,
                                context,
                                statusCode
                                );
                            break;
                        }

                        case WebHandleTypes.JsonWebRequest:
                        {
                            this.BadRequestJson(
                                respTxt,
                                context,
                                statusCode
                                );
                            break;
                        }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                this._iLog.LogError(Properties.Resources.httpListenerExceptionOnReq + e.Message);
            }
        }
Example #3
0
 public void AddHandler(WebHandleTypes ht, Action <string, object, object> httpRequestHandler, string path = null, stIPFilter ipFilter = null, string dir = null)
 {
     this._AddHandler(ht, httpRequestHandler, ipFilter, path, dir);
 }