2001-12-14 23:48:47 +00:00
|
|
|
/*
|
2001-05-29 16:58:52 +00:00
|
|
|
* URL Helper
|
|
|
|
* by Bertrand Baudet <bertrand_baudet@yahoo.com>
|
|
|
|
* (C) 2001, MPlayer team.
|
|
|
|
*
|
|
|
|
* TODO:
|
|
|
|
* Extract the username/password if present
|
|
|
|
*/
|
|
|
|
|
2001-05-18 16:14:06 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "url.h"
|
2002-05-02 10:40:35 +00:00
|
|
|
#include "mp_msg.h"
|
2001-05-18 16:14:06 +00:00
|
|
|
|
|
|
|
URL_t*
|
2001-05-29 16:58:52 +00:00
|
|
|
url_new(char* url) {
|
2001-05-18 16:14:06 +00:00
|
|
|
int pos1, pos2;
|
|
|
|
URL_t* Curl;
|
2001-12-14 20:45:30 +00:00
|
|
|
char *ptr1, *ptr2, *ptr3;
|
2001-05-18 16:14:06 +00:00
|
|
|
|
2002-02-11 05:19:11 +00:00
|
|
|
if( url==NULL ) return NULL;
|
|
|
|
|
2001-05-18 16:14:06 +00:00
|
|
|
// Create the URL container
|
|
|
|
Curl = (URL_t*)malloc(sizeof(URL_t));
|
|
|
|
if( Curl==NULL ) {
|
2002-05-02 10:40:35 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed!\n");
|
2001-08-22 19:40:46 +00:00
|
|
|
return NULL;
|
2001-05-18 16:14:06 +00:00
|
|
|
}
|
2001-05-20 12:42:14 +00:00
|
|
|
// Initialisation of the URL container members
|
2001-05-29 16:58:52 +00:00
|
|
|
memset( Curl, 0, sizeof(URL_t) );
|
2001-05-20 12:42:14 +00:00
|
|
|
|
2001-05-18 16:14:06 +00:00
|
|
|
// Copy the url in the URL container
|
2001-12-19 13:03:22 +00:00
|
|
|
Curl->url = strdup(url);
|
2001-05-18 16:14:06 +00:00
|
|
|
if( Curl->url==NULL ) {
|
2002-05-02 10:40:35 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed!\n");
|
2001-08-22 19:40:46 +00:00
|
|
|
return NULL;
|
2001-05-18 16:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// extract the protocol
|
|
|
|
ptr1 = strstr(url, "://");
|
|
|
|
if( ptr1==NULL ) {
|
2002-05-02 10:40:35 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"Not an URL!\n");
|
2001-05-18 16:14:06 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
pos1 = ptr1-url;
|
|
|
|
Curl->protocol = (char*)malloc(pos1+1);
|
|
|
|
strncpy(Curl->protocol, url, pos1);
|
|
|
|
Curl->protocol[pos1] = '\0';
|
|
|
|
|
|
|
|
// look if the port is given
|
|
|
|
ptr2 = strstr(ptr1+3, ":");
|
2001-12-14 20:45:30 +00:00
|
|
|
// If the : is after the first / it isn't the port
|
|
|
|
ptr3 = strstr(ptr1+3, "/");
|
|
|
|
if(ptr3 && ptr3 - ptr2 < 0) ptr2 = NULL;
|
2001-05-18 16:14:06 +00:00
|
|
|
if( ptr2==NULL ) {
|
|
|
|
// No port is given
|
|
|
|
// Look if a path is given
|
|
|
|
ptr2 = strstr(ptr1+3, "/");
|
|
|
|
if( ptr2==NULL ) {
|
|
|
|
// No path/filename
|
|
|
|
// So we have an URL like http://www.hostname.com
|
|
|
|
pos2 = strlen(url);
|
|
|
|
} else {
|
|
|
|
// We have an URL like http://www.hostname.com/file.txt
|
|
|
|
pos2 = ptr2-url;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We have an URL beginning like http://www.hostname.com:1212
|
|
|
|
// Get the port number
|
|
|
|
Curl->port = atoi(ptr2+1);
|
|
|
|
pos2 = ptr2-url;
|
|
|
|
}
|
|
|
|
// copy the hostname in the URL container
|
2001-06-04 17:47:43 +00:00
|
|
|
Curl->hostname = (char*)malloc(pos2-pos1-3+1);
|
2001-05-18 16:14:06 +00:00
|
|
|
if( Curl->hostname==NULL ) {
|
2002-05-02 10:40:35 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed!\n");
|
2001-08-22 19:40:46 +00:00
|
|
|
return NULL;
|
2001-05-18 16:14:06 +00:00
|
|
|
}
|
|
|
|
strncpy(Curl->hostname, ptr1+3, pos2-pos1-3);
|
2001-05-20 12:42:14 +00:00
|
|
|
Curl->hostname[pos2-pos1-3] = '\0';
|
2001-05-18 16:14:06 +00:00
|
|
|
|
|
|
|
// Look if a path is given
|
|
|
|
ptr2 = strstr(ptr1+3, "/");
|
|
|
|
if( ptr2!=NULL ) {
|
|
|
|
// A path/filename is given
|
|
|
|
// check if it's not a trailing '/'
|
|
|
|
if( strlen(ptr2)>1 ) {
|
|
|
|
// copy the path/filename in the URL container
|
2001-12-19 13:03:22 +00:00
|
|
|
Curl->file = strdup(ptr2);
|
2001-05-20 12:42:14 +00:00
|
|
|
if( Curl->file==NULL ) {
|
2002-05-02 10:40:35 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed!\n");
|
2001-08-22 19:40:46 +00:00
|
|
|
return NULL;
|
2001-05-18 16:14:06 +00:00
|
|
|
}
|
|
|
|
}
|
2001-05-25 13:57:18 +00:00
|
|
|
}
|
2001-06-04 17:47:43 +00:00
|
|
|
// Check if a filenme was given or set, else set it with '/'
|
2001-05-25 13:57:18 +00:00
|
|
|
if( Curl->file==NULL ) {
|
|
|
|
Curl->file = (char*)malloc(2);
|
|
|
|
if( Curl->file==NULL ) {
|
2002-05-02 10:40:35 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed!\n");
|
2001-08-22 19:40:46 +00:00
|
|
|
return NULL;
|
2001-05-25 13:57:18 +00:00
|
|
|
}
|
|
|
|
strcpy(Curl->file, "/");
|
2001-05-18 16:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Curl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-05-29 16:58:52 +00:00
|
|
|
url_free(URL_t* url) {
|
2001-08-15 12:44:05 +00:00
|
|
|
if(!url) return;
|
2001-05-18 16:14:06 +00:00
|
|
|
if(url->url) free(url->url);
|
|
|
|
if(url->protocol) free(url->protocol);
|
|
|
|
if(url->hostname) free(url->hostname);
|
2001-05-20 12:42:14 +00:00
|
|
|
if(url->file) free(url->file);
|
2001-05-29 16:58:52 +00:00
|
|
|
if(url->username) free(url->username);
|
|
|
|
if(url->password) free(url->password);
|
2001-05-18 16:14:06 +00:00
|
|
|
free(url);
|
|
|
|
}
|
2001-12-14 23:48:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Replace escape sequences in an URL (or a part of an URL) */
|
|
|
|
/* works like strcpy(), but without return argument */
|
|
|
|
/* unescape_url_string comes from ASFRecorder */
|
|
|
|
void
|
|
|
|
url_unescape_string(char *outbuf, char *inbuf)
|
|
|
|
{
|
|
|
|
unsigned char c;
|
|
|
|
do {
|
|
|
|
c = *inbuf++;
|
|
|
|
if (c == '%') {
|
|
|
|
unsigned char c1 = *inbuf++;
|
|
|
|
unsigned char c2 = *inbuf++;
|
|
|
|
if ( ((c1>='0' && c1<='9') || (c1>='A' && c1<='F')) &&
|
|
|
|
((c2>='0' && c2<='9') || (c2>='A' && c2<='F')) ) {
|
|
|
|
if (c1>='0' && c1<='9') c1-='0';
|
|
|
|
else c1-='A';
|
|
|
|
if (c2>='0' && c2<='9') c2-='0';
|
|
|
|
else c2-='A';
|
|
|
|
c = (c1<<4) + c2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*outbuf++ = c;
|
|
|
|
} while (c != '\0');
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Replace specific characters in the URL string by an escape sequence */
|
|
|
|
/* works like strcpy(), but without return argument */
|
|
|
|
/* escape_url_string comes from ASFRecorder */
|
|
|
|
void
|
|
|
|
url_escape_string(char *outbuf, char *inbuf) {
|
|
|
|
unsigned char c;
|
|
|
|
do {
|
|
|
|
c = *inbuf++;
|
|
|
|
if( (c >= 'A' && c <= 'Z') ||
|
|
|
|
(c >= 'a' && c <= 'z') ||
|
|
|
|
(c >= '0' && c <= '9') ||
|
|
|
|
(c >= 0x7f) || /* fareast languages(Chinese, Korean, Japanese) */
|
|
|
|
c=='-' || c=='_' || c=='.' || c=='!' || c=='~' || /* mark characters */
|
|
|
|
c=='*' || c=='\'' || c=='(' || c==')' || c=='%' || /* do not touch escape character */
|
|
|
|
c==';' || c=='/' || c=='?' || c==':' || c=='@' || /* reserved characters */
|
|
|
|
c=='&' || c=='=' || c=='+' || c=='$' || c==',' || /* see RFC 2396 */
|
|
|
|
c=='\0' ) {
|
|
|
|
*outbuf++ = c;
|
|
|
|
} else {
|
|
|
|
/* all others will be escaped */
|
|
|
|
unsigned char c1 = ((c & 0xf0) >> 4);
|
|
|
|
unsigned char c2 = (c & 0x0f);
|
|
|
|
if (c1 < 10) c1+='0';
|
|
|
|
else c1+='A';
|
|
|
|
if (c2 < 10) c2+='0';
|
|
|
|
else c2+='A';
|
|
|
|
*outbuf++ = '%';
|
|
|
|
*outbuf++ = c1;
|
|
|
|
*outbuf++ = c2;
|
|
|
|
}
|
|
|
|
} while (c != '\0');
|
|
|
|
}
|
|
|
|
|
2002-01-12 21:02:00 +00:00
|
|
|
#ifdef __URL_DEBUG
|
|
|
|
void
|
|
|
|
url_debug(URL_t *url) {
|
|
|
|
if( url==NULL ) {
|
|
|
|
printf("URL pointer NULL\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if( url->url!=NULL ) {
|
|
|
|
printf("url=%s\n", url->url );
|
|
|
|
}
|
|
|
|
if( url->protocol!=NULL ) {
|
|
|
|
printf("protocol=%s\n", url->protocol );
|
|
|
|
}
|
|
|
|
if( url->hostname!=NULL ) {
|
|
|
|
printf("hostname=%s\n", url->hostname );
|
|
|
|
}
|
|
|
|
printf("port=%d\n", url->port );
|
|
|
|
if( url->file!=NULL ) {
|
|
|
|
printf("file=%s\n", url->file );
|
|
|
|
}
|
|
|
|
if( url->username!=NULL ) {
|
|
|
|
printf("username=%s\n", url->username );
|
|
|
|
}
|
|
|
|
if( url->password!=NULL ) {
|
|
|
|
printf("password=%s\n", url->password );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif //__URL_DEBUG
|