qse/lib/cmn/uri.c

275 lines
6.6 KiB
C
Raw Normal View History

/*
* $Id$
*
Copyright (c) 2006-2019 Chung, Hyung-Hwan. All rights reserved.
2012-09-28 13:32:02 +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.
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.
*/
#include <qse/cmn/uri.h>
2016-04-29 03:55:42 +00:00
#include "mem-prv.h"
2013-02-18 13:45:50 +00:00
int qse_mbstouri (const qse_mchar_t* str, qse_muri_t* uri, int flags)
2012-09-28 13:32:02 +00:00
{
const qse_mchar_t* ptr, * colon;
2013-02-18 13:45:50 +00:00
qse_muri_t xuri;
2012-09-28 13:32:02 +00:00
QSE_MEMSET (&xuri, 0, QSE_SIZEOF(xuri));
2012-09-28 13:32:02 +00:00
/* scheme */
xuri.scheme.ptr = str;
while (*str != QSE_MT(':'))
2012-09-28 13:32:02 +00:00
{
if (*str == QSE_MT('\0')) return -1;
str++;
2012-09-28 13:32:02 +00:00
}
xuri.scheme.len = str - (const qse_mchar_t*)xuri.scheme.ptr;
2012-09-28 13:32:02 +00:00
str++; /* skip : */
if (*str != QSE_MT('/')) return -1;
str++; /* skip / */
if (*str != QSE_MT('/')) return -1;
str++; /* skip / */
2012-09-28 13:32:02 +00:00
/* username, password, host, port */
for (colon = QSE_NULL, ptr = str; ; str++)
2012-09-28 13:32:02 +00:00
{
if (flags & QSE_MBSTOURI_NOAUTH)
{
if (colon == QSE_NULL && *str == QSE_MT(':')) colon = str;
else if (*str == QSE_MT('/') || *str == QSE_MT('\0'))
{
if (colon)
{
xuri.host.ptr = ptr;
xuri.host.len = colon - ptr;
xuri.port.ptr = colon + 1;
xuri.port.len = str - colon - 1;
}
else
{
xuri.host.ptr = ptr;
xuri.host.len = str - ptr;
}
break;
}
}
else
2012-09-28 13:32:02 +00:00
{
if (colon == QSE_NULL && *str == QSE_MT(':')) colon = str;
else if (xuri.auth.user.ptr == QSE_NULL && *str == QSE_MT('@'))
2012-09-28 13:32:02 +00:00
{
if (colon)
{
xuri.auth.user.ptr = ptr;
xuri.auth.user.len = colon - ptr;
xuri.auth.pass.ptr = colon + 1;
xuri.auth.pass.len = str - colon - 1;
2012-09-28 13:32:02 +00:00
colon = QSE_NULL;
}
else
{
xuri.auth.user.ptr = ptr;
xuri.auth.user.len = str - ptr;
2012-09-28 13:32:02 +00:00
}
ptr = str + 1;
2012-09-28 13:32:02 +00:00
}
else if (*str == QSE_MT('/') || *str == QSE_MT('\0'))
2012-09-28 13:32:02 +00:00
{
if (colon)
{
xuri.host.ptr = ptr;
xuri.host.len = colon - ptr;
xuri.port.ptr = colon + 1;
xuri.port.len = str - colon - 1;
}
else
{
xuri.host.ptr = ptr;
xuri.host.len = str - ptr;
}
2012-09-28 13:32:02 +00:00
break;
}
}
}
if (*str == QSE_MT('/'))
{
xuri.path.ptr = str;
while (*str != QSE_MT('\0'))
{
if ((!(flags & QSE_MBSTOURI_NOQUERY) && *str == QSE_MT('?')) ||
(!(flags & QSE_MBSTOURI_NOFRAG) && *str == QSE_MT('#'))) break;
str++;
}
xuri.path.len = str - (const qse_mchar_t*)xuri.path.ptr;
if (!(flags & QSE_MBSTOURI_NOQUERY) && *str == QSE_MT('?'))
{
xuri.query.ptr = ++str;
while (*str != QSE_MT('\0'))
{
if (!(flags & QSE_MBSTOURI_NOFRAG) && *str == QSE_MT('#')) break;
str++;
}
xuri.query.len = str - (const qse_mchar_t*)xuri.query.ptr;
}
if (!(flags & QSE_MBSTOURI_NOFRAG) && *str == QSE_MT('#'))
{
xuri.frag.ptr = ++str;
while (*str != QSE_MT('\0')) str++;
xuri.frag.len = str - (const qse_mchar_t*)xuri.frag.ptr;
}
}
QSE_ASSERT (*str == QSE_MT('\0'));
*uri = xuri;
return 0;
}
/* -------------------------------------------------------- */
2013-02-18 13:45:50 +00:00
int qse_wcstouri (const qse_wchar_t* str, qse_wuri_t* uri, int flags)
{
const qse_wchar_t* ptr, * colon;
2013-02-18 13:45:50 +00:00
qse_wuri_t xuri;
QSE_MEMSET (&xuri, 0, QSE_SIZEOF(xuri));
/* scheme */
xuri.scheme.ptr = str;
while (*str != QSE_WT(':'))
{
if (*str == QSE_WT('\0')) return -1;
str++;
}
xuri.scheme.len = str - (const qse_wchar_t*)xuri.scheme.ptr;
str++; /* skip : */
if (*str != QSE_WT('/')) return -1;
str++; /* skip / */
if (*str != QSE_WT('/')) return -1;
str++; /* skip / */
/* username, password, host, port */
for (colon = QSE_NULL, ptr = str; ; str++)
{
if (flags & QSE_WCSTOURI_NOAUTH)
{
if (colon == QSE_NULL && *str == QSE_WT(':')) colon = str;
else if (*str == QSE_WT('/') || *str == QSE_WT('\0'))
{
if (colon)
{
xuri.host.ptr = ptr;
xuri.host.len = colon - ptr;
xuri.port.ptr = colon + 1;
xuri.port.len = str - colon - 1;
}
else
{
xuri.host.ptr = ptr;
xuri.host.len = str - ptr;
}
2012-09-28 13:32:02 +00:00
break;
}
}
else
{
if (colon == QSE_NULL && *str == QSE_WT(':')) colon = str;
else if (xuri.auth.user.ptr == QSE_NULL && *str == QSE_WT('@'))
{
if (colon)
{
xuri.auth.user.ptr = ptr;
xuri.auth.user.len = colon - ptr;
xuri.auth.pass.ptr = colon + 1;
xuri.auth.pass.len = str - colon - 1;
colon = QSE_NULL;
}
else
{
xuri.auth.user.ptr = ptr;
xuri.auth.user.len = str - ptr;
}
ptr = str + 1;
}
else if (*str == QSE_WT('/') || *str == QSE_WT('\0'))
2012-09-28 13:32:02 +00:00
{
if (colon)
{
xuri.host.ptr = ptr;
xuri.host.len = colon - ptr;
xuri.port.ptr = colon + 1;
xuri.port.len = str - colon - 1;
2012-09-28 13:32:02 +00:00
}
else
{
xuri.host.ptr = ptr;
xuri.host.len = str - ptr;
2012-09-28 13:32:02 +00:00
}
break;
2012-09-28 13:32:02 +00:00
}
}
}
if (*str == QSE_WT('/'))
2012-09-28 13:32:02 +00:00
{
xuri.path.ptr = str;
while (*str != QSE_WT('\0'))
2012-09-28 13:32:02 +00:00
{
if ((!(flags & QSE_WCSTOURI_NOQUERY) && *str == QSE_WT('?')) ||
(!(flags & QSE_WCSTOURI_NOFRAG) && *str == QSE_WT('#'))) break;
str++;
}
xuri.path.len = str - (const qse_wchar_t*)xuri.path.ptr;
2012-09-28 13:32:02 +00:00
if (!(flags & QSE_WCSTOURI_NOQUERY) && *str == QSE_WT('?'))
{
xuri.query.ptr = ++str;
while (*str != QSE_WT('\0'))
2012-09-28 13:32:02 +00:00
{
if (!(flags & QSE_WCSTOURI_NOFRAG) && *str == QSE_WT('#')) break;
str++;
2012-09-28 13:32:02 +00:00
}
xuri.query.len = str - (const qse_wchar_t*)xuri.query.ptr;
}
if (!(flags & QSE_WCSTOURI_NOFRAG) && *str == QSE_WT('#'))
{
xuri.frag.ptr = ++str;
while (*str != QSE_WT('\0')) str++;
xuri.frag.len = str - (const qse_wchar_t*)xuri.frag.ptr;
2012-09-28 13:32:02 +00:00
}
}
QSE_ASSERT (*str == QSE_WT('\0'));
*uri = xuri;
2012-09-28 13:32:02 +00:00
return 0;
}