codeigniter-restserver icon indicating copy to clipboard operation
codeigniter-restserver copied to clipboard

Stop further execution when $this->response called

Open awalls opened this issue 9 years ago • 5 comments

Not sure if I am missing something here but in my code for example I have an additional authorise method in my api controller which performs some additional authorisation steps.

// check user agent string matches expected
if($this->agent->agent_string() !== 'MyUserAgent/1.0')
{            
    // send response
    $this->response(['status' => FALSE, 'error' => 'Invalid user agent'], REST_Controller::HTTP_UNAUTHORIZED);
}

// check API key and prefix match expected
if(!Accounts::authorise_access($this->_api_key, $this->_api_prefix))
{
   // send response
   $this->response(['status' => FALSE, 'error' => 'Invalid access credentials'], REST_Controller::HTTP_UNAUTHORIZED);
}

If my user agent is wrong I'd expect the process to terminate after I send the response however it'll continue on and try to run the authorise_access function and if this fails it results in multiple outputs being sent. Should the $this->response function in the REST_Controller not stop at that point rather than continuing on?

awalls avatar Jan 24 '17 14:01 awalls

I can see in the commit https://github.com/chriskacerguis/codeigniter-restserver/pull/748/commits/c13b5fb1bbd8e8a8a1d53e2a368f7bec4a2549f9 the option to cease and exit was removed. I'm not sure why as I can see any issue relating to this from the PR. I have a wrapper method in my base controller class to the response method in REST_Controller which simply does:

protected function my_response(Api_response $response)
{
    $data = [
        'status' => $response->status,
        'message' => $response->message,
        'txn_id' => $response->txn_id,
        'date' => $response->date,
        'body' => $response->body,
    ];

    // send response via REST server library
    $this->response($data, $response->http_status_code);
        
    // display the data and exit execution
    $this->output->_display();
    exit;
}

awalls avatar Jan 24 '17 14:01 awalls

Check this comment https://github.com/chriskacerguis/codeigniter-restserver/issues/753#issuecomment-273968372

farukgaric avatar Jan 25 '17 08:01 farukgaric

@farukgaric Had spotted that yesterday as well. See my solution above.

awalls avatar Jan 25 '17 11:01 awalls

  1. Exiting from code breaks the tests, that's why it was removed.
  2. The solution above is a nice temp fix that works for me as well.
  3. I submitted a fix today that prevents the call to the API endpoint if request validation failed. It doesn't "exit", just not calling the endpoint. I believe this is the wanted behaviour and I hope it gets merged soon.

Peace :)

benyaminshoham avatar Jan 25 '17 12:01 benyaminshoham

https://github.com/chriskacerguis/codeigniter-restserver/pull/759

would love any feedback.

benyaminshoham avatar Jan 25 '17 12:01 benyaminshoham