|
|
@@ -18,17 +18,13 @@ namespace Astralis {
|
|
|
this.response_contexts = new HashSet<ResponseContext>();
|
|
|
}
|
|
|
|
|
|
- private static int request_counter = 0;
|
|
|
-
|
|
|
private int access_handler (Connection connection, string? url, string? method, string? version, string? upload_data, size_t* upload_data_size, void** con_cls) {
|
|
|
|
|
|
// On initial call for request, simply set up the context
|
|
|
if (con_cls[0] == null) {
|
|
|
var context = new RequestContext();
|
|
|
- context.id = AtomicInt.add(ref request_counter, 1);
|
|
|
request_contexts.add(context);
|
|
|
con_cls[0] = (void*) context;
|
|
|
- debug(@"[REQ-$(context.id)] access_handler() FIRST CALL - url=$(url) method=$(method)");
|
|
|
return Result.YES;
|
|
|
}
|
|
|
|
|
|
@@ -37,8 +33,6 @@ namespace Astralis {
|
|
|
|
|
|
// On the second call we populate the `HttpRequest` object and begin the handler
|
|
|
if (context.handler_context == null) {
|
|
|
- debug(@"[REQ-$(context.id)] access_handler() SECOND CALL - starting handler");
|
|
|
-
|
|
|
// Extract all headers from the connection (case-insensitive keys)
|
|
|
var headers = new Catalogue<string, string>(case_insensitive_hash, case_insensitive_equal);
|
|
|
var headers_collector = new KeyValueCollector(headers);
|
|
|
@@ -73,17 +67,13 @@ namespace Astralis {
|
|
|
|
|
|
// Kick off the handler
|
|
|
pipeline.run.begin(http_context, (obj, res) => {
|
|
|
- debug(@"[REQ-$(context.id)] handler callback fired");
|
|
|
try {
|
|
|
context.handler_result = pipeline.run.end(res);
|
|
|
- debug(@"[REQ-$(context.id)] handler completed successfully");
|
|
|
}
|
|
|
catch(Error e) {
|
|
|
context.handler_error = e;
|
|
|
- debug(@"[REQ-$(context.id)] handler error: $(e.message)");
|
|
|
}
|
|
|
if(context.handler_finished()) {
|
|
|
- debug(@"[REQ-$(context.id)] handler_finished()=true, resuming connection");
|
|
|
// Just resume - access_handler will be re-entered and handle the response
|
|
|
MHD.resume_connection(connection);
|
|
|
}
|
|
|
@@ -92,7 +82,6 @@ namespace Astralis {
|
|
|
|
|
|
// On the second, and all subsequent calls - read the request body:
|
|
|
if (upload_data_size[0] != 0) {
|
|
|
- debug(@"[REQ-$(context.id)] access_handler() receiving $(upload_data_size[0]) bytes body data");
|
|
|
var data = new uint8[upload_data_size[0]];
|
|
|
Memory.copy(data, upload_data, upload_data_size[0]);
|
|
|
context.request_body.write(data);
|
|
|
@@ -101,13 +90,10 @@ namespace Astralis {
|
|
|
}
|
|
|
// End of request body data
|
|
|
else {
|
|
|
- debug(@"[REQ-$(context.id)] access_handler() no more body data");
|
|
|
context.request_body.complete();
|
|
|
if(context.request_reception_finished()) {
|
|
|
- debug(@"[REQ-$(context.id)] request_reception_finished()=true, calling respond()");
|
|
|
return respond(connection, context);
|
|
|
}
|
|
|
- debug(@"[REQ-$(context.id)] SUSPENDING connection (waiting for handler)");
|
|
|
MHD.suspend_connection(connection);
|
|
|
return Result.YES;
|
|
|
}
|
|
|
@@ -136,18 +122,14 @@ namespace Astralis {
|
|
|
}
|
|
|
|
|
|
private Result respond(Connection connection, RequestContext context) {
|
|
|
- debug(@"[REQ-$(context.id)] respond() called");
|
|
|
var result = Result.NO;
|
|
|
if(context.handler_error != null) {
|
|
|
- debug(@"[REQ-$(context.id)] respond() handling error");
|
|
|
result = handle_error(context.handler_error, connection, context);
|
|
|
}
|
|
|
else if(context.handler_result != null) {
|
|
|
- debug(@"[REQ-$(context.id)] respond() sending result");
|
|
|
result = send_result(connection, context.handler_result, context);
|
|
|
}
|
|
|
request_contexts.remove(context);
|
|
|
- debug(@"[REQ-$(context.id)] respond() returning %d", (int)result);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
@@ -156,9 +138,6 @@ namespace Astralis {
|
|
|
uint64 size = result.content_length ?? -1;
|
|
|
var response_context = new ResponseContext(this, connection, result);
|
|
|
response_contexts.add(response_context);
|
|
|
-
|
|
|
- int req_id = request_context != null ? request_context.id : -1;
|
|
|
- debug(@"[REQ-$(req_id)] send_result() creating RC-$(response_context.id), content_length=$(size)");
|
|
|
|
|
|
// Link the ResponseContext to the RequestContext for disconnect notification
|
|
|
if (request_context != null) {
|
|
|
@@ -170,16 +149,12 @@ namespace Astralis {
|
|
|
1048576,
|
|
|
(cls, pos, buf, max) => {
|
|
|
var ctx = (ResponseContext) cls;
|
|
|
- debug(@"[RC-$(ctx.id)] content_reader_callback called, max=$(max)");
|
|
|
var bytes_read = ctx.body_output.read_chunk(buf, max);
|
|
|
- debug(@"[RC-$(ctx.id)] content_reader_callback read $(bytes_read) bytes");
|
|
|
if(bytes_read == 0) {
|
|
|
if(!ctx.send_body_finished) {
|
|
|
- debug(@"[RC-$(ctx.id)] content_reader_callback: no data, body not finished - suspending");
|
|
|
ctx.suspend_connection();
|
|
|
}
|
|
|
else if(ctx.send_body_error == null) {
|
|
|
- debug(@"[RC-$(ctx.id)] content_reader_callback: body finished successfully - END_OF_STREAM");
|
|
|
ctx.server.response_contexts.remove(ctx);
|
|
|
return CONTENT_READER_END_OF_STREAM;
|
|
|
}
|
|
|
@@ -204,7 +179,6 @@ namespace Astralis {
|
|
|
if(res != MHD.Result.YES) {
|
|
|
printerr("Astralis Internal Error: Unable to queue response\n");
|
|
|
}
|
|
|
- debug(@"[REQ-$(req_id)] send_result() queued response, result=%d", (int)res);
|
|
|
return res;
|
|
|
}
|
|
|
|