1 #include "tool.h"
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <ctype.h>
6
parseURL(const char * url)7 serverSettings_t *parseURL(const char *url)
8 {
9 serverSettings_t *settings;
10 int i, j, k;
11 short flag = 0;
12 char c;
13 char *tmpPort;
14
15 if (url == NULL)
16 return NULL;
17
18 settings = (serverSettings_t *) malloc(sizeof(serverSettings_t));
19 if (settings == NULL)
20 {
21 perror("malloc");
22 return NULL;
23 }
24
25 tmpPort = (char *) memory_allocation(5 + 1); /* Maxport == 65535 + 1 (\0) */
26
27 if (strstr(url, "://") == NULL)
28 return NULL;
29
30 /* Init */
31 settings->port = 80;
32 settings->mountpoint = "/";
33 settings->serverAddr = NULL;
34 for (i = 0, j = 0, k = 0; i <= strlen(url); i++)
35 {
36 c = url[i];
37
38 /* Flag == 0: on cherche la fin de http://
39 */
40 if ((flag == 0) && (c == '/') && ((i+1) <= strlen(url)))
41 {
42 if (url[(i+1)] == '/')
43 {
44 i++;
45 settings->serverAddr = (char *) memory_allocation(strlen(url) - 5);
46 flag = 1;
47 }
48 /* Flag == 1:
49 * on a un proto de type "kjklsjdf://", on remplie directement host
50 */
51 } else if (flag == 1) {
52 if (c == ':')
53 {
54 settings->serverAddr[j] = 0;
55 j = 0;
56 flag = 2;
57 } else if (c == '/') {
58 settings->serverAddr[j] = 0;
59 settings->port = 80;
60 j = 0;
61 flag = 3;
62 settings->mountpoint = (char *) memory_allocation(strlen(url) +1 );
63 settings->mountpoint[j++] = '/';
64 } else {
65 settings->serverAddr[j++] = c;
66 }
67 /* Flag == 2:
68 * On a trouv� un :, on a donc le port en suite
69 */
70 } else if (flag == 2) {
71 /* port de comm */
72 if (c != '/')
73 {
74 if (j < 5)
75 tmpPort[j++] = c;
76 } else {
77 if (j <= 5)
78 tmpPort[j] = 0;
79 j = 0;
80 settings->port = atoi(tmpPort);
81 flag = 3;
82 settings->mountpoint = (char *) memory_allocation(strlen(url) - strlen(tmpPort) + 1);
83 /* We should remove 7 bytes for http:// but it'll not work if http:// not given,
84 * so I'm not removing 7 bytes.
85 */
86 settings->mountpoint[j++] = '/';
87 (void) free(tmpPort);
88 }
89 /* Flag == 3
90 * on cherche le mountpoint
91 */
92 } else if (flag == 3) {
93 if (i >= strlen(url)) {
94 settings->mountpoint[j] = 0;
95 break;
96 } else {
97 settings->mountpoint[j++] = c;
98 }
99 }
100 } /* For */
101
102 /* printServSettings(settings); */
103 return settings;
104 }
105
printServSettings(serverSettings_t * set)106 void printServSettings(serverSettings_t *set)
107 {
108 printf("host: %s\n", set->serverAddr);
109 printf("port: %d\n", set->port);
110 printf("mp: %s\n", set->mountpoint);
111 return;
112 }
113
114 /* Usefull for debugging :p */
memory_allocation(int size)115 void *memory_allocation(int size)
116 {
117 void *ptr;
118
119 if ((ptr = calloc(size, sizeof(char))) == NULL) {
120 perror("Error allocating memory. Out Of Memory ?\n");
121 exit(-1);
122 }
123
124 return ptr;
125 }
126
stripcolon(const char * from,char * to)127 int stripcolon(const char *from, char *to)
128 {
129 int len = 0;
130
131 if (! from)
132 return 0;
133
134 while (*from != '\0')
135 {
136 switch (*from)
137 {
138 case ':':
139 break;
140 default:
141 len++;
142 *to++ = *from;
143 break;
144 }
145
146 from++;
147 }
148
149 return len;
150 }
151
stripBadChar(const char * from,char * to)152 void stripBadChar(const char *from, char *to)
153 {
154 if (from == NULL)
155 return;
156
157 while (*from != '\0')
158 {
159 switch (*from)
160 {
161 case '|':
162 *to++ = 'l';
163 break;
164 case '`':
165 *to++ = '\'';
166 break;
167 case '/':
168 case '\\':
169 break;
170
171 default:
172 if (isascii(*from))
173 *to++ = *from;
174 break;
175 }
176 from++;
177 }
178
179 *to = '\0';
180 }
181