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, *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 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 url->LocalPart = strchr(url->Host, '/');
64 if (url->LocalPart != NULL)
66 StrBufPeek(url->URL, url->LocalPart, 0, '\0');
71 if (url->LocalPart == NULL) {
72 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');
96 if (*url->Host == '[') {
99 pEndHost = strchr(url->Host, ']');
100 if (pEndHost == NULL) {
101 FreeStrBuf(&url->URL);
103 return 0; /* invalid syntax, no ipv6 */
105 StrBufPeek(url->URL, pEndHost, 0, '\0');
106 if (*(pEndHost + 1) == ':'){
107 StrBufPeek(url->URL, pEndHost + 1, 0, '\0');
108 pPort = pEndHost + 2;
113 pPort = strchr(url->Host, ':');
115 StrBufPeek(url->URL, pPort, 0, '\0');
120 url->Port = atol(pPort);
123 url->IsIP = inet_pton(AF_INET6, url->Host, &url->Addr.sin6_addr);
126 url->Addr.sin6_port = htons(url->Port);
127 url->Addr.sin6_port = AF_INET6;
132 url->IsIP = inet_pton(AF_INET, url->Host, &((struct sockaddr_in *)&(url->Addr))->sin_addr);
135 ((struct sockaddr_in *)&(url->Addr))->sin_port = htons(url->Port);
136 ((struct sockaddr_in *)&(url->Addr))->sin_family = AF_INET;
140 if (url->User != NULL) {
141 url->UsrName = NewStrBufPlain(url->User, pUserEnd - url->User);
142 StrBufUnescape(url->UsrName, 0);
143 url->User = ChrPtr(url->UsrName);
146 if (url->Pass != NULL) {
147 url->Password = NewStrBufPlain(url->Pass, pCredEnd - url->Pass);
148 StrBufUnescape(url->Password, 0);
149 url->Pass = ChrPtr(url->Password);
158 void CurlPrepareURL(ParsedURL *Url)
160 if (!strcmp(ChrPtr(Url->URL), "http"))
161 Url->UrlWithoutCred = NewStrBufPlain(ChrPtr(Url->URL), -1);
163 Url->UrlWithoutCred = NewStrBufPlain(HKEY("http://"));
164 StrBufAppendBufPlain(Url->UrlWithoutCred, Url->Host, -1, 0);
166 StrBufAppendBufPlain(Url->UrlWithoutCred, HKEY(":"), 0);
168 StrBufAppendPrintf(Url->UrlWithoutCred, "%u", Url->Port);
170 StrBufAppendBufPlain(Url->UrlWithoutCred, HKEY("/"), 0);
173 StrBufAppendBufPlain(Url->UrlWithoutCred, Url->LocalPart, -1, 0);
175 if (Url->User != NULL)
177 Url->CurlCreds = NewStrBufPlain(Url->User, -1);
178 StrBufAppendBufPlain(Url->CurlCreds, HKEY(":"), 0);
179 if (Url->Pass != NULL)
180 StrBufAppendBufPlain(Url->CurlCreds, Url->Pass, -1, 0);
182 Url->PlainUrl = ChrPtr(Url->UrlWithoutCred);