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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2001-05-18 16:14:06 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2004-05-01 23:35:25 +00:00
|
|
|
#include <ctype.h>
|
2006-06-04 22:41:27 +00:00
|
|
|
#include <inttypes.h>
|
2001-05-18 16:14:06 +00:00
|
|
|
|
|
|
|
#include "url.h"
|
2002-05-02 10:40:35 +00:00
|
|
|
#include "mp_msg.h"
|
2005-10-31 23:37:41 +00:00
|
|
|
#include "help_mp.h"
|
2001-05-18 16:14:06 +00:00
|
|
|
|
2006-06-09 23:19:13 +00:00
|
|
|
#ifndef SIZE_MAX
|
|
|
|
#define SIZE_MAX ((size_t)-1)
|
|
|
|
#endif
|
|
|
|
|
2006-11-08 22:42:31 +00:00
|
|
|
URL_t *url_redirect(URL_t **url, const char *redir) {
|
|
|
|
URL_t *u = *url;
|
|
|
|
URL_t *res;
|
2006-11-21 16:26:38 +00:00
|
|
|
if (!strchr(redir, '/') || *redir == '/') {
|
2006-11-08 22:42:31 +00:00
|
|
|
char *tmp;
|
|
|
|
char *newurl = malloc(strlen(u->url) + strlen(redir) + 1);
|
|
|
|
strcpy(newurl, u->url);
|
2006-11-21 16:26:38 +00:00
|
|
|
if (*redir == '/') {
|
|
|
|
redir++;
|
|
|
|
tmp = strstr(newurl, "://");
|
|
|
|
if (tmp) tmp = strchr(tmp + 3, '/');
|
|
|
|
} else
|
2006-11-21 16:27:05 +00:00
|
|
|
tmp = strrchr(newurl, '/');
|
2006-11-08 22:42:31 +00:00
|
|
|
if (tmp) tmp[1] = 0;
|
|
|
|
strcat(newurl, redir);
|
|
|
|
res = url_new(newurl);
|
|
|
|
free(newurl);
|
|
|
|
} else
|
|
|
|
res = url_new(redir);
|
|
|
|
url_free(u);
|
|
|
|
*url = res;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-05-18 16:14:06 +00:00
|
|
|
URL_t*
|
2003-03-26 11:27:48 +00:00
|
|
|
url_new(const char* url) {
|
2004-01-10 14:38:07 +00:00
|
|
|
int pos1, pos2,v6addr = 0;
|
2005-09-06 21:45:40 +00:00
|
|
|
URL_t* Curl = NULL;
|
2004-05-01 23:35:25 +00:00
|
|
|
char *escfilename=NULL;
|
2004-01-10 14:38:07 +00:00
|
|
|
char *ptr1=NULL, *ptr2=NULL, *ptr3=NULL, *ptr4=NULL;
|
2003-05-03 06:16:07 +00:00
|
|
|
int jumpSize = 3;
|
2001-05-18 16:14:06 +00:00
|
|
|
|
2002-02-11 05:19:11 +00:00
|
|
|
if( url==NULL ) return NULL;
|
|
|
|
|
2006-06-04 22:41:27 +00:00
|
|
|
if (strlen(url) > (SIZE_MAX / 3 - 1)) {
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
|
|
|
|
goto err_out;
|
|
|
|
}
|
2004-05-01 23:35:25 +00:00
|
|
|
escfilename=malloc(strlen(url)*3+1);
|
|
|
|
if (!escfilename ) {
|
2005-11-05 00:55:34 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
|
2005-09-06 21:45:40 +00:00
|
|
|
goto err_out;
|
2004-05-01 23:35:25 +00:00
|
|
|
}
|
|
|
|
|
2001-05-18 16:14:06 +00:00
|
|
|
// Create the URL container
|
2006-07-02 08:17:07 +00:00
|
|
|
Curl = malloc(sizeof(URL_t));
|
2001-05-18 16:14:06 +00:00
|
|
|
if( Curl==NULL ) {
|
2005-11-05 00:55:34 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
|
2005-09-06 21:45:40 +00:00
|
|
|
goto err_out;
|
2001-05-18 16:14:06 +00:00
|
|
|
}
|
2004-05-01 23:35:25 +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
|
|
|
|
2005-11-26 15:57:39 +00:00
|
|
|
url_escape_string(escfilename,url);
|
2004-05-01 23:35:25 +00:00
|
|
|
|
2001-05-18 16:14:06 +00:00
|
|
|
// Copy the url in the URL container
|
2004-05-01 23:35:25 +00:00
|
|
|
Curl->url = strdup(escfilename);
|
2001-05-18 16:14:06 +00:00
|
|
|
if( Curl->url==NULL ) {
|
2005-11-05 00:55:34 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
|
2005-09-06 21:45:40 +00:00
|
|
|
goto err_out;
|
2001-05-18 16:14:06 +00:00
|
|
|
}
|
2004-05-01 23:35:25 +00:00
|
|
|
mp_msg(MSGT_OPEN,MSGL_V,"Filename for url is now %s\n",escfilename);
|
2001-05-18 16:14:06 +00:00
|
|
|
|
|
|
|
// extract the protocol
|
2004-05-01 23:35:25 +00:00
|
|
|
ptr1 = strstr(escfilename, "://");
|
2001-05-18 16:14:06 +00:00
|
|
|
if( ptr1==NULL ) {
|
2003-05-03 06:16:07 +00:00
|
|
|
// Check for a special case: "sip:" (without "//"):
|
2004-05-01 23:35:25 +00:00
|
|
|
if (strstr(escfilename, "sip:") == escfilename) {
|
2003-11-08 00:26:51 +00:00
|
|
|
ptr1 = (char *)&url[3]; // points to ':'
|
2003-05-03 06:16:07 +00:00
|
|
|
jumpSize = 1;
|
|
|
|
} else {
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"Not an URL!\n");
|
2005-09-06 21:45:40 +00:00
|
|
|
goto err_out;
|
2003-05-03 06:16:07 +00:00
|
|
|
}
|
2001-05-18 16:14:06 +00:00
|
|
|
}
|
2004-05-01 23:35:25 +00:00
|
|
|
pos1 = ptr1-escfilename;
|
2006-07-02 08:17:07 +00:00
|
|
|
Curl->protocol = malloc(pos1+1);
|
2002-06-23 06:27:19 +00:00
|
|
|
if( Curl->protocol==NULL ) {
|
2005-11-05 00:55:34 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
|
2005-09-06 21:45:40 +00:00
|
|
|
goto err_out;
|
2002-06-23 06:27:19 +00:00
|
|
|
}
|
2004-06-25 14:57:01 +00:00
|
|
|
strncpy(Curl->protocol, escfilename, pos1);
|
2001-05-18 16:14:06 +00:00
|
|
|
Curl->protocol[pos1] = '\0';
|
|
|
|
|
2002-06-23 06:27:19 +00:00
|
|
|
// jump the "://"
|
2003-05-03 06:16:07 +00:00
|
|
|
ptr1 += jumpSize;
|
|
|
|
pos1 += jumpSize;
|
2002-06-23 06:27:19 +00:00
|
|
|
|
|
|
|
// check if a username:password is given
|
|
|
|
ptr2 = strstr(ptr1, "@");
|
2002-09-09 04:16:16 +00:00
|
|
|
ptr3 = strstr(ptr1, "/");
|
|
|
|
if( ptr3!=NULL && ptr3<ptr2 ) {
|
|
|
|
// it isn't really a username but rather a part of the path
|
|
|
|
ptr2 = NULL;
|
|
|
|
}
|
2002-06-23 06:27:19 +00:00
|
|
|
if( ptr2!=NULL ) {
|
|
|
|
// We got something, at least a username...
|
|
|
|
int len = ptr2-ptr1;
|
2006-07-02 08:17:07 +00:00
|
|
|
Curl->username = malloc(len+1);
|
2002-06-23 06:27:19 +00:00
|
|
|
if( Curl->username==NULL ) {
|
2005-11-05 00:55:34 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
|
2005-09-06 21:45:40 +00:00
|
|
|
goto err_out;
|
2002-06-23 06:27:19 +00:00
|
|
|
}
|
|
|
|
strncpy(Curl->username, ptr1, len);
|
|
|
|
Curl->username[len] = '\0';
|
|
|
|
|
|
|
|
ptr3 = strstr(ptr1, ":");
|
|
|
|
if( ptr3!=NULL && ptr3<ptr2 ) {
|
|
|
|
// We also have a password
|
|
|
|
int len2 = ptr2-ptr3-1;
|
|
|
|
Curl->username[ptr3-ptr1]='\0';
|
2006-07-02 08:17:07 +00:00
|
|
|
Curl->password = malloc(len2+1);
|
2002-06-23 06:27:19 +00:00
|
|
|
if( Curl->password==NULL ) {
|
2005-11-05 00:55:34 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
|
2005-09-06 21:45:40 +00:00
|
|
|
goto err_out;
|
2002-06-23 06:27:19 +00:00
|
|
|
}
|
|
|
|
strncpy( Curl->password, ptr3+1, len2);
|
|
|
|
Curl->password[len2]='\0';
|
|
|
|
}
|
|
|
|
ptr1 = ptr2+1;
|
2004-05-01 23:35:25 +00:00
|
|
|
pos1 = ptr1-escfilename;
|
2002-06-23 06:27:19 +00:00
|
|
|
}
|
2003-03-26 11:27:48 +00:00
|
|
|
|
|
|
|
// before looking for a port number check if we have an IPv6 type numeric address
|
|
|
|
// in IPv6 URL the numeric address should be inside square braces.
|
|
|
|
ptr2 = strstr(ptr1, "[");
|
|
|
|
ptr3 = strstr(ptr1, "]");
|
2004-01-10 14:38:07 +00:00
|
|
|
ptr4 = strstr(ptr1, "/");
|
|
|
|
if( ptr2!=NULL && ptr3!=NULL && ptr2 < ptr3 && (!ptr4 || ptr4 > ptr3)) {
|
2003-03-26 11:27:48 +00:00
|
|
|
// we have an IPv6 numeric address
|
|
|
|
ptr1++;
|
|
|
|
pos1++;
|
|
|
|
ptr2 = ptr3;
|
2004-01-10 14:38:07 +00:00
|
|
|
v6addr = 1;
|
2003-03-26 11:27:48 +00:00
|
|
|
} else {
|
|
|
|
ptr2 = ptr1;
|
|
|
|
|
|
|
|
}
|
2002-06-23 06:27:19 +00:00
|
|
|
|
2001-05-18 16:14:06 +00:00
|
|
|
// look if the port is given
|
2003-03-26 11:27:48 +00:00
|
|
|
ptr2 = strstr(ptr2, ":");
|
2001-12-14 20:45:30 +00:00
|
|
|
// If the : is after the first / it isn't the port
|
2002-06-23 06:27:19 +00:00
|
|
|
ptr3 = strstr(ptr1, "/");
|
2001-12-14 20:45:30 +00:00
|
|
|
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
|
2003-03-26 11:27:48 +00:00
|
|
|
if( ptr3==NULL ) {
|
2001-05-18 16:14:06 +00:00
|
|
|
// No path/filename
|
|
|
|
// So we have an URL like http://www.hostname.com
|
2004-05-01 23:35:25 +00:00
|
|
|
pos2 = strlen(escfilename);
|
2001-05-18 16:14:06 +00:00
|
|
|
} else {
|
|
|
|
// We have an URL like http://www.hostname.com/file.txt
|
2004-05-01 23:35:25 +00:00
|
|
|
pos2 = ptr3-escfilename;
|
2001-05-18 16:14:06 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We have an URL beginning like http://www.hostname.com:1212
|
|
|
|
// Get the port number
|
|
|
|
Curl->port = atoi(ptr2+1);
|
2004-05-01 23:35:25 +00:00
|
|
|
pos2 = ptr2-escfilename;
|
2001-05-18 16:14:06 +00:00
|
|
|
}
|
2004-01-10 14:38:07 +00:00
|
|
|
if( v6addr ) pos2--;
|
2001-05-18 16:14:06 +00:00
|
|
|
// copy the hostname in the URL container
|
2006-07-02 08:17:07 +00:00
|
|
|
Curl->hostname = malloc(pos2-pos1+1);
|
2001-05-18 16:14:06 +00:00
|
|
|
if( Curl->hostname==NULL ) {
|
2005-11-05 00:55:34 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
|
2005-09-06 21:45:40 +00:00
|
|
|
goto err_out;
|
2001-05-18 16:14:06 +00:00
|
|
|
}
|
2002-06-23 06:27:19 +00:00
|
|
|
strncpy(Curl->hostname, ptr1, pos2-pos1);
|
|
|
|
Curl->hostname[pos2-pos1] = '\0';
|
2001-05-18 16:14:06 +00:00
|
|
|
|
|
|
|
// Look if a path is given
|
2002-06-23 06:27:19 +00:00
|
|
|
ptr2 = strstr(ptr1, "/");
|
2001-05-18 16:14:06 +00:00
|
|
|
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 ) {
|
2005-11-05 00:55:34 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
|
2005-09-06 21:45:40 +00:00
|
|
|
goto err_out;
|
2001-05-18 16:14:06 +00:00
|
|
|
}
|
|
|
|
}
|
2001-05-25 13:57:18 +00:00
|
|
|
}
|
2003-03-26 11:27:48 +00:00
|
|
|
// Check if a filename was given or set, else set it with '/'
|
2001-05-25 13:57:18 +00:00
|
|
|
if( Curl->file==NULL ) {
|
2006-07-02 08:17:07 +00:00
|
|
|
Curl->file = malloc(2);
|
2001-05-25 13:57:18 +00:00
|
|
|
if( Curl->file==NULL ) {
|
2005-11-05 00:55:34 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
|
2005-09-06 21:45:40 +00:00
|
|
|
goto err_out;
|
2001-05-25 13:57:18 +00:00
|
|
|
}
|
|
|
|
strcpy(Curl->file, "/");
|
2001-05-18 16:14:06 +00:00
|
|
|
}
|
|
|
|
|
2004-05-01 23:35:25 +00:00
|
|
|
free(escfilename);
|
2001-05-18 16:14:06 +00:00
|
|
|
return Curl;
|
2005-09-06 21:45:40 +00:00
|
|
|
err_out:
|
|
|
|
if (escfilename) free(escfilename);
|
|
|
|
if (Curl) url_free(Curl);
|
|
|
|
return NULL;
|
2001-05-18 16:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 */
|
|
|
|
void
|
2003-03-26 11:27:48 +00:00
|
|
|
url_unescape_string(char *outbuf, const char *inbuf)
|
2001-12-14 23:48:47 +00:00
|
|
|
{
|
2004-05-01 23:35:25 +00:00
|
|
|
unsigned char c,c1,c2;
|
|
|
|
int i,len=strlen(inbuf);
|
|
|
|
for (i=0;i<len;i++){
|
|
|
|
c = inbuf[i];
|
|
|
|
if (c == '%' && i<len-2) { //must have 2 more chars
|
|
|
|
c1 = toupper(inbuf[i+1]); // we need uppercase characters
|
|
|
|
c2 = toupper(inbuf[i+2]);
|
2001-12-14 23:48:47 +00:00
|
|
|
if ( ((c1>='0' && c1<='9') || (c1>='A' && c1<='F')) &&
|
|
|
|
((c2>='0' && c2<='9') || (c2>='A' && c2<='F')) ) {
|
|
|
|
if (c1>='0' && c1<='9') c1-='0';
|
2004-01-10 14:38:07 +00:00
|
|
|
else c1-='A'-10;
|
2001-12-14 23:48:47 +00:00
|
|
|
if (c2>='0' && c2<='9') c2-='0';
|
2004-01-10 14:38:07 +00:00
|
|
|
else c2-='A'-10;
|
2001-12-14 23:48:47 +00:00
|
|
|
c = (c1<<4) + c2;
|
2004-05-01 23:35:25 +00:00
|
|
|
i=i+2; //only skip next 2 chars if valid esc
|
2001-12-14 23:48:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
*outbuf++ = c;
|
2004-05-01 23:35:25 +00:00
|
|
|
}
|
|
|
|
*outbuf++='\0'; //add nullterm to string
|
2001-12-14 23:48:47 +00:00
|
|
|
}
|
|
|
|
|
2005-11-26 15:57:39 +00:00
|
|
|
static void
|
|
|
|
url_escape_string_part(char *outbuf, const char *inbuf) {
|
2004-05-01 23:35:25 +00:00
|
|
|
unsigned char c,c1,c2;
|
|
|
|
int i,len=strlen(inbuf);
|
|
|
|
|
|
|
|
for (i=0;i<len;i++) {
|
|
|
|
c = inbuf[i];
|
|
|
|
if ((c=='%') && i<len-2 ) { //need 2 more characters
|
|
|
|
c1=toupper(inbuf[i+1]); c2=toupper(inbuf[i+2]); // need uppercase chars
|
|
|
|
} else {
|
|
|
|
c1=129; c2=129; //not escape chars
|
|
|
|
}
|
|
|
|
|
2001-12-14 23:48:47 +00:00
|
|
|
if( (c >= 'A' && c <= 'Z') ||
|
|
|
|
(c >= 'a' && c <= 'z') ||
|
|
|
|
(c >= '0' && c <= '9') ||
|
2005-11-26 15:57:39 +00:00
|
|
|
(c >= 0x7f)) {
|
2001-12-14 23:48:47 +00:00
|
|
|
*outbuf++ = c;
|
2004-05-01 23:35:25 +00:00
|
|
|
} else if ( c=='%' && ((c1 >= '0' && c1 <= '9') || (c1 >= 'A' && c1 <= 'F')) &&
|
|
|
|
((c2 >= '0' && c2 <= '9') || (c2 >= 'A' && c2 <= 'F'))) {
|
|
|
|
// check if part of an escape sequence
|
|
|
|
*outbuf++=c; // already
|
|
|
|
|
|
|
|
// dont escape again
|
2005-10-31 23:37:41 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_URL_StringAlreadyEscaped,c,c1,c2);
|
2004-05-01 23:35:25 +00:00
|
|
|
// error as this should not happen against RFC 2396
|
|
|
|
// to escape a string twice
|
2001-12-14 23:48:47 +00:00
|
|
|
} else {
|
|
|
|
/* all others will be escaped */
|
2004-05-01 23:35:25 +00:00
|
|
|
c1 = ((c & 0xf0) >> 4);
|
|
|
|
c2 = (c & 0x0f);
|
2001-12-14 23:48:47 +00:00
|
|
|
if (c1 < 10) c1+='0';
|
2003-04-18 12:16:35 +00:00
|
|
|
else c1+='A'-10;
|
2001-12-14 23:48:47 +00:00
|
|
|
if (c2 < 10) c2+='0';
|
2003-04-18 12:16:35 +00:00
|
|
|
else c2+='A'-10;
|
2001-12-14 23:48:47 +00:00
|
|
|
*outbuf++ = '%';
|
|
|
|
*outbuf++ = c1;
|
|
|
|
*outbuf++ = c2;
|
|
|
|
}
|
2004-05-01 23:35:25 +00:00
|
|
|
}
|
|
|
|
*outbuf++='\0';
|
2001-12-14 23:48:47 +00:00
|
|
|
}
|
|
|
|
|
2005-11-26 15:57:39 +00:00
|
|
|
/* Replace specific characters in the URL string by an escape sequence */
|
|
|
|
/* works like strcpy(), but without return argument */
|
|
|
|
void
|
|
|
|
url_escape_string(char *outbuf, const char *inbuf) {
|
|
|
|
unsigned char c;
|
|
|
|
int i = 0,j,len = strlen(inbuf);
|
|
|
|
char* tmp,*unesc = NULL, *in;
|
|
|
|
|
|
|
|
// Look if we have an ip6 address, if so skip it there is
|
|
|
|
// no need to escape anything in there.
|
|
|
|
tmp = strstr(inbuf,"://[");
|
|
|
|
if(tmp) {
|
|
|
|
tmp = strchr(tmp+4,']');
|
|
|
|
if(tmp && (tmp[1] == '/' || tmp[1] == ':' ||
|
|
|
|
tmp[1] == '\0')) {
|
|
|
|
i = tmp+1-inbuf;
|
|
|
|
strncpy(outbuf,inbuf,i);
|
|
|
|
outbuf += i;
|
|
|
|
tmp = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while(i < len) {
|
|
|
|
// look for the next char that must be kept
|
|
|
|
for (j=i;j<len;j++) {
|
|
|
|
c = inbuf[j];
|
|
|
|
if(c=='-' || c=='_' || c=='.' || c=='!' || c=='~' || /* mark characters */
|
|
|
|
c=='*' || c=='\'' || c=='(' || c==')' || /* do not touch escape character */
|
|
|
|
c==';' || c=='/' || c=='?' || c==':' || c=='@' || /* reserved characters */
|
|
|
|
c=='&' || c=='=' || c=='+' || c=='$' || c==',') /* see RFC 2396 */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// we are on a reserved char, write it out
|
|
|
|
if(j == i) {
|
|
|
|
*outbuf++ = c;
|
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// we found one, take that part of the string
|
|
|
|
if(j < len) {
|
|
|
|
if(!tmp) tmp = malloc(len+1);
|
|
|
|
strncpy(tmp,inbuf+i,j-i);
|
|
|
|
tmp[j-i] = '\0';
|
|
|
|
in = tmp;
|
|
|
|
} else // take the rest of the string
|
|
|
|
in = (char*)inbuf+i;
|
|
|
|
|
|
|
|
if(!unesc) unesc = malloc(len+1);
|
|
|
|
// unescape first to avoid escaping escape
|
|
|
|
url_unescape_string(unesc,in);
|
|
|
|
// then escape, including mark and other reserved chars
|
|
|
|
// that can come from escape sequences
|
|
|
|
url_escape_string_part(outbuf,unesc);
|
|
|
|
outbuf += strlen(outbuf);
|
|
|
|
i += strlen(in);
|
|
|
|
}
|
|
|
|
*outbuf = '\0';
|
|
|
|
if(tmp) free(tmp);
|
|
|
|
if(unesc) free(unesc);
|
|
|
|
}
|
|
|
|
|
2002-01-12 21:02:00 +00:00
|
|
|
#ifdef __URL_DEBUG
|
|
|
|
void
|
2003-03-26 11:27:48 +00:00
|
|
|
url_debug(const URL_t *url) {
|
2002-01-12 21:02:00 +00:00
|
|
|
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
|