forked from cms-sw/cmsdist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cherrypy-report-all-bytes.patch
148 lines (137 loc) · 5.19 KB
/
cherrypy-report-all-bytes.patch
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
diff --git a/cherrypy/_cprequest.py b/cherrypy/_cprequest.py
index 50176f0..7e8745f 100644
--- a/cherrypy/_cprequest.py
+++ b/cherrypy/_cprequest.py
@@ -567,8 +567,6 @@ class Request(object):
# HEAD requests MUST NOT return a message-body in the response.
cherrypy.response.body = []
- cherrypy.log.access()
-
if cherrypy.response.timed_out:
raise cherrypy.TimeoutError()
diff --git a/cherrypy/wsgiserver/__init__.py b/cherrypy/wsgiserver/__init__.py
index d6a98d3..7bfeb9b 100644
--- a/cherrypy/wsgiserver/__init__.py
+++ b/cherrypy/wsgiserver/__init__.py
@@ -98,6 +98,7 @@ import traceback
from urllib import unquote
from urlparse import urlparse
import warnings
+import cherrypy
try:
from OpenSSL import SSL
@@ -297,6 +298,7 @@ class HTTPRequest(object):
self.sent_headers = False
self.close_connection = False
self.chunked_write = False
+ self.environ["cherrypy.wfile"] = wfile
def parse_request(self):
"""Parse the next HTTP request start-line and message-headers."""
@@ -565,24 +567,25 @@ class HTTPRequest(object):
# invocation of the write() callable." (PEP 333)
if chunk:
self.write(chunk)
+
+ if (self.ready and not self.sent_headers):
+ self.sent_headers = True
+ self.send_headers()
+ if self.chunked_write:
+ try:
+ trailer = ["0\r\n"] + [k + ": " + v + "\r\n" for k, v in self.outheaders] + ["\r\n"]
+ except TypeError:
+ if not isinstance(k, str):
+ raise TypeError("WSGI trailer header key %r is not a string.")
+ if not isinstance(v, str):
+ raise TypeError("WSGI trailer header value %r is not a string.")
+ else:
+ raise
+ self.wfile.sendall("".join(trailer))
finally:
+ cherrypy.log.access()
if hasattr(response, "close"):
response.close()
-
- if (self.ready and not self.sent_headers):
- self.sent_headers = True
- self.send_headers()
- if self.chunked_write:
- try:
- trailer = ["0\r\n"] + [k + ": " + v + "\r\n" for k, v in self.outheaders] + ["\r\n"]
- except TypeError:
- if not isinstance(k, str):
- raise TypeError("WSGI trailer header key %r is not a string.")
- if not isinstance(v, str):
- raise TypeError("WSGI trailer header value %r is not a string.")
- else:
- raise
- self.wfile.sendall("".join(trailer))
def simple_response(self, status, msg=""):
"""Write a simple response back to the client."""
@@ -733,6 +736,8 @@ if not _fileobject_uses_str_type:
class CP_fileobject(socket._fileobject):
"""Faux file object attached to a socket object."""
__read_pos = 0
+ bytes_read = 0
+ bytes_written = 0
def sendall(self, data):
"""Sendall for non-blocking sockets."""
@@ -740,12 +745,15 @@ if not _fileobject_uses_str_type:
try:
bytes_sent = self.send(data)
data = data[bytes_sent:]
+ self.bytes_written += bytes_sent
except socket.error, e:
if e.args[0] not in socket_errors_nonblocking:
raise
def send(self, data):
- return self._sock.send(data)
+ bytes_sent = self._sock.send(data)
+ self.bytes_written += bytes_sent
+ return bytes_sent
def flush(self):
if self._wbuf:
@@ -756,7 +764,9 @@ if not _fileobject_uses_str_type:
def recv(self, size):
while True:
try:
- return self._sock.recv(size)
+ data = self._sock.recv(size)
+ self.bytes_read += len(data)
+ return data
except socket.error, e:
if (e.args[0] not in socket_errors_nonblocking
and e.args[0] not in socket_error_eintr):
@@ -913,6 +923,8 @@ if not _fileobject_uses_str_type:
else:
class CP_fileobject(socket._fileobject):
"""Faux file object attached to a socket object."""
+ bytes_read = 0
+ bytes_written = 0
def sendall(self, data):
"""Sendall for non-blocking sockets."""
@@ -920,12 +932,15 @@ else:
try:
bytes_sent = self.send(data)
data = data[bytes_sent:]
+ self.bytes_written += bytes_sent
except socket.error, e:
if e.args[0] not in socket_errors_nonblocking:
raise
def send(self, data):
- return self._sock.send(data)
+ bytes_sent = self._sock.send(data)
+ self.bytes_written += bytes_sent
+ return bytes_sent
def flush(self):
if self._wbuf:
@@ -936,7 +951,9 @@ else:
def recv(self, size):
while True:
try:
- return self._sock.recv(size)
+ data = self._sock.recv(size)
+ self.bytes_read += len(data)
+ return data
except socket.error, e:
if (e.args[0] not in socket_errors_nonblocking
and e.args[0] not in socket_error_eintr):