/etc/gnuspool/cupspy/filebuf.py is in gnuspool-cupspy 1.7ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | # Copyright (C) 2009 Free Software Foundation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
# Originally written by John Collins <jmc@xisl.com>.
import select
class filebufEOF(Exception):
"""Throw me on EOF"""
pass
class filebuf:
"""Buffer up file/socket for efficient reading"""
def __init__(self, f, tout = 20):
"""Initialise with file-type argument"""
self.sock = f
self.buffer = ""
self.ptr = 0
self.expected = 0
self.timeout = tout
f.setblocking(0)
def set_expected(self, l):
"""Set expected value from HTTP header
Remember to adjust for anything we already read"""
self.expected = l + self.ptr - len(self.buffer)
def readbuf(self, length):
"""Read a bufferful up to length
We are set to non-blocking so we wait for something to arrive first"""
try:
sr = select.select((self.sock,),(),(), self.timeout)
if len(sr[0]) == 0:
raise filebufEOF()
return self.sock.recv(length)
except:
raise filebufEOF()
def want(self, length):
"""Say we want a string of length given from buffer"""
lenav = len(self.buffer) - self.ptr
while lenav < length:
getl = 1024
self.buffer = self.buffer[self.ptr:]
self.ptr = 0
nb = self.readbuf(getl)
lng = len(nb)
if lng == 0:
raise filebufEOF()
lenav += lng
self.expected -= len(nb)
self.buffer += nb
def get(self, length):
"""Get a string of length given from the file"""
self.want(length)
result = self.buffer[self.ptr:self.ptr+length]
self.ptr += length;
return result
def peek(self, length):
"""Peek at the next string of length given from the file"""
self.want(length)
return self.buffer[self.ptr:self.ptr+length]
def skip(self, length):
"""Skip over the next string of length given from the file"""
self.ptr += length
def getch(self):
"""Get the ord value of the next character"""
return ord(self.get(1))
def peekch(self):
"""Peek at the ord value of the next character"""
return ord(self.peek(1))
def getrem(self):
"""Get some of the remaining stuff we are expecting"""
if self.ptr > 0 and len(self.buffer) - self.ptr > 0:
ret = self.buffer[self.ptr:]
self.ptr = 0
return ret
l = self.expected
if l <= 0:
return ""
if l > 1024:
l = 1024
ret = self.readbuf(l)
self.expected -= len(ret)
return ret
def readline(self):
"""Emulate readline function"""
result = ""
try:
while 1:
ch = self.get(1)
result += ch
if ch == '\n': return result
except filebufEOF:
return None
def write(self, str):
"""Write string to file"""
self.sock.sendall(str)
class httpfilebuf(filebuf):
"""Class for managing HTTP chunked mode"""
def __init__(self, f, tout = 20):
filebuf.__init__(self, f, tout)
self.chunked = False
self.chunkbuf = ""
self.chunkptr = 0
def set_chunked(self):
"""Set the buffering to be chunked"""
self.chunked = True
self.chunkbuf = ""
self.chunkptr = 0
def read_numline(self):
"""Read line probably containing chunk size"""
result = ""
while 1:
ch = filebuf.get(self, 1)
result += ch
if ch == '\n': break
return result.rstrip()
def chunksize(self):
"""Read size of next chunk"""
lin = self.read_numline()
if len(lin) == 0: return None
try:
return int(lin, 16)
except ValueError:
raise filebufEOF()
def wantchunk(self, length):
"""As for want but with chunks"""
if self.chunkptr + length < len(self.chunkbuf):
return
self.chunkbuf = self.chunkbuf[self.chunkptr:]
self.chunkptr = 0
while length > len(self.chunkbuf):
# Get size of next chunk
nxt_sz = self.chunksize()
# We're not expecting end of chunks at this point
if nxt_sz == 0:
raise filebufEOF()
self.chunkbuf += filebuf.get(self, nxt_sz)
# Expect chunk to be followed by empty line
if self.chunksize() is not None:
raise filebufEOF()
def get(self, length):
"""Get a string of given length.
Possibly grab it from the chunked buffer"""
if not self.chunked:
return filebuf.get(self, length)
self.wantchunk(length)
result = self.chunkbuf[self.chunkptr:self.chunkptr+length]
self.chunkptr += length;
return result
def peek(self, length):
"""Peek at the next string of length given from the file
Possibly grab it from the chunked buffer"""
if not self.chunked:
return filebuf.peek(self, length)
self.wantchunk(length)
return self.chunkbuf[self.chunkptr:self.chunkptr+length]
def getrem(self):
"""Get some of the remaining stuff we are expecting
Possibly grab it from the chunked buffer"""
if not self.chunked:
return filebuf.getrem(self)
# Get anything remaining
if self.chunkptr < len(self.chunkbuf):
result = self.chunkbuf[self.chunkptr:]
self.chunkptr = 0
self.chunkbuf = ""
return result
# Read the next chunk
# If at end, turn off chunking
nxt_sz = self.chunksize()
if nxt_sz == 0:
self.chunked = False
return ""
result = filebuf.get(self, nxt_sz)
# Expect chunk to be followed by empty line
if self.chunksize() is not None:
raise filebufEOF()
return result
# Simulate that in an string
class stringbuf:
"""Buffer up string to look like filebuf"""
def __init__(self, f, l):
"""Initialise with file-type argument and expected length"""
self.buffer = f
self.ptr = 0
def want(self, length):
"""Say we want a string of length given from buffer"""
if len(self.buffer) - self.ptr < length:
raise filebufEOF()
def get(self, length):
"""Get a string of length given from the file"""
self.want(length)
result = self.buffer[self.ptr:self.ptr+length]
self.ptr += length;
return result
def peek(self, length):
"""Peek at the next string of length given from the file"""
self.want(length)
return self.buffer[self.ptr:self.ptr+length]
def skip(self, length):
"""Skip over the next string of length given from the file"""
self.ptr += length
def getch(self):
"""Get the ord value of the next character"""
return ord(self.get(1))
def peekch(self):
"""Peek at the ord value of the next character"""
return ord(self.peek(1))
def getrem(self):
"""Get some of the remaining stuff we are expecting"""
ret = self.buffer[self.ptr:]
self.ptr = len(self.buffer)
return ret;
def readline(self):
"""Emulate readline function"""
result = ""
try:
while 1:
ch = self.getch()
result += ch
if ch == '\n': return result
except filebufEOF:
return None
|