Back to index

libcitadel  8.12
Functions
ParsedURL object to handle connection data

Functions

void FreeURL (ParsedURL **Url)
 frees a linked list of ParsedURL
int ParseURL (ParsedURL **Url, StrBuf *UrlStr, unsigned short DefaultPort)
 parses the string provided with UrlStr into *Url

Function Documentation

void FreeURL ( ParsedURL **  Url)

frees a linked list of ParsedURL

Parameters:
Url(list) of ParsedURL to be freet; Pointer is NULL'ed for the caller.

Definition at line 23 of file urlhandling.c.

{
       if (*Url != NULL) {
              FreeStrBuf(&(*Url)->URL);
              FreeStrBuf(&(*Url)->UrlWithoutCred);
              FreeStrBuf(&(*Url)->CurlCreds);
              if ((*Url)->Next != NULL)
                     FreeURL(&(*Url)->Next);
              free(*Url);
              *Url = NULL;
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

int ParseURL ( ParsedURL **  Url,
StrBuf UrlStr,
unsigned short  DefaultPort 
)

parses the string provided with UrlStr into *Url

Parameters:
Urlon success this contains the parsed object; needs to be free'd by caller.
UrlStrString we should parse into parts
DefaultPortWhich is the default port here?

Definition at line 43 of file urlhandling.c.

{
       const char *pch, *pEndHost, *pPort, *pCredEnd, *pUserEnd;
       ParsedURL *url = (ParsedURL *)malloc(sizeof(ParsedURL));
       memset(url, 0, sizeof(ParsedURL));

       url->af = AF_INET;
       url->Port =  DefaultPort;
       /*
        * http://username:passvoid@[ipv6]:port/url 
        */
       url->URL = NewStrBufDup(UrlStr);
       url->Host = pch = ChrPtr(url->URL);
       url->LocalPart = strchr(pch, '/');
       if (url->LocalPart != NULL) {
              if ((*(url->LocalPart + 1) == '/') && 
                  (*(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);
       }

       pCredEnd = strchr(pch, '@');
       if (pCredEnd >= url->LocalPart)
              pCredEnd = NULL;
       if (pCredEnd != NULL)
       {
              url->User = url->Host;
              url->Host = pCredEnd + 1;
              pUserEnd = strchr(url->User, ':');
              
              if (pUserEnd > pCredEnd)
                     pUserEnd = pCredEnd;
              else {
                     url->Pass = pUserEnd + 1;
              }
              StrBufPeek(url->URL, pUserEnd, 0, '\0');
              StrBufPeek(url->URL, pCredEnd, 0, '\0');         
       }
       
       pPort = NULL;
       if (*url->Host == '[') {
              url->Host ++;
              pEndHost = strchr(url->Host, ']');
              if (pEndHost == NULL) {
                     FreeStrBuf(&url->URL);
                     free(url);
                     return 0; /* invalid syntax, no ipv6 */
              }
              StrBufPeek(url->URL, pEndHost, 0, '\0');
              if (*(pEndHost + 1) == ':'){
                     StrBufPeek(url->URL, pEndHost + 1, 0, '\0');
                     pPort = pEndHost + 2;
              }
              url->af = AF_INET6;
       }
       else {
              pPort = strchr(url->Host, ':');
              if (pPort != NULL) {
                     StrBufPeek(url->URL, pPort, 0, '\0');
                     pPort ++;
              }
       }
       if (pPort != NULL)
              url->Port = atol(pPort);
       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;
           }  
       }      
       *Url = url;
       return 1;
}

Here is the call graph for this function: