__init__.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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_GET(self):
  36. """Manage GET ``request``."""
  37. self._parse_path()
  38. answer = self.calendar.vcalendar().encode(config.get("encoding", "request"))
  39. self.send_response(client.OK)
  40. self.send_header("Content-Length", len(answer))
  41. self.end_headers()
  42. self.wfile.write(answer)
  43. def do_DELETE(self):
  44. """Manage DELETE ``request``."""
  45. self._parse_path()
  46. obj = self.headers.get("if-match", None)
  47. answer = xmlutils.delete(obj, self.calendar, self.path)
  48. self.send_response(client.NO_CONTENT)
  49. self.send_header("Content-Length", len(answer))
  50. self.end_headers()
  51. self.wfile.write(answer)
  52. def do_OPTIONS(self):
  53. """Manage OPTIONS ``request``."""
  54. self.send_response(client.OK)
  55. self.send_header("Allow", "DELETE, GET, OPTIONS, PROPFIND, PUT, REPORT")
  56. self.send_header("DAV", "1, calendar-access")
  57. self.end_headers()
  58. def do_PROPFIND(self):
  59. """Manage PROPFIND ``request``."""
  60. self._parse_path()
  61. xml_request = self.rfile.read(int(self.headers["Content-Length"]))
  62. answer = xmlutils.propfind(xml_request, self.calendar, self.path)
  63. self.send_response(client.MULTI_STATUS)
  64. self.send_header("DAV", "1, calendar-access")
  65. self.send_header("Content-Length", len(answer))
  66. self.end_headers()
  67. self.wfile.write(answer)
  68. def do_PUT(self):
  69. """Manage PUT ``request``."""
  70. # TODO: Improve charset detection
  71. self._parse_path()
  72. contentType = self.headers["content-type"]
  73. if contentType and "charset=" in contentType:
  74. charset = contentType.split("charset=")[1].strip()
  75. else:
  76. charset = config.get("encoding", "request")
  77. ical_request = self.rfile.read(int(self.headers["Content-Length"])).decode(charset)
  78. obj = self.headers.get("if-match", None)
  79. xmlutils.put(ical_request, self.calendar, self.path, obj)
  80. self.send_response(client.CREATED)
  81. def do_REPORT(self):
  82. """Manage REPORT ``request``."""
  83. self._parse_path()
  84. xml_request = self.rfile.read(int(self.headers["Content-Length"]))
  85. answer = xmlutils.report(xml_request, self.calendar, self.path)
  86. self.send_response(client.MULTI_STATUS)
  87. self.send_header("Content-Length", len(answer))
  88. self.end_headers()
  89. self.wfile.write(answer)