8 #include <sys/select.h>
10 #include <sys/types.h>
11 #include "libcitadel.h"
14 * @defgroup URLHandling ParsedURL object to handle connection data
18 * @ingroup URLHandling
19 * @brief frees a linked list of ParsedURL
20 * @param Url (list) of ParsedURL to be freet; Pointer is NULL'ed for the caller.
22 void FreeURL(ParsedURL** Url)
25 FreeStrBuf(&(*Url)->URL);
26 if ((*Url)->Next != NULL)
27 FreeURL(&(*Url)->Next);
34 * @ingroup URLHandling
35 * @brief parses the string provided with UrlStr into *Url
36 * @param Url on success this contains the parsed object; needs to be free'd by caller.
37 * @param UrlStr String we should parse into parts
38 * @param DefaultPort Which is the default port here?
40 int ParseURL(ParsedURL **Url, StrBuf *UrlStr, unsigned short DefaultPort)
42 const char *pch, *pEndHost, *pPort, *pCredEnd, *pUserEnd;
43 ParsedURL *url = (ParsedURL *)malloc(sizeof(ParsedURL));
44 memset(url, 0, sizeof(ParsedURL));
47 url->Port = DefaultPort;
49 * http://username:passvoid@[ipv6]:port/url
51 url->URL = NewStrBufDup(UrlStr);
52 url->Host = pch = ChrPtr(url->URL);
53 url->LocalPart = strchr(pch, '/');
54 if (url->LocalPart != NULL) {
55 if ((*(url->LocalPart + 1) == '/') &&
56 (*(url->LocalPart + 2) == ':')) { /* TODO: find default port for this protocol... */
57 url->Host = url->LocalPart + 3;
58 url->LocalPart = strchr(url->Host, '/');
61 if (url->LocalPart == NULL) {
62 url->LocalPart = pch + StrLength(url->URL);
65 pCredEnd = strchr(pch, '@');
66 if (pCredEnd >= url->LocalPart)
70 url->User = url->Host;
71 url->Host = pCredEnd + 1;
72 pUserEnd = strchr(url->User, ':');
74 if (pUserEnd > pCredEnd)
77 url->Pass = pUserEnd + 1;
79 StrBufPeek(url->URL, pUserEnd, 0, '\0');
80 StrBufPeek(url->URL, pCredEnd, 0, '\0');
84 if (*url->Host == '[') {
86 pEndHost = strchr(url->Host, ']');
87 if (pEndHost == NULL) {
88 FreeStrBuf(&url->URL);
90 return 0; /* invalid syntax, no ipv6 */
92 StrBufPeek(url->URL, pEndHost, 0, '\0');
93 if (*(pEndHost + 1) == ':'){
94 StrBufPeek(url->URL, pEndHost + 1, 0, '\0');
100 pPort = strchr(url->Host, ':');
102 StrBufPeek(url->URL, pPort, 0, '\0');
107 url->Port = atol(pPort);
108 url->IsIP = inet_pton(url->af, url->Host, &url->Addr);