Make the swap buffer content function publically available.
[citadel.git] / libcitadel / lib / urlhandling.c
index 6f7ebb58c628b945e7d20a36a668fba2b79b132c..8896770eea84ce4755eaf3aad5864d78b070091f 100644 (file)
@@ -9,6 +9,7 @@
 #include <fcntl.h>
 #include <sys/types.h>
 #include "libcitadel.h"
+#include <sys/socket.h>
 
 /**
  * @defgroup URLHandling ParsedURL object to handle connection data
@@ -23,6 +24,10 @@ void FreeURL(ParsedURL** Url)
 {
        if (*Url != NULL) {
                FreeStrBuf(&(*Url)->URL);
+               FreeStrBuf(&(*Url)->UrlWithoutCred);
+               FreeStrBuf(&(*Url)->CurlCreds);
+               FreeStrBuf(&(*Url)->UsrName);
+               FreeStrBuf(&(*Url)->Password);
                if ((*Url)->Next != NULL)
                        FreeURL(&(*Url)->Next);
                free(*Url);
@@ -39,7 +44,7 @@ void FreeURL(ParsedURL** Url)
  */
 int ParseURL(ParsedURL **Url, StrBuf *UrlStr, unsigned short DefaultPort)
 {
-       const char *pch, *pEndHost, *pPort, *pCredEnd, *pUserEnd;
+       const char *pch, *pPort, *pCredEnd, *pUserEnd;
        ParsedURL *url = (ParsedURL *)malloc(sizeof(ParsedURL));
        memset(url, 0, sizeof(ParsedURL));
 
@@ -53,16 +58,21 @@ int ParseURL(ParsedURL **Url, StrBuf *UrlStr, unsigned short DefaultPort)
        url->LocalPart = strchr(pch, '/');
        if (url->LocalPart != NULL) {
                if ((*(url->LocalPart + 1) == '/') && 
-                   (*(url->LocalPart + 2) == ':')) { /* TODO: find default port for this protocol... */
-                       url->Host = url->LocalPart + 3;
+                   (*(url->LocalPart - 1) == ':')) { /* TODO: find default port for this protocol... */
+                       url->Host = url->LocalPart + 2;
                        url->LocalPart = strchr(url->Host, '/');
+                       if (url->LocalPart != NULL)
+                       {
+                           StrBufPeek(url->URL, url->LocalPart, 0, '\0');
+                           url->LocalPart++;
+                       }
                }
        }
        if (url->LocalPart == NULL) {
-               url->LocalPart = pch + StrLength(url->URL);
+               url->LocalPart = ChrPtr(url->URL) + StrLength(url->URL);
        }
 
-       pCredEnd = strchr(pch, '@');
+       pCredEnd = strrchr(ChrPtr(url->URL), '@');
        if (pCredEnd >= url->LocalPart)
                pCredEnd = NULL;
        if (pCredEnd != NULL)
@@ -79,9 +89,12 @@ int ParseURL(ParsedURL **Url, StrBuf *UrlStr, unsigned short DefaultPort)
                StrBufPeek(url->URL, pUserEnd, 0, '\0');
                StrBufPeek(url->URL, pCredEnd, 0, '\0');                
        }
+       else
+               pUserEnd = NULL;
        
        pPort = NULL;
        if (*url->Host == '[') {
+               const char *pEndHost;
                url->Host ++;
                pEndHost = strchr(url->Host, ']');
                if (pEndHost == NULL) {
@@ -105,7 +118,66 @@ int ParseURL(ParsedURL **Url, StrBuf *UrlStr, unsigned short DefaultPort)
        }
        if (pPort != NULL)
                url->Port = atol(pPort);
-       url->IsIP = inet_pton(url->af, url->Host, &url->Addr.sin6_addr);
+       if (url->IPv6)
+       {
+           url->IsIP = inet_pton(AF_INET6, url->Host, &url->Addr.sin6_addr);
+           if (url->IsIP)
+           {
+               url->Addr.sin6_port = htons(url->Port);
+               url->Addr.sin6_port = AF_INET6;
+           }
+       }
+       else
+       {
+           url->IsIP = inet_pton(AF_INET, url->Host, &((struct sockaddr_in *)&(url->Addr))->sin_addr);
+           if (url->IsIP)
+           {
+               ((struct sockaddr_in *)&(url->Addr))->sin_port = htons(url->Port);
+               ((struct sockaddr_in *)&(url->Addr))->sin_family = AF_INET;
+           }   
+       }
+
+       if (url->User != NULL) {
+               url->UsrName = NewStrBufPlain(url->User, pUserEnd - url->User);
+               StrBufUnescape(url->UsrName, 0);
+               url->User = ChrPtr(url->UsrName);
+       }
+
+       if (url->Pass != NULL) {
+               url->Password = NewStrBufPlain(url->Pass, pCredEnd - url->Pass);
+               StrBufUnescape(url->Password, 0);
+               url->Pass = ChrPtr(url->Password);
+       }
+
+       if (*Url != NULL)
+               url->Next = *Url;
        *Url = url;
        return 1;
 }
+
+void CurlPrepareURL(ParsedURL *Url)
+{
+       if (!strcmp(ChrPtr(Url->URL), "http"))
+               Url->UrlWithoutCred = NewStrBufPlain(ChrPtr(Url->URL), -1);
+       else 
+               Url->UrlWithoutCred = NewStrBufPlain(HKEY("http://"));
+       StrBufAppendBufPlain(Url->UrlWithoutCred, Url->Host, -1, 0);
+
+       StrBufAppendBufPlain(Url->UrlWithoutCred, HKEY(":"), 0);
+
+       StrBufAppendPrintf(Url->UrlWithoutCred, "%u", Url->Port);
+
+       StrBufAppendBufPlain(Url->UrlWithoutCred, HKEY("/"), 0);
+       if (Url->LocalPart)
+       {
+           StrBufAppendBufPlain(Url->UrlWithoutCred, Url->LocalPart, -1, 0);
+       }
+       if (Url->User != NULL)
+       {
+               Url->CurlCreds = NewStrBufPlain(Url->User, -1);
+               StrBufAppendBufPlain(Url->CurlCreds, HKEY(":"), 0);
+               if (Url->Pass != NULL)
+                       StrBufAppendBufPlain(Url->CurlCreds, Url->Pass, -1, 0);
+       }
+       Url->PlainUrl = ChrPtr(Url->UrlWithoutCred);
+}