Statistics
| Branch: | Tag: | Revision:

root / snf-common / synnefo / lib / http_request.py @ 1c65202f

History | View | Annotate | Download (4.3 kB)

1
# Copyright 2012 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, self.list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, self.list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
from urlparse import urlparse
35
from synnefo.lib.pool.http import get_http_connection
36

    
37
class http_request(object):
38

    
39
    url         =   None
40
    scheme      =   None
41
    netloc      =   None
42
    method      =   None
43
    body        =   None
44
    headers     =   None
45

    
46
    conn        =   None
47
    response    =   None
48

    
49
    scheme_ports = {
50
            'http':     '80',
51
            'https':    '443',
52
    }
53

    
54
    def __init__(self,  url     =   None,
55
                        scheme  =   None,
56
                        params  =   None,
57
                        headers =   None,
58
                        host    =   None,
59
                        port    =   None,
60
                        method  =   None,
61
                        **kw):
62
        if url is None:
63
            url = '/'
64

    
65
        if host is None or scheme is None:
66
            p = urlparse(url)
67
            netloc = p.netloc
68
            if not netloc:
69
                netloc = 'localhost'
70
            scheme = p.scheme
71
            if not scheme:
72
                scheme = 'http'
73
            params = '&'.join(params) if params is not None in kw else ''
74
            query = p.query
75
            if query or params:
76
                query = '?' + query + params
77
            url = p.path + p.params + query+ p.fragment
78
        else:
79
            host = host
80
            port = port if port is not None else self.scheme_ports[scheme]
81
            #NOTE: we force host:port as canonical form,
82
            #      lest we have a cache miss 'host' vs 'host:80'
83
            netloc = "%s%s" % (host, port)
84

    
85
        self.netloc = netloc
86
        self.url = url
87
        self.scheme = scheme
88
        self.kw = kw
89

    
90
        self.method = method if method is not None else 'GET'
91

    
92
        if 'body' in kw:
93
            self.body = kw['headers']
94

    
95
        if 'headers' in kw:
96
            self.headers = kw['headers']
97

    
98
        if kw.get('connect', True):
99
            self.connect()
100

    
101
    def connect(self):
102
        if self.conn is not None:
103
            self.dismiss()
104

    
105
        conn = get_http_connection(netloc=self.netloc, scheme=self.scheme)
106
        try:
107
            kw = {}
108
            body = self.body
109
            if body is not None:
110
                kw['body'] = body
111
            headers = self.headers
112
            if headers is not None:
113
                kw['headers'] = headers
114
            conn.request(self.method, self.url, **kw)
115
        except:
116
            conn.close()
117
            raise
118
        self.conn = conn
119

    
120
    def getresponse(self):
121
        conn = self.conn
122
        if conn is None:
123
            self.connect()
124
            conn = self.conn
125
        response = self.conn.getresponse()
126
        self.response = response
127
        return response
128

    
129
    def dismiss(self):
130
        conn = self.conn
131
        if conn is not None:
132
            conn.close()
133
        conn.response = None
134