/*
 *
  ***** BEGIN LICENSE BLOCK *****
 
  Copyright (C) 2009-2019 Olof Hagsand
  Copyright (C) 2020-2022 Olof Hagsand and Rubicon Communications, LLC(Netgate)

  This file is part of CLIXON.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

  Alternatively, the contents of this file may be used under the terms of
  the GNU General Public License Version 3 or later (the "GPL"),
  in which case the provisions of the GPL are applicable instead
  of those above. If you wish to allow use of your version of this file only
  under the terms of the GPL, and not to allow others to
  use your version of this file under the terms of Apache License version 2, 
  indicate your decision by deleting the provisions above and replace them with
  the  notice and other provisions required by the GPL. If you do not delete
  the provisions above, a recipient may use your version of this file under
  the terms of any one of the Apache License version 2 or the GPL.

  ***** END LICENSE BLOCK *****
  
  Restconf event stream implementation. 
  See RFC 8040  RESTCONF Protocol
  Sections 3.8, 6, 9.3

  RFC8040:
   A RESTCONF server MAY send the "retry" field, and if it does, RESTCONF
   clients SHOULD use it.  A RESTCONF server SHOULD NOT send the "event" 
   or "id" fields, as there are no meaningful values. RESTCONF
   servers that do not send the "id" field also do not need to support
   the HTTP header field "Last-Event-ID"

   The RESTCONF client can then use this URL value to start monitoring
   the event stream:

      GET /streams/NETCONF HTTP/1.1
      Host: example.com
      Accept: text/event-stream
      Cache-Control: no-cache
      Connection: keep-alive

   The server MAY support the "start-time", "stop-time", and "filter"
   query parameters, defined in Section 4.8.  Refer to Appendix B.3.6
   for filter parameter examples.

   * Note that this implementation includes some hardcoded things for FCGI.
   * These are:
   * - req->listen_sock is used to register incoming fd events from (nginx) fcgi server
   * - The stream_child struct copies the FCGX_Request by value, so FCGX_Free() can be called 
   *   asynchronously
   * - In the forked variant, FCGX_Finish_r() and  FCGX_Free() are called (minor)
 */

#ifdef HAVE_CONFIG_H
#include "clixon_config.h" /* generated by config & autoconf */
#endif

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <syslog.h>
#include <fcntl.h>
#include <time.h>
#include <limits.h>

#include <signal.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <libgen.h>

/* cligen */
#include <cligen/cligen.h>

/* clixon */
#include <clixon/clixon.h>

#include <fcgiapp.h> /* Need to be after clixon_xml.h due to attribute format */

#include "restconf_lib.h"
#include "restconf_handle.h"
#include "restconf_api.h"
#include "restconf_err.h"
#include "restconf_stream.h"
#include "restconf_lib.h"
#include "restconf_stream.h"

/*
 * Constants
 */
/* Enable for forking stream subscription loop. 
 * Disable to get single threading but blocking on streams
 * XXX: Integrate with top-level events
 */
#undef STREAM_FORK

/* Keep track of children - when they exit - their FCGX handle needs to be 
 * freed with  FCGX_Free(&rbk, 0);
 */
struct stream_child{
    qelem_t              sc_q;   /* queue header */
    int                  sc_pid; /* Child process id */
    FCGX_Request         sc_r;   /* FCGI stream data. XXX this is by value */
};
/* Linked list of children
 * @note could hang STREAM_CHILD list on clicon handle instead.
 */
static struct stream_child *STREAM_CHILD = NULL;

static int backend_eof = 0;

/*! Find restconf child using PID and cleanup FCGI Request data
 *
 * For forked, called on SIGCHILD
 * @param[in]  h   Clixon handle
 * @param[in]  pid Process id of child
 * @note could hang STREAM_CHILD list on clicon handle instead.
 */
int
stream_child_free(clixon_handle h,
                  int           pid)
{
    struct stream_child *sc;

    if ((sc = STREAM_CHILD) != NULL){
        do {
            if (pid == sc->sc_pid){
                DELQ(sc, STREAM_CHILD, struct stream_child *);
                FCGX_Free(&sc->sc_r, 0); /* XXX pointer to actual copied struct */
                free(sc);
                goto done;
            }
            sc = NEXTQ(struct stream_child *, sc);
        } while (sc && sc != STREAM_CHILD);
    }
 done:
    return 0;
}

/*! Free all streams
 *
 * Typically called on restconf exit
 */
int
stream_child_freeall(clixon_handle h)
{
    struct stream_child *sc;

    while ((sc = STREAM_CHILD) != NULL){
        DELQ(sc, STREAM_CHILD, struct stream_child *);
        FCGX_Free(&sc->sc_r, 1); /* XXX pointer to actual copied struct */
        free(sc);
    }
    return 0;
}

/*! Callback when stream notifications arrive from backend
 *
 * @param[in]  s    Socket
 * @param[in]  req  Generic Www handle (can be part of clixon handle)
 * @retval     0    OK
 * @retval    -1    Error
 * @see netconf_notification_cb
 */
static int
stream_fcgi_backend_cb(int   s,
                       void *arg)
{
    int           retval = -1;
    FCGX_Request *req = (FCGX_Request *)arg;
    int           eof;
    cxobj        *xtop = NULL; /* top xml */
    cxobj        *xn;        /* notification xml */
    cbuf         *cb = NULL;
    cbuf         *cbmsg = NULL;
    int           pretty = 0; /* XXX should be via arg */
    int           ret;

    clixon_debug(CLIXON_DBG_STREAM|CLIXON_DBG_DETAIL, "");
    if (clixon_msg_rcv11(s, NULL, 0, &cbmsg, &eof) < 0)
        goto done;
    clixon_debug(CLIXON_DBG_STREAM, "%s", cbuf_get(cbmsg)); // Also MSG
    /* handle close from remote end: this will exit the client */
    if (eof){
        clixon_debug(CLIXON_DBG_STREAM, "eof, terminate stream");
        backend_eof = 1;
        clixon_exit_set(1); // local timeout
        FCGX_FPrintF(req->out, "SHUTDOWN\r\n");
        FCGX_FPrintF(req->out, "\r\n");
        FCGX_FFlush(req->out);
        goto ok;
    }
    if ((ret = clixon_xml_parse_string(cbuf_get(cbmsg), YB_NONE, NULL, &xtop, NULL)) < 0)
        goto done;
    if (ret == 0){
        clixon_err(OE_XML, EFAULT, "Invalid notification");
        goto done;
    }
    /* create event */
    if ((cb = cbuf_new()) == NULL){
        clixon_err(OE_PLUGIN, errno, "cbuf_new");
        goto done;
    }
    if ((xn = xpath_first(xtop, NULL, "notification")) == NULL)
        goto ok;
#ifdef notused
    xt = xpath_first(xn, NULL, "eventTime");
    if ((xe = xpath_first(xn, NULL, "event")) == NULL) /* event can depend on yang? */
        goto ok;

    if (xt)
        FCGX_FPrintF(r->out, "M#id: %s\r\n", xml_body(xt));
    else{ /* XXX */
        gettimeofday(&tv, NULL);
        FCGX_FPrintF(r->out, "M#id: %02d:0\r\n", tv.tv_sec);
    }
#endif
    if (clixon_xml2cbuf(cb, xn, 0, pretty, NULL, -1, 0) < 0)
        goto done;
    FCGX_FPrintF(req->out, "data: %s\r\n", cbuf_get(cb));
    FCGX_FPrintF(req->out, "\r\n");
    FCGX_FFlush(req->out);
 ok:
    retval = 0;
 done:
    clixon_debug(CLIXON_DBG_STREAM|CLIXON_DBG_DETAIL, "retval: %d", retval);
    if (xtop != NULL)
        xml_free(xtop);
    if (cbmsg)
        cbuf_free(cbmsg);
    if (cb)
        cbuf_free(cb);
    return retval;
}

/*! Listen sock callback (from proxy?)
 *
 * @param[in]  s    Socket
 * @param[in]  req  Generic Www handle (can be part of clixon handle)
 */
static int
stream_fcgi_uplink_cb(int   s,
                      void *arg)
{
    FCGX_Request      *r = (FCGX_Request *)arg;

    clixon_debug(CLIXON_DBG_STREAM, "");
    if (FCGX_GetError(r->out) != 0){ /* break loop */
        clixon_debug(CLIXON_DBG_STREAM, "FCGX_GetError upstream");
        clixon_exit_set(1);
    }
    return 0;
}

/*! Timeout of notification stream, check fcgi socket
 */
static int
fcgi_stream_timeout(int   s,
                    void *arg)
{
    struct timeval t;
    FCGX_Request  *r = (FCGX_Request *)arg;

    clixon_debug(CLIXON_DBG_STREAM|CLIXON_DBG_DETAIL, "");
    if (FCGX_GetError(r->out) != 0){ /* break loop */
        clixon_debug(CLIXON_DBG_STREAM, "FCGX_GetError upstream");
        clixon_exit_set(1);
    }
    else{
        gettimeofday(&t, NULL);
        t.tv_sec++;
        clixon_event_reg_timeout(t, fcgi_stream_timeout, arg, "Stream timeout");
    }
    return 0;
}

/*! Timeout of notification stream, limit lifetime, for debug
 */
static int
stream_timeout_end(int   s,
                   void *arg)
{
    clixon_debug(CLIXON_DBG_STREAM, "Terminate stream");
    clixon_exit_set(1); // XXX This is local eventloop see below, not global
    return 0;
}

/*! FCGI specific code for setting up stream sockets
 *
 * @param[in]  h       Clixon handle
 * @param[in]  req     Generic Www handle (can be part of clixon handle)
 * @param[in]  timeout Stream timeout
 * @param[in]  besock  Socket to backend
 * @param[out] finish  Set to zero, if request should not be finnished by upper layer
 * @retval     0       OK
 * @retval    -1       Error
 * Consider moving timeout and backend sock to generic code
 */
int
stream_sockets_setup(clixon_handle h,
                     void         *req,
                     int           timeout,
                     int           besock,
                     int          *finish)
{
    int            retval = -1;
    FCGX_Request  *rfcgi = (FCGX_Request *)req; /* XXX */
#ifdef STREAM_FORK
    int            pid;
    struct stream_child *sc;

    if ((pid = fork()) == 0){ /* child */
#if 0 // Leaks
        if (pvec)
            free(pvec);
        if (qvec)
            cvec_free(qvec);
        if (pcvec)
            cvec_free(pcvec);
#endif
#endif /* STREAM_FORK */
        backend_eof = 0;
        /* Listen to backend socket */
        if (clixon_event_reg_fd(besock,
                                stream_fcgi_backend_cb,
                                req,
                                "stream socket") < 0)
            goto done;
        if (clixon_event_reg_fd(rfcgi->listen_sock,
                                stream_fcgi_uplink_cb,
                                req,
                                "stream socket") < 0)
            goto done;
        /* Timeout of notification stream, close after limited lifetime, for debug */
        if (timeout){
            struct timeval   t;
            gettimeofday(&t, NULL);
            t.tv_sec += timeout;
            clixon_event_reg_timeout(t, stream_timeout_end, req, "Stream timeout");
        }
        /* Poll upstream errors */
        fcgi_stream_timeout(0, req);
        /* Start loop */
        clixon_event_loop(h);
        clixon_debug(CLIXON_DBG_STREAM, "after loop");
        if (backend_eof == 0)
            if (clicon_rpc_close_session(h) < 0)
                goto done;
        clixon_event_unreg_fd(besock, stream_fcgi_backend_cb);
        close(besock);
        clixon_event_unreg_fd(rfcgi->listen_sock, stream_fcgi_uplink_cb);
        clixon_event_unreg_timeout(fcgi_stream_timeout, (void*)req);
        clixon_event_unreg_timeout(stream_timeout_end, (void*)req);
        clixon_exit_set(0); /* reset */
#ifdef STREAM_FORK
#if 0 /* Seems to be a global resource, but there is till some timing error here */
        FCGX_Finish_r(rfcgi);
        FCGX_Free(rfcgi, 0);
#endif
        restconf_terminate(h);
        exit(0);
    }
    /* parent */
    /* Create stream_child struct and store pid and FCGI data, when child
     * killed, call FCGX_Free
     */
    if ((sc = malloc(sizeof(struct stream_child))) == NULL){
        clixon_err(OE_XML, errno, "malloc");
        goto done;
    }
    memset(sc, 0, sizeof(struct stream_child));
    sc->sc_pid = pid;
    sc->sc_r = *rfcgi; /* XXX by value */

    ADDQ(sc, STREAM_CHILD);
    *finish = 0; /* If spawn child, we should not finish this stream */
#endif /* STREAM_FORK */
    retval = 0;
 done:
    clixon_debug(CLIXON_DBG_STREAM, "retval:%d", retval);
    return retval;
}
