#    This file is part of WebBox.
#
#    Copyright 2011-2012 Daniel Alexander Smith
#    Copyright 2011-2012 University of Southampton
#
#    WebBox 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.
#
#    WebBox 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 WebBox.  If not, see <http://www.gnu.org/licenses/>.


""" This file contains http helper functions, and can be used by any module. """
import httplib, logging

from twisted.internet import reactor, threads

def http_get(host, path, headers={}):
    """ Send a GET request to a sub-server, i.e., RWW or 4store """

    connection = httplib.HTTPConnection(host)
    connection.request('GET', path, "", headers)

    try:
        #result = connection.getresponse()
        result = threads.blockingCallFromThread(reactor, connection.getresponse)

        # Now result.status and result.reason contains interesting stuff
        logging.debug("GET file from: "+host+", at path: "+path)

#        data = result.read()
        data = threads.blockingCallFromThread(reactor, result.read)
    except Exception as e:
        logging.debug("Error in http_get: "+str(e))

    if result.status >= 200 and result.status <= 299:
        logging.debug("Status: Successful")
    else:
        logging.debug("Status: ("+str(result.status)+") Not successful, reason: " + result.reason)

    """ status is a HTTP status code as an integer, e.g. 200, and data is a string of data to return. """
    response =  {'status': result.status, "reason": result.reason, 'data': data}

    ctype = result.getheader("Content-type", None) 
    if ctype is not None:
        response['type'] = ctype

    return response


def http_put(host, path, data, content_type):
    """ Perform a PUT to the URL with the data """

    # TODO put ACL authenication etc here ?

    connection = httplib.HTTPConnection(host)
    connection.request('PUT', path, data, {"Content-type": content_type})

    try:
        #result = connection.getresponse()
        result = threads.blockingCallFromThread(reactor, connection.getresponse)
    except Exception as e:
        logging.debug("Error in http_put: "+str(e))

    # Now result.status and result.reason contains interesting stuff
    logging.debug("PUT file to: "+host+", at path: "+path+", content-type: "+content_type+", contents: "+str(data))

    if result.status >= 200 and result.status <= 299:
        logging.debug("Status: Successful")
    else:
        logging.debug("Status: Not successful (%s), reason: " % (str(result.status)) + result.reason)

    """ status is a HTTP status code as an integer, e.g. 200 """
    return {"status": result.status, "reason": result.reason}


def http_post(host, path, data, args):
    """ Perform a POST to the URL with the data """

    # TODO put ACL authentication etc here ?

    connection = httplib.HTTPConnection(host)
    connection.request('POST', path, data, args)

    try:
        #result = connection.getresponse()
        result = threads.blockingCallFromThread(reactor, connection.getresponse)
    except Exception as e:
        logging.debug("Error in http_post: "+str(e))

    # Now result.status and result.reason contains interesting stuff
    logging.debug("POST file to: "+host+", at path: "+path)

    if result.status >= 200 and result.status <= 299:
        logging.debug("Status: Successful")
    else:
        logging.debug("Status: Not successful (%s), reason: " % (str(result.status)) + result.reason)

    return {"status": result.status, "reason": result.reason}

def resolve_uri(self, uri):
    """ Resolve an RDF URI and return the RDF/XML. """
    logging.debug("resolve uri: "+uri)

    opener = urllib2.build_opener(urllib2.HTTPHandler)
    request = urllib2.Request(uri)
    request.add_header('Accept', 'application/rdf+xml')
    request.get_method = lambda: 'GET'
    url = opener.open(request)

    try:
        #data = url.read()
        data = threads.blockingCallFromThread(reactor, url.read)
    except Exception as e:
        logging.debug("Error in resolve_uri: "+str(e))

    return data


@@ -1143,33 +1143,32 @@
    try:%0A        
-#
 result = connect
@@ -1185,32 +1185,33 @@
 ponse()%0A        
+#
 result = threads
@@ -1407,17 +1407,16 @@
 +path)%0A%0A
-#
         
@@ -1436,32 +1436,33 @@
 .read()%0A        
+#
 data = threads.b
@@ -2388,33 +2388,32 @@
    try:%0A        
-#
 result = connect
@@ -2430,32 +2430,33 @@
 ponse()%0A        
+#
 result = threads
@@ -3361,33 +3361,32 @@
    try:%0A        
-#
 result = connect
@@ -3403,32 +3403,33 @@
 ponse()%0A        
+#
 result = threads
@@ -4323,25 +4323,24 @@
 ry:%0A        
-#
 data = url.r
@@ -4349,24 +4349,25 @@
 d()%0A        
+#
 data = threa

Fix sending http calls while in .rpy