replaced a few calls to snprintf() with qse_fmtuintmaxtombs().

changed the behavior when handling a query path without a slash
This commit is contained in:
hyung-hwan 2012-09-27 12:54:35 +00:00
parent 5b07370866
commit e0353fca2f
8 changed files with 74 additions and 78 deletions

View File

@ -21,6 +21,10 @@
#ifndef _QSE_LIB_CMN_SYSCALL_H_ #ifndef _QSE_LIB_CMN_SYSCALL_H_
#define _QSE_LIB_CMN_SYSCALL_H_ #define _QSE_LIB_CMN_SYSCALL_H_
#if defined(_WIN32) || defined(__OS2__) || defined(__DOS__)
# error Do not include this file
#endif
/* This file defines unix/linux system calls */ /* This file defines unix/linux system calls */
#if defined(HAVE_SYS_TYPES_H) #if defined(HAVE_SYS_TYPES_H)

View File

@ -1049,20 +1049,21 @@ qse_printf (QSE_T("TASK_MAIN_CGI_4\n"));
if (cgi->resflags & CGI_RES_CLIENT_CHUNK) if (cgi->resflags & CGI_RES_CLIENT_CHUNK)
{ {
qse_size_t count, extra; qse_size_t count, extra;
qse_mchar_t chunklen[7];
/* this function assumes that the chunk length does not /* this function assumes that the chunk length does not
* exceed 4 hexadecimal digits. */ * exceed 4 hexadecimal digits. */
QSE_ASSERT (QSE_SIZEOF(cgi->buf) <= 0xFFFF); QSE_ASSERT (QSE_SIZEOF(cgi->buf) <= 0xFFFF);
#define CHLEN_RESERVE 6
qse_printf (QSE_T("READING CHUNKED MODE...\n")); qse_printf (QSE_T("READING CHUNKED MODE...\n"));
extra = (QSE_SIZEOF(chunklen) - 1) + 2; extra = CHLEN_RESERVE + 2;
count = QSE_SIZEOF(cgi->buf) - cgi->buflen; count = QSE_SIZEOF(cgi->buf) - cgi->buflen;
if (count > extra) if (count > extra)
{ {
n = qse_pio_read ( n = qse_pio_read (
&cgi->pio, QSE_PIO_OUT, &cgi->pio, QSE_PIO_OUT,
&cgi->buf[cgi->buflen + QSE_SIZEOF(chunklen) - 1], &cgi->buf[cgi->buflen + CHLEN_RESERVE],
count - extra count - extra
); );
if (n <= -1) if (n <= -1)
@ -1085,13 +1086,19 @@ qse_printf (QSE_T("TASK_MAIN_CGI_4\n"));
return 1; return 1;
} }
/* set the chunk length */ /* set the chunk length. if the length string is less
/* TODO: chagne snprintf to qse_fmtuintmaxtombs() */ * than 4 digits, the right side of the string is filled
snprintf (chunklen, QSE_COUNTOF(chunklen), * with space letters. for example, the chunk length line
QSE_MT("%-4lX\r\n"), (unsigned long)n); * for the length 10 will be "A \r\n". */
QSE_MEMCPY (&cgi->buf[cgi->buflen], cgi->buflen += qse_fmtuintmaxtombs (
chunklen, QSE_SIZEOF(chunklen) - 1); &cgi->buf[cgi->buflen], CHLEN_RESERVE - 2 + 1,
cgi->buflen += QSE_SIZEOF(chunklen) - 1 + n; n, 16 | QSE_FMTUINTMAXTOMBS_UPPERCASE | QSE_FMTUINTMAXTOMBS_FILLRIGHT,
-1, QSE_MT(' '), QSE_NULL
);
cgi->buf[cgi->buflen++] = QSE_MT('\r');
cgi->buf[cgi->buflen++] = QSE_MT('\n');
cgi->buflen += n; /* +n for the data read above */
/* set the trailing CR & LF for a chunk */ /* set the trailing CR & LF for a chunk */
cgi->buf[cgi->buflen++] = QSE_MT('\r'); cgi->buf[cgi->buflen++] = QSE_MT('\r');

View File

@ -20,9 +20,20 @@
#include "httpd.h" #include "httpd.h"
#include "../cmn/mem.h" #include "../cmn/mem.h"
#include "../cmn/syscall.h"
#include <qse/cmn/str.h> #include <qse/cmn/str.h>
#include <qse/cmn/fmt.h>
#include <qse/cmn/path.h> #include <qse/cmn/path.h>
#if defined(_WIN32)
/* TODO: */
#elif defined(__OS2__)
/* TODO: */
#elif defined(__DOS__)
/* TODO: */
#else
# include "../cmn/syscall.h"
#endif
#include <qse/cmn/stdio.h> /* TODO: remove this */ #include <qse/cmn/stdio.h> /* TODO: remove this */
typedef struct task_dir_t task_dir_t; typedef struct task_dir_t task_dir_t;
@ -106,13 +117,15 @@ static void fill_chunk_length (task_dseg_t* ctx)
int x; int x;
/* right alignment with space padding on the left */ /* right alignment with space padding on the left */
/* TODO: change snprintf to qse_fmtuintmaxtombs() */ x = qse_fmtuintmaxtombs (
x = snprintf (
ctx->buf, (SIZE_CHLEN + SIZE_CHLENCRLF) - 1, ctx->buf, (SIZE_CHLEN + SIZE_CHLENCRLF) - 1,
QSE_MT("%*lX"), (int)(SIZE_CHLEN + SIZE_CHLENCRLF - 2), (ctx->chunklen - (SIZE_CHLEN + SIZE_CHLENCRLF)), /* value to convert */
(unsigned long)(ctx->chunklen - (SIZE_CHLEN + SIZE_CHLENCRLF))); 16 | QSE_FMTUINTMAXTOMBS_UPPERCASE, /* uppercase hexadecimal letters */
-1, /* no particular precision - want space filled if less than 4 digits */
/* i don't check the error of snprintf because i've secured the QSE_MT(' '), /* fill with space */
QSE_NULL
);
/* i don't check the buffer size error because i've secured the
* suffient space for the chunk length at the beginning of the buffer */ * suffient space for the chunk length at the beginning of the buffer */
/* CHLENCRLF */ /* CHLENCRLF */
@ -380,7 +393,6 @@ static qse_httpd_task_t* entask_directory_segment (
task.fini = task_fini_dseg; task.fini = task_fini_dseg;
task.ctx = &data; task.ctx = &data;
qse_printf (QSE_T("Debug: entasking directory segment (%d)\n"), client->handle.i);
return qse_httpd_entask (httpd, client, pred, &task, QSE_SIZEOF(data) + data.path.len + 1 + data.qpath.len + 1); return qse_httpd_entask (httpd, client, pred, &task, QSE_SIZEOF(data) + data.path.len + 1 + data.qpath.len + 1);
} }

View File

@ -20,11 +20,21 @@
#include "httpd.h" #include "httpd.h"
#include "../cmn/mem.h" #include "../cmn/mem.h"
#include "../cmn/syscall.h"
#include <qse/cmn/str.h> #include <qse/cmn/str.h>
#include <qse/cmn/fmt.h> #include <qse/cmn/fmt.h>
#include <qse/cmn/path.h> #include <qse/cmn/path.h>
#include <qse/cmn/time.h> #include <qse/cmn/time.h>
#if defined(_WIN32)
/* TODO: */
#elif defined(__OS2__)
/* TODO: */
#elif defined(__DOS__)
/* TODO: */
#else
# include "../cmn/syscall.h"
#endif
#include <qse/cmn/stdio.h> /* TODO: remove this */ #include <qse/cmn/stdio.h> /* TODO: remove this */
#define ETAG_LEN_MAX 127 #define ETAG_LEN_MAX 127
@ -120,7 +130,6 @@ static qse_httpd_task_t* entask_file_segment (
task.fini = task_fini_fseg; task.fini = task_fini_fseg;
task.ctx = &data; task.ctx = &data;
qse_printf (QSE_T("Debug: entasking file segment (%d)\n"), client->handle.i);
return qse_httpd_entask (httpd, client, pred, &task, QSE_SIZEOF(data)); return qse_httpd_entask (httpd, client, pred, &task, QSE_SIZEOF(data));
} }
@ -162,8 +171,6 @@ static QSE_INLINE int task_main_file (
/* TODO: if you should deal with files on a network-mounted drive, /* TODO: if you should deal with files on a network-mounted drive,
setting a trigger or non-blocking I/O are needed. */ setting a trigger or non-blocking I/O are needed. */
qse_printf (QSE_T("opening file %hs\n"), file->path);
httpd->errnum = QSE_HTTPD_ENOERR; httpd->errnum = QSE_HTTPD_ENOERR;
if (httpd->scb->file.stat (httpd, file->path.ptr, &st) <= -1) if (httpd->scb->file.stat (httpd, file->path.ptr, &st) <= -1)
{ {

View File

@ -18,10 +18,6 @@
License along with QSE. If not, see <htrd://www.gnu.org/licenses/>. License along with QSE. If not, see <htrd://www.gnu.org/licenses/>.
*/ */
#if defined(_WIN32) || defined(__DOS__) || defined(__OS2__)
/* UNSUPPORTED YET.. */
/* TODO: IMPLEMENT THIS */
#else
#include "httpd.h" #include "httpd.h"
#include "../cmn/mem.h" #include "../cmn/mem.h"
@ -447,10 +443,15 @@ qse_printf (QSE_T("NORMAL REPLY 222222222222222222222 NORMAL REPLY\n"));
if (proxy->resflags & PROXY_RES_CLIENT_CHUNK && if (proxy->resflags & PROXY_RES_CLIENT_CHUNK &&
qse_comparehttpversions (&res->version, &qse_http_v11) < 0) qse_comparehttpversions (&res->version, &qse_http_v11) < 0)
{ {
qse_mchar_t vbuf[64]; qse_mchar_t major[32], minor[32];
snprintf (vbuf, QSE_COUNTOF(vbuf), QSE_MT("HTTP/%d.%d"),
(int)proxy->version.major, (int)proxy->version.minor); qse_fmtuintmaxtombs (major, QSE_COUNTOF(major), proxy->version.major, 10, -1, QSE_MT('\0'), QSE_NULL);
if (qse_mbs_cat (proxy->res, vbuf) == (qse_size_t)-1) qse_fmtuintmaxtombs (minor, QSE_COUNTOF(minor), proxy->version.minor, 10, -1, QSE_MT('\0'), QSE_NULL);
if (qse_mbs_cat (proxy->res, QSE_MT("HTTP/")) == (qse_size_t)-1 ||
qse_mbs_cat (proxy->res, major) == (qse_size_t)-1 ||
qse_mbs_cat (proxy->res, QSE_MT(".")) == (qse_size_t)-1 ||
qse_mbs_cat (proxy->res, minor) == (qse_size_t)-1)
{ {
proxy->httpd->errnum = QSE_HTTPD_ENOMEM; proxy->httpd->errnum = QSE_HTTPD_ENOMEM;
return -1; return -1;
@ -1468,4 +1469,3 @@ qse_httpd_task_t* qse_httpd_entaskproxy (
); );
} }
#endif

View File

@ -1675,34 +1675,7 @@ qse_printf (QSE_T("Host not included....\n"));
#endif #endif
#if 0 /* TODO: investigate if the proxy need to handle 100-continue */
if (peek)
{
if (req->attr.expect &&
(req->version.major > 1 ||
(req->version.major == 1 && req->version.minor >= 1)) &&
!content_received)
{
/* TODO: check method.... */
/* "expect" in the header, version 1.1 or higher,
* and no content received yet */
if (qse_mbscasecmp(req->attr.expect, QSE_MT("100-continue")) != 0)
{
if (qse_httpd_entaskerror (
httpd, client, QSE_NULL, 417, req) == QSE_NULL) return -1;
if (qse_httpd_entaskdisconnect (
httpd, client, QSE_NULL) == QSE_NULL) return -1;
}
else
{
/* TODO: determine if to return 100-continue or other errors */
if (qse_httpd_entaskcontinue (
httpd, client, QSE_NULL, req) == QSE_NULL) return -1;
}
}
}
#endif
if (peek) if (peek)
{ {
@ -1893,12 +1866,15 @@ target->u.proxy.src.u.in4.port = 0;
return 0; return 0;
#endif #endif
if (server_xtn->docroot.ptr) if (server_xtn->docroot.ptr || qpath[0] != QSE_MT('/'))
{ {
const qse_mchar_t* ta[3]; const qse_mchar_t* ta[4];
ta[0] = server_xtn->docroot.ptr; qse_size_t idx = 0;
ta[1] = qpath;
ta[2] = QSE_NULL; if (server_xtn->docroot.ptr) ta[idx++] = server_xtn->docroot.ptr;
if (qpath[0] != QSE_MT('/')) ta[idx++] = QSE_MT("/");
ta[idx++] = qpath;
ta[idx++] = QSE_NULL;
xpath = qse_mbsadup (ta, httpd->mmgr); xpath = qse_mbsadup (ta, httpd->mmgr);
if (xpath == QSE_NULL) if (xpath == QSE_NULL)
{ {
@ -1985,6 +1961,7 @@ return 0;
target->u.file.mime = target->u.file.mime =
qse_mbsend (qpath, QSE_MT(".html"))? QSE_MT("text/html"): qse_mbsend (qpath, QSE_MT(".html"))? QSE_MT("text/html"):
qse_mbsend (qpath, QSE_MT(".txt"))? QSE_MT("text/plain"): qse_mbsend (qpath, QSE_MT(".txt"))? QSE_MT("text/plain"):
qse_mbsend (qpath, QSE_MT(".log"))? QSE_MT("text/plain"):
qse_mbsend (qpath, QSE_MT(".css"))? QSE_MT("text/css"): qse_mbsend (qpath, QSE_MT(".css"))? QSE_MT("text/css"):
qse_mbsend (qpath, QSE_MT(".xml"))? QSE_MT("text/xml"): qse_mbsend (qpath, QSE_MT(".xml"))? QSE_MT("text/xml"):
qse_mbsend (qpath, QSE_MT(".js"))? QSE_MT("application/javascript"): qse_mbsend (qpath, QSE_MT(".js"))? QSE_MT("application/javascript"):

View File

@ -19,19 +19,10 @@
*/ */
#include "httpd.h" #include "httpd.h"
#include "../cmn/mem.h"
#include <qse/cmn/str.h> #include <qse/cmn/str.h>
#include <qse/cmn/fmt.h> #include <qse/cmn/fmt.h>
#include "../cmn/mem.h"
#if defined(_WIN32)
/* TODO */
#elif defined(__DOS__)
/* TODO */
#elif defined(__OS2__)
/* TODO */
#else
# include "../cmn/syscall.h"
#endif
#include <stdarg.h> #include <stdarg.h>
#include <qse/cmn/stdio.h> /* TODO: remove this */ #include <qse/cmn/stdio.h> /* TODO: remove this */

View File

@ -20,8 +20,6 @@
#include "httpd.h" #include "httpd.h"
#include "../cmn/mem.h" #include "../cmn/mem.h"
#include "../cmn/syscall.h"
#include <qse/cmn/chr.h> #include <qse/cmn/chr.h>
#include <qse/cmn/str.h> #include <qse/cmn/str.h>
#include <qse/cmn/mbwc.h> #include <qse/cmn/mbwc.h>
@ -661,7 +659,7 @@ qse_printf (QSE_T("Error: failed to read from a client %d\n"), client->handle.i)
} }
else if (m == 0) else if (m == 0)
{ {
qse_printf (QSE_T("Debug: connection closed %d - errno %d\n"), client->handle.i, errno); qse_printf (QSE_T("Debug: connection closed %d\n"), client->handle.i);
/* reading from the client returned 0. this typically /* reading from the client returned 0. this typically
* happens when the client closes the connection or * happens when the client closes the connection or
* shutdown the writing half of the socket. it's * shutdown the writing half of the socket. it's