95 lines
2.0 KiB
C
95 lines
2.0 KiB
C
#include "url.h"
|
|
|
|
/* safely frees URL struct */
|
|
struct Url *
|
|
free_url(struct Url *url){
|
|
free(url->s);
|
|
free(url->ssp);
|
|
free(url);
|
|
return NULL;
|
|
}
|
|
|
|
/* turns scheme specific part of URL from string into SSP struct */
|
|
/* returns NULL on allocation error or scheme syntax error */
|
|
struct CommonInternetScheme *
|
|
disassemble_commoninternetscheme(char *ssp){
|
|
int i;
|
|
char *p;
|
|
struct CommonInternetScheme *retval;
|
|
|
|
if( /* embarrassingly long if statement */
|
|
(retval
|
|
= (struct CommonInternetScheme *)
|
|
malloc(sizeof(struct CommonInternetScheme))
|
|
) == NULL
|
|
)
|
|
return NULL;
|
|
|
|
parsed = 0;
|
|
|
|
/* the scheme-specific data starts with a double slash to indicate that
|
|
* it complies with the common Internet scheme syntax (RFC 1738 3.1) */
|
|
if(ssp[0] != '/' || ssp[1] != '/')
|
|
return NULL;
|
|
ssp += 2;
|
|
|
|
/* probe to determine what areas of the SSP are filled */
|
|
p = ssp;
|
|
|
|
return retval;
|
|
}
|
|
|
|
/* turns URL string into URL struct */
|
|
struct Url *
|
|
disassemble_url(char *url){
|
|
char *p;
|
|
struct Url *retval;
|
|
size_t s_s;
|
|
size_t ssp_s;
|
|
size_t *s;
|
|
|
|
if((retval = (struct Url *)malloc(sizeof(struct Url))) == NULL)
|
|
return NULL;
|
|
s_s = 0;
|
|
ssp_s = 0;
|
|
|
|
/* get the lengths of the scheme and scheme specific part, excluding
|
|
* the nul byte */
|
|
p = url;
|
|
s = &s_s;
|
|
while(*p != '\0'){
|
|
/* standard; scheme names can't contain colons so the first
|
|
* colon must delimit the scheme from the scheme specific part
|
|
* (RFC 1738 2.1) */
|
|
if(s == &s_s && *p == ':'){
|
|
s = &ssp_s;
|
|
++p;
|
|
}
|
|
++*p;
|
|
}
|
|
|
|
/* malloc the lengths, including the nul byte */
|
|
if((retval->s = (char *)malloc(sizeof(char) * (s_s + 1))) == NULL)
|
|
goto free_retval;
|
|
if((retval->ssp = (char *)malloc(sizeof(char) * (ssp_s + 1))) == NULL)
|
|
goto free_s;
|
|
|
|
/* copy over the scheme and scheme specific part strings */
|
|
p = retval->s;
|
|
while(*url != ':')
|
|
*(p++) = *(url++);
|
|
*p = '\0';
|
|
p = retval->ssp;
|
|
while(*url != '\0')
|
|
*(p++) = *(url++);
|
|
*p = '\0';
|
|
|
|
return retval;
|
|
|
|
free_s:
|
|
free(retval->s);
|
|
free_retval:
|
|
free(retval);
|
|
return NULL;
|
|
}
|