Example #1
0
        public ProcessingResult Process(RequestContext context)
        {
            // URL format : ~/login
              Regex r = new Regex(@"^/login$", RegexOptions.IgnoreCase);
              Match m = r.Match(context.Request.Uri.AbsolutePath.ToLowerInvariant());

              if (!m.Success) return ProcessingResult.Continue;

              IRequest request = context.Request;
              IResponse response = context.Response;

              var assembly = Assembly.GetExecutingAssembly();
              var resourceName = Assembly.GetExecutingAssembly().GetName().Name + @".html.cameras.html";

              string html;
              using (Stream stream = assembly.GetManifestResourceStream(resourceName))
              using (StreamReader reader = new StreamReader(stream))
              {
            html = reader.ReadToEnd();
              }

              response.ContentType = new ContentTypeHeader("txt/html");
              response.ContentLength.Value = html.Length;
              response.Add(new DateHeader("Last-Modified", DateTime.Now.ToUniversalTime()));

              var generator = HttpFactory.Current.Get<ResponseWriter>();
              generator.SendHeaders(context.HttpContext, response);
              generator.SendBody(context.HttpContext, new MemoryStream(Encoding.Default.GetBytes(html)));

              return ProcessingResult.Abort;
        }
Example #2
0
        public ProcessingResult Process(RequestContext context)
        {
            // URL format : ~/camera/{id}
              Regex r = new Regex(@"^/camera/(\w+)$", RegexOptions.IgnoreCase);
              Match m = r.Match(context.Request.Uri.AbsolutePath.ToLowerInvariant());

              if (!m.Success) return ProcessingResult.Continue;

              string cameraId = m.Groups[1].ToString().ToLowerInvariant();
              Camera camera = Locator.Get<ICameraManager>().GetCamera(cameraId);
              string json = JsonConvert.SerializeObject(camera);

              IRequest request = context.Request;
              IResponse response = context.Response;

              response.ContentType = new ContentTypeHeader("application/json");
              response.ContentLength.Value = json.Length;
              response.Add(new DateHeader("Last-Modified", DateTime.Now.ToUniversalTime()));

              var generator = HttpFactory.Current.Get<ResponseWriter>();
              generator.SendHeaders(context.HttpContext, response);
              generator.SendBody(context.HttpContext, new MemoryStream(Encoding.Default.GetBytes(json)));

              return ProcessingResult.Abort;
        }
Example #3
0
        public ProcessingResult Process(RequestContext context)
        {
            // URL format : ~/camera/{id}/thumbnail
              Regex r = new Regex(@"^/camera/(\w+)/thumbnail$", RegexOptions.IgnoreCase);
              Match m = r.Match(context.Request.Uri.AbsolutePath.ToLowerInvariant());

              if (!m.Success) return ProcessingResult.Continue;

              string cameraId = m.Groups[1].ToString().ToLowerInvariant();
              byte[] thumbnail = Locator.Get<ICameraManager>().GetCameraThumbnail(cameraId);

              IRequest request = context.Request;
              IResponse response = context.Response;

              response.ContentType = new ContentTypeHeader("image/bmp");
              response.ContentLength.Value = thumbnail.Length;
              response.Add(new DateHeader("Last-Modified", DateTime.Now.ToUniversalTime()));

              var generator = HttpFactory.Current.Get<ResponseWriter>();
              generator.SendHeaders(context.HttpContext, response);
              generator.SendBody(context.HttpContext, new MemoryStream(thumbnail));

              return ProcessingResult.Abort;
        }
Example #4
0
    /// <summary>
    /// 由模块处理请求消息
    /// </summary>
    /// <param name="context"></param>
    /// <returns></returns>
    private ProcessingResult ProcessModules(RequestContext context)
    {
      foreach (IModule module in _modules)
      {
        ProcessingResult result = module.Process(context);
        if (result != ProcessingResult.Continue)
        {
          Logger.Debug(module.GetType().Name + ": " + result);
          return result;
        }
      }

      return ProcessingResult.Continue;
    }
Example #5
0
    /// <summary>
    /// 由路由器处理请求消息
    /// </summary>
    /// <param name="context">Request context.</param>
    /// <returns>Processing result.</returns>
    private ProcessingResult ProcessRouters(RequestContext context)
    {
      foreach (IRouter router in _routers)
      {
        if (router.Process(context) != ProcessingResult.SendResponse)
          continue;

        Logger.Debug(router.GetType().Name + " sends the response.");
        return ProcessingResult.SendResponse;
      }

      return ProcessingResult.Continue;
    }
Example #6
0
    /// <summary>
    /// 在请求消息已到达,准备处理之前的准备工作
    /// </summary>
    /// <param name="context">The context.</param>
    /// <remarks>
    /// Default implementation adds a <c>Date</c> header and <c>Server</c> header.
    /// </remarks>
    protected virtual void OnBeforeProcessRequest(RequestContext context)
    {
      if (context.Request.ContentLength.Value > MaxContentSize)
        throw new HttpException(HttpStatusCode.RequestEntityTooLarge, "Too large body, limit is " + MaxContentSize);

      context.Response.Add(new DateHeader("Date", DateTime.UtcNow));
      context.Response.Add(new StringHeader("Server", ServerName));
    }
Example #7
0
 /// <summary>
 /// 查看消息头鉴权内容
 /// </summary>
 /// <param name="context">The context.</param>
 /// <remarks>
 /// Looks after a <see cref="AuthorizationHeader"/> in the request and will
 /// use the <see cref="AuthenticationProvider"/> if found.
 /// </remarks>
 protected virtual void AuthenticateRequest(RequestContext context)
 {
   var authHeader = (AuthorizationHeader)context.Request.Headers[AuthorizationHeader.AuthorizationName];
   if (authHeader != null)
   {
     AuthenticationProvider.Authenticate(context.Request);
   }
 }
Example #8
0
    /// <summary>
    /// 处理Http请求
    /// </summary>
    /// <param name="e"></param>
    /// <returns></returns>
    private ProcessingResult HandleRequest(RequestEventArgs e)
    {
      var context = new RequestContext
                        {
                          HttpContext = e.Context,
                          Request = e.Request,
                          Response = e.Response
                        };

      // 鉴权
      AuthenticateRequest(context);

      // 前期处理
      OnBeforeProcessRequest(context);

      // 通知外部请求到达
      BeforeProcessRequest(this, e);

      // 解析消息体
      if (e.Request.ContentLength.Value > 0)
      {
        DecodeBody(e.Request);
      }

      ProcessingResult result = ProcessingResult.Continue;

      // 由路由器处理消息
      result = ProcessRouters(context);
      if (ProcessResult(result, e))
      {
        Logger.Debug("Routers processed the request.");
      }

      // 有模块处理消息
      result = ProcessModules(context);
      if (ProcessResult(result, e))
      {
        return result;
      }

      // 通知请求已收到,由外部模块处理请求
      RequestReceived(this, e);

      return ProcessingResult.Continue;
    }