__init__.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. try:
  23. from http import client, server
  24. except ImportError:
  25. import httplib as client
  26. import BaseHTTPServer as server
  27. from radicale import config, support, xmlutils
  28. class CalendarHandler(server.BaseHTTPRequestHandler):
  29. """HTTP requests handler for calendars."""
  30. def _parse_path(self):
  31. path = self.path.strip("/").split("/")
  32. if len(path) >= 2:
  33. cal = "%s/%s" % (path[0], path[1])
  34. self.calendar = calendar.Calendar("radicale", cal)
  35. def do_DELETE(self):
  36. """Manage DELETE ``request``."""
  37. self._parse_path()
  38. obj = self.headers.get("if-match", None)
  39. answer = xmlutils.delete(obj, self.calendar, self.path)
  40. self.send_response(client.NO_CONTENT)
  41. self.send_header("Content-Length", len(answer))
  42. self.end_headers()
  43. self.wfile.write(answer)
  44. def do_OPTIONS(self):
  45. """Manage OPTIONS ``request``."""
  46. self.send_response(client.OK)
  47. self.send_header("Allow", "DELETE, OPTIONS, PROPFIND, PUT, REPORT")
  48. self.send_header("DAV", "1, calendar-access")
  49. self.end_headers()
  50. def do_PROPFIND(self):
  51. """Manage PROPFIND ``request``."""
  52. self._parse_path()
  53. xml_request = self.rfile.read(int(self.headers["Content-Length"]))
  54. answer = xmlutils.propfind(xml_request, self.calendar, self.path)
  55. self.send_response(client.MULTI_STATUS)
  56. self.send_header("DAV", "1, calendar-access")
  57. self.send_header("Content-Length", len(answer))
  58. self.end_headers()
  59. self.wfile.write(answer)
  60. def do_PUT(self):
  61. """Manage PUT ``request``."""
  62. # TODO: Improve charset detection
  63. self._parse_path()
  64. contentType = self.headers["content-type"]
  65. if contentType and "charset=" in contentType:
  66. charset = contentType.split("charset=")[1].strip()
  67. else:
  68. charset = config.get("encoding", "request")
  69. ical_request = self.rfile.read(int(self.headers["Content-Length"])).decode(charset)
  70. obj = self.headers.get("if-match", None)
  71. xmlutils.put(ical_request, self.calendar, self.path, obj)
  72. self.send_response(client.CREATED)
  73. def do_REPORT(self):
  74. """Manage REPORT ``request``."""
  75. self._parse_path()
  76. xml_request = self.rfile.read(int(self.headers["Content-Length"]))
  77. answer = xmlutils.report(xml_request, self.calendar, self.path)
  78. self.send_response(client.MULTI_STATUS)
  79. self.send_header("Content-Length", len(answer))
  80. self.end_headers()
  81. self.wfile.write(answer)