hio/mio/lib/mio-http.h

335 lines
9.4 KiB
C
Raw Normal View History

2020-03-03 13:47:51 +00:00
/*
Copyright (c) 2016-2020 Chung, Hyung-Hwan. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _MIO_HTTP_H_
#define _MIO_HTTP_H_
2020-04-30 16:20:31 +00:00
#include <mio-ecs.h>
2020-05-05 15:12:08 +00:00
#include <mio-sck.h>
#include <mio-htre.h>
#include <mio-thr.h>
2020-03-03 13:47:51 +00:00
/** \file
* This file provides basic data types and functions for the http protocol.
*/
enum mio_http_range_type_t
{
MIO_HTTP_RANGE_PROPER,
2020-07-13 10:01:42 +00:00
MIO_HTTP_RANGE_PREFIX,
2020-03-03 13:47:51 +00:00
MIO_HTTP_RANGE_SUFFIX
};
typedef enum mio_http_range_type_t mio_http_range_type_t;
/**
* The mio_http_range_t type defines a structure that can represent
* a value for the \b Range: http header.
*
2020-07-13 10:01:42 +00:00
* If type is #MIO_HTTP_RANGE_PREFIX, 'to' is meaningless and 'from' indicates
* the number of bytes from the start.
* - 500- => from the 501st bytes all the way to the back.
2020-03-03 13:47:51 +00:00
*
2020-07-13 10:01:42 +00:00
* If type is #MIO_HTTP_RANGE_SUFFIX, 'from' is meaningless and 'to' indicates
2020-03-03 13:47:51 +00:00
* the number of bytes from the back.
* - -500 => last 500 bytes
*
* If type is #MIO_HTTP_RANGE_PROPER, 'from' and 'to' represents a proper range
* where the value of 0 indicates the first byte. This doesn't require any
* adjustment.
* - 0-999 => first 1000 bytes
* - 99- => from the 100th bytes to the end.
*/
struct mio_http_range_t
{
mio_http_range_type_t type; /**< type indicator */
mio_foff_t from; /**< starting offset */
mio_foff_t to; /**< ending offset */
2020-03-03 13:47:51 +00:00
};
typedef struct mio_http_range_t mio_http_range_t;
enum mio_perenc_http_opt_t
2020-03-03 13:47:51 +00:00
{
MIO_PERENC_HTTP_KEEP_SLASH = (1 << 0)
2020-03-03 13:47:51 +00:00
};
typedef enum mio_perenc_http_opt_t mio_perenc_bcstr_opt_t;
2020-03-03 13:47:51 +00:00
2020-05-05 15:12:08 +00:00
/* -------------------------------------------------------------- */
typedef struct mio_svc_htts_t mio_svc_htts_t;
typedef struct mio_svc_httc_t mio_svc_httc_t;
/* -------------------------------------------------------------- */
typedef struct mio_svc_htts_rsrc_t mio_svc_htts_rsrc_t;
typedef void (*mio_svc_htts_rsrc_on_kill_t) (
mio_svc_htts_rsrc_t* rsrc
);
#define MIO_SVC_HTTS_RSRC_HEADER \
mio_svc_htts_t* htts; \
mio_oow_t rsrc_size; \
mio_oow_t rsrc_refcnt; \
mio_svc_htts_rsrc_on_kill_t rsrc_on_kill
struct mio_svc_htts_rsrc_t
{
MIO_SVC_HTTS_RSRC_HEADER;
};
#define MIO_SVC_HTTS_RSRC_ATTACH(rsrc, var) do { (var) = (rsrc); ++(rsrc)->rsrc_refcnt; } while(0)
#define MIO_SVC_HTTS_RSRC_DETACH(rsrc_var) do { if (--(rsrc_var)->rsrc_refcnt == 0) { mio_svc_htts_rsrc_t* __rsrc_tmp = (rsrc_var); (rsrc_var) = MIO_NULL; mio_svc_htts_rsrc_kill(__rsrc_tmp); } else { (rsrc_var) = MIO_NULL; } } while(0)
/* -------------------------------------------------------------- */
typedef int (*mio_svc_htts_proc_req_t) (
mio_svc_htts_t* htts,
mio_dev_sck_t* sck,
mio_htre_t* req
);
/* -------------------------------------------------------------- */
struct mio_svc_htts_thr_func_info_t
{
mio_t* mio;
mio_http_method_t req_method;
mio_http_version_t req_version;
mio_bch_t* req_path;
mio_bch_t* req_param;
int req_x_http_method_override; /* -1 or mio_http_method_t */
/* TODO: header table */
mio_skad_t client_addr;
mio_skad_t server_addr;
};
typedef struct mio_svc_htts_thr_func_info_t mio_svc_htts_thr_func_info_t;
typedef void (*mio_svc_htts_thr_func_t) (
mio_t* mio,
mio_dev_thr_iopair_t* iop,
mio_svc_htts_thr_func_info_t* info,
void* ctx
);
/* -------------------------------------------------------------- */
2020-03-03 13:47:51 +00:00
#if defined(__cplusplus)
extern "C" {
#endif
2020-05-01 14:00:27 +00:00
MIO_EXPORT int mio_comp_http_versions (
2020-03-03 13:47:51 +00:00
const mio_http_version_t* v1,
const mio_http_version_t* v2
);
MIO_EXPORT int mio_comp_http_version_numbers (
const mio_http_version_t* v1,
int v2_major,
int v2_minor
);
2020-05-01 14:00:27 +00:00
MIO_EXPORT const mio_bch_t* mio_http_status_to_bcstr (
2020-03-03 13:47:51 +00:00
int code
);
2020-05-01 14:00:27 +00:00
MIO_EXPORT const mio_bch_t* mio_http_method_to_bcstr (
2020-03-03 13:47:51 +00:00
mio_http_method_t type
);
2020-04-30 16:20:31 +00:00
MIO_EXPORT mio_http_method_t mio_bcstr_to_http_method (
2020-03-03 13:47:51 +00:00
const mio_bch_t* name
);
2020-04-30 16:20:31 +00:00
MIO_EXPORT mio_http_method_t mio_bchars_to_http_method (
const mio_bch_t* nameptr,
mio_oow_t namelen
2020-03-03 13:47:51 +00:00
);
2020-04-30 16:20:31 +00:00
MIO_EXPORT int mio_parse_http_range_bcstr (
const mio_bch_t* str,
2020-03-03 13:47:51 +00:00
mio_http_range_t* range
);
2020-04-30 16:20:31 +00:00
MIO_EXPORT int mio_parse_http_time_bcstr (
2020-03-03 13:47:51 +00:00
const mio_bch_t* str,
2020-04-30 16:20:31 +00:00
mio_ntime_t* nt
2020-03-03 13:47:51 +00:00
);
2020-05-01 14:00:27 +00:00
MIO_EXPORT mio_bch_t* mio_fmt_http_time_to_bcstr (
2020-03-03 13:47:51 +00:00
const mio_ntime_t* nt,
mio_bch_t* buf,
mio_oow_t bufsz
);
/**
2020-04-30 16:20:31 +00:00
* The mio_is_perenced_http_bcstr() function determines if the given string
2020-03-03 13:47:51 +00:00
* contains a valid percent-encoded sequence.
*/
2020-04-30 16:20:31 +00:00
MIO_EXPORT int mio_is_perenced_http_bcstr (
2020-03-03 13:47:51 +00:00
const mio_bch_t* str
);
/**
* The mio_perdec_http_bcstr() function performs percent-decoding over a string.
2020-03-03 13:47:51 +00:00
* The caller must ensure that the output buffer \a buf is large enough.
* If \a ndecs is not #MIO_NULL, it is set to the number of characters
* decoded. 0 means no characters in the input string required decoding
* \return the length of the output string.
*/
MIO_EXPORT mio_oow_t mio_perdec_http_bcstr (
2020-03-03 13:47:51 +00:00
const mio_bch_t* str,
mio_bch_t* buf,
mio_oow_t* ndecs
);
2020-05-27 02:32:51 +00:00
/**
* The mio_perdec_http_bcstr() function performs percent-decoding over a length-bound string.
* It doesn't insert the terminating null.
*/
MIO_EXPORT mio_oow_t mio_perdec_http_bcs (
const mio_bcs_t* str,
mio_bch_t* buf,
mio_oow_t* ndecs
);
2020-03-03 13:47:51 +00:00
/**
* The mio_perenc_http_bcstr() function performs percent-encoding over a string.
2020-03-03 13:47:51 +00:00
* The caller must ensure that the output buffer \a buf is large enough.
* If \a nencs is not #MIO_NULL, it is set to the number of characters
* encoded. 0 means no characters in the input string required encoding.
* \return the length of the output string.
*/
MIO_EXPORT mio_oow_t mio_perenc_http_bcstr (
int opt, /**< 0 or bitwise-OR'ed of #mio_perenc_http_bcstr_opt_t */
2020-03-03 13:47:51 +00:00
const mio_bch_t* str,
mio_bch_t* buf,
mio_oow_t* nencs
);
#if 0
/* TODO: rename this function according to the naming convension */
MIO_EXPORT mio_bch_t* mio_perenc_http_bcstrdup (
int opt, /**< 0 or bitwise-OR'ed of #mio_perenc_http_bcstr_opt_t */
2020-03-03 13:47:51 +00:00
const mio_bch_t* str,
mio_mmgr_t* mmgr
);
#endif
2020-03-03 13:47:51 +00:00
2020-05-27 02:32:51 +00:00
MIO_EXPORT int mio_scan_http_qparam (
mio_bch_t* qparam,
int (*qparamcb) (mio_bcs_t* key, mio_bcs_t* val, void* ctx),
void* ctx
);
2020-05-05 15:12:08 +00:00
/* ------------------------------------------------------------------------- */
/* HTTP SERVER SERVICE */
/* ------------------------------------------------------------------------- */
MIO_EXPORT mio_svc_htts_t* mio_svc_htts_start (
mio_t* mio,
const mio_skad_t* bind_addr,
mio_svc_htts_proc_req_t proc_req
2020-05-05 15:12:08 +00:00
);
MIO_EXPORT void mio_svc_htts_stop (
mio_svc_htts_t* htts
);
#if defined(MIO_HAVE_INLINE)
static MIO_INLINE mio_t* mio_svc_htts_getmio(mio_svc_htts_t* svc) { return mio_svc_getmio((mio_svc_t*)svc); }
#else
# define mio_svc_htts_getmio(svc) mio_svc_getmio(svc)
#endif
MIO_EXPORT int mio_svc_htts_setservernamewithbcstr (
mio_svc_htts_t* htts,
const mio_bch_t* server_name
2020-05-07 15:47:33 +00:00
);
MIO_EXPORT int mio_svc_htts_docgi (
mio_svc_htts_t* htts,
mio_dev_sck_t* csck,
mio_htre_t* req,
const mio_bch_t* docroot,
2020-07-09 09:54:13 +00:00
const mio_bch_t* script
);
MIO_EXPORT int mio_svc_htts_dofile (
mio_svc_htts_t* htts,
mio_dev_sck_t* csck,
mio_htre_t* req,
const mio_bch_t* docroot,
const mio_bch_t* script
);
MIO_EXPORT int mio_svc_htts_dothr (
mio_svc_htts_t* htts,
mio_dev_sck_t* csck,
mio_htre_t* req,
mio_svc_htts_thr_func_t func,
void* ctx
);
2020-05-26 14:58:55 +00:00
MIO_EXPORT int mio_svc_htts_dotxt (
mio_svc_htts_t* htts,
mio_dev_sck_t* csck,
mio_htre_t* req,
int status_code,
const mio_bch_t* content_type,
const mio_bch_t* content_text
);
MIO_EXPORT mio_svc_htts_rsrc_t* mio_svc_htts_rsrc_make (
mio_svc_htts_t* htts,
mio_oow_t rsrc_size,
mio_svc_htts_rsrc_on_kill_t on_kill
);
MIO_EXPORT void mio_svc_htts_rsrc_kill (
mio_svc_htts_rsrc_t* rsrc
);
2020-05-17 18:09:19 +00:00
MIO_EXPORT void mio_svc_htts_fmtgmtime (
mio_svc_htts_t* htts,
const mio_ntime_t* nt,
mio_bch_t* buf,
mio_oow_t len
);
MIO_EXPORT mio_bch_t* mio_svc_htts_dupmergepaths (
mio_svc_htts_t* htts,
const mio_bch_t* base,
const mio_bch_t* path
);
2020-03-03 13:47:51 +00:00
#if defined(__cplusplus)
}
#endif
#endif