コード例 #1
0
 public void PrintResorceLocation()
 {
     foreach (KeyValuePair <string, stWebResourceLocator> pair in this._ResourceLocator)
     {
         stWebResourceLocator rl = pair.Value as stWebResourceLocator;
         stConsole.WriteLine(
             pair.Key +
             "\n\t - IPFilter: " + (rl.IPFilter != null).ToString() +
             "\n\t - CallBack: " + ((rl.ReqCallBack != null) ? rl.ReqCallBack.Method.Name : "*CallBack none") +
             "\n\t - IpFilterType: " + ((rl.IPFilter != null) ? rl.IPFilter.IpFilterType.ToString() : "*IPFilter is null") +
             "\n\t - GeoFilterType: " + ((rl.IPFilter != null) ? rl.IPFilter.GeoAsnFilterType.ToString() : "*IPFilter is null") +
             "\n\t - List.IpRange: " + (rl.IPFilter.IpRange != null).ToString() + ", Count: " + ((rl.IPFilter != null) ? ((rl.IPFilter.IpRange != null) ? rl.IPFilter.IpRange.Count : 0) : 0) +
             "\n\t - List.GeoDataASN: " + (rl.IPFilter.GeoDataASN != null).ToString() + ", Count: " + ((rl.IPFilter != null) ? ((rl.IPFilter.GeoDataASN != null) ? rl.IPFilter.GeoDataASN.Count : 0) : 0) +
             "\n\t - List.GeoDataCounry: " + (rl.IPFilter.GeoDataCounry != null).ToString() + ", Count: " + ((rl.IPFilter != null) ? ((rl.IPFilter.GeoDataCounry != null) ? rl.IPFilter.GeoDataCounry.Count : 0) : 0)
             );
     }
 }
コード例 #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);
            }
        }
コード例 #3
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();
                        }
                    }
                }
            }
        }