Fix possible endless loop conditions
[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
13 /**
14  * @defgroup URLHandling ParsedURL object to handle connection data
15  */
16
17 /**
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.
21  */
22 void FreeURL(ParsedURL** Url)
23 {
24         if (*Url != NULL) {
25                 FreeStrBuf(&(*Url)->URL);
26                 FreeStrBuf(&(*Url)->UrlWithoutCred);
27                 FreeStrBuf(&(*Url)->CurlCreds);
28                 if ((*Url)->Next != NULL)
29                         FreeURL(&(*Url)->Next);
30                 free(*Url);
31                 *Url = NULL;
32         }
33 }
34
35 /**
36  * @ingroup URLHandling
37  * @brief parses the string provided with UrlStr into *Url
38  * @param Url on success this contains the parsed object; needs to be free'd by caller.
39  * @param UrlStr String we should parse into parts
40  * @param DefaultPort Which is the default port here?
41  */
42 int ParseURL(ParsedURL **Url, StrBuf *UrlStr, unsigned short DefaultPort)
43 {
44         const char *pch, *pEndHost, *pPort, *pCredEnd, *pUserEnd;
45         ParsedURL *url = (ParsedURL *)malloc(sizeof(ParsedURL));
46         memset(url, 0, sizeof(ParsedURL));
47
48         url->af = AF_INET;
49         url->Port =  DefaultPort;
50         /*
51          * http://username:passvoid@[ipv6]:port/url 
52          */
53         url->URL = NewStrBufDup(UrlStr);
54         url->Host = pch = ChrPtr(url->URL);
55         url->LocalPart = strchr(pch, '/');
56         if (url->LocalPart != NULL) {
57                 if ((*(url->LocalPart + 1) == '/') && 
58                     (*(url->LocalPart + 2) == ':')) { /* TODO: find default port for this protocol... */
59                         url->Host = url->LocalPart + 3;
60                         url->LocalPart = strchr(url->Host, '/');
61                 }
62         }
63         if (url->LocalPart == NULL) {
64                 url->LocalPart = pch + StrLength(url->URL);
65         }
66
67         pCredEnd = strchr(pch, '@');
68         if (pCredEnd >= url->LocalPart)
69                 pCredEnd = NULL;
70         if (pCredEnd != NULL)
71         {
72                 url->User = url->Host;
73                 url->Host = pCredEnd + 1;
74                 pUserEnd = strchr(url->User, ':');
75                 
76                 if (pUserEnd > pCredEnd)
77                         pUserEnd = pCredEnd;
78                 else {
79                         url->Pass = pUserEnd + 1;
80                 }
81                 StrBufPeek(url->URL, pUserEnd, 0, '\0');
82                 StrBufPeek(url->URL, pCredEnd, 0, '\0');                
83         }
84         
85         pPort = NULL;
86         if (*url->Host == '[') {
87                 url->Host ++;
88                 pEndHost = strchr(url->Host, ']');
89                 if (pEndHost == NULL) {
90                         FreeStrBuf(&url->URL);
91                         free(url);
92                         return 0; /* invalid syntax, no ipv6 */
93                 }
94                 StrBufPeek(url->URL, pEndHost, 0, '\0');
95                 if (*(pEndHost + 1) == ':'){
96                         StrBufPeek(url->URL, pEndHost + 1, 0, '\0');
97                         pPort = pEndHost + 2;
98                 }
99                 url->af = AF_INET6;
100         }
101         else {
102                 pPort = strchr(url->Host, ':');
103                 if (pPort != NULL) {
104                         StrBufPeek(url->URL, pPort, 0, '\0');
105                         pPort ++;
106                 }
107         }
108         if (pPort != NULL)
109                 url->Port = atol(pPort);
110         url->IsIP = inet_pton(url->af, url->Host, &url->Addr.sin6_addr);
111         *Url = url;
112         return 1;
113 }
114
115 void CurlPrepareURL(ParsedURL *Url)
116 {
117         if (!strcmp(ChrPtr(Url->URL), "http"))
118                 Url->UrlWithoutCred = NewStrBufPlain(ChrPtr(Url->URL), -1);
119         else 
120                 Url->UrlWithoutCred = NewStrBufPlain(HKEY("http://"));
121         StrBufAppendBufPlain(Url->UrlWithoutCred, Url->Host, -1, 0);
122         StrBufAppendBufPlain(Url->UrlWithoutCred, HKEY(":"), 0);
123
124         StrBufAppendPrintf(Url->UrlWithoutCred, "%u", Url->Port);
125         StrBufAppendBufPlain(Url->UrlWithoutCred, HKEY("/"), 0);
126         if (Url->LocalPart)
127                 StrBufAppendBufPlain(Url->UrlWithoutCred, Url->LocalPart, -1, 0);
128         if (Url->User != NULL)
129         {
130                 Url->CurlCreds = NewStrBufPlain(Url->User, -1);
131                 StrBufAppendBufPlain(Url->CurlCreds, HKEY(":"), 0);
132                 if (Url->Pass != NULL)
133                         StrBufAppendBufPlain(Url->CurlCreds, Url->Pass, -1, 0);
134         }
135 }