8 #include <sys/select.h>
10 #include <sys/types.h>
11 #include "libcitadel.h"
12 #include <sys/socket.h>
15 * @defgroup URLHandling ParsedURL object to handle connection data
19 * @ingroup URLHandling
20 * @brief frees a linked list of ParsedURL
21 * @param Url (list) of ParsedURL to be freet; Pointer is NULL'ed for the caller.
23 void FreeURL(ParsedURL** Url)
26 FreeStrBuf(&(*Url)->URL);
27 FreeStrBuf(&(*Url)->UrlWithoutCred);
28 FreeStrBuf(&(*Url)->CurlCreds);
29 FreeStrBuf(&(*Url)->UsrName);
30 FreeStrBuf(&(*Url)->Password);
31 if ((*Url)->Next != NULL)
32 FreeURL(&(*Url)->Next);
39 * @ingroup URLHandling
40 * @brief parses the string provided with UrlStr into *Url
41 * @param Url on success this contains the parsed object; needs to be free'd by caller.
42 * @param UrlStr String we should parse into parts
43 * @param DefaultPort Which is the default port here?
45 int ParseURL(ParsedURL **Url, StrBuf *UrlStr, unsigned short DefaultPort)
47 const char *pch, *pEndHost, *pPort, *pCredEnd, *pUserEnd;
48 ParsedURL *url = (ParsedURL *)malloc(sizeof(ParsedURL));
49 memset(url, 0, sizeof(ParsedURL));
52 url->Port = DefaultPort;
54 * http://username:passvoid@[ipv6]:port/url
56 url->URL = NewStrBufDup(UrlStr);
57 url->Host = pch = ChrPtr(url->URL);
58 pEndHost = url->LocalPart = strchr(pch, '/');
59 if (url->LocalPart != NULL) {
60 if ((*(url->LocalPart + 1) == '/') &&
61 (*(url->LocalPart - 1) == ':')) { /* TODO: find default port for this protocol... */
62 url->Host = url->LocalPart + 2;
63 pEndHost = url->LocalPart = strchr(url->Host, '/');
64 if (url->LocalPart != NULL)
66 StrBufPeek(url->URL, url->LocalPart, 0, '\0');
71 if (url->LocalPart == NULL) {
72 pEndHost = url->LocalPart = ChrPtr(url->URL) + StrLength(url->URL);
75 pCredEnd = strrchr(ChrPtr(url->URL), '@');
76 if (pCredEnd >= url->LocalPart)
80 url->User = url->Host;
81 url->Host = pCredEnd + 1;
82 pUserEnd = strchr(url->User, ':');
84 if (pUserEnd > pCredEnd)
87 url->Pass = pUserEnd + 1;
89 StrBufPeek(url->URL, pUserEnd, 0, '\0');
90 StrBufPeek(url->URL, pCredEnd, 0, '\0');
94 if (*url->Host == '[') {
96 pEndHost = strchr(url->Host, ']');
97 if (pEndHost == NULL) {
98 FreeStrBuf(&url->URL);
100 return 0; /* invalid syntax, no ipv6 */
102 StrBufPeek(url->URL, pEndHost, 0, '\0');
103 if (*(pEndHost + 1) == ':'){
104 StrBufPeek(url->URL, pEndHost + 1, 0, '\0');
105 pPort = pEndHost + 2;
110 pPort = strchr(url->Host, ':');
112 StrBufPeek(url->URL, pPort, 0, '\0');
117 url->Port = atol(pPort);
120 url->IsIP = inet_pton(AF_INET6, url->Host, &url->Addr.sin6_addr);
123 url->Addr.sin6_port = htons(url->Port);
124 url->Addr.sin6_port = AF_INET6;
129 url->IsIP = inet_pton(AF_INET, url->Host, &((struct sockaddr_in *)&(url->Addr))->sin_addr);
132 ((struct sockaddr_in *)&(url->Addr))->sin_port = htons(url->Port);
133 ((struct sockaddr_in *)&(url->Addr))->sin_family = AF_INET;
137 if (url->User != NULL) {
138 url->UsrName = NewStrBufPlain(url->User, pUserEnd - url->User);
139 StrBufUnescape(url->UsrName, 0);
140 url->User = ChrPtr(url->UsrName);
143 if (url->Pass != NULL) {
144 url->Password = NewStrBufPlain(url->Pass, pCredEnd - url->Pass);
145 StrBufUnescape(url->Password, 0);
146 url->Pass = ChrPtr(url->Password);
155 void CurlPrepareURL(ParsedURL *Url)
157 if (!strcmp(ChrPtr(Url->URL), "http"))
158 Url->UrlWithoutCred = NewStrBufPlain(ChrPtr(Url->URL), -1);
160 Url->UrlWithoutCred = NewStrBufPlain(HKEY("http://"));
161 StrBufAppendBufPlain(Url->UrlWithoutCred, Url->Host, -1, 0);
163 StrBufAppendBufPlain(Url->UrlWithoutCred, HKEY(":"), 0);
165 StrBufAppendPrintf(Url->UrlWithoutCred, "%u", Url->Port);
167 StrBufAppendBufPlain(Url->UrlWithoutCred, HKEY("/"), 0);
170 StrBufAppendBufPlain(Url->UrlWithoutCred, Url->LocalPart, -1, 0);
172 if (Url->User != NULL)
174 Url->CurlCreds = NewStrBufPlain(Url->User, -1);
175 StrBufAppendBufPlain(Url->CurlCreds, HKEY(":"), 0);
176 if (Url->Pass != NULL)
177 StrBufAppendBufPlain(Url->CurlCreds, Url->Pass, -1, 0);
179 Url->PlainUrl = ChrPtr(Url->UrlWithoutCred);