2014-06-27 88 views
1

我設法在IIS 7上設置modh264工作得很好,僞流正常工作。 我不能讓jwplayer僞流與中間的httphandler一起工作。 我的意思是,只要你點擊不同的位置,視頻就從頭開始! 如果我刪除處理程序,則僞流按預期工作。 這裏我的問題是阻止人們直接訪問我的視頻(我不關心他們是否通過瀏覽器緩存保存視頻)。 我曾因爲影片是大到足以讓內存例外通過10K字節的塊加載jwplayer無法僞裝流通過HTTP處理程序加載的視頻

,這裏是我的HttpHandler

public class DontStealMyMoviesHandler : IHttpHandler 
{ 
    /// <summary> 
    /// You will need to configure this handler in the web.config file of your 
    /// web and register it with IIS before being able to use it. For more information 
    /// see the following link: http://go.microsoft.com/?linkid=8101007 
    /// </summary> 
    #region IHttpHandler Members 

    public bool IsReusable 
    { 
     // Return false in case your Managed Handler cannot be reused for another request. 
     // Usually this would be false in case you have some state information preserved per request. 
     get { return true; } 
    } 

    public void ProcessRequest(HttpContext context) 
    { 
     HttpRequest req = context.Request; 
     string path = req.PhysicalPath; 
     string extension = null; 
     string contentType = null; 
     string fileName = ""; 
     if (req.UrlReferrer == null) 
     { 
      context.Response.Redirect("~/Home/"); 
     } 
     else 
     { 
      fileName = "file.mp4"; 

      if (req.UrlReferrer.Host.Length > 0) 
      { 
       if (req.UrlReferrer.ToString().ToLower().Contains("/media/")) 
       { 
        context.Response.Redirect("~/Home/"); 
       } 
      } 
     } 

     extension = Path.GetExtension(req.PhysicalPath).ToLower(); 
     switch (extension) 
     { 
      case ".m4v": 
      case ".mp4": 
       contentType = "video/mp4"; 
       break; 
      case ".avi": 
       contentType = "video/x-msvideo"; 
       break; 
      case ".mpeg": 
       contentType = "video/mpeg"; 
       break; 
      //default: 
      // throw new notsupportedexception("unrecognized video type."); 
     } 

     if (!File.Exists(path)) 
     { 
      context.Response.Status = "movie not found"; 
      context.Response.StatusCode = 404; 
     } 
     else 
     { 
      try 
      { 
       //context.Response.Clear(); 
       //context.Response.AddHeader("content-disposition", "attachment; filename=file.mp4"); 
       //context.Response.ContentType = contentType; 
       //context.Response.WriteFile(path, false); 

       //if(HttpRuntime.UsingIntegratedPipeline) 
       // context.Server.TransferRequest(context.Request.Url.ToString(), true); 
       //else 
       // context.RewritePath(context.Request.Url.AbsolutePath.ToString(), true); 


       // Buffer to read 10K bytes in chunk: 
       byte[] buffer = new Byte[10000]; 
       // Length of the file: 
       int length; 
       // Total bytes to read: 
       long dataToRead; 
       using (FileStream iStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)) 
       { 
        // Total bytes to read: 
        dataToRead = iStream.Length; 

        context.Response.Clear(); 
        context.Response.Cache.SetNoStore(); 
        context.Response.Cache.SetLastModified(DateTime.Now); 
        context.Response.AppendHeader("Content-Type", contentType); 
        context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName); 
        // Read the bytes. 
        while (dataToRead > 0) 
        { 
         // Verify that the client is connected. 
         if (context.Response.IsClientConnected) 
         { 
          // Read the data in buffer. 
          length = iStream.Read(buffer, 0, 10000); 

          // Write the data to the current output stream. 
          context.Response.OutputStream.Write(buffer, 0, length); 

          // Flush the data to the HTML output. 
          context.Response.Flush(); 

          buffer = new Byte[10000]; 
          dataToRead = dataToRead - length; 
         } 
         else 
         { 
          //prevent infinite loop if user disconnects 
          dataToRead = -1; 
         } 
        } 
       } 
      } 
      catch (Exception e) 
      { 
       context.Response.Redirect("home"); 
      } 
      finally 
      { 
       context.Response.Close(); 
      } 
     } 
    } 

    #endregion 
} 

預先感謝您

+0

你能提供一個鏈接嗎? – emaxsaun

+0

目前我不能在因爲它在我們的內部網託管。我們沒有可以外部瀏覽的鏈接。我可以在未來的幾個小時內鏈接一個外部鏈接...我希望;) – kaomao

+0

好的,請讓我知道什麼時候鏈接了,謝謝。 – emaxsaun

回答

0

我解決創建一個HTTP模塊來代替,因爲用httpHandler我必須自己管理響應,導致僞流失敗(文件完全加載到輸出流)。雖然這樣,如果有人直接訪問文件,我只是做一個簡單的重定向。我不明白爲什麼重定向到「〜/」不起作用。

public class DontStealMyMoviesModule : IHttpModule 
{ 
    public DontStealMyMoviesModule() 
    { 
    } 
    public void Init(HttpApplication r_objApplication) 
    { 
     // Register our event handler with Application object. 
     r_objApplication.PreSendRequestContent +=new EventHandler(this.AuthorizeContent); 
    } 

    public void Dispose() 
    { 

    } 

    private void AuthorizeContent(object r_objSender, EventArgs r_objEventArgs) 
    { 
     HttpApplication objApp = (HttpApplication)r_objSender; 
     HttpContext objContext = (HttpContext)objApp.Context; 

     HttpRequest req = objContext.Request; 

     if (Path.GetExtension(req.PhysicalPath).ToLower() != ".mp4") return; 

     if (req.UrlReferrer == null) 
     { 
      objContext.Response.Redirect("/"); 
     } 
    } 
}