3cda2ccc6fec500e99a4ae0a593a4b498278e6b4
[odoo/odoo.git] / addons / document_webdav / webdav_server.py
1 # -*- encoding: utf-8 -*-
2
3 #
4 # Copyright P. Christeas <p_christ@hol.gr> 2008,2009
5 #
6 #
7 # WARNING: This program as such is intended to be used by professional
8 # programmers who take the whole responsability of assessing all potential
9 # consequences resulting from its eventual inadequacies and bugs
10 # End users who are looking for a ready-to-use solution with commercial
11 # garantees and support are strongly adviced to contract a Free Software
12 # Service Company
13 #
14 # This program is Free Software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License
16 # as published by the Free Software Foundation; either version 2
17 # of the License, or (at your option) any later version.
18 #
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27 ###############################################################################
28
29
30 import netsvc
31 import tools
32 from dav_fs import openerp_dav_handler
33 from tools.config import config
34 from DAV.WebDAVServer import DAVRequestHandler
35 from service.websrv_lib import HTTPDir, FixSendError, HttpOptions
36 from BaseHTTPServer import BaseHTTPRequestHandler
37 import urlparse
38 import urllib
39 from string import atoi,split
40 from DAV.errors import *
41 # from DAV.constants import DAV_VERSION_1, DAV_VERSION_2
42
43 def OpenDAVConfig(**kw):
44     class OpenDAV:
45         def __init__(self, **kw):
46             self.__dict__.update(**kw)
47             
48         def getboolean(self, word):
49             return self.__dict__.get(word, False)
50
51     class Config:
52         DAV = OpenDAV(**kw)
53
54     return Config()
55
56
57 class DAVHandler(HttpOptions, FixSendError, DAVRequestHandler):
58     verbose = False
59     protocol_version = 'HTTP/1.1'
60     _HTTP_OPTIONS= { 'DAV' : ['1',],
61                     'Allow' : [ 'GET', 'HEAD', 'COPY', 'MOVE', 'POST', 'PUT', 
62                             'PROPFIND', 'PROPPATCH', 'OPTIONS', 'MKCOL',
63                             'DELETE', 'TRACE', 'REPORT', ]
64                     }
65
66     def get_userinfo(self,user,pw):
67         return False
68     def _log(self, message):
69         netsvc.Logger().notifyChannel("webdav",netsvc.LOG_DEBUG,message)
70     
71     def handle(self):
72         self._init_buffer()
73
74     def finish(self):
75         pass
76
77     def get_db_from_path(self, uri):
78         # interface class will handle all cases.
79         res =  self.IFACE_CLASS.get_db(uri, allow_last=True)
80         return res
81
82     def setup(self):
83         self.davpath = '/'+config.get_misc('webdav','vdir','webdav')
84         self.baseuri = "http://%s:%d/"% (self.server.server_name, self.server.server_port)
85         self.IFACE_CLASS  = openerp_dav_handler(self, self.verbose)
86         
87     def copymove(self, CLASS):
88         """ Our uri scheme removes the /webdav/ component from there, so we
89         need to mangle the header, too.
90         """
91         dest = self.headers['Destination']
92         up = urlparse.urlparse(urllib.unquote(self.headers['Destination']))
93         if up.path.startswith(self.davpath):
94             self.headers['Destination'] = up.path[len(self.davpath):]
95         else:
96             raise DAV_Forbidden("Not allowed to copy/move outside webdav path")
97         DAVRequestHandler.copymove(self, CLASS)
98
99     def get_davpath(self):
100         return self.davpath
101     
102     def log_message(self, format, *args):
103         netsvc.Logger().notifyChannel('webdav', netsvc.LOG_DEBUG_RPC, format % args)
104
105     def log_error(self, format, *args):
106         netsvc.Logger().notifyChannel('xmlrpc', netsvc.LOG_WARNING, format % args)
107
108     def _prep_OPTIONS(self, opts):
109         ret = opts
110         dc=self.IFACE_CLASS
111         uri=urlparse.urljoin(self.get_baseuri(dc), self.path)
112         uri=urllib.unquote(uri)
113         try:
114             #location = dc.put(uri,body,ct)
115             ret = dc.prep_http_options(uri, opts)
116         except DAV_Error, (ec,dd):
117             pass
118         except Exception,e:
119             self.log_error("Error at options: %s", str(e))
120             raise
121         return ret
122             
123     def send_response(self, code, message=None):
124         # the BufferingHttpServer will send Connection: close , while
125         # the BaseHTTPRequestHandler will only accept int code.
126         # workaround both of them.
127         BaseHTTPRequestHandler.send_response(self, int(code), message)
128
129     def send_header(self, key, value):
130         if key == 'Connection' and value == 'close':
131             self.close_connection = 1
132         DAVRequestHandler.send_header(self, key, value)
133
134     def send_body(self, DATA, code = None, msg = None, desc = None, ctype='application/octet-stream', headers=None):
135         if headers and 'Connection' in headers:
136             pass
137         elif self.request_version in ('HTTP/1.0', 'HTTP/0.9'):
138             pass
139         elif self.close_connection == 1: # close header already sent
140             pass
141         else:
142             if headers is None:
143                 headers = {}
144             if self.headers.get('Connection',False) == 'Keep-Alive':
145                 headers['Connection'] = 'keep-alive'
146
147         DAVRequestHandler.send_body(self, DATA, code=code, msg=msg, desc=desc,
148                     ctype=ctype, headers=headers)
149
150     def do_PUT(self):
151         dc=self.IFACE_CLASS        
152         uri=urlparse.urljoin(self.get_baseuri(dc), self.path)
153         uri=urllib.unquote(uri)
154         # Handle If-Match
155         if self.headers.has_key('If-Match'):
156             test = False
157             etag = None
158             
159             for match in self.headers['If-Match'].split(','):                
160                 if match.startswith('"') and match.endswith('"'):
161                     match = match[1:-1]
162                 if match == '*':
163                     if dc.exists(uri):
164                         test = True
165                         break
166                 else:
167                     if dc.match_prop(uri, match, "DAV:", "getetag"):
168                         test = True
169                         break
170             if not test:
171                 self._get_body()
172                 self.send_status(412)
173                 return
174
175         # Handle If-None-Match
176         if self.headers.has_key('If-None-Match'):
177             test = True
178             etag = None            
179             for match in self.headers['If-None-Match'].split(','):
180                 if match == '*':
181                     if dc.exists(uri):
182                         test = False
183                         break
184                 else:
185                     if dc.match_prop(uri, match, "DAV:", "getetag"):
186                         test = False
187                         break
188             if not test:
189                 self._get_body()
190                 self.send_status(412)
191                 return
192
193         # Handle expect
194         expect = self.headers.get('Expect', '')
195         if (expect.lower() == '100-continue' and
196                 self.protocol_version >= 'HTTP/1.1' and
197                 self.request_version >= 'HTTP/1.1'):
198             self.send_status(100)
199             self._flush()
200
201         # read the body
202         body=self._get_body()
203
204         # locked resources are not allowed to be overwritten
205         if self._l_isLocked(uri):
206             return self.send_body(None, '423', 'Locked', 'Locked')
207
208         ct=None
209         if self.headers.has_key("Content-Type"):
210             ct=self.headers['Content-Type']
211         try:
212             location = dc.put(uri,body,ct)
213         except DAV_Error, (ec,dd):
214             return self.send_status(ec)
215
216         headers = {}
217         etag = None
218         if location and isinstance(location, tuple):
219             etag = location[1]
220             location = location[0]
221             # note that we have allowed for > 2 elems
222         if location:
223             headers['Location'] = location
224
225         try:
226             if not etag:
227                 etag = dc.get_prop(location or uri, "DAV:", "getetag")
228             if etag:
229                 headers['ETag'] = str(etag)
230         except Exception:
231             pass
232
233         self.send_body(None, '201', 'Created', '', headers=headers)
234
235     def _get_body(self):
236         body = None
237         if self.headers.has_key("Content-Length"):
238             l=self.headers['Content-Length']
239             body=self.rfile.read(atoi(l))
240         return body
241
242     def do_DELETE(self):
243         try:
244             DAVRequestHandler.do_DELETE(self)
245         except DAV_Error, (ec, dd):
246             return self.send_status(ec)
247
248 from service.http_server import reg_http_service,OpenERPAuthProvider
249
250 class DAVAuthProvider(OpenERPAuthProvider):
251     def authenticate(self, db, user, passwd, client_address):
252         """ authenticate, but also allow the False db, meaning to skip
253             authentication when no db is specified.
254         """
255         if db is False:
256             return True
257         return OpenERPAuthProvider.authenticate(self, db, user, passwd, client_address)
258
259 try:
260
261     if (config.get_misc('webdav','enable',True)):
262         directory = '/'+config.get_misc('webdav','vdir','webdav') 
263         handler = DAVHandler
264         verbose = config.get_misc('webdav','verbose',True)
265         handler.debug = config.get_misc('webdav','debug',True)
266         _dc = { 'verbose' : verbose,
267                 'directory' : directory,
268                 'lockemulation' : False,
269                     
270                 }
271
272         conf = OpenDAVConfig(**_dc)
273         handler._config = conf
274         reg_http_service(HTTPDir(directory,DAVHandler,DAVAuthProvider()))
275         netsvc.Logger().notifyChannel('webdav', netsvc.LOG_INFO, "WebDAV service registered at path: %s/ "% directory)
276 except Exception, e:
277     logger = netsvc.Logger()
278     logger.notifyChannel('webdav', netsvc.LOG_ERROR, 'Cannot launch webdav: %s' % e)
279
280 #eof
281
282
283