added uri.c tentatively

This commit is contained in:
hyung-hwan 2012-09-28 13:32:02 +00:00
parent e0353fca2f
commit 8736743eeb
2 changed files with 145 additions and 1 deletions

104
qse/lib/cmn/uri.c Normal file
View File

@ -0,0 +1,104 @@
static int qse_ripuri (qse_httpd_t* httpd, const qse_char_t* uri, int flags)
{
const qse_char_t* ptr, * colon, * at;
qse_size_t len;
qse_uri_t xuri;
QSE_MEMSET (xuri, 0, QSE_SIZEOF(xuri));
/* scheme */
xuri.scheme.ptr = ptr;
while (*uri != QSE_T(':'))
{
if (*uri == QSE_T('\0')) return -1;
uri++;
}
xuri.scheme.len = uri - xuri.scheme.ptr;
uri++; /* skip : */
if (*uri != QSE_T('/')) return -1;
uri++; /* skip / */
if (*uri != QSE_T('/')) return -1;
uri++; /* skip / */
/* username, password, host, port */
for (colon = QSE_NULL, at = QSE_NULL, ptr = uri; ; uri++)
{
if (at == QSE_NULL)
{
if (colon == QSE_NULL && *uri == QSE_T(':')) colon = uri;
else if (*uri == QSE_T('@'))
{
if (colon)
{
xuri.user.ptr = ptr;
xuri.user.len = colon - ptr;
xuri.pass.ptr = colon + 1;
xuri.pass.len = uri - colon - 1;
colon = QSE_NULL;
}
else
{
xuri.user.ptr = ptr;
xuri.user.len = uri - ptr;
}
}
else if (*uri == QSE_T('/') || *uri == QSE_T('\0'))
{
xuri.host = xuri.user;
xuir.port = xuri.pass;
xuri.user.ptr = QSE_NULL;
xuri.user.len = 0;
xuri.pass.ptr = QSE_NULL;
xuri.pass.len = 0;
break;
}
}
else
{
if (colon == QSE_NULL && *uri == QSE_T(':')) colon = uri;
else if (*uri == QSE_T('/') || *uri == QSE_T('\0'))
{
if (colon)
{
xuri.host.ptr = ptr;
xuri.host.len = colon - ptr;
xuri.port.ptr = colon + 1;
xuri.port.len = uri - colon - 1;
}
else
{
xuri.host.ptr = ptr;
xuri.host.len = uri - ptr;
}
}
}
}
if (uri == QSE_T('/'))
{
xuri.path.ptr = uri;
while (*uri != QSE_T('\0') && *uri != QSE_T('?')) uri++;
xuri.path.len = uri - xuri.path.ptr;
if (uri == QSE('#'))
{
xuri.query.ptr = ++uri;
while (*uri != QSE_T('\0') && *uri != QSE_T('#')) uri++;
xuri.query.len = uri - xuri.query.ptr;
if (uri == QSE_T('#'))
{
xuri.query.ptr = ++uri;
while (*uri != QSE_T('\0')) uri++;
xuri.fragment.len = uri - xuri.fragment.ptr;
}
}
}
*xuri = scheme;
return 0;
}

View File

@ -54,6 +54,46 @@ static void sigint (int sig)
if (g_httpd) qse_httpd_stop (g_httpd); if (g_httpd) qse_httpd_stop (g_httpd);
} }
/* --------------------------------------------------------------------- */
static qse_httpd_server_t* attach_server (qse_httpd_t* httpd, const qse_char_t* uri)
{
qse_httpd_server_t server, * xserver;
const qse_char_t* docroot;
server_xtn_t* server_xtn;
qse_uri_t xuri;
if (qse_ripuri (uri, &xuri, QSE_RIPURI_NOQUERY | QSE_RIP_URI_NOFRAGMENT) <= -1)
return QSE_NULL;
/* if (parse_server_uri (httpd, uri, &server, &docroot) <= -1) return QSE_NULL;*/
server.predetach = predetach_server;
xserver = qse_httpd_attachserver (
httpd, &server, QSE_SIZEOF(*server_xtn) + xtnsize);
if (xserver == QSE_NULL) return QSE_NULL;
if (docroot[0] == QSE_T('/') && docroot[1] != QSE_T('\0'))
{
server_xtn = qse_httpd_getserverxtn (httpd, xserver);
#if defined(QSE_CHAR_IS_MCHAR)
server_xtn->docroot.ptr = qse_mbsdup (docroot, httpd->mmgr);
#else
server_xtn->docroot.ptr = qse_wcstombsdup (docroot, httpd->mmgr);
#endif
if (server_xtn->docroot.ptr == QSE_NULL)
{
qse_httpd_detachserver (httpd, xserver);
httpd->errnum = QSE_HTTPD_ENOMEM;
return QSE_NULL;
}
server_xtn->docroot.len = qse_mbslen(server_xtn->docroot.ptr);
}
return xserver;
}
/* --------------------------------------------------------------------- */ /* --------------------------------------------------------------------- */
static int httpd_main (int argc, qse_char_t* argv[]) static int httpd_main (int argc, qse_char_t* argv[])
{ {
@ -76,7 +116,7 @@ static int httpd_main (int argc, qse_char_t* argv[])
for (i = 1; i < argc; i++) for (i = 1; i < argc; i++)
{ {
if (qse_httpd_attachserverstd (httpd, argv[i], 0) == QSE_NULL) if (attach_server (httpd, argv[i]) == QSE_NULL)
{ {
qse_fprintf (QSE_STDERR, qse_fprintf (QSE_STDERR,
QSE_T("Failed to add httpd listener - %s\n"), argv[i]); QSE_T("Failed to add httpd listener - %s\n"), argv[i]);