Skip to content

Commit e0388bb

Browse files
committed
Created a new action which will be used to serve all requests passed to the builder service.
The new action serves both compilation and library-info-fetching requests. Depending on the request type ('compiler' or 'library'), the main action will call the respective function and the pass the response to the caller. Every request should have the correct API settings (auth-key and API version) and must include the must include the type of request in the body. Every other combination of these will be rejected.
1 parent a99e2f4 commit e0388bb

File tree

2 files changed

+71
-20
lines changed

2 files changed

+71
-20
lines changed

Symfony/src/Codebender/ApiBundle/Controller/DefaultController.php

Lines changed: 69 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,18 @@ public function statusAction()
2323
}
2424

2525
/**
26-
* compile action
26+
* Gets a request for compilation or library fetching (depends on the 'type' field of the request)
27+
* and passes the request to either the compiler or the library manager.
2728
*
28-
* @return Response Response intance.
29+
* Includes several checks in order to ensure the validity of the data provided as well
30+
* as authentication.
2931
*
32+
* @param $auth_key
33+
* @param $version
34+
* @return Response
3035
*/
31-
public function compileAction($auth_key, $version)
36+
public function handleRequestAction($auth_key, $version)
3237
{
33-
3438
if ($auth_key !== $this->container->getParameter('auth_key'))
3539
{
3640
return new Response(json_encode(array("success" => false, "message" => "Invalid authorization key.")));
@@ -49,18 +53,48 @@ public function compileAction($auth_key, $version)
4953

5054
$contents = json_decode($request, true);
5155

56+
if ($contents === NULL)
57+
{
58+
return new Response(json_encode(array("success" => false, "message" => "Wrong data.")));
59+
}
60+
61+
if (!array_key_exists("data", $contents))
62+
{
63+
return new Response(json_encode(array("success" => false, "message" => "Insufficient data provided.")));
64+
}
65+
66+
if ($contents["type"] == "compiler") {
67+
return new Response($this->compile($contents["data"]));
68+
}
69+
70+
if ($contents["type"] == "library")
71+
{
72+
return new Response($this->getLibraryInfo(json_encode($contents["data"])));
73+
}
74+
75+
return new Response(json_encode(array("success" => false, "message" => "Invalid request type (can handle only 'compiler' or 'library' requests)")));
76+
}
77+
78+
/**
79+
* Gets the data from the handleRequestAction and proceeds with the compilation
80+
*
81+
* @param $contents
82+
* @return Response
83+
*/
84+
protected function compile($contents)
85+
{
5286
$apihandler = $this->get('codebender_api.handler');
5387

5488
$files = $contents["files"];
5589

5690
$this->checkForUserProject($files);
5791

58-
$userlibs = array();
92+
$userLibs = array();
5993

6094
if (array_key_exists('libraries', $contents))
61-
$userlibs = $contents['libraries'];
95+
$userLibs = $contents['libraries'];
6296

63-
$parsedLibs = $this->checkHeaders($files, $userlibs);
97+
$parsedLibs = $this->checkHeaders($files, $userLibs);
6498

6599
$contents["libraries"] = $parsedLibs['libraries'];
66100

@@ -70,18 +104,37 @@ public function compileAction($auth_key, $version)
70104
$data = $apihandler->post_raw_data($this->container->getParameter('compiler'), $request_content);
71105

72106
$decoded = json_decode($data, true);
73-
if ($decoded == null)
107+
if ($decoded === NULL)
74108
{
75-
return new Response(json_encode(array("success" => false, "message"=> "Failed to get compiler response.")));
109+
return json_encode(array("success" => false, "message"=> "Failed to get compiler response."));
76110
}
77111

78112
if ($decoded["success"] === false && !array_key_exists("step", $decoded))
113+
{
79114
$decoded["step"] = "unknown";
115+
}
80116

81117
unset($parsedLibs['libraries']);
82118
$decoded['additionalCode'] = $parsedLibs;
83119

84-
return new Response(json_encode($decoded));
120+
return json_encode($decoded);
121+
}
122+
123+
/**
124+
* Gets a request for library information from the handleRequestAction and makes the
125+
* actual call to the library manager.
126+
* The data must be already json encoded before passing them to this function.
127+
*
128+
* @param $data
129+
* @return mixed
130+
*/
131+
protected function getLibraryInfo($data)
132+
{
133+
$handler = $this->get('codebender_api.handler');
134+
135+
$libraryManager = $this->container->getParameter('library');
136+
137+
return $handler->post_raw_data($libraryManager, $data);
85138
}
86139

87140
/**
@@ -91,7 +144,7 @@ public function compileAction($auth_key, $version)
91144
*
92145
* @return array
93146
*/
94-
protected function checkHeaders($files, array $userlibs)
147+
protected function checkHeaders($files, $userLibs)
95148
{
96149
$apiHandler = $this->get('codebender_api.handler');
97150

@@ -100,17 +153,14 @@ protected function checkHeaders($files, array $userlibs)
100153
// declare arrays
101154
$libraries = $notFoundHeaders = $foundHeaders = $fetchedLibs = $providedLibs = array();
102155

103-
$providedLibs = array_keys($userlibs);
104-
105-
// get library manager url
106-
$libmanager_url = $this->container->getParameter('library');
156+
$providedLibs = array_keys($userLibs);
107157

108-
$libraries = $userlibs;
158+
$libraries = $userLibs;
109159

110160
foreach ($headers as $header) {
111161

112162
$exists_in_request = false;
113-
foreach ($userlibs as $lib){
163+
foreach ($userLibs as $lib){
114164
foreach ($lib as $libcontent){
115165
if ($libcontent["filename"] == $header.".h") {
116166
$exists_in_request = true;
@@ -120,7 +170,8 @@ protected function checkHeaders($files, array $userlibs)
120170
}
121171
if ($exists_in_request === false) {
122172

123-
$data = $apiHandler->get($libmanager_url . "/fetch?library=" . urlencode($header));
173+
$requestContent = array("type" => "fetch", "library" => $header);
174+
$data = $this->getLibraryInfo(json_encode($requestContent));
124175
$data = json_decode($data, true);
125176

126177
if ($data["success"]) {

Symfony/src/Codebender/ApiBundle/Resources/config/routing.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ CodebenderApiBundle_status_check:
33
defaults: { _controller: CodebenderApiBundle:Default:status }
44

55
CodebenderApiBundle_compile:
6-
pattern: /{auth_key}/{version}/compile
7-
defaults: { _controller: CodebenderApiBundle:Default:compile }
6+
pattern: /{auth_key}/{version}
7+
defaults: { _controller: CodebenderApiBundle:Default:handleRequest }
88
requirements:
99
_method: POST

0 commit comments

Comments
 (0)