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');
96 if (*url->Host == '[') {
98 pEndHost = strchr(url->Host, ']');
99 if (pEndHost == NULL) {
100 FreeStrBuf(&url->URL);
102 return 0; /* invalid syntax, no ipv6 */
104 StrBufPeek(url->URL, pEndHost, 0, '\0');
105 if (*(pEndHost + 1) == ':'){
106 StrBufPeek(url->URL, pEndHost + 1, 0, '\0');
107 pPort = pEndHost + 2;
112 pPort = strchr(url->Host, ':');
114 StrBufPeek(url->URL, pPort, 0, '\0');
119 url->Port = atol(pPort);
122 url->IsIP = inet_pton(AF_INET6, url->Host, &url->Addr.sin6_addr);
125 url->Addr.sin6_port = htons(url->Port);
126 url->Addr.sin6_port = AF_INET6;
131 url->IsIP = inet_pton(AF_INET, url->Host, &((struct sockaddr_in *)&(url->Addr))->sin_addr);
134 ((struct sockaddr_in *)&(url->Addr))->sin_port = htons(url->Port);
135 ((struct sockaddr_in *)&(url->Addr))->sin_family = AF_INET;
139 if (url->User != NULL) {
140 url->UsrName = NewStrBufPlain(url->User, pUserEnd - url->User);
141 StrBufUnescape(url->UsrName, 0);
142 url->User = ChrPtr(url->UsrName);
145 if (url->Pass != NULL) {
146 url->Password = NewStrBufPlain(url->Pass, pCredEnd - url->Pass);
147 StrBufUnescape(url->Password, 0);
148 url->Pass = ChrPtr(url->Password);
157 void CurlPrepareURL(ParsedURL *Url)
159 if (!strcmp(ChrPtr(Url->URL), "http"))
160 Url->UrlWithoutCred = NewStrBufPlain(ChrPtr(Url->URL), -1);
162 Url->UrlWithoutCred = NewStrBufPlain(HKEY("http://"));
163 StrBufAppendBufPlain(Url->UrlWithoutCred, Url->Host, -1, 0);
165 StrBufAppendBufPlain(Url->UrlWithoutCred, HKEY(":"), 0);
167 StrBufAppendPrintf(Url->UrlWithoutCred, "%u", Url->Port);
169 StrBufAppendBufPlain(Url->UrlWithoutCred, HKEY("/"), 0);
172 StrBufAppendBufPlain(Url->UrlWithoutCred, Url->LocalPart, -1, 0);
174 if (Url->User != NULL)
176 Url->CurlCreds = NewStrBufPlain(Url->User, -1);
177 StrBufAppendBufPlain(Url->CurlCreds, HKEY(":"), 0);
178 if (Url->Pass != NULL)
179 StrBufAppendBufPlain(Url->CurlCreds, Url->Pass, -1, 0);
181 Url->PlainUrl = ChrPtr(Url->UrlWithoutCred);