137 lines
2.6 KiB
Python
137 lines
2.6 KiB
Python
|
import io
|
||
|
import string
|
||
|
|
||
|
import hserver.utils
|
||
|
|
||
|
class MIMEHeader:
|
||
|
|
||
|
def __init__(self,line):
|
||
|
self.__line = line
|
||
|
self.__parameters ={}
|
||
|
|
||
|
self.__parseline()
|
||
|
|
||
|
def name(self):
|
||
|
return self.__name
|
||
|
|
||
|
def rawvalue(self):
|
||
|
return self.__rawvalue
|
||
|
|
||
|
def value(self):
|
||
|
return self.__value
|
||
|
|
||
|
def parameters(self):
|
||
|
return self.__parameters
|
||
|
|
||
|
|
||
|
def __parseparameters(self,parameters):
|
||
|
p = 0
|
||
|
while (p < len(parameters)):
|
||
|
pe = parameters.find("=",p)
|
||
|
if (pe < 0):
|
||
|
return
|
||
|
|
||
|
name = parameters[p:pe].strip()
|
||
|
|
||
|
pe = pe + 1
|
||
|
|
||
|
if (parameters[pe] == '"'):
|
||
|
pe = pe + 1
|
||
|
pq = parameters.find('"',pe)
|
||
|
pr = parameters.find(';',pq)
|
||
|
value = parameters[ pe : pq]
|
||
|
else:
|
||
|
pq = parameters.find(";",pe)
|
||
|
if (pq < 0):
|
||
|
pq = len(parameters)
|
||
|
pr = pq
|
||
|
value = parameters[ pe : pq].strip()
|
||
|
|
||
|
self.__parameters[name] = value
|
||
|
if (pr < 0):
|
||
|
return
|
||
|
p = pr + 1
|
||
|
|
||
|
def __parseline(self):
|
||
|
i = self.__line.find(":")
|
||
|
if (i > 0):
|
||
|
self.__name = self.__line[:i].upper()
|
||
|
self.__rawvalue = self.__line[ i+1 : ].strip()
|
||
|
|
||
|
i = self.__rawvalue.find(";")
|
||
|
if (i>0):
|
||
|
self.__parseparameters(self.__rawvalue[ i+1 : ])
|
||
|
self.__value = self.__rawvalue[ :i ]
|
||
|
else:
|
||
|
self.__value = self.__rawvalue
|
||
|
|
||
|
|
||
|
# print("MIMEHeader: %s: %s %s" % (self.__name,self.__value,self.__parameters))
|
||
|
|
||
|
|
||
|
|
||
|
class MIMEHeaders:
|
||
|
|
||
|
def __init__(self, fp=None, src=None):
|
||
|
self.__items = {}
|
||
|
self.__parameters = {}
|
||
|
|
||
|
if not fp is None:
|
||
|
self.__read(fp)
|
||
|
|
||
|
def get(self,name):
|
||
|
name = name.upper()
|
||
|
if not name in self.__items.keys():
|
||
|
return None
|
||
|
|
||
|
return self.__items[name]
|
||
|
|
||
|
def __getitem__(self,name):
|
||
|
name = name.upper()
|
||
|
if name in self.__items.keys():
|
||
|
return self.__items[name].rawvalue()
|
||
|
raise KeyError()
|
||
|
|
||
|
def value(self,name,default=None):
|
||
|
h = self.get(name)
|
||
|
if h is None:
|
||
|
return default
|
||
|
return h.value()
|
||
|
|
||
|
def rawvalue(self,name,default=None):
|
||
|
h = self.get(name)
|
||
|
if h is None:
|
||
|
return default
|
||
|
return h.rawvalue()
|
||
|
|
||
|
def parameters(self,name):
|
||
|
h = self.get(name)
|
||
|
if h is None:
|
||
|
return {}
|
||
|
return h.parameters()
|
||
|
|
||
|
|
||
|
def __read(self,fp):
|
||
|
cont = True
|
||
|
lines = []
|
||
|
|
||
|
while (cont):
|
||
|
l = str(fp.readline(),"iso-8859-1").strip()
|
||
|
if (l.strip() == ""):
|
||
|
cont = False
|
||
|
else:
|
||
|
if (l[0] in string.whitespace ):
|
||
|
lines[-1] = lines[-1] + " " + l.lstrip()
|
||
|
else:
|
||
|
lines.append(l)
|
||
|
|
||
|
for line in lines:
|
||
|
h = MIMEHeader(line)
|
||
|
self.__items[ h.name() ] = h
|
||
|
|
||
|
if self.get("CONTENT-TYPE") is None:
|
||
|
self.__items[ "CONTENT-TYPE" ] = MIMEHeader("Content-Type: text/plain")
|
||
|
|
||
|
|
||
|
|