This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
veridic/acs/utils.py

50 lines
1.6 KiB
Python

import urlparse
import os.path
import urllib
import httplib
import logging
import re
import datetime
from django.http import HttpResponseRedirect, Http404, HttpResponse
def get_soap_message(request, on_error_raise = True):
'''Verify that POST content looks like a SOAP message and returns it'''
if request.method != 'POST' or \
request.META['CONTENT_TYPE'] != 'text/xml':
if on_error_raise:
raise Http404(_('Only SOAP messages here'))
else:
return None
return request.raw_post_data
class SOAPException(Exception):
url = None
def __init__(self, url):
self.url = url
def soap_call(url, msg, client_cert = None):
if url.startswith('http://'):
host, query = urllib.splithost(url[5:])
conn = httplib.HTTPConnection(host)
else:
host, query = urllib.splithost(url[6:])
conn = httplib.HTTPSConnection(host,
key_file = client_cert, cert_file = client_cert)
try:
conn.request('POST', query, msg, {'Content-Type': 'text/xml'})
response = conn.getresponse()
except Exception, err:
logging.error('SOAP error (on %s): %s' % (url, err))
raise SOAPException(url)
try:
data = response.read()
except Exception, err:
logging.error('SOAP error (on %s): %s' % (url, err))
raise SOAPException(url)
conn.close()
if response.status not in (200, 204): # 204 ok for federation termination
logging.warning('SOAP error (%s) (on %s)' % (response.status, url))
raise SOAPException(url)
return data