Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to include a custom http header in a <video> source call?

I have a simple html5 player, implemented with videoJS. In order to properly retrieve the source files from the server, I need to set a custom header in the request for the video.

Since the application uses AngularJS, I implemented an Interceptor to set the header:

myApp.factory('headerInterceptor', function () {
  return {
    request: function (config) {
        config.headers['my-header'] = 'test';
        return config;
    }
  };
});

myApp.config(['$httpProvider', function($httpProvider) {
    $httpProvider.interceptors.push('headerInterceptor');
}

Problem with this, is that the call to the video is not catched by it, so no header is set (it is though for other resources). So angular does not load the videos. No big surprise in that. Checking the Network tab in developer tools, found out that videoJS initiates the call:

failed source retrieval

But finding my way in the videoJS plugin has been difficult, and couln't find where the calls are made. Im just wondering, is there a simple way to set the header for this call? doesn't matter if it's plain javascript, or through angular, or even modifying videoJS plugin.

like image 455
ezabaw Avatar asked May 15 '15 17:05

ezabaw


People also ask

What are some examples of custom HTTP headers?

Another example of using a custom HTTP header would be to implement the X-Pull header. You can use this custom header for a variety of purposes including rate limiting bandwidth on your origin server, restricting CDN traffic, creating custom logic on your origin server, etc.

What is the difference between HTTP headers and proprietary headers?

HTTP headers let the client and the server pass additional information with an HTTP request or response. An HTTP header consists of its case-insensitive name followed by a colon (: ), then by its value. Whitespace before the value is ignored. Custom proprietary headers have historically been used with an X- prefix,...

Why do some HTTP headers start with X-?

Initially, it was recommended to begin naming custom headers with X- so that users would be aware that these headers were custom and not standardized. However, according to RFC 6648, this recommendation has since been deprecated.

What is the purpose of the Host HTTP header?

It is a request-type header. It is used to identify the original host requested by the client in the Host HTTP request header. It is an request-type header. It is used to identifying the protocol that the client used to connect with a proxy or load balancer. It can be HTTP or HTTPS.


1 Answers

Inside an Angular run block, you can override the videojs plugin XHR beforeRequest function. YMMV, but if you use HLS plugin for example, you could do something like that:

angular.module('app', []).run(function($localStorage) {

    var _beforeRequest = videojs.Hls.xhr.beforeRequest;
    videojs.Hls.xhr.beforeRequest = function(options) {
        if (_.isFunction(_beforeRequest)) {
            options = _beforeRequest(options);
        }
        if ($localStorage.token) {
            options.headers = options.headers || {};
            options.headers.Authorization = 'Token ' + $localStorage.token;
        }
        return options;
    };
});
like image 152
blokfyuh Avatar answered Oct 11 '22 14:10

blokfyuh