Composer.py
1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""
@author Georg Hopp
"""
import Message
class Composer(object):
"""
compose to HTTP
=====================================================================
"""
def composeStartLine(self, message):
"""
compose a HTTP message StartLine... currently this does no check for
the validity of the StartLine.
returns str The composed HTTP start line (either Request or Status)
@message: HttpMessage The message that should be composed.
"""
return message.getStartLine() + '\r\n'
def composeHeaders(self, message):
"""
this creates header lines for each key/value[n] pair.
returns str All headers composed to an HTTP string.
@message: HttpMessage The message to compose the header from.
"""
headers = message.getHeaders()
return '\r\n'.join([':'.join(h) for h in headers]) + '\r\n'
def composeStartLineHeaders(self, message):
"""
Compose the start line and the headers.
returns str The start line and the headers as HTTP string.
@message: HttpMessage The message to be composed.
"""
return self.composeStartLine(message) + self.composeHeaders(message)
def compose(self, message):
"""
Compose the whole message to an HTTP string.
returns str The whole message as an HTTP string.
@message: HttpMessage The message to be composed.
"""
return self.composeStartLineHeaders(message) + "\r\n" + message.getBody()
# vim: set ft=python et ts=8 sw=4 sts=4: