1
0
Fork 0
mirror of https://github.com/Kozea/Radicale.git synced 2025-06-26 16:45:52 +00:00
Radicale/radicale/__init__.py

140 lines
4.8 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
#
# This file is part of Radicale Server - Calendar Server
# Copyright © 2008-2010 Guillaume Ayoub
# Copyright © 2008 Nicolas Kandel
# Copyright © 2008 Pascal Halter
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Radicale. If not, see <http://www.gnu.org/licenses/>.
"""
Radicale Server module.
This module offers 3 useful classes:
- ``HTTPServer`` is a simple HTTP server;
- ``HTTPSServer`` is a HTTPS server, wrapping the HTTP server in a socket
managing SSL connections;
- ``CalendarHTTPHandler`` is a CalDAV request handler for HTTP(S) servers.
To use this module, you should take a look at the file ``radicale.py`` that
should have been included in this package.
"""
# TODO: Manage errors (see xmlutils)
2010-01-19 17:49:32 +01:00
import socket
2010-01-15 16:04:03 +01:00
try:
from http import client, server
except ImportError:
import httplib as client
import BaseHTTPServer as server
2010-01-15 16:04:03 +01:00
from radicale import config, support, xmlutils
class HTTPServer(server.HTTPServer):
"""HTTP server."""
pass
2010-01-19 17:49:32 +01:00
class HTTPSServer(HTTPServer):
"""HTTPS server."""
2010-01-19 17:49:32 +01:00
def __init__(self, address, handler):
"""Create server by wrapping HTTP socket in an SSL socket."""
2010-01-19 17:49:32 +01:00
# Fails with Python 2.5, import if needed
import ssl
super(HTTPSServer, self).__init__(address, handler)
self.socket = ssl.wrap_socket(
socket.socket(self.address_family, self.socket_type),
server_side=True,
certfile=config.get("server", "certificate"),
keyfile=config.get("server", "key"),
ssl_version=ssl.PROTOCOL_SSLv23)
self.server_bind()
self.server_activate()
2010-01-19 17:49:32 +01:00
class CalendarHTTPHandler(server.BaseHTTPRequestHandler):
2010-01-10 18:55:32 +01:00
"""HTTP requests handler for calendars."""
_encoding = config.get("encoding", "request")
@property
def calendar(self):
"""The ``calendar.Calendar`` object corresponding to the given path."""
2010-01-10 18:55:32 +01:00
path = self.path.strip("/").split("/")
if len(path) >= 2:
cal = "%s/%s" % (path[0], path[1])
return calendar.Calendar("radicale", cal)
2010-01-16 13:33:50 +01:00
def do_GET(self):
"""Manage GET request."""
answer = self.calendar.vcalendar.encode(_encoding)
2010-01-16 13:33:50 +01:00
self.send_response(client.OK)
self.send_header("Content-Length", len(answer))
self.end_headers()
self.wfile.write(answer)
2010-01-10 18:55:32 +01:00
def do_DELETE(self):
"""Manage DELETE request."""
2010-01-10 18:55:32 +01:00
obj = self.headers.get("if-match", None)
answer = xmlutils.delete(obj, self.calendar, self.path)
2010-01-15 16:04:03 +01:00
self.send_response(client.NO_CONTENT)
2010-01-10 18:55:32 +01:00
self.send_header("Content-Length", len(answer))
self.end_headers()
self.wfile.write(answer)
2010-01-10 18:55:32 +01:00
def do_OPTIONS(self):
"""Manage OPTIONS request."""
2010-01-15 16:04:03 +01:00
self.send_response(client.OK)
2010-01-16 13:33:50 +01:00
self.send_header("Allow", "DELETE, GET, OPTIONS, PROPFIND, PUT, REPORT")
2010-01-10 18:55:32 +01:00
self.send_header("DAV", "1, calendar-access")
self.end_headers()
2010-01-10 18:55:32 +01:00
def do_PROPFIND(self):
"""Manage PROPFIND request."""
2010-01-10 18:55:32 +01:00
xml_request = self.rfile.read(int(self.headers["Content-Length"]))
answer = xmlutils.propfind(xml_request, self.calendar, self.path)
2010-01-15 16:04:03 +01:00
self.send_response(client.MULTI_STATUS)
2010-01-10 18:55:32 +01:00
self.send_header("DAV", "1, calendar-access")
self.send_header("Content-Length", len(answer))
self.end_headers()
self.wfile.write(answer)
def do_PUT(self):
"""Manage PUT request."""
# TODO: Improve charset detection
2010-01-10 18:55:32 +01:00
contentType = self.headers["content-type"]
if contentType and "charset=" in contentType:
charset = contentType.split("charset=")[1].strip()
else:
charset = self._encoding
2010-01-10 18:55:32 +01:00
ical_request = self.rfile.read(int(self.headers["Content-Length"])).decode(charset)
obj = self.headers.get("if-match", None)
xmlutils.put(ical_request, self.calendar, self.path, obj)
2010-01-15 16:04:03 +01:00
self.send_response(client.CREATED)
2010-01-10 18:55:32 +01:00
def do_REPORT(self):
"""Manage REPORT request."""
2010-01-10 18:55:32 +01:00
xml_request = self.rfile.read(int(self.headers["Content-Length"]))
answer = xmlutils.report(xml_request, self.calendar, self.path)
2010-01-15 16:04:03 +01:00
self.send_response(client.MULTI_STATUS)
2010-01-10 18:55:32 +01:00
self.send_header("Content-Length", len(answer))
self.end_headers()
self.wfile.write(answer)