paul@6 | 1 | # -*- coding: iso-8859-1 -*- |
paul@6 | 2 | """ |
paul@6 | 3 | MoinMoin - MoinMessage library |
paul@6 | 4 | |
paul@16 | 5 | @copyright: 2012, 2013 by Paul Boddie <paul@boddie.org.uk> |
paul@6 | 6 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@6 | 7 | """ |
paul@6 | 8 | |
paul@6 | 9 | from email import message_from_string |
paul@6 | 10 | from email.encoders import encode_noop |
paul@6 | 11 | from email.mime.multipart import MIMEMultipart |
paul@6 | 12 | from email.mime.application import MIMEApplication |
paul@6 | 13 | from email.mime.base import MIMEBase |
paul@20 | 14 | from email.utils import formatdate, parsedate |
paul@6 | 15 | from subprocess import Popen, PIPE |
paul@8 | 16 | from tempfile import mkstemp |
paul@12 | 17 | from urlparse import urlsplit |
paul@6 | 18 | import httplib |
paul@8 | 19 | import os |
paul@6 | 20 | |
paul@15 | 21 | def is_collection(message): |
paul@15 | 22 | return message.get("Update-Type") == "collection" |
paul@15 | 23 | |
paul@29 | 24 | def to_replace(message): |
paul@29 | 25 | return message.get("Update-Action") == "replace" |
paul@29 | 26 | |
paul@29 | 27 | def to_store(message): |
paul@29 | 28 | return message.get("Update-Action") == "store" |
paul@29 | 29 | |
paul@85 | 30 | def get_update_action(message): |
paul@85 | 31 | return message.get("Update-Action", "update") |
paul@85 | 32 | |
paul@6 | 33 | class Message: |
paul@6 | 34 | |
paul@6 | 35 | "An update message." |
paul@6 | 36 | |
paul@15 | 37 | def __init__(self, text=None): |
paul@20 | 38 | self.date = None |
paul@6 | 39 | self.updates = [] |
paul@15 | 40 | if text: |
paul@15 | 41 | self.parse_text(text) |
paul@15 | 42 | |
paul@20 | 43 | def init_date(self, message): |
paul@20 | 44 | |
paul@20 | 45 | "Obtain the date of the given 'message'." |
paul@20 | 46 | |
paul@20 | 47 | if message.has_key("Date"): |
paul@20 | 48 | self.date = parsedate(message["Date"]) |
paul@20 | 49 | else: |
paul@20 | 50 | self.date = None |
paul@20 | 51 | |
paul@15 | 52 | def parse_text(self, text): |
paul@15 | 53 | |
paul@15 | 54 | "Parse the given 'text' as a message." |
paul@15 | 55 | |
paul@15 | 56 | self.handle_message(message_from_string(text)) |
paul@15 | 57 | |
paul@15 | 58 | def handle_message(self, message): |
paul@15 | 59 | |
paul@15 | 60 | "Handle the given 'message', recording the separate updates." |
paul@15 | 61 | |
paul@20 | 62 | self.init_date(message) |
paul@20 | 63 | |
paul@15 | 64 | # The message either consists of a collection of updates. |
paul@15 | 65 | |
paul@15 | 66 | if message.is_multipart() and is_collection(message): |
paul@15 | 67 | for part in message.get_payload(): |
paul@15 | 68 | self.updates.append(part) |
paul@15 | 69 | |
paul@15 | 70 | # Or the message is a single update. |
paul@15 | 71 | |
paul@15 | 72 | else: |
paul@15 | 73 | self.updates.append(message) |
paul@6 | 74 | |
paul@16 | 75 | def add_updates(self, parts): |
paul@13 | 76 | |
paul@13 | 77 | """ |
paul@16 | 78 | Add the given 'parts' to a message. |
paul@13 | 79 | """ |
paul@13 | 80 | |
paul@16 | 81 | for part in updates: |
paul@16 | 82 | self.add_update(part) |
paul@16 | 83 | |
paul@16 | 84 | def add_update(self, part): |
paul@16 | 85 | |
paul@16 | 86 | """ |
paul@16 | 87 | Add an update 'part' to a message. |
paul@16 | 88 | """ |
paul@16 | 89 | |
paul@16 | 90 | self.updates.append(part) |
paul@16 | 91 | |
paul@16 | 92 | def get_update(self, alternatives): |
paul@16 | 93 | |
paul@16 | 94 | """ |
paul@16 | 95 | Return a suitable multipart object containing the supplied |
paul@16 | 96 | 'alternatives'. |
paul@16 | 97 | """ |
paul@16 | 98 | |
paul@16 | 99 | part = MIMEMultipart() |
paul@16 | 100 | for alternative in alternatives: |
paul@16 | 101 | part.attach(alternative) |
paul@16 | 102 | return part |
paul@6 | 103 | |
paul@20 | 104 | def get_payload(self, timestamped=True): |
paul@13 | 105 | |
paul@20 | 106 | """ |
paul@20 | 107 | Get the multipart payload for the message. If the 'timestamped' |
paul@20 | 108 | parameter is omitted or set to a true value, the payload will be given a |
paul@20 | 109 | date header set to the current date and time that can be used to assess |
paul@20 | 110 | the validity of a message and to determine whether it has already been |
paul@20 | 111 | received by a recipient. |
paul@20 | 112 | """ |
paul@13 | 113 | |
paul@6 | 114 | if len(self.updates) == 1: |
paul@6 | 115 | message = self.updates[0] |
paul@6 | 116 | else: |
paul@6 | 117 | message = MIMEMultipart() |
paul@6 | 118 | message.add_header("Update-Type", "collection") |
paul@6 | 119 | for update in self.updates: |
paul@6 | 120 | message.attach(update) |
paul@6 | 121 | |
paul@20 | 122 | if timestamped: |
paul@20 | 123 | timestamp(message) |
paul@20 | 124 | self.init_date(message) |
paul@20 | 125 | |
paul@6 | 126 | return message |
paul@6 | 127 | |
paul@15 | 128 | class Mailbox: |
paul@15 | 129 | |
paul@15 | 130 | "A collection of messages within a multipart message." |
paul@15 | 131 | |
paul@15 | 132 | def __init__(self, text=None): |
paul@15 | 133 | self.messages = [] |
paul@15 | 134 | if text: |
paul@15 | 135 | self.parse_text(text) |
paul@15 | 136 | |
paul@15 | 137 | def parse_text(self, text): |
paul@15 | 138 | |
paul@15 | 139 | "Parse the given 'text' as a mailbox." |
paul@15 | 140 | |
paul@15 | 141 | message = message_from_string(text) |
paul@15 | 142 | |
paul@15 | 143 | if message.is_multipart(): |
paul@15 | 144 | for part in message.get_payload(): |
paul@15 | 145 | self.messages.append(part) |
paul@15 | 146 | else: |
paul@15 | 147 | self.messages.append(message) |
paul@15 | 148 | |
paul@15 | 149 | def add_message(self, message): |
paul@15 | 150 | |
paul@15 | 151 | "Add the given 'message' to the mailbox." |
paul@15 | 152 | |
paul@15 | 153 | self.messages.append(message) |
paul@15 | 154 | |
paul@15 | 155 | def get_payload(self): |
paul@15 | 156 | |
paul@15 | 157 | "Get the multipart payload for the mailbox." |
paul@15 | 158 | |
paul@15 | 159 | mailbox = MIMEMultipart() |
paul@15 | 160 | for message in self.messages: |
paul@15 | 161 | mailbox.attach(message) |
paul@15 | 162 | |
paul@15 | 163 | return mailbox |
paul@15 | 164 | |
paul@6 | 165 | class MoinMessageError(Exception): |
paul@6 | 166 | pass |
paul@6 | 167 | |
paul@33 | 168 | class MoinMessageDecodingError(Exception): |
paul@33 | 169 | pass |
paul@33 | 170 | |
paul@33 | 171 | class MoinMessageMissingPart(MoinMessageDecodingError): |
paul@33 | 172 | pass |
paul@33 | 173 | |
paul@33 | 174 | class MoinMessageBadContent(MoinMessageDecodingError): |
paul@33 | 175 | pass |
paul@33 | 176 | |
paul@8 | 177 | class GPG: |
paul@8 | 178 | |
paul@8 | 179 | "A wrapper around the gpg command using a particular configuration." |
paul@6 | 180 | |
paul@8 | 181 | def __init__(self, homedir=None): |
paul@8 | 182 | self.conf_args = [] |
paul@6 | 183 | |
paul@8 | 184 | if homedir: |
paul@8 | 185 | self.conf_args += ["--homedir", homedir] |
paul@8 | 186 | |
paul@8 | 187 | self.errors = None |
paul@6 | 188 | |
paul@8 | 189 | def run(self, args, text=None): |
paul@6 | 190 | |
paul@8 | 191 | """ |
paul@8 | 192 | Invoke gpg with the given 'args', supplying the given 'text' to the |
paul@8 | 193 | command directly or, if 'text' is omitted, using a file provided as part |
paul@8 | 194 | of the 'args' if appropriate. |
paul@6 | 195 | |
paul@8 | 196 | Failure to complete the operation will result in a MoinMessageError |
paul@8 | 197 | being raised. |
paul@8 | 198 | """ |
paul@8 | 199 | |
paul@8 | 200 | cmd = Popen(["gpg"] + self.conf_args + list(args), stdin=PIPE, stdout=PIPE, stderr=PIPE) |
paul@6 | 201 | |
paul@58 | 202 | # Attempt to write input to the command and to read output from the |
paul@58 | 203 | # command. |
paul@11 | 204 | |
paul@58 | 205 | text, self.errors = cmd.communicate(text) |
paul@8 | 206 | |
paul@58 | 207 | # Test for a zero result. |
paul@6 | 208 | |
paul@58 | 209 | if not cmd.returncode: |
paul@58 | 210 | return text |
paul@58 | 211 | else: |
paul@58 | 212 | raise MoinMessageError, self.errors |
paul@6 | 213 | |
paul@33 | 214 | def verifyMessageText(self, signature, content): |
paul@8 | 215 | |
paul@8 | 216 | "Using the given 'signature', verify the given message 'content'." |
paul@6 | 217 | |
paul@8 | 218 | # Write the detached signature and content to files. |
paul@8 | 219 | |
paul@8 | 220 | signature_fd, signature_filename = mkstemp() |
paul@8 | 221 | content_fd, content_filename = mkstemp() |
paul@6 | 222 | |
paul@8 | 223 | try: |
paul@8 | 224 | signature_fp = os.fdopen(signature_fd, "w") |
paul@8 | 225 | content_fp = os.fdopen(content_fd, "w") |
paul@8 | 226 | try: |
paul@8 | 227 | signature_fp.write(signature) |
paul@8 | 228 | content_fp.write(content) |
paul@8 | 229 | finally: |
paul@8 | 230 | signature_fp.close() |
paul@8 | 231 | content_fp.close() |
paul@6 | 232 | |
paul@8 | 233 | # Verify the message text. |
paul@6 | 234 | |
paul@10 | 235 | text = self.run(["--status-fd", "1", "--verify", signature_filename, content_filename]) |
paul@10 | 236 | |
paul@10 | 237 | # Return the details of the signing key. |
paul@10 | 238 | |
paul@11 | 239 | identity = None |
paul@11 | 240 | fingerprint = None |
paul@11 | 241 | |
paul@10 | 242 | for line in text.split("\n"): |
paul@10 | 243 | try: |
paul@11 | 244 | prefix, msgtype, digest, details = line.strip().split(" ", 3) |
paul@10 | 245 | except ValueError: |
paul@10 | 246 | continue |
paul@10 | 247 | |
paul@10 | 248 | # Return the fingerprint and identity details. |
paul@10 | 249 | |
paul@10 | 250 | if msgtype == "GOODSIG": |
paul@11 | 251 | identity = details |
paul@11 | 252 | elif msgtype == "VALIDSIG": |
paul@11 | 253 | fingerprint = digest |
paul@11 | 254 | |
paul@11 | 255 | if identity and fingerprint: |
paul@11 | 256 | return fingerprint, identity |
paul@10 | 257 | |
paul@10 | 258 | return None |
paul@6 | 259 | |
paul@8 | 260 | finally: |
paul@8 | 261 | os.remove(signature_filename) |
paul@8 | 262 | os.remove(content_filename) |
paul@8 | 263 | |
paul@33 | 264 | def verifyMessage(self, message): |
paul@33 | 265 | |
paul@33 | 266 | """ |
paul@33 | 267 | Verify the given RFC 3156 'message', returning a tuple of the form |
paul@33 | 268 | (fingerprint, identity, content). |
paul@33 | 269 | """ |
paul@33 | 270 | |
paul@36 | 271 | content, signature = getContentAndSignature(message) |
paul@33 | 272 | |
paul@33 | 273 | # Verify the message format. |
paul@33 | 274 | |
paul@33 | 275 | if signature.get_content_type() != "application/pgp-signature": |
paul@33 | 276 | raise MoinMessageBadContent |
paul@33 | 277 | |
paul@33 | 278 | # Verify the message. |
paul@33 | 279 | |
paul@33 | 280 | fingerprint, identity = self.verifyMessageText(signature.get_payload(), content.as_string()) |
paul@33 | 281 | return fingerprint, identity, content |
paul@33 | 282 | |
paul@8 | 283 | def signMessage(self, message, keyid): |
paul@6 | 284 | |
paul@8 | 285 | """ |
paul@8 | 286 | Return a signed version of 'message' using the given 'keyid'. |
paul@8 | 287 | """ |
paul@6 | 288 | |
paul@8 | 289 | text = message.as_string() |
paul@8 | 290 | signature = self.run(["--armor", "-u", keyid, "--detach-sig"], text) |
paul@8 | 291 | |
paul@8 | 292 | # Make the container for the message. |
paul@8 | 293 | |
paul@8 | 294 | signed_message = MIMEMultipart("signed", protocol="application/pgp-signature") |
paul@8 | 295 | signed_message.attach(message) |
paul@6 | 296 | |
paul@8 | 297 | signature_part = MIMEBase("application", "pgp-signature") |
paul@8 | 298 | signature_part.set_payload(signature) |
paul@8 | 299 | signed_message.attach(signature_part) |
paul@8 | 300 | |
paul@8 | 301 | return signed_message |
paul@8 | 302 | |
paul@33 | 303 | def decryptMessageText(self, message): |
paul@6 | 304 | |
paul@8 | 305 | "Return a decrypted version of 'message'." |
paul@8 | 306 | |
paul@8 | 307 | return self.run(["--decrypt"], message) |
paul@6 | 308 | |
paul@33 | 309 | def decryptMessage(self, message): |
paul@33 | 310 | |
paul@33 | 311 | """ |
paul@33 | 312 | Decrypt the given RFC 3156 'message', returning the message text. |
paul@33 | 313 | """ |
paul@33 | 314 | |
paul@33 | 315 | try: |
paul@33 | 316 | declaration, content = message.get_payload() |
paul@33 | 317 | except ValueError: |
paul@33 | 318 | raise MoinMessageMissingPart |
paul@33 | 319 | |
paul@33 | 320 | # Verify the message format. |
paul@33 | 321 | |
paul@33 | 322 | if content.get_content_type() != "application/octet-stream": |
paul@33 | 323 | raise MoinMessageBadContent |
paul@33 | 324 | |
paul@33 | 325 | # Return the decrypted message text. |
paul@33 | 326 | |
paul@33 | 327 | return self.decryptMessageText(content.get_payload()) |
paul@33 | 328 | |
paul@8 | 329 | def encryptMessage(self, message, keyid): |
paul@6 | 330 | |
paul@8 | 331 | """ |
paul@8 | 332 | Return an encrypted version of 'message' using the given 'keyid'. |
paul@8 | 333 | """ |
paul@6 | 334 | |
paul@8 | 335 | text = message.as_string() |
paul@8 | 336 | encrypted = self.run(["--armor", "-r", keyid, "--encrypt", "--trust-model", "always"], text) |
paul@8 | 337 | |
paul@8 | 338 | # Make the container for the message. |
paul@8 | 339 | |
paul@8 | 340 | encrypted_message = MIMEMultipart("encrypted", protocol="application/pgp-encrypted") |
paul@8 | 341 | |
paul@8 | 342 | # For encrypted content, add the declaration and content. |
paul@6 | 343 | |
paul@8 | 344 | declaration = MIMEBase("application", "pgp-encrypted") |
paul@8 | 345 | declaration.set_payload("Version: 1") |
paul@8 | 346 | encrypted_message.attach(declaration) |
paul@6 | 347 | |
paul@8 | 348 | content = MIMEApplication(encrypted, "octet-stream", encode_noop) |
paul@8 | 349 | encrypted_message.attach(content) |
paul@6 | 350 | |
paul@8 | 351 | return encrypted_message |
paul@8 | 352 | |
paul@75 | 353 | def exportKey(self, keyid): |
paul@75 | 354 | |
paul@75 | 355 | """ |
paul@75 | 356 | Return the "armoured" public key text for 'keyid' as a message part with |
paul@75 | 357 | a suitable media type. |
paul@75 | 358 | See: https://tools.ietf.org/html/rfc3156#section-7 |
paul@75 | 359 | """ |
paul@75 | 360 | |
paul@75 | 361 | text = self.run(["--armor", "--export", keyid]) |
paul@75 | 362 | return MIMEApplication(text, "pgp-keys", encode_noop) |
paul@75 | 363 | |
paul@78 | 364 | def listKeys(self, keyid=None): |
paul@78 | 365 | |
paul@78 | 366 | """ |
paul@78 | 367 | Return a list of key details for keys on the keychain, selecting only |
paul@78 | 368 | one specific key if 'keyid' is specified. |
paul@78 | 369 | """ |
paul@78 | 370 | |
paul@78 | 371 | text = self.run(["--list-keys", "--with-colons", "--with-fingerprint"] + |
paul@78 | 372 | (keyid and ["0x%s" % keyid] or [])) |
paul@78 | 373 | return self._getKeysFromResult(text) |
paul@78 | 374 | |
paul@78 | 375 | def listSignatures(self, keyid=None): |
paul@78 | 376 | |
paul@78 | 377 | """ |
paul@78 | 378 | Return a list of key and signature details for keys on the keychain, |
paul@78 | 379 | selecting only one specific key if 'keyid' is specified. |
paul@78 | 380 | """ |
paul@78 | 381 | |
paul@78 | 382 | text = self.run(["--list-sigs", "--with-colons", "--with-fingerprint"] + |
paul@78 | 383 | (keyid and ["0x%s" % keyid] or [])) |
paul@78 | 384 | return self._getKeysFromResult(text) |
paul@78 | 385 | |
paul@77 | 386 | def getKeysFromMessagePart(self, part): |
paul@77 | 387 | |
paul@77 | 388 | """ |
paul@77 | 389 | Process an application/pgp-keys message 'part', returning a list of |
paul@77 | 390 | key details. |
paul@77 | 391 | """ |
paul@77 | 392 | |
paul@77 | 393 | return self.getKeysFromString(part.get_payload()) |
paul@77 | 394 | |
paul@77 | 395 | def getKeysFromString(self, s): |
paul@77 | 396 | |
paul@78 | 397 | """ |
paul@78 | 398 | Return a list of key details extracted from the given key block string |
paul@79 | 399 | 's'. Signature information is also included through the use of the gpg |
paul@79 | 400 | verbose option. |
paul@78 | 401 | """ |
paul@77 | 402 | |
paul@79 | 403 | text = self.run(["--with-colons", "--with-fingerprint", "-v"], s) |
paul@78 | 404 | return self._getKeysFromResult(text) |
paul@78 | 405 | |
paul@78 | 406 | def _getKeysFromResult(self, text): |
paul@78 | 407 | |
paul@78 | 408 | """ |
paul@78 | 409 | Return a list of key details extracted from the given command result |
paul@78 | 410 | 'text'. |
paul@78 | 411 | """ |
paul@78 | 412 | |
paul@77 | 413 | keys = [] |
paul@77 | 414 | for line in text.split("\n"): |
paul@77 | 415 | try: |
paul@77 | 416 | recordtype, trust, keylength, algorithm, keyid, cdate, expdate, serial, ownertrust, _rest = line.split(":", 9) |
paul@77 | 417 | except ValueError: |
paul@78 | 418 | continue |
paul@77 | 419 | |
paul@77 | 420 | if recordtype == "pub": |
paul@77 | 421 | userid, _rest = _rest.split(":", 1) |
paul@77 | 422 | keys.append({ |
paul@77 | 423 | "type" : recordtype, "trust" : trust, "keylength" : keylength, |
paul@77 | 424 | "algorithm" : algorithm, "keyid" : keyid, "cdate" : cdate, |
paul@77 | 425 | "expdate" : expdate, "userid" : userid, "ownertrust" : ownertrust, |
paul@78 | 426 | "fingerprint" : None, "subkeys" : [], "signatures" : [] |
paul@77 | 427 | }) |
paul@77 | 428 | elif recordtype == "sub" and keys: |
paul@77 | 429 | keys[-1]["subkeys"].append({ |
paul@77 | 430 | "trust" : trust, "keylength" : keylength, "algorithm" : algorithm, |
paul@77 | 431 | "keyid" : keyid, "cdate" : cdate, "expdate" : expdate, |
paul@77 | 432 | "ownertrust" : ownertrust |
paul@77 | 433 | }) |
paul@77 | 434 | elif recordtype == "fpr" and keys: |
paul@77 | 435 | fingerprint, _rest = _rest.split(":", 1) |
paul@77 | 436 | keys[-1]["fingerprint"] = fingerprint |
paul@78 | 437 | elif recordtype == "sig" and keys: |
paul@78 | 438 | userid, _rest = _rest.split(":", 1) |
paul@78 | 439 | keys[-1]["signatures"].append({ |
paul@78 | 440 | "keyid" : keyid, "cdate" : cdate, "expdate" : expdate, |
paul@78 | 441 | "userid" : userid |
paul@78 | 442 | }) |
paul@77 | 443 | |
paul@77 | 444 | return keys |
paul@77 | 445 | |
paul@33 | 446 | # Message decoding functions. |
paul@33 | 447 | |
paul@33 | 448 | # Detect PGP/GPG-encoded payloads. |
paul@33 | 449 | # See: http://tools.ietf.org/html/rfc3156 |
paul@33 | 450 | |
paul@33 | 451 | def is_signed(message): |
paul@33 | 452 | mimetype = message.get_content_type() |
paul@33 | 453 | encoding = message.get_content_charset() |
paul@33 | 454 | |
paul@33 | 455 | return mimetype == "multipart/signed" and \ |
paul@33 | 456 | message.get_param("protocol") == "application/pgp-signature" |
paul@33 | 457 | |
paul@33 | 458 | def is_encrypted(message): |
paul@33 | 459 | mimetype = message.get_content_type() |
paul@33 | 460 | encoding = message.get_content_charset() |
paul@33 | 461 | |
paul@33 | 462 | return mimetype == "multipart/encrypted" and \ |
paul@33 | 463 | message.get_param("protocol") == "application/pgp-encrypted" |
paul@33 | 464 | |
paul@36 | 465 | def getContentAndSignature(message): |
paul@36 | 466 | |
paul@36 | 467 | """ |
paul@36 | 468 | Return the content and signature parts of the given RFC 3156 'message'. |
paul@36 | 469 | |
paul@36 | 470 | NOTE: RFC 3156 states that signed messages should employ a detached |
paul@36 | 471 | NOTE: signature but then shows "BEGIN PGP MESSAGE" for signatures |
paul@36 | 472 | NOTE: instead of "BEGIN PGP SIGNATURE". |
paul@36 | 473 | NOTE: The "micalg" parameter is currently not supported. |
paul@36 | 474 | """ |
paul@36 | 475 | |
paul@36 | 476 | try: |
paul@36 | 477 | content, signature = message.get_payload() |
paul@36 | 478 | return content, signature |
paul@36 | 479 | except ValueError: |
paul@36 | 480 | raise MoinMessageMissingPart |
paul@36 | 481 | |
paul@8 | 482 | # Communications functions. |
paul@6 | 483 | |
paul@20 | 484 | def timestamp(message): |
paul@20 | 485 | |
paul@20 | 486 | """ |
paul@20 | 487 | Timestamp the given 'message' so that its validity can be assessed by the |
paul@20 | 488 | recipient. |
paul@20 | 489 | """ |
paul@20 | 490 | |
paul@20 | 491 | datestr = formatdate() |
paul@20 | 492 | |
paul@20 | 493 | if not message.has_key("Date"): |
paul@20 | 494 | message.add_header("Date", datestr) |
paul@20 | 495 | else: |
paul@20 | 496 | message["Date"] = datestr |
paul@20 | 497 | |
paul@44 | 498 | def sendMessage(message, url, method="PUT"): |
paul@6 | 499 | |
paul@44 | 500 | """ |
paul@44 | 501 | Send 'message' to the given 'url' using the given 'method' (using PUT as the |
paul@44 | 502 | default if omitted). |
paul@44 | 503 | """ |
paul@6 | 504 | |
paul@12 | 505 | scheme, host, port, path = parseURL(url) |
paul@6 | 506 | text = message.as_string() |
paul@6 | 507 | |
paul@12 | 508 | if scheme == "http": |
paul@12 | 509 | cls = httplib.HTTPConnection |
paul@12 | 510 | elif scheme == "https": |
paul@12 | 511 | cls = httplib.HTTPSConnection |
paul@12 | 512 | else: |
paul@12 | 513 | raise MoinMessageError, "Communications protocol not supported: %s" % scheme |
paul@12 | 514 | |
paul@12 | 515 | req = cls(host, port) |
paul@44 | 516 | req.request(method, path, text) |
paul@6 | 517 | resp = req.getresponse() |
paul@39 | 518 | |
paul@39 | 519 | if resp.status >= 400: |
paul@39 | 520 | raise MoinMessageError, "Message sending failed: %s" % resp.status |
paul@39 | 521 | |
paul@6 | 522 | return resp.read() |
paul@6 | 523 | |
paul@12 | 524 | def parseURL(url): |
paul@12 | 525 | |
paul@12 | 526 | "Return the scheme, host, port and path for the given 'url'." |
paul@12 | 527 | |
paul@12 | 528 | scheme, host_port, path, query, fragment = urlsplit(url) |
paul@12 | 529 | host_port = host_port.split(":") |
paul@12 | 530 | |
paul@12 | 531 | if query: |
paul@12 | 532 | path += "?" + query |
paul@12 | 533 | |
paul@12 | 534 | if len(host_port) > 1: |
paul@12 | 535 | host = host_port[0] |
paul@12 | 536 | port = int(host_port[1]) |
paul@12 | 537 | else: |
paul@12 | 538 | host = host_port[0] |
paul@12 | 539 | port = 80 |
paul@12 | 540 | |
paul@12 | 541 | return scheme, host, port, path |
paul@12 | 542 | |
paul@6 | 543 | # vim: tabstop=4 expandtab shiftwidth=4 |