Skip to content

Commit

Permalink
Fix for #49: Added optional scrict=True parameter to avoid silently a…
Browse files Browse the repository at this point in the history
…ccepting things like html by raising ParseError. (Defaults to false for backward compatibility.)
  • Loading branch information
GrumpyOldTroll committed Jul 22, 2015
1 parent bdea70a commit 4e3e672
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
4 changes: 2 additions & 2 deletions m3u8/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@


from m3u8.model import M3U8, Playlist, IFramePlaylist, Media, Segment
from m3u8.parser import parse, is_url
from m3u8.parser import parse, is_url, ParseError

__all__ = ('M3U8', 'Playlist', 'IFramePlaylist', 'Media',
'Segment', 'loads', 'load', 'parse')
'Segment', 'loads', 'load', 'parse', 'ParseError')

def loads(content):
'''
Expand Down
4 changes: 2 additions & 2 deletions m3u8/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ class M3U8(object):
('playlist_type', 'playlist_type')
)

def __init__(self, content=None, base_path=None, base_uri=None):
def __init__(self, content=None, base_path=None, base_uri=None, strict=False):
if content is not None:
self.data = parser.parse(content)
self.data = parser.parse(content, strict)
else:
self.data = {}
self._base_uri = base_uri
Expand Down
20 changes: 19 additions & 1 deletion m3u8/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import itertools
import re
from m3u8 import protocol
import exceptions

'''
http://tools.ietf.org/html/draft-pantos-http-live-streaming-08#section-3.2
Expand All @@ -21,7 +22,15 @@ def cast_date_time(value):
def format_date_time(value):
return value.isoformat()

def parse(content):
class ParseError(exceptions.Exception):
def __init__(self, lineno, line):
self.lineno = lineno
self.line = line

def __str__(self):
return 'Syntax error in manifest on line %d: %s' % (self.lineno, self.line)

def parse(content, strict=False):
'''
Given a M3U8 playlist content returns a dictionary with all data found
'''
Expand All @@ -42,7 +51,9 @@ def parse(content):
'expect_playlist': False,
}

lineno = 0
for line in string_to_lines(content):
lineno += 1
line = line.strip()

if line.startswith(protocol.ext_x_byterange):
Expand Down Expand Up @@ -100,6 +111,13 @@ def parse(content):
elif line.startswith(protocol.ext_x_endlist):
data['is_endlist'] = True

elif line.startswith('#'):
# comment
pass

elif strict:
raise ParseError(lineno, line)

return data

def _parse_key(line):
Expand Down

0 comments on commit 4e3e672

Please sign in to comment.