blob: 5c59aadc90b56fe269f7964e51e188233aa73bcf [file] [log] [blame]
Simran Basi71206ef2014-08-13 13:51:18 -07001import httplib2, os, sys, traceback, cgi
mblighe8819cd2008-02-15 16:48:40 +00002
mblighe8819cd2008-02-15 16:48:40 +00003from django.http import HttpResponse, HttpResponsePermanentRedirect
showard37c7fe62008-07-24 16:35:02 +00004from django.http import HttpResponseServerError
5from django.template import Context, loader
showard5deef7f2009-09-09 18:16:58 +00006from autotest_lib.client.common_lib import utils
showard26b7ec72009-12-21 22:43:57 +00007from autotest_lib.frontend import views_common
showard5deef7f2009-09-09 18:16:58 +00008from autotest_lib.frontend.afe import models, rpc_handler, rpc_interface
9from autotest_lib.frontend.afe import rpc_utils
10
11site_rpc_interface = utils.import_site_module(
12 __file__, 'autotest_lib.frontend.afe.site_rpc_interface',
13 dummy=object())
mblighe8819cd2008-02-15 16:48:40 +000014
xixuanba232a32016-08-25 17:01:59 -070015moblab_rpc_interface = utils.import_site_module(
16 __file__, 'autotest_lib.frontend.afe.moblab_rpc_interface',
17 dummy=object())
18
showard7c785282008-05-29 19:45:12 +000019# since site_rpc_interface is later in the list, its methods will override those
20# of rpc_interface
xixuanba232a32016-08-25 17:01:59 -070021rpc_handler_obj = rpc_handler.RpcHandler((rpc_interface, site_rpc_interface,
22 moblab_rpc_interface),
jadmanski0afbb632008-06-06 21:10:57 +000023 document_module=rpc_interface)
showard7c785282008-05-29 19:45:12 +000024
25
26def handle_rpc(request):
xixuanba232a32016-08-25 17:01:59 -070027 """Handle the RPC request.
28
29 @param request: the RPC request.
30 """
jadmanski0afbb632008-06-06 21:10:57 +000031 return rpc_handler_obj.handle_rpc_request(request)
showard7c785282008-05-29 19:45:12 +000032
33
showardef6fe022009-03-27 20:55:16 +000034def rpc_documentation(request):
xixuanba232a32016-08-25 17:01:59 -070035 """Return the rpc documentation.
36
37 @param request: the RPC request.
38 """
showardef6fe022009-03-27 20:55:16 +000039 return rpc_handler_obj.get_rpc_documentation()
40
41
mblighe8819cd2008-02-15 16:48:40 +000042def model_documentation(request):
xixuanba232a32016-08-25 17:01:59 -070043 """Get the model documentation.
44
45 @param request: the RPC request.
46 """
showard26b7ec72009-12-21 22:43:57 +000047 model_names = ('Label', 'Host', 'Test', 'User', 'AclGroup', 'Job',
48 'AtomicGroup')
49 return views_common.model_documentation(models, model_names)
mblighe8819cd2008-02-15 16:48:40 +000050
51
52def redirect_with_extra_data(request, url, **kwargs):
xixuanba232a32016-08-25 17:01:59 -070053 """Redirect according to the extra data.
54
55 @param request: the RPC request.
56 @param url: the partial redirected url.
57 @param kwargs: the parameters used in redirection.
58 """
jadmanski0afbb632008-06-06 21:10:57 +000059 kwargs['getdata'] = request.GET.urlencode()
60 kwargs['server_name'] = request.META['SERVER_NAME']
61 return HttpResponsePermanentRedirect(url % kwargs)
mblighe8819cd2008-02-15 16:48:40 +000062
63
64GWT_SERVER = 'http://localhost:8888/'
65def gwt_forward(request, forward_addr):
xixuanba232a32016-08-25 17:01:59 -070066 """Get the response from forwarding address.
67
68 @param request: the RPC request.
69 @param forward_addr: the forwarding address.
70 """
jamesrenf2341eb2010-06-21 21:52:09 +000071 url = GWT_SERVER + forward_addr
jadmanski0afbb632008-06-06 21:10:57 +000072 if len(request.POST) == 0:
jamesrenf2341eb2010-06-21 21:52:09 +000073 headers, content = httplib2.Http().request(url, 'GET')
jadmanski0afbb632008-06-06 21:10:57 +000074 else:
jamesrenf2341eb2010-06-21 21:52:09 +000075 headers, content = httplib2.Http().request(url, 'POST',
76 body=request.raw_post_data)
77 http_response = HttpResponse(content)
78 for header, value in headers.iteritems():
jadmanski0afbb632008-06-06 21:10:57 +000079 if header not in ('connection',):
80 http_response[header] = value
81 return http_response
showard37c7fe62008-07-24 16:35:02 +000082
83
84def handler500(request):
xixuanba232a32016-08-25 17:01:59 -070085 """Redirect to error website page.
86
87 @param request: the RPC request.
88 """
showard37c7fe62008-07-24 16:35:02 +000089 t = loader.get_template('500.html')
90 trace = traceback.format_exc()
91 context = Context({
92 'type': sys.exc_type,
93 'value': sys.exc_value,
94 'traceback': cgi.escape(trace)
95 })
96 return HttpResponseServerError(t.render(context))
Simran Basi71206ef2014-08-13 13:51:18 -070097
98
99def handle_file_upload(request):
100 """Handler for uploading files.
101
102 Saves the files to /tmp and returns the resulting paths on disk.
103
104 @param request: request containing the file data.
105
106 @returns HttpResponse: with the paths of the saved files.
107 """
108 if request.method == 'POST':
109 TEMPT_DIR = '/tmp/'
110 file_paths = []
111 for file_name, upload_file in request.FILES.iteritems():
112 file_path = os.path.join(
113 TEMPT_DIR, '_'.join([file_name, upload_file.name]))
114 with open(file_path, 'wb+') as destination:
115 for chunk in upload_file.chunks():
116 destination.write(chunk)
117 file_paths.append(file_path)
118 return HttpResponse(rpc_utils.prepare_for_serialization(file_paths))