| 1 | =head1 LICENSE | ||
| 2 | |||
| 3 | See the NOTICE file distributed with this work for additional information | ||
| 4 | regarding copyright ownership. | ||
| 5 | |||
| 6 | Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 7 | you may not use this file except in compliance with the License. | ||
| 8 | You may obtain a copy of the License at | ||
| 9 | |||
| 10 | http://www.apache.org/licenses/LICENSE-2.0 | ||
| 11 | |||
| 12 | Unless required by applicable law or agreed to in writing, software | ||
| 13 | distributed under the License is distributed on an "AS IS" BASIS, | ||
| 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 15 | See the License for the specific language governing permissions and | ||
| 16 | limitations under the License. | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | |||
| 21 | =head1 CONTACT | ||
| 22 | |||
| 23 | Please email comments or questions to the public Ensembl | ||
| 24 | developers list at <http://lists.ensembl.org/mailman/listinfo/dev>. | ||
| 25 | |||
| 26 | Questions may also be sent to the Ensembl help desk at | ||
| 27 | <http://www.ensembl.org/Help/Contact>. | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | =head1 NAME | ||
| 32 | |||
| 33 | Bio::EnsEMBL::Utils::URI | ||
| 34 | |||
| 35 | =head1 SYNOPSIS | ||
| 36 | |||
| 37 | use Bio::EnsEMBL::Utils::URI qw/parse_uri is_uri/; | ||
| 38 | # or use Bio::EnsEMBL::Utils::URI qw/:all/; # to bring everything in | ||
| 39 | |||
| 40 | my $db_uri = parse_uri('mysql://user@host:3157/db'); | ||
| 41 | my $http_uri = parse_uri('http://www.google.co.uk:80/search?q=t'); | ||
| 42 | |||
| 43 | is_uri('mysql://user@host'); # returns 1 | ||
| 44 | is_uri('file:///my/path'); # returns 1 | ||
| 45 | is_uri('/my/path'); # returns 0 | ||
| 46 | |||
| 47 | =head1 DESCRIPTION | ||
| 48 | |||
| 49 | This object is a generic URI parser which is primarily used in the | ||
| 50 | parsing of database URIs into a more managable data structure. We also provide | ||
| 51 | the resulting URI object | ||
| 52 | |||
| 53 | =head1 DEPENDENCIES | ||
| 54 | |||
| 55 | L<URI::Escape> is an optional dependency but if available the code will attempt | ||
| 56 | to perform URI encoding/decoding on parameters. If you do not want this | ||
| 57 | functionality then modify the global C<$Bio::EnsEMBL::Utils::URI::URI_ESCAPE> | ||
| 58 | to false; | ||
| 59 | |||
| 60 | =head1 METHODS | ||
| 61 | |||
| 62 | =cut | ||
| 63 | |||
| 64 | package Bio::EnsEMBL::Utils::URI; | ||
| 65 | $Bio::EnsEMBL::Utils::URI::VERSION = '114.0.0_50'; # TRIAL | ||
| 66 | $Bio::EnsEMBL::Utils::URI::VERSION = '114.0.050'; | ||
| 67 | use strict; | ||
| 68 | use warnings; | ||
| 69 | |||
| 70 | use Scalar::Util qw/looks_like_number/; | ||
| 71 | use Bio::EnsEMBL::Utils::Exception qw(throw); | ||
| 72 | use File::Spec; | ||
| 73 | |||
| 74 | our $URI_ESCAPE; | ||
| 75 | $URI_ESCAPE = 0; | ||
| 76 | eval { | ||
| 77 | require URI::Escape; | ||
| 78 | URI::Escape->import(); | ||
| 79 | $URI_ESCAPE = 1; | ||
| 80 | }; | ||
| 81 | |||
| 82 | use base qw/Exporter/; | ||
| 83 | our @EXPORT_OK; | ||
| 84 | our %EXPORT_TAGS; | ||
| 85 | @EXPORT_OK = qw/parse_uri is_uri/; | ||
| 86 | %EXPORT_TAGS = ( all => [@EXPORT_OK] ); | ||
| 87 | |||
| 88 | ####URI Parsing | ||
| 89 | |||
| 90 | =head2 is_uri | ||
| 91 | |||
| 92 | Arg[1] : Scalar; URI to parse | ||
| 93 | Example : is_uri('mysql://user:pass@host:415/db'); | ||
| 94 | Description : Looks for the existence of a URI scheme to decide if this | ||
| 95 | is a classical URI. Whilst non-scheme based URIs can still be | ||
| 96 | interprited it is useful to use when you need to know if | ||
| 97 | you are going to work with a URI or not | ||
| 98 | Returntype : Boolean | ||
| 99 | Caller : General | ||
| 100 | Status : Beta | ||
| 101 | |||
| 102 | =cut | ||
| 103 | |||
| 104 | sub is_uri { | ||
| 105 | my ($uri) = @_; | ||
| 106 | return 0 if ! $uri; | ||
| 107 | my $SCHEME = qr{ ([^:]*) :// }xms; | ||
| 108 | return ($uri =~ $SCHEME) ? 1 : 0; | ||
| 109 | } | ||
| 110 | |||
| 111 | =head2 parse_uri | ||
| 112 | |||
| 113 | Arg[1] : Scalar; URI to parse | ||
| 114 | Example : my $uri = parse_uri('mysql://user:pass@host:415/db'); | ||
| 115 | Description : A URL parser which attempts to convert many different types | ||
| 116 | of URL into a common data structure. | ||
| 117 | Returntype : Bio::EnsEMBL::Utils::URI | ||
| 118 | Caller : General | ||
| 119 | Status : Beta | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | sub parse_uri { | ||
| 124 | my ($url) = @_; | ||
| 125 | |||
| 126 | my $SCHEME = qr{ ([^:]*) :// }xms; | ||
| 127 | my $USER = qr{ ([^/:\@]+)? :? ([^/\@]+)? \@ }xms; | ||
| 128 | my $HOST = qr{ ([^/:]+)? :? ([^/]+)? }xms; | ||
| 129 | my $DB = qr{ / ([^/?]+)? /? ([^/?]+)? }xms; | ||
| 130 | my $PARAMS = qr{ \? (.+)}xms; | ||
| 131 | |||
| 132 | my $p; | ||
| 133 | |||
| 134 | if($url =~ qr{ $SCHEME ([^?]+) (?:$PARAMS)? }xms) { | ||
| 135 | my $scheme = $1; | ||
| 136 | $scheme = ($URI_ESCAPE) ? uri_unescape($scheme) : $scheme; | ||
| 137 | $p = Bio::EnsEMBL::Utils::URI->new($scheme); | ||
| 138 | my ($locator, $params) = ($2, $3); | ||
| 139 | |||
| 140 | if($scheme eq 'file') { | ||
| 141 | $p->path($locator); | ||
| 142 | } | ||
| 143 | elsif($scheme eq 'sqlite') { | ||
| 144 | $p->path($locator); | ||
| 145 | } | ||
| 146 | else { | ||
| 147 | if($locator =~ s/^$USER//) { | ||
| 148 | $p->user($1); | ||
| 149 | $p->pass($2); | ||
| 150 | } | ||
| 151 | if($locator =~ s/^$HOST//) { | ||
| 152 | $p->host(($URI_ESCAPE) ? uri_unescape($1) : $1); | ||
| 153 | $p->port(($URI_ESCAPE) ? uri_unescape($2) : $2); | ||
| 154 | } | ||
| 155 | |||
| 156 | if($p->is_db_scheme() || $scheme eq q{}) { | ||
| 157 | if($locator =~ $DB) { | ||
| 158 | $p->db_params()->{dbname} = ($URI_ESCAPE) ? uri_unescape($1) : $1; | ||
| 159 | $p->db_params()->{table} = ($URI_ESCAPE) ? uri_unescape($2) : $2; | ||
| 160 | } | ||
| 161 | } | ||
| 162 | else { | ||
| 163 | $p->path($locator); | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | if(defined $params) { | ||
| 168 | my @kv_pairs = split(/;|&/, $params); | ||
| 169 | foreach my $kv_string (@kv_pairs) { | ||
| 170 | my ($key, $value) = map { ($URI_ESCAPE) ? uri_unescape($_) : $_ } split(/=/, $kv_string); | ||
| 171 | $p->add_param($key, $value); | ||
| 172 | } | ||
| 173 | } | ||
| 174 | } | ||
| 175 | |||
| 176 | return $p; | ||
| 177 | } | ||
| 178 | |||
| 179 | ####URI Object | ||
| 180 | |||
| 181 | =pod | ||
| 182 | |||
| 183 | =head2 new() | ||
| 184 | |||
| 185 | Arg[1] : String; scheme the URI will confrom to | ||
| 186 | Description : New object call | ||
| 187 | Returntype : Bio::EnsEMBL::Utils::URIParser::URI | ||
| 188 | Exceptions : Thrown if scheme is undefined. | ||
| 189 | Status : Stable | ||
| 190 | |||
| 191 | =cut | ||
| 192 | |||
| 193 | sub new { | ||
| 194 | my ($class, $scheme) = @_; | ||
| 195 | $class = ref($class) || $class; | ||
| 196 | throw "Scheme cannot be undefined. Empty string is allowed" if ! defined $scheme; | ||
| 197 | |||
| 198 | my $self = bless ({ | ||
| 199 | params => {}, | ||
| 200 | param_keys => [], | ||
| 201 | db_params => {}, | ||
| 202 | scheme => $scheme, | ||
| 203 | }, $class); | ||
| 204 | |||
| 205 | return $self; | ||
| 206 | } | ||
| 207 | |||
| 208 | =head2 db_schemes() | ||
| 209 | |||
| 210 | Description: Returns a hash of scheme names known to be databases | ||
| 211 | Returntype : HashRef | ||
| 212 | Exceptions : None | ||
| 213 | Status : Stable | ||
| 214 | |||
| 215 | =cut | ||
| 216 | |||
| 217 | sub db_schemes { | ||
| 218 | my ($self) = @_; | ||
| 219 | return {map { $_ => 1 } qw/mysql ODBC sqlite Oracle Sybase/}; | ||
| 220 | } | ||
| 221 | |||
| 222 | |||
| 223 | =head2 is_db_scheme() | ||
| 224 | |||
| 225 | Description: Returns true if the code believes the scheme to be a Database | ||
| 226 | Returntype : Boolean | ||
| 227 | Exceptions : None | ||
| 228 | Status : Stable | ||
| 229 | |||
| 230 | =cut | ||
| 231 | |||
| 232 | sub is_db_scheme { | ||
| 233 | my ($self) = @_; | ||
| 234 | return ( exists $self->db_schemes()->{$self->scheme()} ) ? 1 : 0; | ||
| 235 | } | ||
| 236 | |||
| 237 | =head2 scheme() | ||
| 238 | |||
| 239 | Description : Getter for the scheme attribute | ||
| 240 | Returntype : String | ||
| 241 | Exceptions : None | ||
| 242 | Status : Stable | ||
| 243 | |||
| 244 | =cut | ||
| 245 | |||
| 246 | sub scheme { | ||
| 247 | my ($self) = @_; | ||
| 248 | return $self->{scheme}; | ||
| 249 | } | ||
| 250 | |||
| 251 | =head2 path() | ||
| 252 | |||
| 253 | Arg[1] : Setter argument | ||
| 254 | Description : Getter/setter for the path attribute | ||
| 255 | Returntype : String | ||
| 256 | Exceptions : None | ||
| 257 | Status : Stable | ||
| 258 | |||
| 259 | =cut | ||
| 260 | |||
| 261 | sub path { | ||
| 262 | my ($self, $path) = @_; | ||
| 263 | $self->{path} = $path if defined $path; | ||
| 264 | return $self->{path}; | ||
| 265 | } | ||
| 266 | |||
| 267 | =head2 user() | ||
| 268 | |||
| 269 | Arg[1] : Setter argument | ||
| 270 | Description : Getter/setter for the user attribute | ||
| 271 | Returntype : String | ||
| 272 | Exceptions : None | ||
| 273 | Status : Stable | ||
| 274 | |||
| 275 | =cut | ||
| 276 | |||
| 277 | sub user { | ||
| 278 | my ($self, $user) = @_; | ||
| 279 | $self->{user} = $user if defined $user; | ||
| 280 | return $self->{user}; | ||
| 281 | } | ||
| 282 | |||
| 283 | =head2 pass() | ||
| 284 | |||
| 285 | Arg[1] : Setter argument | ||
| 286 | Description : Getter/setter for the password attribute | ||
| 287 | Returntype : String | ||
| 288 | Exceptions : None | ||
| 289 | Status : Stable | ||
| 290 | |||
| 291 | =cut | ||
| 292 | |||
| 293 | sub pass { | ||
| 294 | my ($self, $pass) = @_; | ||
| 295 | $self->{pass} = $pass if defined $pass; | ||
| 296 | return $self->{pass}; | ||
| 297 | } | ||
| 298 | |||
| 299 | =head2 host() | ||
| 300 | |||
| 301 | Arg[1] : Setter argument | ||
| 302 | Description : Getter/setter for the host attribute | ||
| 303 | Returntype : String | ||
| 304 | Exceptions : None | ||
| 305 | Status : Stable | ||
| 306 | |||
| 307 | =cut | ||
| 308 | |||
| 309 | sub host { | ||
| 310 | my ($self, $host) = @_; | ||
| 311 | $self->{host} = $host if defined $host; | ||
| 312 | return $self->{host}; | ||
| 313 | } | ||
| 314 | |||
| 315 | =head2 port() | ||
| 316 | |||
| 317 | Arg[1] : Setter argument | ||
| 318 | Description : Getter/setter for the port attribute | ||
| 319 | Returntype : Integer | ||
| 320 | Exceptions : If port is not a number, less than 1 or not a whole integer | ||
| 321 | Status : Stable | ||
| 322 | |||
| 323 | =cut | ||
| 324 | |||
| 325 | sub port { | ||
| 326 | my ($self, $port) = @_; | ||
| 327 | if(defined $port) { | ||
| 328 | if(! looks_like_number($port) || $port < 1 || int($port) != $port) { | ||
| 329 | throw "Port $port is not a number, less than 1 or not a whole integer"; | ||
| 330 | } | ||
| 331 | $self->{port} = $port if defined $port; | ||
| 332 | } | ||
| 333 | return $self->{port}; | ||
| 334 | } | ||
| 335 | |||
| 336 | =head2 param_keys() | ||
| 337 | |||
| 338 | Description : Getter for the paramater map keys in the order they were first | ||
| 339 | seen. Keys should only appear once in this array | ||
| 340 | Returntype : ArrayRef | ||
| 341 | Exceptions : None | ||
| 342 | Status : Stable | ||
| 343 | |||
| 344 | =cut | ||
| 345 | |||
| 346 | sub param_keys { | ||
| 347 | my ($self) = @_; | ||
| 348 | return [@{$self->{param_keys}}]; | ||
| 349 | } | ||
| 350 | |||
| 351 | =head2 param_exists_ci() | ||
| 352 | |||
| 353 | Arg[1] : String; Key | ||
| 354 | Description : Performs a case-insensitive search for the given key | ||
| 355 | Returntype : Boolean; returns true if your given key was seen | ||
| 356 | Exceptions : None | ||
| 357 | Status : Stable | ||
| 358 | |||
| 359 | =cut | ||
| 360 | |||
| 361 | sub param_exists_ci { | ||
| 362 | my ($self, $key) = @_; | ||
| 363 | my %keys = map { uc($_) => 1 } @{$self->param_keys()}; | ||
| 364 | return ($keys{uc($key)}) ? 1 : 0; | ||
| 365 | } | ||
| 366 | |||
| 367 | =head2 add_param() | ||
| 368 | |||
| 369 | Arg[1] : String; key | ||
| 370 | Arg[1] : Scalar; value | ||
| 371 | Description : Add a key/value to the params map. Multiple inserts of the same | ||
| 372 | key is allowed | ||
| 373 | Returntype : None | ||
| 374 | Exceptions : None | ||
| 375 | Status : Stable | ||
| 376 | |||
| 377 | =cut | ||
| 378 | |||
| 379 | sub add_param { | ||
| 380 | my ($self, $key, $value) = @_; | ||
| 381 | if(!exists $self->{params}->{$key}) { | ||
| 382 | $self->{params}->{$key} = []; | ||
| 383 | push(@{$self->{param_keys}}, $key); | ||
| 384 | } | ||
| 385 | push(@{$self->{params}->{$key}}, $value); | ||
| 386 | return; | ||
| 387 | } | ||
| 388 | |||
| 389 | =head2 get_params() | ||
| 390 | |||
| 391 | Arg[1] : String; key | ||
| 392 | Description : Returns the values which were found to be linked to the given | ||
| 393 | key. Arrays are returned because one key can have many | ||
| 394 | values in a URI | ||
| 395 | Returntype : ArrayRef[Scalar] | ||
| 396 | Exceptions : None | ||
| 397 | Status : Stable | ||
| 398 | |||
| 399 | =cut | ||
| 400 | |||
| 401 | sub get_params { | ||
| 402 | my ($self, $key) = @_; | ||
| 403 | return [] if ! exists $self->{params}->{$key}; | ||
| 404 | return [@{$self->{params}->{$key}}]; | ||
| 405 | } | ||
| 406 | |||
| 407 | =head2 db_params() | ||
| 408 | |||
| 409 | Description : Storage of parameters used only for database URIs since | ||
| 410 | they require | ||
| 411 | Returntype : HashRef; Database name is keyed under C<dbname> and the | ||
| 412 | table is keyed under C<table> | ||
| 413 | Exceptions : None | ||
| 414 | Status : Stable | ||
| 415 | |||
| 416 | =cut | ||
| 417 | |||
| 418 | sub db_params { | ||
| 419 | my ($self) = @_; | ||
| 420 | return $self->{db_params}; | ||
| 421 | } | ||
| 422 | |||
| 423 | =head2 generate_dbsql_params() | ||
| 424 | |||
| 425 | Arg[1] : boolean $no_table alows you to avoid pushing -TABLE as an | ||
| 426 | output value | ||
| 427 | Description : Generates a Hash of Ensembl compatible parameters to be used | ||
| 428 | to construct a DB object. We combine those parameters | ||
| 429 | which are deemed to be part of the C<db_params()> method | ||
| 430 | under C<-DBNAME> and C<-TABLE>. We also search for a number | ||
| 431 | of optional parameters which are lowercased equivalents | ||
| 432 | of the construction parameters available from a | ||
| 433 | L<Bio::EnsEMBL::DBSQL::DBAdaptor>, | ||
| 434 | L<Bio::EnsEMBL::DBSQL::DBConnection> as well as C<verbose> | ||
| 435 | being supported. | ||
| 436 | |||
| 437 | We also convert the scheme type into the driver attribute | ||
| 438 | |||
| 439 | Returntype : Hash (not a reference). Output can be put into a C<DBConnection> | ||
| 440 | constructor. | ||
| 441 | Exceptions : None | ||
| 442 | Status : Stable | ||
| 443 | |||
| 444 | =cut | ||
| 445 | |||
| 446 | sub generate_dbsql_params { | ||
| 447 | my ($self, $no_table) = @_; | ||
| 448 | my %db_params; | ||
| 449 | |||
| 450 | $db_params{-DRIVER} = $self->scheme(); | ||
| 451 | $db_params{-HOST} = $self->host() if $self->host(); | ||
| 452 | $db_params{-PORT} = $self->port() if $self->port(); | ||
| 453 | $db_params{-USER} = $self->user() if $self->user(); | ||
| 454 | $db_params{-PASS} = $self->pass() if $self->pass(); | ||
| 455 | |||
| 456 | my $dbname; | ||
| 457 | my $table; | ||
| 458 | if($self->scheme() eq 'sqlite') { | ||
| 459 | ($dbname, $table) = $self->_decode_sqlite(); | ||
| 460 | } | ||
| 461 | else { | ||
| 462 | $dbname = $self->db_params()->{dbname}; | ||
| 463 | $table = $self->db_params()->{table}; | ||
| 464 | } | ||
| 465 | |||
| 466 | $db_params{-DBNAME} = $dbname if $dbname; | ||
| 467 | $db_params{-TABLE} = $table if ! $no_table && $table; | ||
| 468 | |||
| 469 | foreach my $boolean_param (qw/disconnect_when_inactive reconnect_when_connection_lost is_multispecies no_cache verbose/) { | ||
| 470 | if($self->param_exists_ci($boolean_param)) { | ||
| 471 | $db_params{q{-}.uc($boolean_param)} = 1; | ||
| 472 | } | ||
| 473 | } | ||
| 474 | foreach my $value_param (qw/species group species_id wait_timeout/) { | ||
| 475 | if($self->param_exists_ci($value_param)) { | ||
| 476 | $db_params{q{-}.uc($value_param)} = $self->get_params($value_param)->[0]; | ||
| 477 | } | ||
| 478 | } | ||
| 479 | |||
| 480 | return %db_params; | ||
| 481 | } | ||
| 482 | |||
| 483 | =head2 _decode_sqlite | ||
| 484 | |||
| 485 | Description : Performs path gymnastics to decode into a number of possible | ||
| 486 | options. The issue with SQLite is that the normal URI scheme | ||
| 487 | looks like sqlite:///my/path.sqlite/table but how do we know | ||
| 488 | that the DB name is C</my/path.sqlite> and the table is | ||
| 489 | C<table>? | ||
| 490 | |||
| 491 | The code takes a path, looks for the full path & if it cannot | ||
| 492 | be found looks for the file a directory back. In the above | ||
| 493 | example it would have looked for C</my/path.sqlite/table>, | ||
| 494 | found it to be non-existant, looked for C</my/path.sqlite> | ||
| 495 | and found it. | ||
| 496 | |||
| 497 | If the path splitting procdure resulted in just 1 file after | ||
| 498 | the first existence check e.g. C<sqlite://db.sqlite> it assumes | ||
| 499 | that should be the name. If no file can be found we default to | ||
| 500 | the full length path. | ||
| 501 | Caller : internal | ||
| 502 | |||
| 503 | =cut | ||
| 504 | |||
| 505 | sub _decode_sqlite { | ||
| 506 | my ($self) = @_; | ||
| 507 | my $dbname; | ||
| 508 | my $table; | ||
| 509 | my $path = $self->path(); | ||
| 510 | if(-f $path) { | ||
| 511 | $dbname = $path; | ||
| 512 | } | ||
| 513 | else { | ||
| 514 | my ($volume, $directories, $file) = File::Spec->splitpath($path); | ||
| 515 | my @splitdirs = File::Spec->splitdir($directories); | ||
| 516 | if(@splitdirs == 1) { | ||
| 517 | $dbname = $path; | ||
| 518 | } | ||
| 519 | else { | ||
| 520 | my $new_file = pop(@splitdirs); | ||
| 521 | $new_file ||= q{}; | ||
| 522 | my $new_path = File::Spec->catpath($volume, File::Spec->catdir(@splitdirs), $new_file); | ||
| 523 | if($new_path ne File::Spec->rootdir() && -f $new_path) { | ||
| 524 | $dbname = $new_path; | ||
| 525 | $table = $file; | ||
| 526 | } | ||
| 527 | else { | ||
| 528 | $dbname = $path; | ||
| 529 | } | ||
| 530 | } | ||
| 531 | } | ||
| 532 | |||
| 533 | $self->db_params()->{dbname} = $dbname if $dbname; | ||
| 534 | $self->db_params()->{table} = $table if $table; | ||
| 535 | |||
| 536 | return ($dbname, $table); | ||
| 537 | } | ||
| 538 | |||
| 539 | =head2 generate_uri() | ||
| 540 | |||
| 541 | Description : Generates a URI string from the paramaters in this object | ||
| 542 | Returntype : String | ||
| 543 | Exceptions : None | ||
| 544 | Status : Stable | ||
| 545 | |||
| 546 | =cut | ||
| 547 | |||
| 548 | sub generate_uri { | ||
| 549 | my ($self) = @_; | ||
| 550 | my $scheme = sprintf('%s://', ($URI_ESCAPE) ? uri_escape($self->scheme()) : $self->scheme()); | ||
| 551 | my $user_credentials = q{}; | ||
| 552 | my $host_credentials = q{}; | ||
| 553 | my $location = q{}; | ||
| 554 | |||
| 555 | if($self->user() || $self->pass()) { | ||
| 556 | my $user = $self->user(); | ||
| 557 | my $pass = $self->pass(); | ||
| 558 | if($URI_ESCAPE) { | ||
| 559 | $user = uri_escape($user) if $user; | ||
| 560 | $pass = uri_escape($pass) if $pass; | ||
| 561 | } | ||
| 562 | $user_credentials = sprintf('%s%s@', | ||
| 563 | ( $user ? $user : q{} ), | ||
| 564 | ( $pass ? q{:}.$pass : q{} ) | ||
| 565 | ); | ||
| 566 | } | ||
| 567 | |||
| 568 | if($self->host() || $self->port()) { | ||
| 569 | my $host = $self->host(); | ||
| 570 | my $port = $self->port(); | ||
| 571 | if($URI_ESCAPE) { | ||
| 572 | $host = uri_escape($host) if $host; | ||
| 573 | $port = uri_escape($port) if $port; | ||
| 574 | } | ||
| 575 | $host_credentials = sprintf('%s%s', | ||
| 576 | ( $host ? $host : q{} ), | ||
| 577 | ( $port ? q{:}.$port : q{} ) | ||
| 578 | ); | ||
| 579 | } | ||
| 580 | |||
| 581 | if($self->is_db_scheme() || $self->scheme() eq '') { | ||
| 582 | if($self->scheme() eq 'sqlite') { | ||
| 583 | if(! $self->path()) { | ||
| 584 | my $tmp_loc = $self->db_params()->{dbname}; | ||
| 585 | throw "There is no dbname available" unless $tmp_loc; | ||
| 586 | $tmp_loc .= q{/}.$self->db_params()->{table} if $self->db_params()->{table}; | ||
| 587 | $self->path($tmp_loc); | ||
| 588 | } | ||
| 589 | $location = $self->path(); | ||
| 590 | } | ||
| 591 | else { | ||
| 592 | my $dbname = $self->db_params()->{dbname}; | ||
| 593 | my $table = $self->db_params()->{table}; | ||
| 594 | if($dbname || $table) { | ||
| 595 | if($URI_ESCAPE) { | ||
| 596 | $dbname = uri_escape($dbname) if $dbname; | ||
| 597 | $table = uri_escape($table) if $table; | ||
| 598 | } | ||
| 599 | $location = sprintf('/%s%s', | ||
| 600 | ($dbname ? $dbname : q{}), | ||
| 601 | ($table ? q{/}.$table : q{}) | ||
| 602 | ); | ||
| 603 | } | ||
| 604 | } | ||
| 605 | } | ||
| 606 | else { | ||
| 607 | $location = $self->path() if $self->path(); | ||
| 608 | } | ||
| 609 | |||
| 610 | my $param_string = q{}; | ||
| 611 | if(@{$self->param_keys()}) { | ||
| 612 | $param_string = q{?}; | ||
| 613 | my @params; | ||
| 614 | foreach my $key (@{$self->param_keys}) { | ||
| 615 | my $values_array = $self->get_params($key); | ||
| 616 | foreach my $value (@{$values_array}) { | ||
| 617 | my $encoded_key = ($URI_ESCAPE) ? uri_escape($key) : $key; | ||
| 618 | my $encoded_value = ($URI_ESCAPE) ? uri_escape($value) : $value; | ||
| 619 | push(@params, ($encoded_value) ? "$encoded_key=$encoded_value" : $encoded_key); | ||
| 620 | } | ||
| 621 | } | ||
| 622 | $param_string .= join(q{;}, @params); | ||
| 623 | } | ||
| 624 | |||
| 625 | return join(q{}, $scheme, $user_credentials, $host_credentials, $location, $param_string); | ||
| 626 | } | ||
| 627 | |||
| 628 | 1; |