__init__.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Radicale Server - Calendar Server
  4. # Copyright © 2008-2010 Guillaume Ayoub
  5. # Copyright © 2008 Nicolas Kandel
  6. # Copyright © 2008 Pascal Halter
  7. #
  8. # This library is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This library is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  20. # TODO: Manage errors (see xmlutils)
  21. import posixpath
  22. import httplib
  23. import BaseHTTPServer
  24. import config
  25. import support
  26. import acl
  27. import xmlutils
  28. import calendar
  29. _users = acl.users()
  30. _calendars = support.calendars()
  31. class CalendarHandler(BaseHTTPServer.BaseHTTPRequestHandler):
  32. """HTTP requests handler for calendars."""
  33. def _parse_path(self):
  34. path = self.path.strip("/").split("/")
  35. if len(path) >= 2:
  36. cal = "%s/%s" % (path[0], path[1])
  37. self.calendar = calendar.Calendar(_users[0], cal)
  38. def do_DELETE(self):
  39. """Manage DELETE ``request``."""
  40. self._parse_path()
  41. obj = self.headers.get("if-match", None)
  42. answer = xmlutils.delete(obj, self.calendar, self.path)
  43. self.send_response(httplib.NO_CONTENT)
  44. self.send_header("Content-Length", len(answer))
  45. self.end_headers()
  46. self.wfile.write(answer)
  47. def do_OPTIONS(self):
  48. """Manage OPTIONS ``request``."""
  49. self.send_response(httplib.OK)
  50. self.send_header("Allow", "DELETE, OPTIONS, PROPFIND, PUT, REPORT")
  51. self.send_header("DAV", "1, calendar-access")
  52. self.end_headers()
  53. def do_PROPFIND(self):
  54. """Manage PROPFIND ``request``."""
  55. self._parse_path()
  56. xml_request = self.rfile.read(int(self.headers["Content-Length"]))
  57. answer = xmlutils.propfind(xml_request, self.calendar, self.path)
  58. self.send_response(httplib.MULTI_STATUS)
  59. self.send_header("DAV", "1, calendar-access")
  60. self.send_header("Content-Length", len(answer))
  61. self.end_headers()
  62. self.wfile.write(answer)
  63. def do_PUT(self):
  64. """Manage PUT ``request``."""
  65. # TODO: Improve charset detection
  66. self._parse_path()
  67. contentType = self.headers["content-type"]
  68. if contentType and "charset=" in contentType:
  69. charset = contentType.split("charset=")[1].strip()
  70. else:
  71. charset = config.get("encoding", "request")
  72. ical_request = self.rfile.read(int(self.headers["Content-Length"])).decode(charset)
  73. obj = self.headers.get("if-match", None)
  74. xmlutils.put(ical_request, self.calendar, self.path, obj)
  75. self.send_response(httplib.CREATED)
  76. def do_REPORT(self):
  77. """Manage REPORT ``request``."""
  78. self._parse_path()
  79. xml_request = self.rfile.read(int(self.headers["Content-Length"]))
  80. answer = xmlutils.report(xml_request, self.calendar, self.path)
  81. self.send_response(httplib.MULTI_STATUS)
  82. self.send_header("Content-Length", len(answer))
  83. self.end_headers()
  84. self.wfile.write(answer)