+// This program is open source software. Use, duplication, or disclosure
+// is subject to the terms of the GNU General Public License, version 3.
#include "sysdep.h"
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include "libcitadel.h"
+#include <sys/socket.h>
/**
* @defgroup URLHandling ParsedURL object to handle connection data
FreeStrBuf(&(*Url)->URL);
FreeStrBuf(&(*Url)->UrlWithoutCred);
FreeStrBuf(&(*Url)->CurlCreds);
+ FreeStrBuf(&(*Url)->UsrName);
+ FreeStrBuf(&(*Url)->Password);
if ((*Url)->Next != NULL)
FreeURL(&(*Url)->Next);
free(*Url);
*/
int ParseURL(ParsedURL **Url, StrBuf *UrlStr, unsigned short DefaultPort)
{
- const char *pch, *pEndHost, *pPort, *pCredEnd, *pUserEnd;
+ const char *pch, *pPort, *pCredEnd, *pUserEnd;
ParsedURL *url = (ParsedURL *)malloc(sizeof(ParsedURL));
memset(url, 0, sizeof(ParsedURL));
}
}
if (url->LocalPart == NULL) {
- url->LocalPart = pch + StrLength(url->URL);
+ url->LocalPart = ChrPtr(url->URL) + StrLength(url->URL);
}
- pCredEnd = strchr(pch, '@');
+ pCredEnd = strrchr(ChrPtr(url->URL), '@');
if (pCredEnd >= url->LocalPart)
pCredEnd = NULL;
if (pCredEnd != NULL)
StrBufPeek(url->URL, pUserEnd, 0, '\0');
StrBufPeek(url->URL, pCredEnd, 0, '\0');
}
+ else
+ pUserEnd = NULL;
pPort = NULL;
if (*url->Host == '[') {
+ const char *pEndHost;
url->Host ++;
pEndHost = strchr(url->Host, ']');
if (pEndHost == NULL) {
}
if (pPort != NULL)
url->Port = atol(pPort);
- url->IsIP = inet_pton(url->af, url->Host, &url->Addr.sin6_addr);
+ if (url->IPv6)
+ {
+ url->IsIP = inet_pton(AF_INET6, url->Host, &url->Addr.sin6_addr);
+ if (url->IsIP)
+ {
+ url->Addr.sin6_port = htons(url->Port);
+ url->Addr.sin6_port = AF_INET6;
+ }
+ }
+ else
+ {
+ url->IsIP = inet_pton(AF_INET, url->Host, &((struct sockaddr_in *)&(url->Addr))->sin_addr);
+ if (url->IsIP)
+ {
+ ((struct sockaddr_in *)&(url->Addr))->sin_port = htons(url->Port);
+ ((struct sockaddr_in *)&(url->Addr))->sin_family = AF_INET;
+ }
+ }
+
+ if (url->User != NULL) {
+ url->UsrName = NewStrBufPlain(url->User, pUserEnd - url->User);
+ StrBufUnescape(url->UsrName, 0);
+ url->User = ChrPtr(url->UsrName);
+ }
+
+ if (url->Pass != NULL) {
+ url->Password = NewStrBufPlain(url->Pass, pCredEnd - url->Pass);
+ StrBufUnescape(url->Password, 0);
+ url->Pass = ChrPtr(url->Password);
+ }
+
+ if (*Url != NULL)
+ url->Next = *Url;
*Url = url;
return 1;
}