[Release] Service to pass cookies/HTTP headers to XBMC video player
#1
Update: There might be a better solution - I just found this AFTER writing this script tonight. Just for correctness, please check this thing first: http://forum.xbmc.org/showthread.php?tid=77500

Hi,
I did not find a way to pass cookies and other HTTP headers to XBMC for video playback, so I wrote a small python script that can be installed as an XBMC service. It works a bit like a proxy server (remember VeohProxy?) and allows you to specify:
- the URL to download and
- the HTTP headers to send with each request XBMC makes to that URL
It will just pass through the request headers that XBMC sends to the local webserver and add the ones you specified. It will then send back all the data it receives from the real server.

The script might be useful for streaming directly from one-click-hosting services like Rapidshare, FileServe and others. These services typically use an HTML form or some kind of HTTP API to validate the user's credentials and sets a cookie with a session key - without this cookie, there is no way to download the file.

It provides a webserver on 127.0.0.1:64653. To make a request, first encode the URL you want to retrieve in Base64 and do the same with the HTTP headers to be sent. The HTTP headers need to be in the format a web server would expect:
Code:
Key: Value\r\n
Key2: Value2\r\n
The URL is formed like this:
Code:
http://127.0.0.1:64653/withheaders/[urlBase64]/[HeadersBase64]
An example:
if the video URL is
Code:
http://my.test.com/testfile_needs_cookies.avi
and the header needs to contain the following information:
Code:
Cookie: AuthorizedUser0815
, the resulting URL is:
Code:
http://127.0.0.1:64653/withheaders/aHR0cDovL215LnRlc3QuY29tL3Rlc3RmaWxlX25lZWRzX2Nvb2tpZXMuYXZp/Q29va2llOiBBdXRob3JpemVkVXNlcjA4MTUNCg==
You can now pass this URL to XBMC as the video URL.

You can check in your addon if the service is running by querying
Code:
http://127.0.0.1:64653/version
, which should return:
Code:
Proxy: Running
Version: 0.1

Here's the script:
http://pastebin.com/b1mJzTbL

I wrote this only for testing purposes, but figured it might help other people to create better streaming plugins.
Reply
#2
Cheers - I was actually researching gateway proxy servers in python for use in my plugin - so this has come along at the right time!

For mine I only need to send a couple of headers for authetication, and this script looks perfect. I'll give it a bash and let you know the outcome..
Reply
#3
hippojay Wrote:Cheers - I was actually researching gateway proxy servers in python for use in my plugin - so this has come along at the right time!

For mine I only need to send a couple of headers for authetication, and this script looks perfect. I'll give it a bash and let you know the outcome..

Hey, please see this post first: http://forum.xbmc.org/showthread.php?tid=77500 - the functionality of manipulating headers seems to be implemented natively in XBMC. Just use the pipe character after the url and set the headers using key=value touples.
Reply
#4
Wow - thanks for finding that. I shall be giving that test later then...
Reply
#5
neat thanks for this...
Reply
#6
Just to confirm that the | and headers has fixed my authtication issue when playing back media and getting thumbnails.

That was a great find, I spent ages going over the various XBMC documents/functions to see if adding headers was possible - for some reason I didn't think to search in thie forum...
Reply
#7
note that you can achieve this without any proxy as well.

http://some/url|Cookie=<urlencodedcookie> - set a cookie
http://some/url|Something=<urlencodedvalue> - set a value in the request header

and of course the combination
http://some/url|Cookie=<urlencodedcookie...codedvalue> - set a cookie
Reply
#8
Unbehagen - I was just wondering - could this proxy be altered so that it concatenates file requests into a single stream?

I want to take a playlist that serves out .TS file segments: get the proxy to retrieve the data in the correct order and serve out a continous stream of data. I guess all I need to do is add some extra logic in for parsing the playlist and then a routine to take the extra data - somewhere in lines 106 -112?
Reply
#9
That should be doable. You have to think about something for the Content-Length HTTP header though and think about something for range requests. Without one, XBMC is usually not able to skip parts. You can have a look at the slightly chaotic VeohProxy - it does something like that for Veoh/Ninjavideo file parts. Sorry for the messy code in that project.
Reply
#10
@spiff - I know. I found that function AFTER I wrote that thing and updated the top post with that info and a link to a thread discussing the pipe character in XBMC URLs.
Reply
#11
Unbehagen Wrote:That should be doable. You have to think about something for the Content-Length HTTP header though and think about something for range requests.

Thanks - it's for m3u8 playlists, so the playlist tags actually provides quite a bit of the data needed (such as segments lengths and total lengths). If I can parse that I should be ok might give it a blast as see what happens.

Not sure about range requests - i guess I can put something together to deal with those once I work out how they are requested...
Reply
#12
Ahh - tried and failed. Could get the list together but the range requests and the multiple page requests (HEAD, GET and GET) are just too much for me right now...

I did manage to play 4-5 seconds though!!
Reply
#13
yeah, it's a bit complicated. did you have a look at veohproxy's crappy code? the head request is easy, just close the connection before any actual data is transmitted. the range requests are more complicated though. you have to add up the size of all files as the total size of your artificially joined file. then you have to calculate the file part of the requested position and the position within that file part.

edit: maybe there is an option to tell the client that range requests are not possible and files can only be read in a sequential way?
Reply
#14
Yeah, the issue was the two GETs.

I assume the first one is to read the stream to get the details (make sure it is supported, set up the demuxer) and the second is to complete the stream.

As I have a collection of .TS file (~840) it's impossible to know the full size. I suppose the best thing would be to return a fake size, send the first file as the 1st GET (as I know the size of that anyway) and the rest of the files as the stream...

But i'm not sure if XBMC is expecting a response of content-length size? I'm not sure if I tried a content-length of *?
Reply
#15
I wish I would have found your post earlier. I was working on similar script in order to pass 'X-Forwarded-For' header to the stream in order to play it and stream forward to a local IP. My issue is to stop the server properly.

I will look at your addon, thanks for that.
Image
_____________________________

Repositories Installer: select and install unofficial repositories / TAC.TV: watch videos on TAC.TV
Installer Passion-XBMC: Download and Install Add-ons (pre-Dharma only)

Image
Reply

Logout Mark Read Team Forum Stats Members Help
[Release] Service to pass cookies/HTTP headers to XBMC video player0