2020-03-03 13:47:51 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <mio-htre.h>
|
2020-05-25 08:04:30 +00:00
|
|
|
#include <mio-http.h>
|
2020-03-03 13:47:51 +00:00
|
|
|
#include "mio-prv.h"
|
|
|
|
|
2020-04-30 16:20:31 +00:00
|
|
|
static void free_hdrval (mio_htb_t* htb, void* vptr, mio_oow_t vlen)
|
2020-03-03 13:47:51 +00:00
|
|
|
{
|
|
|
|
mio_htre_hdrval_t* val;
|
|
|
|
mio_htre_hdrval_t* tmp;
|
|
|
|
|
|
|
|
val = vptr;
|
|
|
|
while (val)
|
|
|
|
{
|
|
|
|
tmp = val;
|
|
|
|
val = val->next;
|
2020-04-30 16:20:31 +00:00
|
|
|
mio_freemem (htb->mio, tmp);
|
2020-03-03 13:47:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 16:20:31 +00:00
|
|
|
int mio_htre_init (mio_htre_t* re, mio_t* mio)
|
2020-03-03 13:47:51 +00:00
|
|
|
{
|
|
|
|
static mio_htb_style_t style =
|
|
|
|
{
|
|
|
|
{
|
|
|
|
MIO_HTB_COPIER_DEFAULT,
|
|
|
|
MIO_HTB_COPIER_DEFAULT
|
|
|
|
},
|
|
|
|
{
|
|
|
|
MIO_HTB_FREEER_DEFAULT,
|
|
|
|
free_hdrval
|
|
|
|
},
|
|
|
|
MIO_HTB_COMPER_DEFAULT,
|
|
|
|
MIO_HTB_KEEPER_DEFAULT,
|
|
|
|
MIO_HTB_SIZER_DEFAULT,
|
|
|
|
MIO_HTB_HASHER_DEFAULT
|
|
|
|
};
|
|
|
|
|
|
|
|
MIO_MEMSET (re, 0, MIO_SIZEOF(*re));
|
2020-04-30 16:20:31 +00:00
|
|
|
re->mio = mio;
|
2020-03-03 13:47:51 +00:00
|
|
|
|
2020-04-30 16:20:31 +00:00
|
|
|
if (mio_htb_init(&re->hdrtab, mio, 60, 70, 1, 1) <= -1) return -1;
|
|
|
|
if (mio_htb_init(&re->trailers, mio, 20, 70, 1, 1) <= -1) return -1;
|
2020-03-03 13:47:51 +00:00
|
|
|
|
|
|
|
mio_htb_setstyle (&re->hdrtab, &style);
|
|
|
|
mio_htb_setstyle (&re->trailers, &style);
|
|
|
|
|
2020-04-30 16:20:31 +00:00
|
|
|
mio_becs_init (&re->content, mio, 0);
|
2020-03-03 13:47:51 +00:00
|
|
|
#if 0
|
2020-04-30 16:20:31 +00:00
|
|
|
mio_becs_init (&re->iniline, mio, 0);
|
2020-03-03 13:47:51 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mio_htre_fini (mio_htre_t* re)
|
|
|
|
{
|
|
|
|
#if 0
|
2020-04-30 16:20:31 +00:00
|
|
|
mio_becs_fini (&re->iniline);
|
2020-03-03 13:47:51 +00:00
|
|
|
#endif
|
2020-04-30 16:20:31 +00:00
|
|
|
mio_becs_fini (&re->content);
|
2020-03-03 13:47:51 +00:00
|
|
|
mio_htb_fini (&re->trailers);
|
|
|
|
mio_htb_fini (&re->hdrtab);
|
|
|
|
|
2020-05-06 09:28:36 +00:00
|
|
|
if (re->orgqpath.buf)
|
|
|
|
{
|
|
|
|
mio_freemem (re->mio, re->orgqpath.buf);
|
|
|
|
re->orgqpath.buf = MIO_NULL;
|
|
|
|
re->orgqpath.capa = 0;
|
2020-05-06 09:55:35 +00:00
|
|
|
re->orgqpath.ptr = MIO_NULL;
|
|
|
|
re->orgqpath.len = 0;
|
2020-05-06 09:28:36 +00:00
|
|
|
}
|
2020-03-03 13:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void mio_htre_clear (mio_htre_t* re)
|
|
|
|
{
|
|
|
|
if (!(re->state & MIO_HTRE_COMPLETED) &&
|
|
|
|
!(re->state & MIO_HTRE_DISCARDED))
|
|
|
|
{
|
|
|
|
if (re->concb)
|
|
|
|
{
|
|
|
|
re->concb (re, MIO_NULL, 0, re->concb_ctx); /* indicate end of content */
|
|
|
|
mio_htre_unsetconcb (re);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
re->state = 0;
|
|
|
|
re->flags = 0;
|
|
|
|
|
2020-05-06 09:55:35 +00:00
|
|
|
re->orgqpath.ptr = MIO_NULL;
|
|
|
|
re->orgqpath.len = 0;
|
2020-03-03 13:47:51 +00:00
|
|
|
|
|
|
|
MIO_MEMSET (&re->version, 0, MIO_SIZEOF(re->version));
|
|
|
|
MIO_MEMSET (&re->attr, 0, MIO_SIZEOF(re->attr));
|
|
|
|
|
|
|
|
mio_htb_clear (&re->hdrtab);
|
|
|
|
mio_htb_clear (&re->trailers);
|
|
|
|
|
2020-04-30 16:20:31 +00:00
|
|
|
mio_becs_clear (&re->content);
|
2020-03-03 13:47:51 +00:00
|
|
|
#if 0
|
2020-04-30 16:20:31 +00:00
|
|
|
mio_becs_clear (&re->iniline);
|
2020-03-03 13:47:51 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-04-30 16:20:31 +00:00
|
|
|
const mio_htre_hdrval_t* mio_htre_getheaderval (const mio_htre_t* re, const mio_bch_t* name)
|
2020-03-03 13:47:51 +00:00
|
|
|
{
|
|
|
|
mio_htb_pair_t* pair;
|
2020-04-30 16:20:31 +00:00
|
|
|
pair = mio_htb_search(&re->hdrtab, name, mio_count_bcstr(name));
|
2020-03-03 13:47:51 +00:00
|
|
|
if (pair == MIO_NULL) return MIO_NULL;
|
|
|
|
return MIO_HTB_VPTR(pair);
|
|
|
|
}
|
|
|
|
|
2020-04-30 16:20:31 +00:00
|
|
|
const mio_htre_hdrval_t* mio_htre_gettrailerval (const mio_htre_t* re, const mio_bch_t* name)
|
2020-03-03 13:47:51 +00:00
|
|
|
{
|
|
|
|
mio_htb_pair_t* pair;
|
2020-04-30 16:20:31 +00:00
|
|
|
pair = mio_htb_search(&re->trailers, name, mio_count_bcstr(name));
|
2020-03-03 13:47:51 +00:00
|
|
|
if (pair == MIO_NULL) return MIO_NULL;
|
|
|
|
return MIO_HTB_VPTR(pair);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct header_walker_ctx_t
|
|
|
|
{
|
|
|
|
mio_htre_t* re;
|
|
|
|
mio_htre_header_walker_t walker;
|
|
|
|
void* ctx;
|
|
|
|
int ret;
|
|
|
|
};
|
|
|
|
|
2020-04-30 16:20:31 +00:00
|
|
|
static mio_htb_walk_t walk_headers (mio_htb_t* htb, mio_htb_pair_t* pair, void* ctx)
|
2020-03-03 13:47:51 +00:00
|
|
|
{
|
|
|
|
struct header_walker_ctx_t* hwctx = (struct header_walker_ctx_t*)ctx;
|
|
|
|
if (hwctx->walker (hwctx->re, MIO_HTB_KPTR(pair), MIO_HTB_VPTR(pair), hwctx->ctx) <= -1)
|
|
|
|
{
|
|
|
|
hwctx->ret = -1;
|
|
|
|
return MIO_HTB_WALK_STOP;
|
|
|
|
}
|
|
|
|
return MIO_HTB_WALK_FORWARD;
|
|
|
|
}
|
|
|
|
|
2020-04-30 16:20:31 +00:00
|
|
|
int mio_htre_walkheaders (mio_htre_t* re, mio_htre_header_walker_t walker, void* ctx)
|
2020-03-03 13:47:51 +00:00
|
|
|
{
|
|
|
|
struct header_walker_ctx_t hwctx;
|
|
|
|
hwctx.re = re;
|
|
|
|
hwctx.walker = walker;
|
|
|
|
hwctx.ctx = ctx;
|
|
|
|
hwctx.ret = 0;
|
|
|
|
mio_htb_walk (&re->hdrtab, walk_headers, &hwctx);
|
|
|
|
return hwctx.ret;
|
|
|
|
}
|
|
|
|
|
2020-04-30 16:20:31 +00:00
|
|
|
int mio_htre_walktrailers (mio_htre_t* re, mio_htre_header_walker_t walker, void* ctx)
|
2020-03-03 13:47:51 +00:00
|
|
|
{
|
|
|
|
struct header_walker_ctx_t hwctx;
|
|
|
|
hwctx.re = re;
|
|
|
|
hwctx.walker = walker;
|
|
|
|
hwctx.ctx = ctx;
|
|
|
|
hwctx.ret = 0;
|
|
|
|
mio_htb_walk (&re->trailers, walk_headers, &hwctx);
|
|
|
|
return hwctx.ret;
|
|
|
|
}
|
|
|
|
|
2020-04-30 16:20:31 +00:00
|
|
|
int mio_htre_addcontent (mio_htre_t* re, const mio_bch_t* ptr, mio_oow_t len)
|
2020-03-03 13:47:51 +00:00
|
|
|
{
|
|
|
|
/* see comments in mio_htre_discardcontent() */
|
|
|
|
|
|
|
|
if (re->state & (MIO_HTRE_COMPLETED | MIO_HTRE_DISCARDED)) return 0; /* skipped */
|
|
|
|
|
|
|
|
if (re->concb)
|
|
|
|
{
|
|
|
|
/* if the callback is set, the content goes to the callback. */
|
2020-04-30 16:20:31 +00:00
|
|
|
if (re->concb(re, ptr, len, re->concb_ctx) <= -1) return -1;
|
2020-03-03 13:47:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* if the callback is not set, the contents goes to the internal buffer */
|
2020-04-30 16:20:31 +00:00
|
|
|
if (mio_becs_ncat(&re->content, ptr, len) == (mio_oow_t)-1) return -1;
|
2020-03-03 13:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1; /* added successfully */
|
|
|
|
}
|
|
|
|
|
|
|
|
void mio_htre_completecontent (mio_htre_t* re)
|
|
|
|
{
|
|
|
|
/* see comments in mio_htre_discardcontent() */
|
|
|
|
|
|
|
|
if (!(re->state & MIO_HTRE_COMPLETED) &&
|
|
|
|
!(re->state & MIO_HTRE_DISCARDED))
|
|
|
|
{
|
|
|
|
re->state |= MIO_HTRE_COMPLETED;
|
|
|
|
if (re->concb)
|
|
|
|
{
|
|
|
|
/* indicate end of content */
|
|
|
|
re->concb (re, MIO_NULL, 0, re->concb_ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void mio_htre_discardcontent (mio_htre_t* re)
|
|
|
|
{
|
|
|
|
/* you can't discard this if it's completed.
|
|
|
|
* you can't complete this if it's discarded
|
|
|
|
* you can't add contents to this if it's completed or discarded
|
|
|
|
*/
|
|
|
|
|
2020-04-30 16:20:31 +00:00
|
|
|
if (!(re->state & MIO_HTRE_COMPLETED) && !(re->state & MIO_HTRE_DISCARDED))
|
2020-03-03 13:47:51 +00:00
|
|
|
{
|
|
|
|
re->state |= MIO_HTRE_DISCARDED;
|
|
|
|
|
|
|
|
/* mio_htre_addcontent()...
|
|
|
|
* mio_thre_setconcb()...
|
|
|
|
* mio_htre_discardcontent()... <-- POINT A.
|
|
|
|
*
|
|
|
|
* at point A, the content must contain something
|
|
|
|
* and concb is also set. for simplicity,
|
|
|
|
* clear the content buffer and invoke the callback
|
|
|
|
*
|
|
|
|
* likewise, you may produce many weird combinations
|
|
|
|
* of these functions. however, these functions are
|
|
|
|
* designed to serve a certain usage pattern not including
|
|
|
|
* weird combinations.
|
|
|
|
*/
|
2020-04-30 16:20:31 +00:00
|
|
|
mio_becs_clear (&re->content);
|
2020-03-03 13:47:51 +00:00
|
|
|
if (re->concb)
|
|
|
|
{
|
|
|
|
/* indicate end of content */
|
|
|
|
re->concb (re, MIO_NULL, 0, re->concb_ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void mio_htre_unsetconcb (mio_htre_t* re)
|
|
|
|
{
|
|
|
|
re->concb = MIO_NULL;
|
|
|
|
re->concb_ctx = MIO_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mio_htre_setconcb (mio_htre_t* re, mio_htre_concb_t concb, void* ctx)
|
|
|
|
{
|
|
|
|
re->concb = concb;
|
|
|
|
re->concb_ctx = ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mio_htre_perdecqpath (mio_htre_t* re)
|
|
|
|
{
|
2020-04-30 16:20:31 +00:00
|
|
|
mio_oow_t dec_count;
|
2020-03-03 13:47:51 +00:00
|
|
|
|
|
|
|
/* percent decode the query path*/
|
|
|
|
|
|
|
|
if (re->type != MIO_HTRE_Q || (re->flags & MIO_HTRE_QPATH_PERDEC)) return -1;
|
|
|
|
|
2020-04-30 16:20:31 +00:00
|
|
|
MIO_ASSERT (re->mio, re->orgqpath.len <= 0);
|
|
|
|
MIO_ASSERT (re->mio, re->orgqpath.ptr == MIO_NULL);
|
2020-03-03 13:47:51 +00:00
|
|
|
|
2020-04-30 16:20:31 +00:00
|
|
|
if (mio_is_perenced_http_bcstr(re->u.q.path.ptr))
|
2020-03-03 13:47:51 +00:00
|
|
|
{
|
|
|
|
/* the string is percent-encoded. keep the original request
|
|
|
|
* in a separately allocated buffer */
|
|
|
|
|
|
|
|
if (re->orgqpath.buf && re->u.q.path.len <= re->orgqpath.capa)
|
|
|
|
{
|
2020-04-30 16:20:31 +00:00
|
|
|
re->orgqpath.len = mio_copy_bcstr_unlimited(re->orgqpath.buf, re->u.q.path.ptr);
|
2020-03-03 13:47:51 +00:00
|
|
|
re->orgqpath.ptr = re->orgqpath.buf;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (re->orgqpath.buf)
|
|
|
|
{
|
2020-04-30 16:20:31 +00:00
|
|
|
mio_freemem (re->mio, re->orgqpath.buf);
|
2020-03-03 13:47:51 +00:00
|
|
|
re->orgqpath.capa = 0;
|
2020-05-06 09:55:35 +00:00
|
|
|
re->orgqpath.ptr = MIO_NULL;
|
|
|
|
re->orgqpath.len = 0;
|
2020-03-03 13:47:51 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 14:00:27 +00:00
|
|
|
re->orgqpath.buf = mio_dupbchars(re->mio, re->u.q.path.ptr, re->u.q.path.len);
|
|
|
|
if (MIO_UNLIKELY(!re->orgqpath.buf)) return -1;
|
2020-03-03 13:47:51 +00:00
|
|
|
re->orgqpath.capa = re->u.q.path.len;
|
|
|
|
|
|
|
|
re->orgqpath.ptr = re->orgqpath.buf;
|
|
|
|
re->orgqpath.len = re->orgqpath.capa;
|
|
|
|
|
|
|
|
/* orgqpath.buf and orgqpath.ptr are the same here. the caller
|
|
|
|
* is free to change orgqpath.ptr to point to a differnt position
|
|
|
|
* in the buffer. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 09:28:36 +00:00
|
|
|
re->u.q.path.len = mio_perdec_http_bcstr(re->u.q.path.ptr, re->u.q.path.ptr, &dec_count);
|
2020-03-03 13:47:51 +00:00
|
|
|
if (dec_count > 0)
|
|
|
|
{
|
2020-04-30 16:20:31 +00:00
|
|
|
/* this assertion is to ensure that mio_is_perenced_http_bstr()
|
2020-03-03 13:47:51 +00:00
|
|
|
* returned true when dec_count is greater than 0 */
|
2020-04-30 16:20:31 +00:00
|
|
|
MIO_ASSERT (re->mio, re->orgqpath.buf != MIO_NULL);
|
|
|
|
MIO_ASSERT (re->mio, re->orgqpath.ptr != MIO_NULL);
|
2020-03-03 13:47:51 +00:00
|
|
|
re->flags |= MIO_HTRE_QPATH_PERDEC;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-05-26 13:15:25 +00:00
|
|
|
|
|
|
|
int mio_htre_getreqcontentlen (mio_htre_t* req, mio_oow_t* len)
|
|
|
|
{
|
|
|
|
/* return the potential content length to expect to receive if used as a request */
|
|
|
|
|
|
|
|
if (req->flags & MIO_HTRE_ATTR_CHUNKED)
|
|
|
|
{
|
|
|
|
/* "Transfer-Encoding: chunked" take precedence over "Content-Length: XXX".
|
|
|
|
*
|
|
|
|
* [RFC7230]
|
|
|
|
* If a message is received with both a Transfer-Encoding and a
|
|
|
|
* Content-Length header field, the Transfer-Encoding overrides the
|
|
|
|
* Content-Length. */
|
|
|
|
return 1; /* unable to determine content-length in advance. unlimited */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req->flags & MIO_HTRE_ATTR_LENGTH)
|
|
|
|
{
|
|
|
|
*len = req->attr.content_length;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* If no Content-Length is specified in a request, it's Content-Length: 0 */
|
|
|
|
*len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0; /* limited to the length set in *len */
|
|
|
|
}
|