__init__.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 socket
  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. HTTPServer = server.HTTPServer
  29. class HTTPSServer(HTTPServer):
  30. def __init__(self, address, handler):
  31. # Fails with Python 2.5, import if needed
  32. import ssl
  33. super(HTTPSServer, self).__init__(address, handler)
  34. self.socket = ssl.wrap_socket(
  35. socket.socket(self.address_family, self.socket_type),
  36. server_side=True,
  37. certfile=config.get("server", "certificate"),
  38. keyfile=config.get("server", "key"),
  39. ssl_version=ssl.PROTOCOL_SSLv23)
  40. self.server_bind()
  41. self.server_activate()
  42. class CalendarHTTPHandler(server.BaseHTTPRequestHandler):
  43. """HTTP requests handler for calendars."""
  44. def _parse_path(self):
  45. path = self.path.strip("/").split("/")
  46. if len(path) >= 2:
  47. cal = "%s/%s" % (path[0], path[1])
  48. self.calendar = calendar.Calendar("radicale", cal)
  49. def do_GET(self):
  50. """Manage GET ``request``."""
  51. self._parse_path()
  52. answer = self.calendar.vcalendar().encode(config.get("encoding", "request"))
  53. self.send_response(client.OK)
  54. self.send_header("Content-Length", len(answer))
  55. self.end_headers()
  56. self.wfile.write(answer)
  57. def do_DELETE(self):
  58. """Manage DELETE ``request``."""
  59. self._parse_path()
  60. obj = self.headers.get("if-match", None)
  61. answer = xmlutils.delete(obj, self.calendar, self.path)
  62. self.send_response(client.NO_CONTENT)
  63. self.send_header("Content-Length", len(answer))
  64. self.end_headers()
  65. self.wfile.write(answer)
  66. def do_OPTIONS(self):
  67. """Manage OPTIONS ``request``."""
  68. self.send_response(client.OK)
  69. self.send_header("Allow", "DELETE, GET, OPTIONS, PROPFIND, PUT, REPORT")
  70. self.send_header("DAV", "1, calendar-access")
  71. self.end_headers()
  72. def do_PROPFIND(self):
  73. """Manage PROPFIND ``request``."""
  74. self._parse_path()
  75. xml_request = self.rfile.read(int(self.headers["Content-Length"]))
  76. answer = xmlutils.propfind(xml_request, self.calendar, self.path)
  77. self.send_response(client.MULTI_STATUS)
  78. self.send_header("DAV", "1, calendar-access")
  79. self.send_header("Content-Length", len(answer))
  80. self.end_headers()
  81. self.wfile.write(answer)
  82. def do_PUT(self):
  83. """Manage PUT ``request``."""
  84. # TODO: Improve charset detection
  85. self._parse_path()
  86. contentType = self.headers["content-type"]
  87. if contentType and "charset=" in contentType:
  88. charset = contentType.split("charset=")[1].strip()
  89. else:
  90. charset = config.get("encoding", "request")
  91. ical_request = self.rfile.read(int(self.headers["Content-Length"])).decode(charset)
  92. obj = self.headers.get("if-match", None)
  93. xmlutils.put(ical_request, self.calendar, self.path, obj)
  94. self.send_response(client.CREATED)
  95. def do_REPORT(self):
  96. """Manage REPORT ``request``."""
  97. self._parse_path()
  98. xml_request = self.rfile.read(int(self.headers["Content-Length"]))
  99. answer = xmlutils.report(xml_request, self.calendar, self.path)
  100. self.send_response(client.MULTI_STATUS)
  101. self.send_header("Content-Length", len(answer))
  102. self.end_headers()
  103. self.wfile.write(answer)