X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fhttp-svc.c;h=c767de719972972f5d6523c74abab07caf950c1f;hb=11d36a9f7e16aa9992835f8ce06f0e1e5297b131;hp=e8ccfe8329bf8c68d75b3585595ef5c5e3b55c48;hpb=20f5ff02e455580c6e7129479f3328787b3333ff;p=src%2Fapp-framework-binder.git diff --git a/src/http-svc.c b/src/http-svc.c index e8ccfe83..c767de71 100644 --- a/src/http-svc.c +++ b/src/http-svc.c @@ -16,19 +16,27 @@ */ #define _GNU_SOURCE -#include + +#include +#include #include #include +#include #include -#include "../include/local-def.h" +#include + +#include "local-def.h" #include "afb-method.h" #include "afb-hreq.h" #include "afb-websock.h" +#include "afb-apis.h" +#include "afb-req-itf.h" #define JSON_CONTENT "application/json" #define FORM_CONTENT MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA + struct afb_hsrv_handler { struct afb_hsrv_handler *next; const char *prefix; @@ -129,21 +137,42 @@ int afb_hsrv_add_handler( return 1; } -static int relay_to_doRestApi(struct afb_hreq *hreq, void *data) +static int afb_hreq_websocket_switch(struct afb_hreq *hreq, void *data) { int later; - if (hreq->lentail == 0 && afb_websock_check(hreq, &later)) { - if (!later) { - struct afb_websock *ws = afb_websock_create(hreq->connection); + + afb_hreq_context(hreq); + if (hreq->lentail != 0 || !afb_websock_check(hreq, &later)) + return 0; + + if (!later) { + struct afb_websock *ws = afb_websock_create(hreq->connection); + if (ws == NULL) { + /* TODO */ + } else { + /* TODO */ } - return 1; } + return 1; +} -return 0; -/* - return doRestApi(hreq->connection, hreq->session, &hreq->tail[1], get_method_name(hreq->method), - post->upload_data, post->upload_data_size, (void **)hreq->recorder); -*/ +static int afb_hreq_rest_api(struct afb_hreq *hreq, void *data) +{ + const char *api, *verb; + size_t lenapi, lenverb; + struct AFB_clientCtx *context; + + api = &hreq->tail[strspn(hreq->tail, "/")]; + lenapi = strcspn(api, "/"); + verb = &api[lenapi]; + verb = &verb[strspn(verb, "/")]; + lenverb = strcspn(verb, "/"); + + if (!(*api && *verb && lenapi && lenverb)) + return 0; + + context = afb_hreq_context(hreq); + return afb_apis_handle(afb_hreq_to_req(hreq), context, api, lenapi, verb, lenverb); } static int handle_alias(struct afb_hreq *hreq, void *data) @@ -232,6 +261,7 @@ static int access_handler( size_t *upload_data_size, void **recordreq) { + int rc; struct afb_hreq *hreq; enum afb_method method; AFB_session *session; @@ -250,10 +280,8 @@ static int access_handler( /* get the method */ method = get_method(methodstr); method &= afb_method_get | afb_method_post; - if (method == afb_method_none) { - afb_hsrv_reply_error(connection, MHD_HTTP_BAD_REQUEST); - return MHD_YES; - } + if (method == afb_method_none) + goto bad_request; /* init the request */ hreq->session = cls; @@ -295,6 +323,12 @@ static int access_handler( /* flush the data */ afb_hreq_post_end(hreq); + if (hreq->postform != NULL) { + rc = MHD_destroy_post_processor(hreq->postform); + hreq->postform = NULL; + if (rc == MHD_NO) + goto bad_request; + } /* search an handler for the request */ iter = session->handlers; @@ -312,6 +346,10 @@ static int access_handler( afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND); return MHD_YES; +bad_request: + afb_hsrv_reply_error(connection, MHD_HTTP_BAD_REQUEST); + return MHD_YES; + internal_error: afb_hsrv_reply_error(connection, MHD_HTTP_INTERNAL_SERVER_ERROR); return MHD_YES; @@ -321,16 +359,11 @@ internal_error: static void end_handler(void *cls, struct MHD_Connection *connection, void **recordreq, enum MHD_RequestTerminationCode toe) { - AFB_session *session; struct afb_hreq *hreq; - session = cls; hreq = *recordreq; - if (hreq != NULL) { - if (hreq->postform != NULL) - MHD_destroy_post_processor(hreq->postform); - } + afb_hreq_free(hreq); } static int new_client_handler(void *cls, const struct sockaddr *addr, socklen_t addrlen) @@ -372,7 +405,10 @@ static int my_default_init(AFB_session * session) { int idx; - if (!afb_hsrv_add_handler(session, session->config->rootapi, relay_to_doRestApi, NULL, 1)) + if (!afb_hsrv_add_handler(session, session->config->rootapi, afb_hreq_websocket_switch, NULL, 20)) + return 0; + + if (!afb_hsrv_add_handler(session, session->config->rootapi, afb_hreq_rest_api, NULL, 10)) return 0; for (idx = 0; session->config->aliasdir[idx].url != NULL; idx++)