Statistics
| Branch: | Tag: | Revision:

root / snf-common / synnefo / lib / http_request.py @ 5f6ad491

History | View | Annotate | Download (4.3 kB)

1 818bb9bf Georgios D. Tsoukalas
# Copyright 2012 GRNET S.A. All rights reserved.
2 818bb9bf Georgios D. Tsoukalas
#
3 818bb9bf Georgios D. Tsoukalas
# Redistribution and use in source and binary forms, with or
4 818bb9bf Georgios D. Tsoukalas
# without modification, are permitted provided that the following
5 818bb9bf Georgios D. Tsoukalas
# conditions are met:
6 818bb9bf Georgios D. Tsoukalas
#
7 818bb9bf Georgios D. Tsoukalas
#   1. Redistributions of source code must retain the above
8 818bb9bf Georgios D. Tsoukalas
#      copyright notice, self.list of conditions and the following
9 818bb9bf Georgios D. Tsoukalas
#      disclaimer.
10 818bb9bf Georgios D. Tsoukalas
#
11 818bb9bf Georgios D. Tsoukalas
#   2. Redistributions in binary form must reproduce the above
12 818bb9bf Georgios D. Tsoukalas
#      copyright notice, self.list of conditions and the following
13 818bb9bf Georgios D. Tsoukalas
#      disclaimer in the documentation and/or other materials
14 818bb9bf Georgios D. Tsoukalas
#      provided with the distribution.
15 818bb9bf Georgios D. Tsoukalas
#
16 818bb9bf Georgios D. Tsoukalas
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 818bb9bf Georgios D. Tsoukalas
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 818bb9bf Georgios D. Tsoukalas
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 818bb9bf Georgios D. Tsoukalas
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 818bb9bf Georgios D. Tsoukalas
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 818bb9bf Georgios D. Tsoukalas
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 818bb9bf Georgios D. Tsoukalas
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 818bb9bf Georgios D. Tsoukalas
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 818bb9bf Georgios D. Tsoukalas
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 818bb9bf Georgios D. Tsoukalas
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 818bb9bf Georgios D. Tsoukalas
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 818bb9bf Georgios D. Tsoukalas
# POSSIBILITY OF SUCH DAMAGE.
28 818bb9bf Georgios D. Tsoukalas
#
29 818bb9bf Georgios D. Tsoukalas
# The views and conclusions contained in the software and
30 818bb9bf Georgios D. Tsoukalas
# documentation are those of the authors and should not be
31 818bb9bf Georgios D. Tsoukalas
# interpreted as representing official policies, either expressed
32 818bb9bf Georgios D. Tsoukalas
# or implied, of GRNET S.A.
33 818bb9bf Georgios D. Tsoukalas
34 818bb9bf Georgios D. Tsoukalas
from urlparse import urlparse
35 818bb9bf Georgios D. Tsoukalas
from synnefo.lib.pool.http import get_http_connection
36 818bb9bf Georgios D. Tsoukalas
37 818bb9bf Georgios D. Tsoukalas
class http_request(object):
38 818bb9bf Georgios D. Tsoukalas
39 818bb9bf Georgios D. Tsoukalas
    url         =   None
40 818bb9bf Georgios D. Tsoukalas
    scheme      =   None
41 818bb9bf Georgios D. Tsoukalas
    netloc      =   None
42 818bb9bf Georgios D. Tsoukalas
    method      =   None
43 818bb9bf Georgios D. Tsoukalas
    body        =   None
44 818bb9bf Georgios D. Tsoukalas
    headers     =   None
45 818bb9bf Georgios D. Tsoukalas
46 fb02390b Georgios D. Tsoukalas
    conn        =   None
47 fb02390b Georgios D. Tsoukalas
    response    =   None
48 fb02390b Georgios D. Tsoukalas
49 818bb9bf Georgios D. Tsoukalas
    scheme_ports = {
50 818bb9bf Georgios D. Tsoukalas
            'http':     '80',
51 818bb9bf Georgios D. Tsoukalas
            'https':    '443',
52 818bb9bf Georgios D. Tsoukalas
    }
53 818bb9bf Georgios D. Tsoukalas
54 818bb9bf Georgios D. Tsoukalas
    def __init__(self,  url     =   None,
55 818bb9bf Georgios D. Tsoukalas
                        scheme  =   None,
56 818bb9bf Georgios D. Tsoukalas
                        params  =   None,
57 818bb9bf Georgios D. Tsoukalas
                        headers =   None,
58 818bb9bf Georgios D. Tsoukalas
                        host    =   None,
59 818bb9bf Georgios D. Tsoukalas
                        port    =   None,
60 818bb9bf Georgios D. Tsoukalas
                        method  =   None,
61 818bb9bf Georgios D. Tsoukalas
                        **kw):
62 818bb9bf Georgios D. Tsoukalas
        if url is None:
63 818bb9bf Georgios D. Tsoukalas
            url = '/'
64 818bb9bf Georgios D. Tsoukalas
65 818bb9bf Georgios D. Tsoukalas
        if host is None or scheme is None:
66 818bb9bf Georgios D. Tsoukalas
            p = urlparse(url)
67 818bb9bf Georgios D. Tsoukalas
            netloc = p.netloc
68 818bb9bf Georgios D. Tsoukalas
            if not netloc:
69 818bb9bf Georgios D. Tsoukalas
                netloc = 'localhost'
70 818bb9bf Georgios D. Tsoukalas
            scheme = p.scheme
71 818bb9bf Georgios D. Tsoukalas
            if not scheme:
72 818bb9bf Georgios D. Tsoukalas
                scheme = 'http'
73 818bb9bf Georgios D. Tsoukalas
            params = '&'.join(params) if params is not None in kw else ''
74 818bb9bf Georgios D. Tsoukalas
            query = p.query
75 818bb9bf Georgios D. Tsoukalas
            if query or params:
76 818bb9bf Georgios D. Tsoukalas
                query = '?' + query + params
77 818bb9bf Georgios D. Tsoukalas
            url = p.path + p.params + query+ p.fragment
78 818bb9bf Georgios D. Tsoukalas
        else:
79 818bb9bf Georgios D. Tsoukalas
            host = host
80 818bb9bf Georgios D. Tsoukalas
            port = port if port is not None else self.scheme_ports[scheme]
81 818bb9bf Georgios D. Tsoukalas
            #NOTE: we force host:port as canonical form,
82 818bb9bf Georgios D. Tsoukalas
            #      lest we have a cache miss 'host' vs 'host:80'
83 818bb9bf Georgios D. Tsoukalas
            netloc = "%s%s" % (host, port)
84 818bb9bf Georgios D. Tsoukalas
85 818bb9bf Georgios D. Tsoukalas
        self.netloc = netloc
86 818bb9bf Georgios D. Tsoukalas
        self.url = url
87 818bb9bf Georgios D. Tsoukalas
        self.scheme = scheme
88 818bb9bf Georgios D. Tsoukalas
        self.kw = kw
89 818bb9bf Georgios D. Tsoukalas
90 818bb9bf Georgios D. Tsoukalas
        self.method = method if method is not None else 'GET'
91 818bb9bf Georgios D. Tsoukalas
92 818bb9bf Georgios D. Tsoukalas
        if 'body' in kw:
93 818bb9bf Georgios D. Tsoukalas
            self.body = kw['headers']
94 818bb9bf Georgios D. Tsoukalas
95 818bb9bf Georgios D. Tsoukalas
        if 'headers' in kw:
96 818bb9bf Georgios D. Tsoukalas
            self.headers = kw['headers']
97 818bb9bf Georgios D. Tsoukalas
98 818bb9bf Georgios D. Tsoukalas
        if kw.get('connect', True):
99 818bb9bf Georgios D. Tsoukalas
            self.connect()
100 818bb9bf Georgios D. Tsoukalas
101 818bb9bf Georgios D. Tsoukalas
    def connect(self):
102 fb02390b Georgios D. Tsoukalas
        if self.conn is not None:
103 fb02390b Georgios D. Tsoukalas
            self.dismiss()
104 fb02390b Georgios D. Tsoukalas
105 818bb9bf Georgios D. Tsoukalas
        conn = get_http_connection(netloc=self.netloc, scheme=self.scheme)
106 818bb9bf Georgios D. Tsoukalas
        try:
107 818bb9bf Georgios D. Tsoukalas
            kw = {}
108 818bb9bf Georgios D. Tsoukalas
            body = self.body
109 818bb9bf Georgios D. Tsoukalas
            if body is not None:
110 818bb9bf Georgios D. Tsoukalas
                kw['body'] = body
111 818bb9bf Georgios D. Tsoukalas
            headers = self.headers
112 818bb9bf Georgios D. Tsoukalas
            if headers is not None:
113 818bb9bf Georgios D. Tsoukalas
                kw['headers'] = headers
114 818bb9bf Georgios D. Tsoukalas
            conn.request(self.method, self.url, **kw)
115 818bb9bf Georgios D. Tsoukalas
        except:
116 818bb9bf Georgios D. Tsoukalas
            conn.close()
117 818bb9bf Georgios D. Tsoukalas
            raise
118 818bb9bf Georgios D. Tsoukalas
        self.conn = conn
119 818bb9bf Georgios D. Tsoukalas
120 818bb9bf Georgios D. Tsoukalas
    def getresponse(self):
121 fb02390b Georgios D. Tsoukalas
        conn = self.conn
122 fb02390b Georgios D. Tsoukalas
        if conn is None:
123 fb02390b Georgios D. Tsoukalas
            self.connect()
124 fb02390b Georgios D. Tsoukalas
            conn = self.conn
125 fb02390b Georgios D. Tsoukalas
        response = self.conn.getresponse()
126 fb02390b Georgios D. Tsoukalas
        self.response = response
127 fb02390b Georgios D. Tsoukalas
        return response
128 fb02390b Georgios D. Tsoukalas
129 fb02390b Georgios D. Tsoukalas
    def dismiss(self):
130 fb02390b Georgios D. Tsoukalas
        conn = self.conn
131 fb02390b Georgios D. Tsoukalas
        if conn is not None:
132 fb02390b Georgios D. Tsoukalas
            conn.close()
133 fb02390b Georgios D. Tsoukalas
        conn.response = None