Mercurial > mplayer.hg
annotate libmpdemux/url.c @ 4115:1de4b71cab3a
Additional AWK notes
author | nick |
---|---|
date | Sat, 12 Jan 2002 18:58:41 +0000 |
parents | a1522fa7728a |
children | 639b3b47b138 |
rev | line source |
---|---|
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
1 /* |
902 | 2 * URL Helper |
3 * by Bertrand Baudet <bertrand_baudet@yahoo.com> | |
4 * (C) 2001, MPlayer team. | |
5 * | |
6 * TODO: | |
7 * Extract the username/password if present | |
8 */ | |
9 | |
833 | 10 #include <string.h> |
11 #include <stdlib.h> | |
12 #include <stdio.h> | |
13 | |
14 #include "url.h" | |
15 | |
16 URL_t* | |
902 | 17 url_new(char* url) { |
833 | 18 int pos1, pos2; |
19 URL_t* Curl; | |
3494
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3040
diff
changeset
|
20 char *ptr1, *ptr2, *ptr3; |
833 | 21 |
22 // Create the URL container | |
23 Curl = (URL_t*)malloc(sizeof(URL_t)); | |
24 if( Curl==NULL ) { | |
25 printf("Memory allocation failed!\n"); | |
1631 | 26 return NULL; |
833 | 27 } |
840 | 28 // Initialisation of the URL container members |
902 | 29 memset( Curl, 0, sizeof(URL_t) ); |
840 | 30 |
833 | 31 // Copy the url in the URL container |
3612 | 32 Curl->url = strdup(url); |
833 | 33 if( Curl->url==NULL ) { |
34 printf("Memory allocation failed!\n"); | |
1631 | 35 return NULL; |
833 | 36 } |
37 | |
38 // extract the protocol | |
39 ptr1 = strstr(url, "://"); | |
40 if( ptr1==NULL ) { | |
997 | 41 printf("Not an URL!\n"); |
833 | 42 return NULL; |
43 } | |
44 pos1 = ptr1-url; | |
45 Curl->protocol = (char*)malloc(pos1+1); | |
46 strncpy(Curl->protocol, url, pos1); | |
47 Curl->protocol[pos1] = '\0'; | |
48 | |
49 // look if the port is given | |
50 ptr2 = strstr(ptr1+3, ":"); | |
3494
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3040
diff
changeset
|
51 // If the : is after the first / it isn't the port |
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3040
diff
changeset
|
52 ptr3 = strstr(ptr1+3, "/"); |
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3040
diff
changeset
|
53 if(ptr3 && ptr3 - ptr2 < 0) ptr2 = NULL; |
833 | 54 if( ptr2==NULL ) { |
55 // No port is given | |
56 // Look if a path is given | |
57 ptr2 = strstr(ptr1+3, "/"); | |
58 if( ptr2==NULL ) { | |
59 // No path/filename | |
60 // So we have an URL like http://www.hostname.com | |
61 pos2 = strlen(url); | |
62 } else { | |
63 // We have an URL like http://www.hostname.com/file.txt | |
64 pos2 = ptr2-url; | |
65 } | |
66 } else { | |
67 // We have an URL beginning like http://www.hostname.com:1212 | |
68 // Get the port number | |
69 Curl->port = atoi(ptr2+1); | |
70 pos2 = ptr2-url; | |
71 } | |
72 // copy the hostname in the URL container | |
997 | 73 Curl->hostname = (char*)malloc(pos2-pos1-3+1); |
833 | 74 if( Curl->hostname==NULL ) { |
75 printf("Memory allocation failed!\n"); | |
1631 | 76 return NULL; |
833 | 77 } |
78 strncpy(Curl->hostname, ptr1+3, pos2-pos1-3); | |
840 | 79 Curl->hostname[pos2-pos1-3] = '\0'; |
833 | 80 |
81 // Look if a path is given | |
82 ptr2 = strstr(ptr1+3, "/"); | |
83 if( ptr2!=NULL ) { | |
84 // A path/filename is given | |
85 // check if it's not a trailing '/' | |
86 if( strlen(ptr2)>1 ) { | |
87 // copy the path/filename in the URL container | |
3612 | 88 Curl->file = strdup(ptr2); |
840 | 89 if( Curl->file==NULL ) { |
833 | 90 printf("Memory allocation failed!\n"); |
1631 | 91 return NULL; |
833 | 92 } |
93 } | |
869
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
94 } |
997 | 95 // Check if a filenme was given or set, else set it with '/' |
869
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
96 if( Curl->file==NULL ) { |
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
97 Curl->file = (char*)malloc(2); |
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
98 if( Curl->file==NULL ) { |
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
99 printf("Memory allocation failed!\n"); |
1631 | 100 return NULL; |
869
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
101 } |
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
102 strcpy(Curl->file, "/"); |
833 | 103 } |
104 | |
105 return Curl; | |
106 } | |
107 | |
3040 | 108 URL_t * |
109 url_copy(URL_t* url) { | |
110 URL_t *dup_url; | |
111 | |
112 if( url==NULL ) return NULL; | |
113 dup_url = (URL_t*)malloc(sizeof(URL_t)); | |
114 if( dup_url==NULL ) { | |
115 printf("Memory allocation failed!\n"); | |
116 return NULL; | |
117 } | |
118 memcpy( dup_url, url, sizeof(URL_t) ); | |
119 | |
120 return dup_url; | |
121 } | |
122 | |
123 | |
833 | 124 void |
902 | 125 url_free(URL_t* url) { |
1530
3effaac0ddb7
silly bug fixed - thanx Szekeres Istvan <szekeres@webvilag.com>
arpi
parents:
997
diff
changeset
|
126 if(!url) return; |
833 | 127 if(url->url) free(url->url); |
128 if(url->protocol) free(url->protocol); | |
129 if(url->hostname) free(url->hostname); | |
840 | 130 if(url->file) free(url->file); |
902 | 131 if(url->username) free(url->username); |
132 if(url->password) free(url->password); | |
833 | 133 free(url); |
134 } | |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
135 |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
136 |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
137 /* Replace escape sequences in an URL (or a part of an URL) */ |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
138 /* works like strcpy(), but without return argument */ |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
139 /* unescape_url_string comes from ASFRecorder */ |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
140 void |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
141 url_unescape_string(char *outbuf, char *inbuf) |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
142 { |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
143 unsigned char c; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
144 do { |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
145 c = *inbuf++; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
146 if (c == '%') { |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
147 unsigned char c1 = *inbuf++; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
148 unsigned char c2 = *inbuf++; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
149 if ( ((c1>='0' && c1<='9') || (c1>='A' && c1<='F')) && |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
150 ((c2>='0' && c2<='9') || (c2>='A' && c2<='F')) ) { |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
151 if (c1>='0' && c1<='9') c1-='0'; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
152 else c1-='A'; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
153 if (c2>='0' && c2<='9') c2-='0'; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
154 else c2-='A'; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
155 c = (c1<<4) + c2; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
156 } |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
157 } |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
158 *outbuf++ = c; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
159 } while (c != '\0'); |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
160 } |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
161 |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
162 /* Replace specific characters in the URL string by an escape sequence */ |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
163 /* works like strcpy(), but without return argument */ |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
164 /* escape_url_string comes from ASFRecorder */ |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
165 void |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
166 url_escape_string(char *outbuf, char *inbuf) { |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
167 unsigned char c; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
168 do { |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
169 c = *inbuf++; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
170 if( (c >= 'A' && c <= 'Z') || |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
171 (c >= 'a' && c <= 'z') || |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
172 (c >= '0' && c <= '9') || |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
173 (c >= 0x7f) || /* fareast languages(Chinese, Korean, Japanese) */ |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
174 c=='-' || c=='_' || c=='.' || c=='!' || c=='~' || /* mark characters */ |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
175 c=='*' || c=='\'' || c=='(' || c==')' || c=='%' || /* do not touch escape character */ |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
176 c==';' || c=='/' || c=='?' || c==':' || c=='@' || /* reserved characters */ |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
177 c=='&' || c=='=' || c=='+' || c=='$' || c==',' || /* see RFC 2396 */ |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
178 c=='\0' ) { |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
179 *outbuf++ = c; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
180 } else { |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
181 /* all others will be escaped */ |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
182 unsigned char c1 = ((c & 0xf0) >> 4); |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
183 unsigned char c2 = (c & 0x0f); |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
184 if (c1 < 10) c1+='0'; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
185 else c1+='A'; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
186 if (c2 < 10) c2+='0'; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
187 else c2+='A'; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
188 *outbuf++ = '%'; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
189 *outbuf++ = c1; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
190 *outbuf++ = c2; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
191 } |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
192 } while (c != '\0'); |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
193 } |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
194 |