Attempt at a simple webdav server, but not near as complete as pywebdav.
[odfkit:webodf.git] / simplerevisionserver / webdavserver.py
1 #! /bin/env python
2
3 import BaseHTTPServer, SimpleHTTPServer, re, os, sys, xml.etree.ElementTree
4 from xml.etree.ElementTree import Element
5 from xml.etree.ElementTree import ElementTree
6 from xml.etree.ElementTree import QName
7 from xml.etree.ElementTree import SubElement
8
9 class WebDAVRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
10         def do_GET(self):
11                 # handle headers like Range: bytes=0-1023
12                 if 'Range' in self.headers:
13                         m = re.match('\s*bytes\s*=\s*(\d+)\s*-\s*(\d+)\s*',
14                                         self.headers['Range'])
15                         if m:
16                                 start = int(m.group(1))
17                                 end = int(m.group(2))
18                                 f = self.send_range_head(start, end)
19                                 if f:
20                                         self.copyfilerange(f, self.wfile, start,
21                                                         end)
22                                         f.close()
23                                 return
24                 return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
25
26         def do_PROPFIND(self):
27                 print 'PROPFIND ' + self.path
28                 for key in self.headers.keys():
29                         print key + '\t' + self.headers[key]
30                 req = self.parseinputxml()
31                 req = ElementTree(req)
32                 res = ElementTree(Element(QName("DAV:", 'multistatus')))
33                 self.addresponse('/', res.getroot(), 0)
34
35                 self.writeresponse(res)
36
37         def do_OPTIONS(self):
38                 req = self.parseinputxml()
39                 print req
40                 self.send_response(200)
41                 self.send_header("DAV", "1");
42                 self.end_headers()
43                 self.wfile.close()
44
45         def parseinputxml(self):
46                 try:
47                         contentlength = int(self.headers['content-length'])
48                 except:
49                         return None
50                 data = self.rfile.read(contentlength)
51                 print data
52                 return xml.etree.ElementTree.fromstring(data)
53                 
54         def writeresponse(self, response):
55                 self.send_response(200)
56                 self.send_header("Content-Type", 'text/xml; charset="utf-8"')
57                 self.end_headers()
58                 response.write(self.wfile, 'utf-8')
59 #               response.write(sys.stdout, 'utf-8')
60 #               sys.stdout.flush()
61                 d = xml.etree.ElementTree.tostring(response.getroot(), 'utf-8')
62                 print d
63                 self.wfile.close()
64
65         def addresponse(self, path, root, depth):
66                 e = SubElement(root, QName("DAV:", 'response'))
67                 href = SubElement(e, QName("DAV:", 'href'))
68                 href.text = path
69                 propstat = SubElement(e, QName("DAV:", 'propstat'))
70                 prop = SubElement(propstat, QName("DAV:", 'resourcetype'))
71                 if os.path.isdir(path):
72                         SubElement(prop, QName("DAV:", 'collection'))
73
74 if __name__ == '__main__':
75         server_address = ('', 8080)
76         httpd = BaseHTTPServer.HTTPServer(server_address, WebDAVRequestHandler)
77         httpd.serve_forever()