X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafb-hsrv.c;h=a83382519a502502c6e60d4ab0f2e0893f592849;hb=f262b0f726ac0577f40525038b779185f144873f;hp=efbb93560abebfc990f1422fdd4018be0757bf74;hpb=94014f46d4751492133f65b12f1dea1cfa36d021;p=src%2Fapp-framework-binder.git diff --git a/src/afb-hsrv.c b/src/afb-hsrv.c index efbb9356..a8338251 100644 --- a/src/afb-hsrv.c +++ b/src/afb-hsrv.c @@ -59,25 +59,16 @@ struct afb_hsrv { struct hsrv_handler *handlers; struct MHD_Daemon *httpd; struct upoll *upoll; + int in_run; char *cache_to; }; +static int global_reqids = 0; static void reply_error(struct MHD_Connection *connection, unsigned int status) { - char *buffer; - int length; - struct MHD_Response *response; - - length = asprintf(&buffer, "error %u", status); - if (length > 0) - response = MHD_create_response_from_buffer((unsigned)length, buffer, MHD_RESPMEM_MUST_FREE); - else { - buffer = "error"; - response = MHD_create_response_from_buffer(strlen(buffer), buffer, MHD_RESPMEM_PERSISTENT); - } - if (!MHD_queue_response(connection, status, response)) - fprintf(stderr, "Failed to reply error code %u", status); + struct MHD_Response *response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT); + MHD_queue_response(connection, status, response); MHD_destroy_response(response); } @@ -118,25 +109,34 @@ static int access_handler( hsrv = cls; hreq = *recordreq; if (hreq == NULL) { - /* create the request */ - hreq = calloc(1, sizeof *hreq); - if (hreq == NULL) - goto internal_error; - *recordreq = hreq; - /* get the method */ method = get_method(methodstr); method &= afb_method_get | afb_method_post; - if (method == afb_method_none) - goto bad_request; + if (method == afb_method_none) { + reply_error(connection, MHD_HTTP_BAD_REQUEST); + return MHD_YES; + } + + /* create the request */ + hreq = calloc(1, sizeof *hreq); + if (hreq == NULL) { + reply_error(connection, MHD_HTTP_INTERNAL_SERVER_ERROR); + return MHD_YES; + } /* init the request */ + hreq->hsrv = hsrv; hreq->cacheTimeout = hsrv->cache_to; + hreq->reqid = ++global_reqids; + hreq->scanned = 0; + hreq->suspended = 0; + hreq->replied = 0; hreq->connection = connection; hreq->method = method; hreq->version = version; hreq->tail = hreq->url = url; hreq->lentail = hreq->lenurl = strlen(url); + *recordreq = hreq; /* init the post processing */ if (method == afb_method_post) { @@ -147,10 +147,10 @@ static int access_handler( } else if (strcasestr(type, FORM_CONTENT) != NULL) { hreq->postform = MHD_create_post_processor (connection, 65500, postproc, hreq); if (hreq->postform == NULL) - goto internal_error; + afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR); return MHD_YES; } else if (strcasestr(type, JSON_CONTENT) == NULL) { - reply_error(connection, MHD_HTTP_UNSUPPORTED_MEDIA_TYPE); + afb_hreq_reply_error(hreq, MHD_HTTP_UNSUPPORTED_MEDIA_TYPE); return MHD_YES; } } @@ -159,30 +159,50 @@ static int access_handler( /* process further data */ if (*upload_data_size) { if (hreq->postform != NULL) { - if (!MHD_post_process (hreq->postform, upload_data, *upload_data_size)) - goto internal_error; + if (!MHD_post_process (hreq->postform, upload_data, *upload_data_size)) { + afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR); + return MHD_YES; + } } else { - if (!afb_hreq_post_add(hreq, NULL, upload_data, *upload_data_size)) - goto internal_error; + if (!afb_hreq_post_add(hreq, "", upload_data, *upload_data_size)) { + afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR); + return MHD_YES; + } } *upload_data_size = 0; - return MHD_YES; + return MHD_YES; } /* flush the data */ if (hreq->postform != NULL) { rc = MHD_destroy_post_processor(hreq->postform); hreq->postform = NULL; - if (rc == MHD_NO) - goto bad_request; + if (rc == MHD_NO) { + afb_hreq_reply_error(hreq, MHD_HTTP_BAD_REQUEST); + return MHD_YES; + } + } + + if (hreq->scanned != 0) { + if (hreq->replied == 0 && hreq->suspended == 0) { + MHD_suspend_connection (connection); + hreq->suspended = 1; + } + return MHD_YES; } /* search an handler for the request */ + hreq->scanned = 1; iter = hsrv->handlers; while (iter) { if (afb_hreq_unprefix(hreq, iter->prefix, iter->length)) { - if (iter->handler(hreq, iter->data)) + if (iter->handler(hreq, iter->data)) { + if (hreq->replied == 0 && hreq->suspended == 0) { + MHD_suspend_connection (connection); + hreq->suspended = 1; + } return MHD_YES; + } hreq->tail = hreq->url; hreq->lentail = hreq->lenurl; } @@ -192,14 +212,6 @@ static int access_handler( /* no handler */ afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND); return MHD_YES; - -bad_request: - reply_error(connection, MHD_HTTP_BAD_REQUEST); - return MHD_YES; - -internal_error: - reply_error(connection, MHD_HTTP_INTERNAL_SERVER_ERROR); - return MHD_YES; } /* Because of POST call multiple time requestApi we need to free POST handle here */ @@ -214,6 +226,21 @@ static void end_handler(void *cls, struct MHD_Connection *connection, void **rec afb_hreq_free(hreq); } +void run_micro_httpd(struct afb_hsrv *hsrv) +{ + if (hsrv->in_run != 0) + hsrv->in_run = 2; + else { + upoll_on_readable(hsrv->upoll, NULL); + do { + hsrv->in_run = 1; + MHD_run(hsrv->httpd); + } while(hsrv->in_run == 2); + hsrv->in_run = 0; + upoll_on_readable(hsrv->upoll, (void*)run_micro_httpd); + } +}; + static int new_client_handler(void *cls, const struct sockaddr *addr, socklen_t addrlen) { return MHD_YES; @@ -347,7 +374,7 @@ int afb_hsrv_start(struct afb_hsrv *hsrv, uint16_t port, unsigned int connection MHD_OPTION_END); /* options-end */ if (httpd == NULL) { - printf("Error: httpStart invalid httpd port: %d", (int)port); + fprintf(stderr, "Error: httpStart invalid httpd port: %d", (int)port); return 0; } @@ -358,13 +385,13 @@ int afb_hsrv_start(struct afb_hsrv *hsrv, uint16_t port, unsigned int connection return 0; } - upoll = upoll_open(info->listen_fd, httpd); + upoll = upoll_open(info->listen_fd, hsrv); if (upoll == NULL) { MHD_stop_daemon(httpd); fprintf(stderr, "Error: connection to upoll of httpd failed"); return 0; } - upoll_on_readable(upoll, (void*)MHD_run); + upoll_on_readable(upoll, (void*)run_micro_httpd); hsrv->httpd = httpd; hsrv->upoll = upoll;