[En-Nut-Discussion] Need Help on URL Parse

Michel unreal at home.nl
Mon Jul 26 16:54:26 CEST 2004


Yes, I did :-)
 
This function basically modifies the source string to place \0 at the proper
places and changes the pointers in your struct to appropriate start points.
If a part is not present, that pointer will point to an empty string (also
inside the source string b.t.w.). 
 
Hope it helps,
Michel
 
H File:
 
typedef struct _TUrlParts
{
 char *szHost;
 char *szPort;
 char *szUri;
} TUrlParts;

C File:
 
/*!
 * \brief Break a Url down in parts
 *
 * \note Modifies the source string!
 *
 * \param szUrl URL to parse
 * \param tUrlParts Pointer to where the pointers are written 
 *
 * The hostname, port and URI pointers are
 * set to the appropriate locations or an empty string
 * if not present.
 *
 * \param szUrl Url to parse
 */
void HttpParseUrl(CONST char *szUrl, TUrlParts *tUrlParts)
{
    char *szStart;      /* Points to the first character of the part */
    char *szEnd;        /* Points to the last character of the part */
 
    /*
     * In case we don't find a Host, port or URI, point
     * to empty string
     */
    tUrlParts->szHost = tUrlParts->szPort = tUrlParts->szUri = (char
*)(szUrl + strlen(szUrl));
 
    /*
     * skip the prefix
     */
    szStart = strstr(szUrl, "://");
    if (szStart != NULL)
    {
        szStart += 3;
    }
    else
    {
        /*
         * Apparently there is no prefix
         */
        szStart  = (char *)szUrl;
    }
 
    /*
     * We have found the hostname
     */
    tUrlParts->szHost = szStart;
 
    /*
     * Find the end of the hostname
     * End of it is indicated by ':' or '/'
     * If neither are found, assume we have a URL in 
     * the form 'http://demeterkast.net'
     */
    szEnd = strchr(szStart, ':');
    if (szEnd != NULL)
    {
        /*
         * There is a port specification, get it now
         */
        *szEnd = '\0';          /* Terminate the previous part */
        szStart = szEnd + 1;        /* point to the portnumber */
        tUrlParts->szPort = szStart;
    }
 
    szEnd = strchr(szStart, '/');
    if (szEnd != NULL)
    {
        /*
         * There is a URI specification, get it now
         */
        *szEnd = '\0';          /* Terminate the previous part */
        tUrlParts->szUri = szEnd + 1;   /* point to the URI */
    }
}
 
 

-----Original Message-----
From: en-nut-discussion-bounces at egnite.de
[mailto:en-nut-discussion-bounces at egnite.de] On Behalf Of iVesWorking
Sent: maandag 26 juli 2004 15:00
To: en-nut-discussion at egnite.de
Subject: [En-Nut-Discussion] Need Help on URL Parse


I using strtok_r to parse the URL, But i facing memory leak with strtok_r
 
this is what show on below 
 
  char *cp;  
  char *url;
 
  char *temp;
 
  url = NutHeapAlloc(128); 
  temp = NutHeapAlloc(128); 
     strcpy(temp,"192.168.125.0:800/index.html");
     cp = strtok_r(temp,":",&url);
     printf("IP%s\n",cp);
 
  NutHeapFree(url);
  NutHeapFree(temp);

This program will loss around 20-30Byte of the memory everytime it execute.
Any idea how to solve this problem ?
 
or any people have ready function that did  URL split to Host,Port,Path ?
 
 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.egnite.de/pipermail/en-nut-discussion/attachments/20040726/419bcf02/attachment.html>


More information about the En-Nut-Discussion mailing list