2001-05-18 16:14:06 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "url.h"
|
|
|
|
|
|
|
|
URL_t*
|
|
|
|
set_url(char* url) {
|
|
|
|
int pos1, pos2;
|
|
|
|
URL_t* Curl;
|
|
|
|
char *ptr1, *ptr2;
|
|
|
|
|
|
|
|
// Create the URL container
|
|
|
|
Curl = (URL_t*)malloc(sizeof(URL_t));
|
|
|
|
if( Curl==NULL ) {
|
|
|
|
printf("Memory allocation failed!\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2001-05-20 12:42:14 +00:00
|
|
|
// Initialisation of the URL container members
|
|
|
|
Curl->url = NULL;
|
|
|
|
Curl->protocol = NULL;
|
|
|
|
Curl->hostname = NULL;
|
|
|
|
Curl->file = NULL;
|
|
|
|
Curl->port = 0;
|
|
|
|
|
2001-05-18 16:14:06 +00:00
|
|
|
// Copy the url in the URL container
|
|
|
|
Curl->url = (char*)malloc(strlen(url)+1);
|
|
|
|
if( Curl->url==NULL ) {
|
|
|
|
printf("Memory allocation failed!\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
strcpy(Curl->url, url);
|
|
|
|
|
|
|
|
// extract the protocol
|
|
|
|
ptr1 = strstr(url, "://");
|
|
|
|
if( ptr1==NULL ) {
|
2001-05-20 12:42:14 +00:00
|
|
|
printf("Malformed URL or 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, ":");
|
|
|
|
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
|
|
|
|
Curl->hostname = (char*)malloc(strlen(url)+1);
|
|
|
|
if( Curl->hostname==NULL ) {
|
|
|
|
printf("Memory allocation failed!\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
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-05-25 13:57:18 +00:00
|
|
|
Curl->file = (char*)malloc(strlen(ptr2)+1);
|
2001-05-20 12:42:14 +00:00
|
|
|
if( Curl->file==NULL ) {
|
2001-05-18 16:14:06 +00:00
|
|
|
printf("Memory allocation failed!\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2001-05-25 13:57:18 +00:00
|
|
|
Curl->file[0]='/';
|
|
|
|
strcpy(Curl->file+1, ptr2+1);
|
2001-05-18 16:14:06 +00:00
|
|
|
}
|
2001-05-25 13:57:18 +00:00
|
|
|
}
|
|
|
|
// Check if a filenme was given or set else set it with '/'
|
|
|
|
if( Curl->file==NULL ) {
|
|
|
|
Curl->file = (char*)malloc(2);
|
|
|
|
if( Curl->file==NULL ) {
|
|
|
|
printf("Memory allocation failed!\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
strcpy(Curl->file, "/");
|
2001-05-18 16:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Curl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
free_url(URL_t* url) {
|
|
|
|
if(url) return;
|
|
|
|
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-18 16:14:06 +00:00
|
|
|
free(url);
|
|
|
|
}
|