library name fixups
[libmvfs.git] / libmvfs / urlparse.c
blob5f969eb7298a0aca652317aca3b809dea6d87cc5
1 /*
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
8 */
10 #include "mvfs-internal.h"
12 #include <string.h>
13 #include <malloc.h>
14 #include <stdio.h>
15 #include <mvfs/url.h>
16 #include <mvfs/_utils.h>
18 #define RET_OK \
19 { \
20 url->error = 0; \
21 return url; \
24 #define RET_ERR(err) \
25 { \
26 url->error = err; \
27 DEBUGMSG("(%s) error: %d \n", url->url, err); \
28 return url; \
31 MVFS_URL* mvfs_url_parse(const char* u)
33 MVFS_URL* url;
34 url = (MVFS_URL*)calloc(1,sizeof(MVFS_URL));
35 url->error = MVFS_ERR_URL_MISSING_TYPE;
37 char* ptr1 = (char*)&(url->buffer);
38 char* ptr2 = NULL;
39 char* ptr3 = NULL;
41 strcpy(url->buffer, u);
42 strcpy(url->url, u);
44 // check if this might be an abolute local filename
45 if ((*ptr1)=='/')
47 url->pathname = ptr1;
48 RET_OK;
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
56 url->type = ptr1;
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);
62 ptr1+=2;
64 // if the url ends here, we've got something like "file://"
65 // quite strange, but maybe correct ?
66 if (!(*ptr1))
67 RET_OK;
69 // if we have have an / here, we've got no hostspec, just path
70 if ((*ptr1)=='/')
72 url->pathname = ptr1;
73 RET_OK;
76 // parse the host spec ...
77 // do we have some @ ? - then cut out the username(+secret)
78 if (ptr2 = strchr(ptr1,'@'))
80 (*ptr2) = 0;
81 if (url->secret = strchr(ptr1,':')) // got username + secret
83 (*url->secret) = 0;
84 url->secret++;
86 url->username = ptr1;
87 ptr1 = ptr2+1;
91 char* ptr_slash = strchr(ptr1, '/');
92 char* ptr_colon = strchr(ptr1, ':');
94 if (ptr_slash)
96 // pathname found. if we found a colon behind it, ignore
97 if (ptr_colon > ptr_slash)
98 ptr_colon = 0;
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);
102 *ptr_slash = 0;
103 ptr_slash++;
104 *ptr_slash = 0;
105 ptr_slash++;
106 strcpy(ptr_slash, pathname);
107 url->pathname = ptr_slash;
110 if (ptr_colon)
113 *ptr_colon = 0;
114 ptr_colon++;
115 url->port = ptr_colon;
118 url->hostname = ptr1;
121 RET_OK;