"""twisted server for CubicWeb web instances:organization: Logilab:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses"""__docformat__="restructuredtext en"fromcubicweb.web.http_headersimportHeadersclassHTTPResponse(object):"""An object representing an HTTP Response to be sent to the client. """def__init__(self,twisted_request,code=None,headers=None,stream=None):self._headers_out=headersself._twreq=twisted_requestself._stream=streamself._code=codeself._init_headers()self._finalize()def_init_headers(self):ifself._headers_outisNone:return# initialize cookiescookies=self._headers_out.getHeader('set-cookie')or[]forcookieincookies:self._twreq.addCookie(cookie.name,cookie.value,cookie.expires,cookie.domain,cookie.path,#TODO max-agecomment=cookie.comment,secure=cookie.secure)self._headers_out.removeHeader('set-cookie')# initialize other headersfork,vinself._headers_out.getAllRawHeaders():self._twreq.setHeader(k,v[0])# add content-length if not presentif(self._headers_out.getHeader('content-length')isNoneandself._streamisnotNone):self._twreq.setHeader('content-length',len(self._stream))def_finalize(self):# we must set code before writing anything, else it's too lateifself._codeisnotNone:self._twreq.setResponseCode(self._code)ifself._streamisnotNone:self._twreq.write(str(self._stream))self._twreq.finish()def__repr__(self):return"<%s.%s code=%d>"%(self.__module__,self.__class__.__name__,self._code)defnot_modified_response(twisted_request,headers_in):headers_out=Headers()forheaderin(# Required from sec 10.3.5:'date','etag','content-location','expires','cache-control','vary',# Others:'server','proxy-authenticate','www-authenticate','warning'):value=headers_in.getRawHeaders(header)ifvalueisnotNone:headers_out.setRawHeaders(header,value)returnHTTPResponse(twisted_request=twisted_request,headers=headers_out)