- int later;
-
- 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);
- if (ws != NULL)
- hreq->upgrade = 1;
- }
- return 1;
-}
-
-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)
-{
- struct afb_diralias *da = data;
-
- if (hreq->method != afb_method_get) {
- afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
- return 1;
- }
-
- if (!afb_hreq_valid_tail(hreq)) {
- afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
- return 1;
- }
-
- return afb_hreq_reply_file(hreq, da->dirfd, &hreq->tail[1]);
-}
-
-int afb_hsrv_add_alias(AFB_session * session, const char *prefix, const char *alias, int priority)
-{
- struct afb_diralias *da;
- int dirfd;
-
- dirfd = open(alias, O_PATH|O_DIRECTORY);
- if (dirfd < 0) {
- /* TODO message */
- return 0;
- }
- da = malloc(sizeof *da);
- if (da != NULL) {
- da->alias = prefix;
- da->directory = alias;
- da->lendir = strlen(da->directory);
- da->dirfd = dirfd;
- if (afb_hsrv_add_handler(session, prefix, handle_alias, da, priority))
- return 1;
- free(da);
- }
- close(dirfd);
- return 0;
-}
-
-void afb_hsrv_reply_error(struct MHD_Connection *connection, unsigned int status)
-{
- char *buffer;
- int length;
- struct MHD_Response *response;
-
- length = asprintf(&buffer, "<html><body>error %u</body></html>", status);
- if (length > 0)
- response = MHD_create_response_from_buffer((unsigned)length, buffer, MHD_RESPMEM_MUST_FREE);
- else {
- buffer = "<html><body>error</body></html>";
- 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);