forked from shivateja/cloudstack-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
64 lines (58 loc) · 2.78 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#Licensed to the Apache Software Foundation (ASF) under one
#or more contributor license agreements. See the NOTICE file
#distributed with this work for additional information
#regarding copyright ownership. The ASF licenses this file
#to you under the Apache License, Version 2.0 (the
#"License"); you may not use this file except in compliance
#with the License. You may obtain a copy of the License at
#http://www.apache.org/licenses/LICENSE-2.0
#Unless required by applicable law or agreed to in writing,
#software distributed under the License is distributed on an
#"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#KIND, either express or implied. See the License for the
#specific language governing permissions and limitations
#under the License.
from flask import Flask, url_for, render_template, request, json, abort, send_from_directory, make_response
import requests
app = Flask(__name__)
csurl = 'http://localhost:8080/'
def get_args(multidict):
"""Default type of request.args or request.json is multidict. Converts it to dict so that can be passed to make_request"""
data = {}
for key in multidict.keys():
data[key] = multidict.get(key)
return data
@app.route('/<path:path>', methods=['GET', 'POST'])
def api(path):
"""This method catches any URL that is not registered
by any other methods. It sends the request to the ACS server with
the same headers, cookies and arguments and sends the same response
from ACS server to the browser. Just mirroring the
ACS server."""
headers = dict(request.headers)
if request.method == 'GET':
args = get_args(request.args)
#Not sure where these headers are coming from in GET requests
#Probably flask is added them by default to all the requests
#If these headers are not deleted,
#management server produces 400 Bad Request error
#3 days of extensive debugging, sigh!
del headers['Content-Length']
del headers['Content-Type']
#Send the GET request to CS server with the
#headers, cookies and arguments from the browser request
cs_response = requests.get(csurl + path, params = args, cookies = request.cookies, headers = headers)
elif request.method == 'POST':
#Same as the above, but a post request
data = get_args(request.form)
cs_response = requests.post(csurl + path, data = data, cookies = request.cookies, headers = headers)
#Generate the response to be sent
our_response = make_response(cs_response.content, cs_response.status_code)
#Add the headers from the server
our_response.headers.extend(dict(cs_response.headers))
return our_response
@app.route('/')
def index():
return send_from_directory("templates", "index.html")
if __name__ == '__main__':
app.run(debug=True)