asp.net mvc - jwplayer unable to pseudo stream a video loaded via http handler -
i managed set modh264 on iis 7 working fine, pseudo streaming working great. can't jwplayer pseudo streaming work httphandler in-between. mean video starts beginning whenever click in different position! if remove handler pseudo streaming works expected. problem here prevent people gaining direct access videos (i don't care if save video via browser cache). had load via 10k bytes chunks since videos big enough memory exception
here's httphandler
public class dontstealmymovieshandler : ihttphandler { /// <summary> /// need configure handler in web.config file of /// web , register iis before being able use it. more information /// see following link: http://go.microsoft.com/?linkid=8101007 /// </summary> #region ihttphandler members public bool isreusable { // return false in case managed handler cannot reused request. // false in case have state information preserved per request. { 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 read 10k bytes in chunk: byte[] buffer = new byte[10000]; // length of file: int length; // total bytes read: long datatoread; using (filestream istream = new filestream(path, filemode.open, fileaccess.read, fileshare.read)) { // total bytes 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 bytes. while (datatoread > 0) { // verify client connected. if (context.response.isclientconnected) { // read data in buffer. length = istream.read(buffer, 0, 10000); // write data current output stream. context.response.outputstream.write(buffer, 0, length); // flush data 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"); } { context.response.close(); } } } #endregion }
thank in advance
i solved creating httpmodule instead, because httphandler had manage response myself causing pseudo stream fail (the file loaded entirely output stream). while way if accessing file directly simple redirect. don't why redirect "~/" doesn't work.
public class dontstealmymoviesmodule : ihttpmodule { public dontstealmymoviesmodule() { } public void init(httpapplication r_objapplication) { // register our event handler 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("/"); } } }
Comments
Post a Comment