2012-09-11 16:19:52 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
2019-06-06 05:28:23 +00:00
|
|
|
Copyright (c) 2006-2019 Chung, Hyung-Hwan. All rights reserved.
|
2012-09-11 16:19:52 +00:00
|
|
|
|
2014-11-19 14:42:24 +00:00
|
|
|
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.
|
2012-09-11 16:19:52 +00:00
|
|
|
|
2014-11-19 14:42:24 +00:00
|
|
|
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.
|
2012-09-11 16:19:52 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "httpd.h"
|
2016-04-29 03:55:42 +00:00
|
|
|
#include "../cmn/mem-prv.h"
|
2012-09-11 16:19:52 +00:00
|
|
|
#include <qse/cmn/str.h>
|
2012-09-27 12:54:35 +00:00
|
|
|
#include <qse/cmn/fmt.h>
|
|
|
|
|
2012-09-11 16:19:52 +00:00
|
|
|
typedef struct task_dir_t task_dir_t;
|
|
|
|
struct task_dir_t
|
|
|
|
{
|
2014-07-08 14:30:42 +00:00
|
|
|
qse_mcstr_t path;
|
|
|
|
qse_mcstr_t qpath;
|
|
|
|
qse_mcstr_t head;
|
|
|
|
qse_mcstr_t foot;
|
2012-09-11 16:19:52 +00:00
|
|
|
qse_http_version_t version;
|
|
|
|
int keepalive;
|
2013-03-28 09:43:59 +00:00
|
|
|
int method;
|
2014-10-24 07:25:31 +00:00
|
|
|
qse_httpd_hnd_t handle;
|
2012-09-11 16:19:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct task_dseg_t task_dseg_t;
|
|
|
|
struct task_dseg_t
|
|
|
|
{
|
2012-09-12 15:47:41 +00:00
|
|
|
qse_http_version_t version;
|
|
|
|
int keepalive;
|
|
|
|
int chunked;
|
2014-10-24 07:25:31 +00:00
|
|
|
|
2014-07-08 14:30:42 +00:00
|
|
|
qse_mcstr_t path;
|
|
|
|
qse_mcstr_t qpath;
|
|
|
|
qse_mcstr_t head;
|
|
|
|
qse_mcstr_t foot;
|
2014-09-16 14:14:51 +00:00
|
|
|
qse_httpd_hnd_t handle;
|
2012-10-15 16:39:23 +00:00
|
|
|
qse_httpd_dirent_t dent;
|
2012-09-11 16:19:52 +00:00
|
|
|
|
2012-09-12 15:47:41 +00:00
|
|
|
#define HEADER_ADDED (1 << 0)
|
|
|
|
#define FOOTER_ADDED (1 << 1)
|
|
|
|
#define FOOTER_PENDING (1 << 2)
|
|
|
|
#define DIRENT_PENDING (1 << 3)
|
2012-10-15 16:39:23 +00:00
|
|
|
#define DIRENT_NOMORE (1 << 4)
|
2012-09-12 15:47:41 +00:00
|
|
|
int state;
|
2012-09-11 16:19:52 +00:00
|
|
|
|
2012-09-12 15:47:41 +00:00
|
|
|
qse_size_t tcount; /* total directory entries */
|
|
|
|
qse_size_t dcount; /* the number of items in the buffer */
|
|
|
|
|
2013-03-31 15:19:24 +00:00
|
|
|
qse_mchar_t buf[4096*2]; /* is this large enough? */
|
2012-10-16 08:54:07 +00:00
|
|
|
int bufpos;
|
|
|
|
int buflen;
|
|
|
|
int bufrem;
|
2012-09-11 16:19:52 +00:00
|
|
|
qse_size_t chunklen;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int task_init_dseg (
|
|
|
|
qse_httpd_t* httpd, qse_httpd_client_t* client, qse_httpd_task_t* task)
|
|
|
|
{
|
|
|
|
task_dseg_t* xtn = qse_httpd_gettaskxtn (httpd, task);
|
2012-09-25 02:47:25 +00:00
|
|
|
task_dseg_t* arg = (task_dseg_t*)task->ctx;
|
|
|
|
|
|
|
|
QSE_MEMCPY (xtn, arg, QSE_SIZEOF(*xtn));
|
|
|
|
|
|
|
|
xtn->path.ptr = (qse_mchar_t*)(xtn + 1);
|
|
|
|
qse_mbscpy ((qse_mchar_t*)xtn->path.ptr, arg->path.ptr);
|
2012-10-16 08:54:07 +00:00
|
|
|
xtn->qpath.ptr = xtn->path.ptr + xtn->path.len + 1;
|
2012-09-25 02:47:25 +00:00
|
|
|
qse_mbscpy ((qse_mchar_t*)xtn->qpath.ptr, arg->qpath.ptr);
|
2012-09-11 16:19:52 +00:00
|
|
|
|
2013-04-04 15:04:45 +00:00
|
|
|
xtn->head.ptr = xtn->qpath.ptr + xtn->qpath.len + 1;
|
|
|
|
qse_mbscpy ((qse_mchar_t*)xtn->head.ptr, arg->head.ptr);
|
|
|
|
xtn->foot.ptr = xtn->head.ptr + xtn->head.len + 1;
|
|
|
|
qse_mbscpy ((qse_mchar_t*)xtn->foot.ptr, arg->foot.ptr);
|
|
|
|
|
2012-09-11 16:19:52 +00:00
|
|
|
task->ctx = xtn;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void task_fini_dseg (
|
|
|
|
qse_httpd_t* httpd, qse_httpd_client_t* client, qse_httpd_task_t* task)
|
|
|
|
{
|
|
|
|
task_dseg_t* ctx = (task_dseg_t*)task->ctx;
|
2013-02-20 08:47:22 +00:00
|
|
|
httpd->opt.scb.dir.close (httpd, ctx->handle);
|
2012-09-11 16:19:52 +00:00
|
|
|
}
|
|
|
|
|
2012-09-12 15:47:41 +00:00
|
|
|
#define SIZE_CHLEN 4 /* the space size to hold the hexadecimal chunk length */
|
|
|
|
#define SIZE_CHLENCRLF 2 /* the space size to hold CRLF after the chunk length */
|
|
|
|
#define SIZE_CHENDCRLF 2 /* the sapce size to hold CRLF after the chunk data */
|
|
|
|
|
|
|
|
static QSE_INLINE void close_chunk_data (task_dseg_t* ctx, qse_size_t len)
|
|
|
|
{
|
|
|
|
ctx->chunklen = len;
|
|
|
|
|
|
|
|
/* CHENDCRLF - there is always space for these two.
|
|
|
|
* reserved with SIZE_CHENDCRLF */
|
|
|
|
ctx->buf[ctx->buflen++] = QSE_MT('\r');
|
|
|
|
ctx->buf[ctx->buflen++] = QSE_MT('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fill_chunk_length (task_dseg_t* ctx)
|
|
|
|
{
|
|
|
|
int x;
|
|
|
|
|
|
|
|
/* right alignment with space padding on the left */
|
2012-09-27 12:54:35 +00:00
|
|
|
x = qse_fmtuintmaxtombs (
|
2012-09-12 15:47:41 +00:00
|
|
|
ctx->buf, (SIZE_CHLEN + SIZE_CHLENCRLF) - 1,
|
2012-09-27 12:54:35 +00:00
|
|
|
(ctx->chunklen - (SIZE_CHLEN + SIZE_CHLENCRLF)), /* value to convert */
|
|
|
|
16 | QSE_FMTUINTMAXTOMBS_UPPERCASE, /* uppercase hexadecimal letters */
|
|
|
|
-1, /* no particular precision - want space filled if less than 4 digits */
|
|
|
|
QSE_MT(' '), /* fill with space */
|
|
|
|
QSE_NULL
|
|
|
|
);
|
|
|
|
/* i don't check the buffer size error because i've secured the
|
2012-09-12 15:47:41 +00:00
|
|
|
* suffient space for the chunk length at the beginning of the buffer */
|
|
|
|
|
|
|
|
/* CHLENCRLF */
|
|
|
|
ctx->buf[x] = QSE_MT('\r');
|
|
|
|
ctx->buf[x+1] = QSE_MT('\n');
|
|
|
|
|
|
|
|
/* skip leading space padding */
|
|
|
|
QSE_ASSERT (ctx->bufpos == 0);
|
|
|
|
while (ctx->buf[ctx->bufpos] == QSE_MT(' ')) ctx->bufpos++;
|
|
|
|
}
|
|
|
|
|
2014-09-11 15:03:12 +00:00
|
|
|
static qse_size_t format_dirent (
|
2013-04-04 15:04:45 +00:00
|
|
|
qse_httpd_t* httpd,
|
|
|
|
qse_httpd_client_t* client,
|
|
|
|
const qse_httpd_dirent_t* dirent,
|
|
|
|
qse_mchar_t* buf, int bufsz)
|
|
|
|
{
|
2014-09-11 15:03:12 +00:00
|
|
|
qse_size_t x;
|
2013-04-04 15:04:45 +00:00
|
|
|
|
|
|
|
qse_mchar_t* encname;
|
|
|
|
qse_mchar_t* escname;
|
|
|
|
qse_btime_t bt;
|
|
|
|
qse_mchar_t tmbuf[32];
|
|
|
|
qse_mchar_t fszbuf[64];
|
|
|
|
|
|
|
|
/* TODO: better buffer management in case there are
|
|
|
|
* a lot of file names to escape. */
|
|
|
|
|
|
|
|
/* perform percent-encoding for the anchor */
|
2019-06-25 02:36:32 +00:00
|
|
|
encname = qse_perenchttpstrdup (0, dirent->name, qse_httpd_getmmgr(httpd));
|
2013-04-04 15:04:45 +00:00
|
|
|
if (encname == QSE_NULL)
|
|
|
|
{
|
|
|
|
httpd->errnum = QSE_HTTPD_ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* perform html escaping for the text */
|
|
|
|
escname = qse_httpd_escapehtml (httpd, dirent->name);
|
|
|
|
if (escname == QSE_NULL)
|
|
|
|
{
|
2019-06-25 02:36:32 +00:00
|
|
|
if (encname != dirent->name) QSE_MMGR_FREE (qse_httpd_getmmgr(httpd), encname);
|
2013-04-04 15:04:45 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
qse_localtime (&dirent->stat.mtime, &bt);
|
2014-09-11 15:03:12 +00:00
|
|
|
qse_mbsxfmt (tmbuf, QSE_COUNTOF(tmbuf),
|
2013-04-04 15:04:45 +00:00
|
|
|
QSE_MT("%04d-%02d-%02d %02d:%02d:%02d"),
|
2014-09-11 15:03:12 +00:00
|
|
|
bt.year + QSE_BTIME_YEAR_BASE, bt.mon + 1, bt.mday,
|
2013-04-04 15:04:45 +00:00
|
|
|
bt.hour, bt.min, bt.sec);
|
|
|
|
|
|
|
|
if (dirent->stat.isdir)
|
|
|
|
{
|
|
|
|
fszbuf[0] = QSE_MT('\0');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qse_fmtuintmaxtombs (
|
|
|
|
fszbuf, QSE_COUNTOF(fszbuf),
|
|
|
|
dirent->stat.size, 10, -1, QSE_MT('\0'), QSE_NULL
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-09-11 15:03:12 +00:00
|
|
|
x = qse_mbsxfmts (
|
2013-04-04 15:04:45 +00:00
|
|
|
buf, bufsz,
|
|
|
|
QSE_MT("<tr><td class='name'><a href='%s%s' class='%s'>%s%s</a></td><td class='time'>%s</td><td class='size'>%s</td></tr>\n"),
|
|
|
|
encname,
|
|
|
|
(dirent->stat.isdir? QSE_MT("/"): QSE_MT("")),
|
|
|
|
(dirent->stat.isdir? QSE_MT("dir"): QSE_MT("file")),
|
|
|
|
escname,
|
|
|
|
(dirent->stat.isdir? QSE_MT("/"): QSE_MT("")),
|
|
|
|
tmbuf, fszbuf
|
|
|
|
);
|
|
|
|
|
|
|
|
if (escname != dirent->name) qse_httpd_freemem (httpd, escname);
|
2019-06-25 02:36:32 +00:00
|
|
|
if (encname != dirent->name) QSE_MMGR_FREE (qse_httpd_getmmgr(httpd), encname);
|
2013-04-04 15:04:45 +00:00
|
|
|
|
2014-09-11 15:03:12 +00:00
|
|
|
if (x == (qse_size_t)-1) httpd->errnum = QSE_HTTPD_ENOBUF;
|
|
|
|
return x;
|
2013-04-04 15:04:45 +00:00
|
|
|
}
|
|
|
|
|
2012-10-16 08:54:07 +00:00
|
|
|
static int add_footer (qse_httpd_t* httpd, qse_httpd_client_t* client, task_dseg_t* ctx)
|
2012-09-12 15:47:41 +00:00
|
|
|
{
|
2014-09-11 15:03:12 +00:00
|
|
|
qse_size_t x;
|
|
|
|
int rem;
|
2012-09-12 15:47:41 +00:00
|
|
|
|
2014-09-11 15:03:12 +00:00
|
|
|
rem = ctx->chunked? (ctx->bufrem - 5): ctx->bufrem;
|
2012-10-16 08:54:07 +00:00
|
|
|
if (rem < 1)
|
2012-09-12 15:47:41 +00:00
|
|
|
{
|
2012-10-16 08:54:07 +00:00
|
|
|
httpd->errnum = QSE_HTTPD_ENOBUF;
|
2012-09-12 15:47:41 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-09-11 15:03:12 +00:00
|
|
|
x = qse_mbsxfmts (&ctx->buf[ctx->buflen], rem, QSE_MT("</table></div>\n<div class='footer'>%s</div>\n</body></html>"), ctx->foot.ptr);
|
|
|
|
if (x == (qse_size_t)-1)
|
2013-04-04 15:04:45 +00:00
|
|
|
{
|
|
|
|
httpd->errnum = QSE_HTTPD_ENOBUF;
|
|
|
|
return -1;
|
|
|
|
}
|
2012-10-16 08:54:07 +00:00
|
|
|
|
|
|
|
QSE_ASSERT (x < rem);
|
|
|
|
|
2012-09-12 15:47:41 +00:00
|
|
|
ctx->buflen += x;
|
|
|
|
ctx->bufrem -= x;
|
|
|
|
|
2012-10-16 08:54:07 +00:00
|
|
|
if (ctx->chunked)
|
|
|
|
{
|
|
|
|
qse_mbscpy (&ctx->buf[ctx->buflen], QSE_MT("\r\n0\r\n"));
|
|
|
|
ctx->buflen += 5;
|
|
|
|
ctx->bufrem -= 5;
|
|
|
|
|
|
|
|
/* -5 for \r\n0\r\n added above */
|
|
|
|
if (ctx->chunked) close_chunk_data (ctx, ctx->buflen - 5);
|
|
|
|
}
|
2012-09-12 15:47:41 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int task_main_dseg (
|
2012-09-11 16:19:52 +00:00
|
|
|
qse_httpd_t* httpd, qse_httpd_client_t* client, qse_httpd_task_t* task)
|
|
|
|
{
|
|
|
|
task_dseg_t* ctx = (task_dseg_t*)task->ctx;
|
|
|
|
qse_ssize_t n;
|
2014-09-11 15:03:12 +00:00
|
|
|
qse_size_t x;
|
2012-09-11 16:19:52 +00:00
|
|
|
|
2012-09-12 15:47:41 +00:00
|
|
|
if (ctx->bufpos < ctx->buflen)
|
|
|
|
{
|
|
|
|
/* buffer contents not fully sent yet */
|
|
|
|
goto send_dirlist;
|
|
|
|
}
|
2012-09-11 16:19:52 +00:00
|
|
|
|
|
|
|
/* the buffer size is fixed to QSE_COUNTOF(ctx->buf).
|
2012-09-13 12:31:01 +00:00
|
|
|
*
|
2012-09-11 16:19:52 +00:00
|
|
|
* the number of digits need to hold the the size converted to
|
|
|
|
* a hexadecimal notation is roughly (log16(QSE_COUNTOF(ctx->buf) + 1).
|
|
|
|
* it should be safter to use ceil(log16(QSE_COUNTOF(ctx->buf)) + 1
|
|
|
|
* for precision issues.
|
|
|
|
*
|
|
|
|
* 16**X = QSE_COUNTOF(ctx->buf).
|
|
|
|
* X = log16(QSE_COUNTOF(ctx->buf).
|
|
|
|
* X + 1 is a required number of digits.
|
|
|
|
*
|
|
|
|
* Since log16 is not provided, we should use a natural log function
|
|
|
|
* whose base is the constant e (2.718).
|
|
|
|
*
|
|
|
|
* log16(n) = log(n) / log(16)
|
|
|
|
*
|
|
|
|
* The final fomula is here.
|
|
|
|
*
|
|
|
|
* X = ceil((log(QSE_COUNTOF(ctx->buf)) / log(16))) + 1;
|
|
|
|
*
|
|
|
|
* However, i won't use these floating-point opertions.
|
|
|
|
* instead i'll reserve a hardcoded size. so when you change
|
|
|
|
* the size of the buffer arrray, you should check this size.
|
|
|
|
*/
|
|
|
|
|
2012-09-12 15:47:41 +00:00
|
|
|
/* initialize buffer */
|
|
|
|
ctx->dcount = 0; /* reset the entry counter */
|
|
|
|
ctx->bufpos = 0;
|
|
|
|
if (ctx->chunked)
|
|
|
|
{
|
|
|
|
/* reserve space to fill with the chunk length
|
|
|
|
* 4 for the actual chunk length and +2 for \r\n */
|
|
|
|
ctx->buflen = SIZE_CHLEN + SIZE_CHLENCRLF;
|
|
|
|
/* free space remaing in the buffer for the chunk data */
|
|
|
|
ctx->bufrem = QSE_COUNTOF(ctx->buf) - ctx->buflen - SIZE_CHENDCRLF;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ctx->buflen = 0;
|
|
|
|
ctx->bufrem = QSE_COUNTOF(ctx->buf);
|
|
|
|
}
|
2012-09-11 16:19:52 +00:00
|
|
|
|
2012-09-12 15:47:41 +00:00
|
|
|
if (ctx->state & FOOTER_PENDING)
|
2012-09-11 16:19:52 +00:00
|
|
|
{
|
2012-09-12 15:47:41 +00:00
|
|
|
/* only footers yet to be sent */
|
2012-10-16 08:54:07 +00:00
|
|
|
if (add_footer (httpd, client, ctx) <= -1)
|
2012-09-11 16:19:52 +00:00
|
|
|
{
|
|
|
|
/* return an error if the buffer is too small to hold the
|
2013-04-04 15:04:45 +00:00
|
|
|
* trailing footer. you need to increase the buffer size */
|
2012-09-11 16:19:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-09-12 15:47:41 +00:00
|
|
|
ctx->state &= ~FOOTER_PENDING;
|
|
|
|
ctx->state |= FOOTER_ADDED;
|
2012-09-11 16:19:52 +00:00
|
|
|
|
2012-09-12 15:47:41 +00:00
|
|
|
if (ctx->chunked) fill_chunk_length (ctx);
|
|
|
|
goto send_dirlist;
|
2012-09-11 16:19:52 +00:00
|
|
|
}
|
|
|
|
|
2012-09-12 15:47:41 +00:00
|
|
|
if (!(ctx->state & HEADER_ADDED))
|
2012-09-11 16:19:52 +00:00
|
|
|
{
|
2012-09-12 15:47:41 +00:00
|
|
|
/* compose the header since this is the first time. */
|
2013-04-04 15:04:45 +00:00
|
|
|
int is_root;
|
|
|
|
qse_mchar_t* qpath_esc;
|
|
|
|
|
|
|
|
is_root = (qse_mbscmp (ctx->qpath.ptr, QSE_MT("/")) == 0);
|
|
|
|
|
|
|
|
qpath_esc = qse_httpd_escapehtml (httpd, ctx->qpath.ptr);
|
|
|
|
if (qpath_esc == QSE_NULL) return -1;
|
|
|
|
|
2014-12-15 04:26:41 +00:00
|
|
|
/* TODO: page encoding?? utf-8??? or derive name from cmgr or current locale??? */
|
|
|
|
/* TODO: SUPPORT character set. DON't HARD_CODE it to UTF8 */
|
2014-09-11 15:03:12 +00:00
|
|
|
x = qse_mbsxfmts (&ctx->buf[ctx->buflen], ctx->bufrem,
|
2014-12-15 04:26:41 +00:00
|
|
|
QSE_MT("<html><meta http-equiv=\"Content-Type\" content=\"text/html; charset='UTF-8'\">\n<head>%s</head>\n<body>\n<div class='header'>%s</div>\n<div class='body'><table>%s"),
|
|
|
|
ctx->head.ptr, qpath_esc,
|
2013-04-04 15:04:45 +00:00
|
|
|
(is_root? QSE_MT(""): QSE_MT("<tr><td class='name'><a href='../' class='dir'>..</a></td><td class='time'></td><td class='size'></td></tr>\n"))
|
|
|
|
);
|
|
|
|
|
|
|
|
if (qpath_esc != ctx->qpath.ptr) qse_httpd_freemem (httpd, qpath_esc);
|
|
|
|
|
2014-09-11 15:03:12 +00:00
|
|
|
if (x == (qse_size_t)-1)
|
2012-09-11 16:19:52 +00:00
|
|
|
{
|
2012-10-16 08:54:07 +00:00
|
|
|
/* return an error if the buffer is too small to
|
|
|
|
* hold the header(httpd->errnum == QSE_HTTPD_ENOBUF).
|
2013-04-04 15:04:45 +00:00
|
|
|
* i need to increase the buffer size. or i have make
|
2012-10-16 08:54:07 +00:00
|
|
|
* the buffer dynamic. */
|
2013-04-04 15:04:45 +00:00
|
|
|
httpd->errnum = QSE_HTTPD_ENOBUF;
|
2012-09-11 16:19:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-10-16 08:54:07 +00:00
|
|
|
QSE_ASSERT (x < ctx->bufrem);
|
|
|
|
|
2012-09-11 16:19:52 +00:00
|
|
|
ctx->buflen += x;
|
|
|
|
ctx->bufrem -= x;
|
|
|
|
|
2012-09-12 15:47:41 +00:00
|
|
|
ctx->state |= HEADER_ADDED;
|
|
|
|
ctx->dcount++;
|
2012-09-11 16:19:52 +00:00
|
|
|
}
|
|
|
|
|
2012-09-12 15:47:41 +00:00
|
|
|
if (ctx->state & DIRENT_PENDING)
|
2012-10-15 16:39:23 +00:00
|
|
|
{
|
2012-09-12 15:47:41 +00:00
|
|
|
ctx->state &= ~DIRENT_PENDING;
|
2012-10-15 16:39:23 +00:00
|
|
|
}
|
2012-09-12 15:47:41 +00:00
|
|
|
else
|
2012-10-15 16:39:23 +00:00
|
|
|
{
|
2013-02-20 08:47:22 +00:00
|
|
|
if (httpd->opt.scb.dir.read (httpd, ctx->handle, &ctx->dent) <= 0)
|
2014-09-11 15:03:12 +00:00
|
|
|
ctx->state |= DIRENT_NOMORE;
|
2012-10-15 16:39:23 +00:00
|
|
|
}
|
2012-09-11 16:19:52 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2012-10-15 16:39:23 +00:00
|
|
|
if (ctx->state & DIRENT_NOMORE)
|
2012-09-11 16:19:52 +00:00
|
|
|
{
|
2012-10-15 16:39:23 +00:00
|
|
|
/* no more directory entry */
|
|
|
|
|
2012-10-16 08:54:07 +00:00
|
|
|
if (add_footer (httpd, client, ctx) <= -1)
|
2012-09-11 16:19:52 +00:00
|
|
|
{
|
2012-09-12 15:47:41 +00:00
|
|
|
/* failed to add the footer part */
|
|
|
|
if (ctx->chunked)
|
|
|
|
{
|
|
|
|
close_chunk_data (ctx, ctx->buflen);
|
|
|
|
fill_chunk_length (ctx);
|
|
|
|
}
|
|
|
|
ctx->state |= FOOTER_PENDING;
|
2012-09-11 16:19:52 +00:00
|
|
|
}
|
2012-09-12 15:47:41 +00:00
|
|
|
else if (ctx->chunked) fill_chunk_length (ctx);
|
2014-09-11 15:03:12 +00:00
|
|
|
break;
|
2012-09-11 16:19:52 +00:00
|
|
|
}
|
2012-10-15 16:39:23 +00:00
|
|
|
|
|
|
|
if (qse_mbscmp (ctx->dent.name, QSE_MT(".")) != 0 &&
|
|
|
|
qse_mbscmp (ctx->dent.name, QSE_MT("..")) != 0)
|
2012-09-11 16:19:52 +00:00
|
|
|
{
|
2013-04-04 15:04:45 +00:00
|
|
|
x = format_dirent (httpd, client, &ctx->dent, &ctx->buf[ctx->buflen], ctx->bufrem);
|
2014-09-11 15:03:12 +00:00
|
|
|
if (x == (qse_size_t)-1)
|
2012-09-11 16:19:52 +00:00
|
|
|
{
|
|
|
|
/* buffer not large enough to hold this entry */
|
2012-09-12 15:47:41 +00:00
|
|
|
if (ctx->dcount <= 0)
|
|
|
|
{
|
|
|
|
/* neither directory entry nor the header
|
|
|
|
* has been added to the buffer so far. and
|
|
|
|
* this attempt has failed. the buffer size must
|
|
|
|
* be too small. you must increase it */
|
|
|
|
httpd->errnum = QSE_HTTPD_EINTERN;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->chunked)
|
|
|
|
{
|
|
|
|
close_chunk_data (ctx, ctx->buflen);
|
|
|
|
fill_chunk_length (ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->state |= DIRENT_PENDING;
|
2012-09-11 16:19:52 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-10-16 08:54:07 +00:00
|
|
|
QSE_ASSERT (x < ctx->bufrem);
|
|
|
|
|
2012-09-11 16:19:52 +00:00
|
|
|
ctx->buflen += x;
|
|
|
|
ctx->bufrem -= x;
|
2012-09-12 15:47:41 +00:00
|
|
|
ctx->dcount++;
|
|
|
|
ctx->tcount++;
|
2012-09-11 16:19:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-20 08:47:22 +00:00
|
|
|
if (httpd->opt.scb.dir.read (httpd, ctx->handle, &ctx->dent) <= 0)
|
2012-10-15 16:39:23 +00:00
|
|
|
ctx->state |= DIRENT_NOMORE;
|
2012-09-11 16:19:52 +00:00
|
|
|
}
|
|
|
|
while (1);
|
|
|
|
|
|
|
|
|
|
|
|
send_dirlist:
|
|
|
|
httpd->errnum = QSE_HTTPD_ENOERR;
|
2013-02-20 08:47:22 +00:00
|
|
|
n = httpd->opt.scb.client.send (
|
2012-09-12 15:47:41 +00:00
|
|
|
httpd, client, &ctx->buf[ctx->bufpos], ctx->buflen - ctx->bufpos);
|
2012-09-11 16:19:52 +00:00
|
|
|
if (n <= -1) return -1;
|
|
|
|
|
|
|
|
/* NOTE if (n == 0), it will enter an infinite loop */
|
2014-09-11 15:03:12 +00:00
|
|
|
|
2012-09-11 16:19:52 +00:00
|
|
|
ctx->bufpos += n;
|
2012-10-15 16:39:23 +00:00
|
|
|
return (ctx->bufpos < ctx->buflen || (ctx->state & FOOTER_PENDING) || !(ctx->state & DIRENT_NOMORE))? 1: 0;
|
2012-09-11 16:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static qse_httpd_task_t* entask_directory_segment (
|
|
|
|
qse_httpd_t* httpd, qse_httpd_client_t* client,
|
2014-09-16 14:14:51 +00:00
|
|
|
qse_httpd_task_t* pred, qse_httpd_hnd_t handle, task_dir_t* dir)
|
2012-09-11 16:19:52 +00:00
|
|
|
{
|
|
|
|
qse_httpd_task_t task;
|
|
|
|
task_dseg_t data;
|
|
|
|
|
|
|
|
QSE_MEMSET (&data, 0, QSE_SIZEOF(data));
|
|
|
|
data.handle = handle;
|
2012-09-12 15:47:41 +00:00
|
|
|
data.version = dir->version;
|
|
|
|
data.keepalive = dir->keepalive;
|
|
|
|
data.chunked = dir->keepalive;
|
|
|
|
data.path = dir->path;
|
2012-09-25 02:47:25 +00:00
|
|
|
data.qpath = dir->qpath;
|
2013-04-04 15:04:45 +00:00
|
|
|
data.head = dir->head;
|
|
|
|
data.foot = dir->foot;
|
2012-09-11 16:19:52 +00:00
|
|
|
|
|
|
|
QSE_MEMSET (&task, 0, QSE_SIZEOF(task));
|
|
|
|
task.init = task_init_dseg;
|
2012-09-12 15:47:41 +00:00
|
|
|
task.main = task_main_dseg;
|
2012-09-11 16:19:52 +00:00
|
|
|
task.fini = task_fini_dseg;
|
|
|
|
task.ctx = &data;
|
|
|
|
|
2013-04-04 15:04:45 +00:00
|
|
|
return qse_httpd_entask (httpd, client, pred, &task, QSE_SIZEOF(data) + data.path.len + 1 + data.qpath.len + 1 + data.head.len + 1 + data.foot.len + 1);
|
2012-09-11 16:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------*/
|
|
|
|
|
2013-03-28 09:43:59 +00:00
|
|
|
static int task_init_getdir (
|
2012-09-11 16:19:52 +00:00
|
|
|
qse_httpd_t* httpd, qse_httpd_client_t* client, qse_httpd_task_t* task)
|
|
|
|
{
|
|
|
|
task_dir_t* xtn = qse_httpd_gettaskxtn (httpd, task);
|
2012-09-25 02:47:25 +00:00
|
|
|
task_dir_t* arg = (task_dir_t*)task->ctx;
|
2012-09-11 16:19:52 +00:00
|
|
|
|
|
|
|
/* deep-copy the context data to the extension area */
|
2012-09-25 02:47:25 +00:00
|
|
|
QSE_MEMCPY (xtn, arg, QSE_SIZEOF(*xtn));
|
2012-09-12 15:47:41 +00:00
|
|
|
|
2012-09-25 02:47:25 +00:00
|
|
|
xtn->path.ptr = (qse_mchar_t*)(xtn + 1);
|
|
|
|
qse_mbscpy ((qse_mchar_t*)xtn->path.ptr, arg->path.ptr);
|
2012-10-16 08:54:07 +00:00
|
|
|
xtn->qpath.ptr = xtn->path.ptr + xtn->path.len + 1;
|
2012-09-25 02:47:25 +00:00
|
|
|
qse_mbscpy ((qse_mchar_t*)xtn->qpath.ptr, arg->qpath.ptr);
|
2012-09-11 16:19:52 +00:00
|
|
|
|
2013-04-04 15:04:45 +00:00
|
|
|
xtn->head.ptr = xtn->qpath.ptr + xtn->qpath.len + 1;
|
|
|
|
qse_mbscpy ((qse_mchar_t*)xtn->head.ptr, arg->head.ptr);
|
|
|
|
xtn->foot.ptr = xtn->head.ptr + xtn->head.len + 1;
|
|
|
|
qse_mbscpy ((qse_mchar_t*)xtn->foot.ptr, arg->foot.ptr);
|
|
|
|
|
2012-09-11 16:19:52 +00:00
|
|
|
/* switch the context to the extension area */
|
|
|
|
task->ctx = xtn;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-28 09:43:59 +00:00
|
|
|
static QSE_INLINE int task_main_getdir (
|
2012-09-11 16:19:52 +00:00
|
|
|
qse_httpd_t* httpd, qse_httpd_client_t* client, qse_httpd_task_t* task)
|
|
|
|
{
|
|
|
|
task_dir_t* dir;
|
|
|
|
qse_httpd_task_t* x;
|
|
|
|
|
|
|
|
dir = (task_dir_t*)task->ctx;
|
|
|
|
x = task;
|
|
|
|
|
2013-04-04 15:04:45 +00:00
|
|
|
/* arrange to return the header part first */
|
|
|
|
if (dir->method == QSE_HTTP_HEAD)
|
2012-09-11 16:19:52 +00:00
|
|
|
{
|
2013-04-04 15:04:45 +00:00
|
|
|
x = qse_httpd_entaskformat (
|
|
|
|
httpd, client, x,
|
|
|
|
QSE_MT("HTTP/%d.%d 200 OK\r\nServer: %s\r\nDate: %s\r\nConnection: %s\r\nContent-Type: text/html\r\nContent-Length: 0\r\n\r\n"),
|
|
|
|
dir->version.major, dir->version.minor,
|
|
|
|
qse_httpd_getname (httpd),
|
|
|
|
qse_httpd_fmtgmtimetobb (httpd, QSE_NULL, 0),
|
|
|
|
(dir->keepalive? QSE_MT("keep-alive"): QSE_MT("close"))
|
|
|
|
);
|
|
|
|
|
|
|
|
httpd->opt.scb.dir.close (httpd, dir->handle);
|
|
|
|
return (x == QSE_NULL)? -1: 0;
|
2012-09-11 16:19:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-10-24 07:25:31 +00:00
|
|
|
int keepalive_ignored = 0;
|
|
|
|
|
2014-10-24 15:37:42 +00:00
|
|
|
if (dir->keepalive && qse_comparehttpversions (&dir->version, &qse_http_v11) < 0)
|
2014-10-24 07:25:31 +00:00
|
|
|
{
|
|
|
|
/* this task does chunking when keepalive is set.
|
|
|
|
* chunking is not supported for http 1.0 or earlier.
|
|
|
|
* force switch to the close mode */
|
|
|
|
dir->keepalive = 0;
|
|
|
|
keepalive_ignored = 1;
|
|
|
|
}
|
|
|
|
|
2013-04-04 15:04:45 +00:00
|
|
|
x = qse_httpd_entaskformat (
|
|
|
|
httpd, client, x,
|
2014-10-24 07:25:31 +00:00
|
|
|
QSE_MT("HTTP/%d.%d 200 OK\r\nServer: %s\r\nDate: %s\r\nConnection: %s\r\nContent-Type: text/html\r\n%s\r\n"),
|
2013-04-04 15:04:45 +00:00
|
|
|
dir->version.major, dir->version.minor,
|
|
|
|
qse_httpd_getname (httpd),
|
|
|
|
qse_httpd_fmtgmtimetobb (httpd, QSE_NULL, 0),
|
|
|
|
(dir->keepalive? QSE_MT("keep-alive"): QSE_MT("close")),
|
|
|
|
(dir->keepalive? QSE_MT("Transfer-Encoding: chunked\r\n"): QSE_MT(""))
|
|
|
|
);
|
|
|
|
if (x)
|
|
|
|
{
|
2013-04-06 13:39:56 +00:00
|
|
|
#if 0
|
|
|
|
if (httpd->opt.trait & QSE_HTTPD_LOGACC)
|
|
|
|
{
|
|
|
|
qse_httpd_reqsum_t reqsum;
|
|
|
|
|
|
|
|
acc.remote = remote;
|
2014-08-25 16:18:17 +00:00
|
|
|
acc.qpath = qpath;
|
2013-04-06 13:39:56 +00:00
|
|
|
acc.status = 200;
|
|
|
|
acc.version = ...;
|
|
|
|
acc.method = ...;
|
|
|
|
|
|
|
|
httpd->opt.rcb.logacc (httpd, &reqsum);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-04-04 15:04:45 +00:00
|
|
|
/* arrange to send the actual directory contents */
|
|
|
|
x = entask_directory_segment (httpd, client, x, dir->handle, dir);
|
2014-10-24 07:25:31 +00:00
|
|
|
if (keepalive_ignored && x)
|
|
|
|
x = qse_httpd_entaskdisconnect (httpd, client, x);
|
2013-04-04 15:04:45 +00:00
|
|
|
if (x) return 0;
|
2014-10-24 07:25:31 +00:00
|
|
|
|
2013-04-04 15:04:45 +00:00
|
|
|
}
|
2012-10-16 08:54:07 +00:00
|
|
|
|
2013-04-04 15:04:45 +00:00
|
|
|
httpd->opt.scb.dir.close (httpd, dir->handle);
|
|
|
|
return -1;
|
2012-09-11 16:19:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
qse_httpd_task_t* qse_httpd_entaskdir (
|
|
|
|
qse_httpd_t* httpd,
|
|
|
|
qse_httpd_client_t* client,
|
|
|
|
qse_httpd_task_t* pred,
|
2013-04-04 15:04:45 +00:00
|
|
|
qse_httpd_rsrc_dir_t* dir,
|
2012-09-11 16:19:52 +00:00
|
|
|
qse_htre_t* req)
|
|
|
|
{
|
2013-04-04 15:04:45 +00:00
|
|
|
int method;
|
2013-03-28 09:43:59 +00:00
|
|
|
|
2013-04-04 15:04:45 +00:00
|
|
|
method = qse_htre_getqmethodtype(req);
|
2012-09-11 16:19:52 +00:00
|
|
|
|
2013-03-28 09:43:59 +00:00
|
|
|
/* i don't need contents for directories */
|
|
|
|
qse_htre_discardcontent (req);
|
2013-03-26 16:29:26 +00:00
|
|
|
|
2013-04-04 15:04:45 +00:00
|
|
|
switch (method)
|
2013-03-24 16:35:44 +00:00
|
|
|
{
|
2013-03-28 09:43:59 +00:00
|
|
|
case QSE_HTTP_OPTIONS:
|
|
|
|
return qse_httpd_entaskallow (httpd, client, pred,
|
|
|
|
QSE_MT("OPTIONS,GET,HEAD,POST,PUT,DELETE"), req);
|
|
|
|
|
2013-03-24 16:35:44 +00:00
|
|
|
case QSE_HTTP_HEAD:
|
|
|
|
case QSE_HTTP_GET:
|
|
|
|
case QSE_HTTP_POST:
|
2013-04-04 15:04:45 +00:00
|
|
|
{
|
|
|
|
task_dir_t data;
|
|
|
|
QSE_MEMSET (&data, 0, QSE_SIZEOF(data));
|
|
|
|
|
|
|
|
/* check if the directory stream can be opened before
|
|
|
|
* creating an actual task. */
|
|
|
|
if (httpd->opt.scb.dir.open (httpd, dir->path, &data.handle) <= -1)
|
|
|
|
{
|
|
|
|
/* arrange a status code to return */
|
|
|
|
int status;
|
|
|
|
status = (httpd->errnum == QSE_HTTPD_ENOENT)? 404:
|
|
|
|
(httpd->errnum == QSE_HTTPD_EACCES)? 403: 500;
|
2014-10-09 14:42:02 +00:00
|
|
|
return qse_httpd_entaskerror (httpd, client, pred, status, req);
|
2013-04-04 15:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* create a directory listing task */
|
|
|
|
qse_httpd_task_t task, * x;
|
|
|
|
|
2014-08-11 05:44:03 +00:00
|
|
|
data.path.ptr = (qse_mchar_t*)dir->path;
|
2013-04-04 15:04:45 +00:00
|
|
|
data.path.len = qse_mbslen(data.path.ptr);
|
|
|
|
data.qpath.ptr = qse_htre_getqpath(req);
|
|
|
|
data.qpath.len = qse_mbslen(data.qpath.ptr);
|
2014-08-11 05:44:03 +00:00
|
|
|
data.head.ptr = dir->head? (qse_mchar_t*)dir->head: QSE_MT("<style type='text/css'>body { background-color:#d0e4fe; font-size: 0.9em; } div.header { font-weight: bold; margin-bottom: 5px; } div.footer { border-top: 1px solid #99AABB; text-align: right; } table { font-size: 0.9em; } td { white-space: nowrap; } td.size { text-align: right; }</style>");
|
2013-04-04 15:04:45 +00:00
|
|
|
data.head.len = qse_mbslen(data.head.ptr);
|
|
|
|
data.foot.ptr = dir->foot? dir->foot: qse_httpd_getname(httpd);
|
|
|
|
data.foot.len = qse_mbslen(data.foot.ptr);
|
|
|
|
data.version = *qse_htre_getversion(req);
|
2014-10-24 07:25:31 +00:00
|
|
|
data.keepalive = req->flags & QSE_HTRE_ATTR_KEEPALIVE;
|
2013-04-04 15:04:45 +00:00
|
|
|
data.method = method;
|
|
|
|
|
|
|
|
QSE_MEMSET (&task, 0, QSE_SIZEOF(task));
|
|
|
|
task.init = task_init_getdir;
|
|
|
|
task.main = task_main_getdir;
|
|
|
|
task.ctx = &data;
|
|
|
|
|
|
|
|
x = qse_httpd_entask (httpd, client, pred, &task,
|
|
|
|
QSE_SIZEOF(task_dir_t) + data.path.len + 1 + data.qpath.len + 1 +
|
|
|
|
data.head.len + 1 + data.foot.len + 1);
|
|
|
|
if (x == QSE_NULL)
|
|
|
|
{
|
|
|
|
httpd->opt.scb.dir.close (httpd, data.handle);
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
}
|
2014-10-24 07:25:31 +00:00
|
|
|
|
2013-03-28 09:43:59 +00:00
|
|
|
case QSE_HTTP_PUT:
|
|
|
|
{
|
|
|
|
int status = 201; /* 201 Created */
|
|
|
|
|
2013-04-04 15:04:45 +00:00
|
|
|
if (httpd->opt.scb.dir.make (httpd, dir->path) <= -1)
|
2013-03-28 09:43:59 +00:00
|
|
|
{
|
|
|
|
if (httpd->errnum == QSE_HTTPD_EEXIST)
|
|
|
|
{
|
|
|
|
/* an entry with the same name exists.
|
|
|
|
* if it is a directory, it's considered ok.
|
|
|
|
* if not, send '403 forbidden' indicating you can't
|
|
|
|
* change a file to a directory */
|
|
|
|
qse_httpd_stat_t st;
|
2013-04-04 15:04:45 +00:00
|
|
|
status = (httpd->opt.scb.dir.stat (httpd, dir->path, &st) <= -1)? 403: 204;
|
2013-03-28 09:43:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = (httpd->errnum == QSE_HTTPD_ENOENT)? 404:
|
|
|
|
(httpd->errnum == QSE_HTTPD_EACCES)? 403: 500;
|
|
|
|
}
|
|
|
|
}
|
2014-10-24 07:25:31 +00:00
|
|
|
|
2014-10-09 14:42:02 +00:00
|
|
|
return qse_httpd_entaskerror (httpd, client, pred, status, req);
|
2013-03-28 09:43:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
case QSE_HTTP_DELETE:
|
|
|
|
{
|
|
|
|
int status = 200;
|
|
|
|
|
2013-04-04 15:04:45 +00:00
|
|
|
if (httpd->opt.scb.dir.purge (httpd, dir->path) <= -1)
|
2013-03-28 09:43:59 +00:00
|
|
|
{
|
|
|
|
status = (httpd->errnum == QSE_HTTPD_ENOENT)? 404:
|
|
|
|
(httpd->errnum == QSE_HTTPD_EACCES)? 403: 500;
|
|
|
|
}
|
2014-09-11 15:03:12 +00:00
|
|
|
|
2014-10-09 14:42:02 +00:00
|
|
|
return qse_httpd_entaskerror (httpd, client, pred, status, req);
|
2013-03-28 09:43:59 +00:00
|
|
|
}
|
2013-03-24 16:35:44 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
/* Method not allowed */
|
2014-10-09 14:42:02 +00:00
|
|
|
return qse_httpd_entaskerror (httpd, client, pred, 405, req);
|
2013-03-24 16:35:44 +00:00
|
|
|
}
|
2012-09-11 16:19:52 +00:00
|
|
|
}
|