blob: b2acfeb754bc6e78ca999d2eb56157235b9f2f43 [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
xixuanba232a32016-08-25 17:01:59 -070011moblab_rpc_interface = utils.import_site_module(
12 __file__, 'autotest_lib.frontend.afe.moblab_rpc_interface',
13 dummy=object())
14
Allen Licdd00f22017-02-01 18:01:52 -080015# since moblab_rpc_interface is later in the list, its methods will
16# override those of rpc_interface
17rpc_handler_obj = rpc_handler.RpcHandler((rpc_interface,
xixuanba232a32016-08-25 17:01:59 -070018 moblab_rpc_interface),
jadmanski0afbb632008-06-06 21:10:57 +000019 document_module=rpc_interface)
showard7c785282008-05-29 19:45:12 +000020
21
22def handle_rpc(request):
xixuanba232a32016-08-25 17:01:59 -070023 """Handle the RPC request.
24
25 @param request: the RPC request.
26 """
jadmanski0afbb632008-06-06 21:10:57 +000027 return rpc_handler_obj.handle_rpc_request(request)
showard7c785282008-05-29 19:45:12 +000028
29
showardef6fe022009-03-27 20:55:16 +000030def rpc_documentation(request):
xixuanba232a32016-08-25 17:01:59 -070031 """Return the rpc documentation.
32
33 @param request: the RPC request.
34 """
showardef6fe022009-03-27 20:55:16 +000035 return rpc_handler_obj.get_rpc_documentation()
36
37
mblighe8819cd2008-02-15 16:48:40 +000038def model_documentation(request):
xixuanba232a32016-08-25 17:01:59 -070039 """Get the model documentation.
40
41 @param request: the RPC request.
42 """
showard26b7ec72009-12-21 22:43:57 +000043 model_names = ('Label', 'Host', 'Test', 'User', 'AclGroup', 'Job',
44 'AtomicGroup')
45 return views_common.model_documentation(models, model_names)
mblighe8819cd2008-02-15 16:48:40 +000046
47
48def redirect_with_extra_data(request, url, **kwargs):
xixuanba232a32016-08-25 17:01:59 -070049 """Redirect according to the extra data.
50
51 @param request: the RPC request.
52 @param url: the partial redirected url.
53 @param kwargs: the parameters used in redirection.
54 """
jadmanski0afbb632008-06-06 21:10:57 +000055 kwargs['getdata'] = request.GET.urlencode()
56 kwargs['server_name'] = request.META['SERVER_NAME']
57 return HttpResponsePermanentRedirect(url % kwargs)
mblighe8819cd2008-02-15 16:48:40 +000058
59
60GWT_SERVER = 'http://localhost:8888/'
61def gwt_forward(request, forward_addr):
xixuanba232a32016-08-25 17:01:59 -070062 """Get the response from forwarding address.
63
64 @param request: the RPC request.
65 @param forward_addr: the forwarding address.
66 """
jamesrenf2341eb2010-06-21 21:52:09 +000067 url = GWT_SERVER + forward_addr
jadmanski0afbb632008-06-06 21:10:57 +000068 if len(request.POST) == 0:
jamesrenf2341eb2010-06-21 21:52:09 +000069 headers, content = httplib2.Http().request(url, 'GET')
jadmanski0afbb632008-06-06 21:10:57 +000070 else:
jamesrenf2341eb2010-06-21 21:52:09 +000071 headers, content = httplib2.Http().request(url, 'POST',
72 body=request.raw_post_data)
73 http_response = HttpResponse(content)
74 for header, value in headers.iteritems():
jadmanski0afbb632008-06-06 21:10:57 +000075 if header not in ('connection',):
76 http_response[header] = value
77 return http_response
showard37c7fe62008-07-24 16:35:02 +000078
79
80def handler500(request):
xixuanba232a32016-08-25 17:01:59 -070081 """Redirect to error website page.
82
83 @param request: the RPC request.
84 """
showard37c7fe62008-07-24 16:35:02 +000085 t = loader.get_template('500.html')
86 trace = traceback.format_exc()
87 context = Context({
88 'type': sys.exc_type,
89 'value': sys.exc_value,
90 'traceback': cgi.escape(trace)
91 })
92 return HttpResponseServerError(t.render(context))
Simran Basi71206ef2014-08-13 13:51:18 -070093
94
95def handle_file_upload(request):
96 """Handler for uploading files.
97
98 Saves the files to /tmp and returns the resulting paths on disk.
99
100 @param request: request containing the file data.
101
102 @returns HttpResponse: with the paths of the saved files.
103 """
104 if request.method == 'POST':
105 TEMPT_DIR = '/tmp/'
106 file_paths = []
107 for file_name, upload_file in request.FILES.iteritems():
108 file_path = os.path.join(
109 TEMPT_DIR, '_'.join([file_name, upload_file.name]))
110 with open(file_path, 'wb+') as destination:
111 for chunk in upload_file.chunks():
112 destination.write(chunk)
113 file_paths.append(file_path)
114 return HttpResponse(rpc_utils.prepare_for_serialization(file_paths))