2 libmvfs - metux Virtual Filesystem Library
4 tiny URL parsing function
6 Copyright (C) 2008 Enrico Weigelt, metux IT service <weigelt@metux.de>
7 This code is published under the terms of the GNU Public License 2.0
10 #include "mvfs-internal.h"
16 #include <mvfs/_utils.h>
24 #define RET_ERR(err) \
27 DEBUGMSG("(%s) error: %d \n", url->url, err); \
31 MVFS_URL
* mvfs_url_parse(const char* u
)
34 url
= (MVFS_URL
*)calloc(1,sizeof(MVFS_URL
));
35 url
->error
= MVFS_ERR_URL_MISSING_TYPE
;
37 char* ptr1
= (char*)&(url
->buffer
);
41 strcpy(url
->buffer
, u
);
44 // check if this might be an abolute local filename
51 // first try to get the url type - it's separated by a :
52 if (!(ptr2
= strchr(ptr1
, ':')))
53 RET_ERR(MVFS_ERR_URL_MISSING_TYPE
);
55 *ptr2
= 0; // terminate and set the type field
57 ptr1
= ptr2
+1; // jump right after the :
59 // now we expect two slashes
60 if (!((ptr1
[0] == '/') && (ptr1
[1] == '/')))
61 RET_ERR(MVFS_ERR_URL_MISSING_SLASHSLASH
);
64 // if the url ends here, we've got something like "file://"
65 // quite strange, but maybe correct ?
69 // if we have have an / here, we've got no hostspec, just path
76 // parse the host spec ...
77 // do we have some @ ? - then cut out the username(+secret)
78 if (ptr2
= strchr(ptr1
,'@'))
81 if (url
->secret
= strchr(ptr1
,':')) // got username + secret
91 char* ptr_slash
= strchr(ptr1
, '/');
92 char* ptr_colon
= strchr(ptr1
, ':');
96 // pathname found. if we found a colon behind it, ignore
97 if (ptr_colon
> ptr_slash
)
99 // it's a bit tricky - we must make at least 1 byte more room ;-o
100 char pathname
[MVFS_URL_MAX
];
101 strcpy(pathname
, ptr_slash
);
106 strcpy(ptr_slash
, pathname
);
107 url
->pathname
= ptr_slash
;
115 url
->port
= ptr_colon
;
118 url
->hostname
= ptr1
;