/*************************************************************************
	> File Name: http_stream.h
	> Author:
	> Mail:
	> Created Time: 2018å¹?09æœ?12æ—? æ˜ŸæœŸä¸? 15æ—?56åˆ?05ç§?
 ************************************************************************/

#ifndef _HTTP_STREAM_H
#define _HTTP_STREAM_H

#include <tfe_http.h>
#include <tfe_stream.h>

#include <nghttp2/nghttp2.h>
#include <sys/queue.h>

typedef int (*event_cb)(struct tfe_h2_half_private * hf_private,
                        tfe_http_event ev, const unsigned char * data,
                        size_t len, void * user);

enum h2_read_state
{
    H2_READ_STATE_BEGIN,
    H2_READ_STATE_READING,
    H2_READ_STATE_COMPLETE,
};

struct tfe_h2_payload
{
    int gzip;
    uint8_t flags;
    ssize_t padlen;
    struct z_stream_st *inflate;
    struct z_stream_st *deflate;
	struct evbuffer * evbuf_body;
};

struct tfe_h2_field{	
    TAILQ_ENTRY(tfe_h2_field) next;
    nghttp2_nv nv;
    struct http_field_name *field;
};

struct tfe_h2_header{	
	TAILQ_HEAD(h2_field_list_head, tfe_h2_field) h2_field_list;
	int nvlen;
	uint8_t flag;
};

struct tfe_h2_half_private
{
	/* PUBLIC STRUCTURE */
	struct tfe_http_half half_public;

	struct http_frame_session_ctx *frame_ctx;

	/* UNDERLAY BUFFER */
	int method_or_status;
    int by_stream;

	char * url_storage;
    struct tfe_h2_payload h2_payload;
    struct tfe_h2_header header;
    struct tfe_h2_header promised;

    /*Read Cache**/
    int32_t stream_id;
    nghttp2_session *session;
    struct tfe_h2_session *father_session;

	enum h2_read_state body_state;
	enum h2_read_state message_state;

    /* Callback Function */
    int (*event_cb)(struct tfe_h2_half_private * half_private, tfe_http_event ev,
                    const unsigned char * data, size_t len, void * user);
	/* Callback Function User Pointer */
	void * event_cb_user;
	/* Callback Function User Pointer Deleter */
	void (* event_cb_user_deleter)(void *);
};

struct cache_trapper_t{
    int spd_set;
	int spd_valid;
    int rse_set;
    int spd_set_cnt;
    int spd_cnt;
	tfe_http_event spd_event;
};

struct tfe_h2_session{
    struct tfe_http_session tfe_session;
    TAILQ_ENTRY(tfe_h2_session) next;
    const struct tfe_stream *tf_stream;

	int32_t ngh2_stream_id;
	nghttp2_session *session;
	struct tfe_h2_stream *father_stream;

    struct cache_trapper_t cache;
	struct http_frame_session_ctx *frame_ctx;
    struct tfe_h2_half_private *req, *plugin_built_req;
    struct tfe_h2_half_private *resp, *plugin_built_resp;
};

struct tfe_h2_stream
{
	TAILQ_HEAD(list_head, tfe_h2_session) h2_session_list;

    int goaway;
	int kill_signal;

    enum tfe_stream_action stream_action;

    unsigned int thread_id;

    /** for down stream as server */
    nghttp2_session *as_server;
    /** for up stream as client*/
    nghttp2_session *as_client;

    const struct tfe_stream *tf_stream;
};

struct stream_tap_info_t
{
    /** IS PREEMPTED */
    unsigned int preempted;
    /** sess manage */
    struct tfe_h2_stream *h2_stream_info;
};

#define ACTION_USER_DATA 3

static inline const struct tfe_h2_half_private *
nghttp2_to_half_private(const struct tfe_http_half * half_public)
{
	if(!half_public)
        return NULL;
	return container_of(half_public, struct tfe_h2_half_private, half_public);
}

static inline struct tfe_h2_half_private *
nghttp2_to_half_private(struct tfe_http_half * half_public)
{
	if(!half_public)
        return NULL;
	return container_of(half_public, struct tfe_h2_half_private, half_public);
}

static inline struct tfe_h2_session *
nghttp2_to_stream_data(struct tfe_http_session * hs_public)
{
	if (hs_public == NULL)
        return NULL;
	return container_of(hs_public, struct tfe_h2_session, tfe_session);
}

extern struct tfe_h2_stream* tfe_session_info_init();
extern void sess_data_ctx_fini(struct tfe_h2_stream *h2_stream_info,	const struct tfe_stream * stream,
                               unsigned int thread_id);

extern enum tfe_stream_action
detect_down_stream_protocol(struct tfe_h2_stream *h2_stream_info, const struct tfe_stream *tfe_stream,
					      	unsigned int thread_id, const unsigned char *data, size_t len);

extern enum tfe_stream_action
detect_up_stream_protocol(struct tfe_h2_stream *h2_stream_info, const struct tfe_stream *tfe_stream,
					      unsigned int thread_id, const unsigned char *data, size_t len);

enum tfe_stream_action
nghttp2_client_mem_send(struct tfe_h2_stream *h2_stream_info);

enum tfe_stream_action
nghttp2_server_mem_send(struct tfe_h2_stream *h2_stream_info);

/*for test **/

#endif
