comparison url.c @ 833:b8cecdc0c67f

Starting implementation of ASF network streaming.
author bertrand
date Fri, 18 May 2001 16:14:06 +0000
parents
children 9141234715a2
comparison
equal deleted inserted replaced
832:369697a87773 833:b8cecdc0c67f
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4
5 #include "url.h"
6
7 URL_t*
8 set_url(char* url) {
9 int pos1, pos2;
10 URL_t* Curl;
11 char *ptr1, *ptr2;
12
13 // Create the URL container
14 Curl = (URL_t*)malloc(sizeof(URL_t));
15 if( Curl==NULL ) {
16 printf("Memory allocation failed!\n");
17 exit(1);
18 }
19 // Copy the url in the URL container
20 Curl->url = (char*)malloc(strlen(url)+1);
21 if( Curl->url==NULL ) {
22 printf("Memory allocation failed!\n");
23 exit(1);
24 }
25 strcpy(Curl->url, url);
26
27 // extract the protocol
28 ptr1 = strstr(url, "://");
29 if( ptr1==NULL ) {
30 printf("Malformed URL!\n");
31 return NULL;
32 }
33 pos1 = ptr1-url;
34 Curl->protocol = (char*)malloc(pos1+1);
35 strncpy(Curl->protocol, url, pos1);
36 Curl->protocol[pos1] = '\0';
37
38 // look if the port is given
39 ptr2 = strstr(ptr1+3, ":");
40 if( ptr2==NULL ) {
41 // No port is given
42 // Look if a path is given
43 ptr2 = strstr(ptr1+3, "/");
44 if( ptr2==NULL ) {
45 // No path/filename
46 // So we have an URL like http://www.hostname.com
47 pos2 = strlen(url);
48 } else {
49 // We have an URL like http://www.hostname.com/file.txt
50 pos2 = ptr2-url;
51 }
52 } else {
53 // We have an URL beginning like http://www.hostname.com:1212
54 // Get the port number
55 Curl->port = atoi(ptr2+1);
56 pos2 = ptr2-url;
57 }
58 // copy the hostname in the URL container
59 Curl->hostname = (char*)malloc(strlen(url)+1);
60 if( Curl->hostname==NULL ) {
61 printf("Memory allocation failed!\n");
62 exit(1);
63 }
64 strncpy(Curl->hostname, ptr1+3, pos2-pos1-3);
65
66 // Look if a path is given
67 ptr2 = strstr(ptr1+3, "/");
68 if( ptr2!=NULL ) {
69 // A path/filename is given
70 // check if it's not a trailing '/'
71 if( strlen(ptr2)>1 ) {
72 // copy the path/filename in the URL container
73 Curl->path = (char*)malloc(strlen(ptr2));
74 if( Curl->path==NULL ) {
75 printf("Memory allocation failed!\n");
76 exit(1);
77 }
78 strcpy(Curl->path, ptr2+1);
79 }
80 }
81
82 return Curl;
83 }
84
85 void
86 free_url(URL_t* url) {
87 if(url) return;
88 if(url->url) free(url->url);
89 if(url->protocol) free(url->protocol);
90 if(url->hostname) free(url->hostname);
91 if(url->path) free(url->path);
92 free(url);
93 }