1 | /* Copyright (c) 2001, Stanford University
|
---|
2 | * All rights reserved
|
---|
3 | *
|
---|
4 | * See the file LICENSE.txt for information on redistributing this software.
|
---|
5 | */
|
---|
6 |
|
---|
7 | #include <stdio.h>
|
---|
8 | #include <stdlib.h>
|
---|
9 | #include <ctype.h>
|
---|
10 |
|
---|
11 | #include "cr_string.h"
|
---|
12 | #include "cr_url.h"
|
---|
13 | #include "cr_error.h"
|
---|
14 |
|
---|
15 | static int is_digit_string( const char *s )
|
---|
16 | {
|
---|
17 | if (!isdigit( (int) *s))
|
---|
18 | {
|
---|
19 | return 0;
|
---|
20 | }
|
---|
21 |
|
---|
22 | while (*s && isdigit ( (int) *s))
|
---|
23 | {
|
---|
24 | s++;
|
---|
25 | }
|
---|
26 |
|
---|
27 | return ( *s == 0 );
|
---|
28 | }
|
---|
29 |
|
---|
30 | int crParseURL( const char *url, char *protocol, char *hostname,
|
---|
31 | unsigned short *port, unsigned short default_port )
|
---|
32 | {
|
---|
33 | const char *temp, *temp2;
|
---|
34 |
|
---|
35 | /* pull off the protocol */
|
---|
36 | temp = crStrstr( url, "://" );
|
---|
37 | if ( temp == NULL && protocol != NULL )
|
---|
38 | {
|
---|
39 | crStrcpy( protocol, "tcpip" );
|
---|
40 | temp = url;
|
---|
41 | }
|
---|
42 | else
|
---|
43 | {
|
---|
44 | if (protocol != NULL) {
|
---|
45 | int len = temp - url;
|
---|
46 | crStrncpy( protocol, url, len );
|
---|
47 | protocol[len] = 0;
|
---|
48 | }
|
---|
49 | temp += 3;
|
---|
50 | }
|
---|
51 |
|
---|
52 | /* handle a trailing :<digits> to specify the port */
|
---|
53 |
|
---|
54 | /* there might be a filename here */
|
---|
55 | temp2 = crStrrchr( temp, '/' );
|
---|
56 | if ( temp2 == NULL )
|
---|
57 | {
|
---|
58 | temp2 = crStrrchr( temp, '\\' );
|
---|
59 | }
|
---|
60 | if ( temp2 == NULL )
|
---|
61 | {
|
---|
62 | temp2 = temp;
|
---|
63 | }
|
---|
64 |
|
---|
65 | temp2 = crStrrchr( temp2, ':' );
|
---|
66 | if ( temp2 )
|
---|
67 | {
|
---|
68 | if (hostname != NULL) {
|
---|
69 | int len = temp2 - temp;
|
---|
70 | crStrncpy( hostname, temp, len );
|
---|
71 | hostname[len] = 0;
|
---|
72 | }
|
---|
73 | temp2++;
|
---|
74 | if ( !is_digit_string( temp2 ) )
|
---|
75 | goto bad_url;
|
---|
76 |
|
---|
77 | if (port != NULL)
|
---|
78 | *port = (unsigned short) atoi( temp2 );
|
---|
79 | }
|
---|
80 | else
|
---|
81 | {
|
---|
82 | if (hostname != NULL)
|
---|
83 | crStrcpy( hostname, temp );
|
---|
84 | if (port != NULL)
|
---|
85 | *port = default_port;
|
---|
86 | }
|
---|
87 |
|
---|
88 | return 1;
|
---|
89 |
|
---|
90 | bad_url:
|
---|
91 | crWarning( "URL: expected <protocol>://"
|
---|
92 | "<destination>[:<port>], what is \"%s\"?", url );
|
---|
93 | return 0;
|
---|
94 | }
|
---|