__init__.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. """
  21. Radicale Server module.
  22. This module offers 3 useful classes:
  23. - ``HTTPServer`` is a simple HTTP server;
  24. - ``HTTPSServer`` is a HTTPS server, wrapping the HTTP server in a socket
  25. managing SSL connections;
  26. - ``CalendarHTTPHandler`` is a CalDAV request handler for HTTP(S) servers.
  27. To use this module, you should take a look at the file ``radicale.py`` that
  28. should have been included in this package.
  29. """
  30. # TODO: Manage errors (see xmlutils)
  31. import socket
  32. try:
  33. from http import client, server
  34. except ImportError:
  35. import httplib as client
  36. import BaseHTTPServer as server
  37. from radicale import config, support, xmlutils
  38. class HTTPServer(server.HTTPServer):
  39. """HTTP server."""
  40. pass
  41. class HTTPSServer(HTTPServer):
  42. """HTTPS server."""
  43. def __init__(self, address, handler):
  44. """Create server by wrapping HTTP socket in an SSL socket."""
  45. # Fails with Python 2.5, import if needed
  46. import ssl
  47. super(HTTPSServer, self).__init__(address, handler)
  48. self.socket = ssl.wrap_socket(
  49. socket.socket(self.address_family, self.socket_type),
  50. server_side=True,
  51. certfile=config.get("server", "certificate"),
  52. keyfile=config.get("server", "key"),
  53. ssl_version=ssl.PROTOCOL_SSLv23)
  54. self.server_bind()
  55. self.server_activate()
  56. class CalendarHTTPHandler(server.BaseHTTPRequestHandler):
  57. """HTTP requests handler for calendars."""
  58. _encoding = config.get("encoding", "request")
  59. @property
  60. def calendar(self):
  61. """The ``calendar.Calendar`` object corresponding to the given path."""
  62. path = self.path.strip("/").split("/")
  63. if len(path) >= 2:
  64. cal = "%s/%s" % (path[0], path[1])
  65. return calendar.Calendar("radicale", cal)
  66. def do_GET(self):
  67. """Manage GET request."""
  68. answer = self.calendar.vcalendar.encode(_encoding)
  69. self.send_response(client.OK)
  70. self.send_header("Content-Length", len(answer))
  71. self.end_headers()
  72. self.wfile.write(answer)
  73. def do_DELETE(self):
  74. """Manage DELETE request."""
  75. obj = self.headers.get("if-match", None)
  76. answer = xmlutils.delete(obj, self.calendar, self.path)
  77. self.send_response(client.NO_CONTENT)
  78. self.send_header("Content-Length", len(answer))
  79. self.end_headers()
  80. self.wfile.write(answer)
  81. def do_OPTIONS(self):
  82. """Manage OPTIONS request."""
  83. self.send_response(client.OK)
  84. self.send_header("Allow", "DELETE, GET, OPTIONS, PROPFIND, PUT, REPORT")
  85. self.send_header("DAV", "1, calendar-access")
  86. self.end_headers()
  87. def do_PROPFIND(self):
  88. """Manage PROPFIND request."""
  89. xml_request = self.rfile.read(int(self.headers["Content-Length"]))
  90. answer = xmlutils.propfind(xml_request, self.calendar, self.path)
  91. self.send_response(client.MULTI_STATUS)
  92. self.send_header("DAV", "1, calendar-access")
  93. self.send_header("Content-Length", len(answer))
  94. self.end_headers()
  95. self.wfile.write(answer)
  96. def do_PUT(self):
  97. """Manage PUT request."""
  98. # TODO: Improve charset detection
  99. contentType = self.headers["content-type"]
  100. if contentType and "charset=" in contentType:
  101. charset = contentType.split("charset=")[1].strip()
  102. else:
  103. charset = self._encoding
  104. ical_request = self.rfile.read(int(self.headers["Content-Length"])).decode(charset)
  105. obj = self.headers.get("if-match", None)
  106. xmlutils.put(ical_request, self.calendar, self.path, obj)
  107. self.send_response(client.CREATED)
  108. def do_REPORT(self):
  109. """Manage REPORT request."""
  110. xml_request = self.rfile.read(int(self.headers["Content-Length"]))
  111. answer = xmlutils.report(xml_request, self.calendar, self.path)
  112. self.send_response(client.MULTI_STATUS)
  113. self.send_header("Content-Length", len(answer))
  114. self.end_headers()
  115. self.wfile.write(answer)