X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafb-hsrv.c;h=afe865f78afaec67024a522425ebc003c0e8555f;hb=c0453c34a58aac8150300ab829149a0ca4d9e5ee;hp=a358cbd77685d95a9ca46d77e38d52a060c26e32;hpb=13549775092afa9215de8468e34f6d194c2fd8db;p=src%2Fapp-framework-binder.git diff --git a/src/afb-hsrv.c b/src/afb-hsrv.c index a358cbd7..afe865f7 100644 --- a/src/afb-hsrv.c +++ b/src/afb-hsrv.c @@ -29,6 +29,7 @@ #include "local-def.h" #include "afb-method.h" #include "afb-hreq.h" +#include "afb-hsrv.h" #include "afb-websock.h" #include "afb-apis.h" #include "afb-req-itf.h" @@ -55,34 +56,13 @@ struct afb_diralias { int dirfd; }; -static struct upoll *upoll = NULL; - -int afb_hreq_one_page_api_redirect( - struct afb_hreq *hreq, - void *data) -{ - size_t plen; - char *url; +struct afb_hsrv { + struct MHD_Daemon *httpd; + struct afb_hsrv_handler *handlers; + struct upoll *upoll; +}; - if (hreq->lentail >= 2 && hreq->tail[1] == '#') - return 0; - /* - * Here we have for example: - * url = "/pre/dir/page" lenurl = 13 - * tail = "/dir/page" lentail = 9 - * - * We will produce "/pre/#!dir/page" - * - * Let compute plen that include the / at end (for "/pre/") - */ - plen = hreq->lenurl - hreq->lentail + 1; - url = alloca(hreq->lenurl + 3); - memcpy(url, hreq->url, plen); - url[plen++] = '#'; - url[plen++] = '!'; - memcpy(&url[plen], &hreq->tail[1], hreq->lentail); - return afb_hreq_redirect_to(hreq, url); -} +static struct upoll *upoll = NULL; static struct afb_hsrv_handler *new_handler( struct afb_hsrv_handler *head, @@ -141,6 +121,33 @@ int afb_hsrv_add_handler( return 1; } +int afb_hreq_one_page_api_redirect( + struct afb_hreq *hreq, + void *data) +{ + size_t plen; + char *url; + + if (hreq->lentail >= 2 && hreq->tail[1] == '#') + return 0; + /* + * Here we have for example: + * url = "/pre/dir/page" lenurl = 13 + * tail = "/dir/page" lentail = 9 + * + * We will produce "/pre/#!dir/page" + * + * Let compute plen that include the / at end (for "/pre/") + */ + plen = hreq->lenurl - hreq->lentail + 1; + url = alloca(hreq->lenurl + 3); + memcpy(url, hreq->url, plen); + url[plen++] = '#'; + url[plen++] = '!'; + memcpy(&url[plen], &hreq->tail[1], hreq->lentail); + return afb_hreq_redirect_to(hreq, url); +} + static int afb_hreq_websocket_switch(struct afb_hreq *hreq, void *data) { int later; @@ -150,12 +157,9 @@ static int afb_hreq_websocket_switch(struct afb_hreq *hreq, void *data) return 0; if (!later) { - struct afb_websock *ws = afb_websock_create(hreq->connection); - if (ws == NULL) { - /* TODO */ - } else { - /* TODO */ - } + struct afb_websock *ws = afb_websock_create(hreq); + if (ws != NULL) + hreq->upgrade = 1; } return 1; } @@ -305,6 +309,7 @@ static int access_handler( hreq->postform = MHD_create_post_processor (connection, 65500, postproc, hreq); if (hreq->postform == NULL) goto internal_error; + return MHD_YES; } else if (strcasestr(type, JSON_CONTENT) == NULL) { afb_hsrv_reply_error(connection, MHD_HTTP_UNSUPPORTED_MEDIA_TYPE); return MHD_YES; @@ -366,7 +371,8 @@ static void end_handler(void *cls, struct MHD_Connection *connection, void **rec struct afb_hreq *hreq; hreq = *recordreq; - + if (hreq->upgrade) + MHD_suspend_connection (connection); afb_hreq_free(hreq); } @@ -375,36 +381,6 @@ static int new_client_handler(void *cls, const struct sockaddr *addr, socklen_t return MHD_YES; } -#if defined(USE_MAGIC_MIME_TYPE) - -#if !defined(MAGIC_DB) -#define MAGIC_DB "/usr/share/misc/magic.mgc" -#endif - -static int init_lib_magic (AFB_session *session) -{ - /* MAGIC_MIME tells magic to return a mime of the file, but you can specify different things */ - if (verbosity) - printf("Loading mimetype default magic database\n"); - - session->magic = magic_open(MAGIC_MIME_TYPE); - if (session->magic == NULL) { - fprintf(stderr,"ERROR: unable to initialize magic library\n"); - return 0; - } - - /* Warning: should not use NULL for DB [libmagic bug wont pass efence check] */ - if (magic_load(session->magic, MAGIC_DB) != 0) { - fprintf(stderr,"cannot load magic database - %s\n", magic_error(session->magic)); - magic_close(session->magic); - session->magic = NULL; - return 0; - } - - return 1; -} -#endif - static int my_default_init(AFB_session * session) { int idx; @@ -425,11 +401,6 @@ static int my_default_init(AFB_session * session) if (!afb_hsrv_add_handler(session, session->config->rootbase, afb_hreq_one_page_api_redirect, NULL, -20)) return 0; -#if defined(USE_MAGIC_MIME_TYPE) - /*TBD open libmagic cache [fail to pass EFENCE check (allocating 0 bytes)] */ - init_lib_magic (session); -#endif - return 1; }