EFL 1.7 svn doobies
[profile/ivi/efreet.git] / src / lib / efreet_uri.c
1 #ifdef HAVE_CONFIG_H
2 # include <config.h>
3 #endif
4
5 #include <ctype.h>
6
7 #ifndef _POSIX_HOST_NAME_MAX
8 #define _POSIX_HOST_NAME_MAX 255
9 #endif
10
11 #ifdef HAVE_EVIL
12 # include <Evil.h>
13 #endif
14
15 /* define macros and variable for using the eina logging system  */
16 #define EFREET_MODULE_LOG_DOM /* no logging in this file */
17
18 #include "Efreet.h"
19 #include "efreet_private.h"
20
21
22 EAPI Efreet_Uri *
23 efreet_uri_decode(const char *full_uri)
24 {
25     Efreet_Uri *uri;
26     const char *p;
27     char protocol[64], hostname[_POSIX_HOST_NAME_MAX], path[PATH_MAX];
28     int i = 0;
29
30     EINA_SAFETY_ON_NULL_RETURN_VAL(full_uri, NULL);
31
32     /* An uri should be in the form <protocol>://<hostname>/<path> */
33     if (!strstr(full_uri, "://")) return NULL;
34
35     memset(protocol, 0, 64);
36     memset(hostname, 0, _POSIX_HOST_NAME_MAX);
37     memset(path, 0, PATH_MAX);
38
39     /* parse protocol */
40     p = full_uri;
41     for (i = 0; *p != ':' && *p != '\0' && i < 64; p++, i++)
42          protocol[i] = *p;
43     protocol[i] = '\0';
44
45     /* parse hostname */
46     p += 3;
47     if (*p != '/')
48     {
49         for (i = 0; *p != '/' && *p != '\0' && i < _POSIX_HOST_NAME_MAX; p++, i++)
50             hostname[i] = *p;
51         hostname[i] = '\0';
52     }
53     else
54         hostname[0] = '\0';
55
56     /* parse path */
57     /* See http://www.faqs.org/rfcs/rfc1738.html for the escaped chars */
58     for (i = 0; *p != '\0' && i < PATH_MAX; i++, p++)
59     {
60         if (*p == '%')
61         {
62             path[i] = *(++p);
63             path[i + 1] = *(++p);
64             path[i] = (char)strtol(&(path[i]), NULL, 16);
65             path[i + 1] = '\0';
66         }
67         else
68             path[i] = *p;
69     }
70
71     uri = NEW(Efreet_Uri, 1);
72     if (!uri) return NULL;
73
74     uri->protocol = eina_stringshare_add(protocol);
75     uri->hostname = eina_stringshare_add(hostname);
76     uri->path = eina_stringshare_add(path);
77
78     return uri;
79 }
80
81 EAPI const char *
82 efreet_uri_encode(Efreet_Uri *uri)
83 {
84     char dest[PATH_MAX * 3 + 4];
85     const char *p;
86     int i;
87
88     EINA_SAFETY_ON_NULL_RETURN_VAL(uri, NULL);
89     EINA_SAFETY_ON_NULL_RETURN_VAL(uri->path, NULL);
90     EINA_SAFETY_ON_NULL_RETURN_VAL(uri->protocol, NULL);
91
92     memset(dest, 0, PATH_MAX * 3 + 4);
93     snprintf(dest, strlen(uri->protocol) + 4, "%s://", uri->protocol);
94
95     /* Most app doesn't handle the hostname in the uri so it's put to NULL */
96     for (i = strlen(uri->protocol) + 3, p = uri->path; *p != '\0'; p++, i++)
97     {
98         if (isalnum(*p) || strchr("/$-_.+!*'()", *p))
99             dest[i] = *p;
100         else
101         {
102             snprintf(&(dest[i]), 4, "%%%02X", (unsigned char) *p);
103             i += 2;
104         }
105     }
106
107     return eina_stringshare_add(dest);
108 }
109
110 EAPI void
111 efreet_uri_free(Efreet_Uri *uri)
112 {
113     if (!uri) return;
114
115     IF_RELEASE(uri->protocol);
116     IF_RELEASE(uri->path);
117     IF_RELEASE(uri->hostname);
118     FREE(uri);
119 }