Make the swap buffer content function publically available.
[citadel.git] / libcitadel / lib / urlhandling.c
1 #include "sysdep.h"
2 #include <ctype.h>
3 #include <errno.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <string.h>
7 #include <stdio.h>
8 #include <sys/select.h>
9 #include <fcntl.h>
10 #include <sys/types.h>
11 #include "libcitadel.h"
12 #include <sys/socket.h>
13
14 /**
15  * @defgroup URLHandling ParsedURL object to handle connection data
16  */
17
18 /**
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.
22  */
23 void FreeURL(ParsedURL** Url)
24 {
25         if (*Url != NULL) {
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);
33                 free(*Url);
34                 *Url = NULL;
35         }
36 }
37
38 /**
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?
44  */
45 int ParseURL(ParsedURL **Url, StrBuf *UrlStr, unsigned short DefaultPort)
46 {
47         const char *pch, *pPort, *pCredEnd, *pUserEnd;
48         ParsedURL *url = (ParsedURL *)malloc(sizeof(ParsedURL));
49         memset(url, 0, sizeof(ParsedURL));
50
51         url->af = AF_INET;
52         url->Port =  DefaultPort;
53         /*
54          * http://username:passvoid@[ipv6]:port/url 
55          */
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)
65                         {
66                             StrBufPeek(url->URL, url->LocalPart, 0, '\0');
67                             url->LocalPart++;
68                         }
69                 }
70         }
71         if (url->LocalPart == NULL) {
72                 url->LocalPart = ChrPtr(url->URL) + StrLength(url->URL);
73         }
74
75         pCredEnd = strrchr(ChrPtr(url->URL), '@');
76         if (pCredEnd >= url->LocalPart)
77                 pCredEnd = NULL;
78         if (pCredEnd != NULL)
79         {
80                 url->User = url->Host;
81                 url->Host = pCredEnd + 1;
82                 pUserEnd = strchr(url->User, ':');
83                 
84                 if (pUserEnd > pCredEnd)
85                         pUserEnd = pCredEnd;
86                 else {
87                         url->Pass = pUserEnd + 1;
88                 }
89                 StrBufPeek(url->URL, pUserEnd, 0, '\0');
90                 StrBufPeek(url->URL, pCredEnd, 0, '\0');                
91         }
92         else
93                 pUserEnd = NULL;
94         
95         pPort = NULL;
96         if (*url->Host == '[') {
97                 const char *pEndHost;
98                 url->Host ++;
99                 pEndHost = strchr(url->Host, ']');
100                 if (pEndHost == NULL) {
101                         FreeStrBuf(&url->URL);
102                         free(url);
103                         return 0; /* invalid syntax, no ipv6 */
104                 }
105                 StrBufPeek(url->URL, pEndHost, 0, '\0');
106                 if (*(pEndHost + 1) == ':'){
107                         StrBufPeek(url->URL, pEndHost + 1, 0, '\0');
108                         pPort = pEndHost + 2;
109                 }
110                 url->af = AF_INET6;
111         }
112         else {
113                 pPort = strchr(url->Host, ':');
114                 if (pPort != NULL) {
115                         StrBufPeek(url->URL, pPort, 0, '\0');
116                         pPort ++;
117                 }
118         }
119         if (pPort != NULL)
120                 url->Port = atol(pPort);
121         if (url->IPv6)
122         {
123             url->IsIP = inet_pton(AF_INET6, url->Host, &url->Addr.sin6_addr);
124             if (url->IsIP)
125             {
126                 url->Addr.sin6_port = htons(url->Port);
127                 url->Addr.sin6_port = AF_INET6;
128             }
129         }
130         else
131         {
132             url->IsIP = inet_pton(AF_INET, url->Host, &((struct sockaddr_in *)&(url->Addr))->sin_addr);
133             if (url->IsIP)
134             {
135                 ((struct sockaddr_in *)&(url->Addr))->sin_port = htons(url->Port);
136                 ((struct sockaddr_in *)&(url->Addr))->sin_family = AF_INET;
137             }   
138         }
139
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);
144         }
145
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);
150         }
151
152         if (*Url != NULL)
153                 url->Next = *Url;
154         *Url = url;
155         return 1;
156 }
157
158 void CurlPrepareURL(ParsedURL *Url)
159 {
160         if (!strcmp(ChrPtr(Url->URL), "http"))
161                 Url->UrlWithoutCred = NewStrBufPlain(ChrPtr(Url->URL), -1);
162         else 
163                 Url->UrlWithoutCred = NewStrBufPlain(HKEY("http://"));
164         StrBufAppendBufPlain(Url->UrlWithoutCred, Url->Host, -1, 0);
165
166         StrBufAppendBufPlain(Url->UrlWithoutCred, HKEY(":"), 0);
167
168         StrBufAppendPrintf(Url->UrlWithoutCred, "%u", Url->Port);
169
170         StrBufAppendBufPlain(Url->UrlWithoutCred, HKEY("/"), 0);
171         if (Url->LocalPart)
172         {
173             StrBufAppendBufPlain(Url->UrlWithoutCred, Url->LocalPart, -1, 0);
174         }
175         if (Url->User != NULL)
176         {
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);
181         }
182         Url->PlainUrl = ChrPtr(Url->UrlWithoutCred);
183 }