Skip to content
Snippets Groups Projects
Commit cc1f1ecd authored by Markus Ottensmann's avatar Markus Ottensmann Committed by Per Hedbor
Browse files

Add optional argument error_callback to attach_fd().

Add an optional argument error_callback to Request->attach_fd().
The argument is a function, which is called when parse_post() cannot parse
the request data. That may happen if a server receives malformed mime data.
The error_callback is called with two arguments:
- the Protocols.HTTP.Server.Request instance,
- the error-description that was returned by catch.
A typical way to handle such an error is to send a "400 Bad Request" response:

  void my_error_callback(Protocols.HTTP.Server.Request req, mixed error)
  {
    req->response_and_finish(([ "error": 400,
                                "type": "text/plain",
                                "data": "Bad Request\n" ]));
  }
parent 029be106
No related branches found
No related tags found
No related merge requests found
...@@ -104,15 +104,18 @@ int send_timeout_delay=180; ...@@ -104,15 +104,18 @@ int send_timeout_delay=180;
int connection_timeout_delay=180; int connection_timeout_delay=180;
function(this_program:void) request_callback; function(this_program:void) request_callback;
function(this_program,array:void) error_callback;
void attach_fd(Stdio.File _fd, Port server, void attach_fd(Stdio.File _fd, Port server,
function(this_program:void) _request_callback, function(this_program:void) _request_callback,
void|string already_data) void|string already_data,
void|function(this_program,array:void) _error_callback)
{ {
my_fd=_fd; my_fd=_fd;
server_port=server; server_port=server;
headerparser = .HeaderParser(); headerparser = .HeaderParser();
request_callback=_request_callback; request_callback=_request_callback;
error_callback = _error_callback;
my_fd->set_nonblocking(read_cb,0,close_cb); my_fd->set_nonblocking(read_cb,0,close_cb);
...@@ -430,15 +433,19 @@ protected void finalize() ...@@ -430,15 +433,19 @@ protected void finalize()
{ {
my_fd->set_blocking(); my_fd->set_blocking();
flatten_headers(); flatten_headers();
parse_post(); if (array err = catch {parse_post();})
{
if (error_callback) error_callback(this, err);
}
else
{
if (request_headers->cookie) if (request_headers->cookie)
foreach (request_headers->cookie/";";;string cookie) foreach (request_headers->cookie/";";;string cookie)
if (sscanf(String.trim_whites(cookie),"%s=%s",string a,string b)==2) if (sscanf(String.trim_whites(cookie),"%s=%s",string a,string b)==2)
cookies[a]=b; cookies[a]=b;
request_callback(this); request_callback(this);
} }
}
// Adds incoming data to raw and buf. Once content-length or // Adds incoming data to raw and buf. Once content-length or
// max_request_size data has been received, finalize is called. // max_request_size data has been received, finalize is called.
...@@ -742,7 +749,7 @@ void finish(int clean) ...@@ -742,7 +749,7 @@ void finish(int clean)
// create new request // create new request
this_program r=this_program(); this_program r=this_program();
r->attach_fd(my_fd,server_port,request_callback,buf); r->attach_fd(my_fd,server_port,request_callback,buf,error_callback);
my_fd=0; // and drop this object my_fd=0; // and drop this object
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment