octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

urlparts and fullurl


From: Bill Denney
Subject: urlparts and fullurl
Date: Tue, 17 Oct 2006 22:05:45 -0400
User-agent: Thunderbird 1.5.0.7 (Windows/20060909)

Here are a couple of files that parse out all possible parts of a url and then put them back together. I think that this can be useful to help add the ability to enter a url for use in decompress.m and untar, gunzip, unzip, etc.

I haven't seen these files in matlab, but they seem as though they could be useful for several things. What I'm thinking of right now is using it for part of the decompress.m function.

Bill

scripts/Changelog:
2006-10-17  Bill Denney  <address@hidden>

* miscellaneous/urlparts.m, miscellaneous/fullurl.m: parse the parts out of a url and put those parts back together.
## Copyright (C) 2003 Bill Denney <address@hidden>
##
## This file is part of Octave.
##
## Octave is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2, or (at your option)
## any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, write to the Free
## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
## 02110-1301, USA.

## -*- texinfo -*-
## @deftypefn {Function File} address@hidden, @var{username}, @var{password}, 
@var{server}, @var{port}, @var{pathstr}, @var{file}, @var{querystring}] =} 
urlparts (@var{url})
## @deftypefnx {Function File} address@hidden =} urlparts (@var{url}, 
@var{asstruct})
## Return the @var{protocol}, @var{username}, @var{password},
## @var{server}, @var{port}, @var{pathstr}, @var{file}, and
## @var{querystring} of a @var{url}.  If @var{asstruct} is true, then
## the return value is a structure with the above mentioned fields.
## @seealso{fileparts,fullurl}
## @end deftypefn

function varargout = urlparts (url, varargin)

  if (nargin < 1) || (nargin > 2)
    print_usage ();
  elseif (~ ischar (url))
    error ("urlparts: expecting url to be a string");
  endif

  ## set all outputs to empty so that they don't have to be set empty
  ## throughout the code
  protocol = "";
  username = "";
  password = "";
  server = "";
  port = [];
  pathstr = "";
  name = "";
  ext = "";
  querystring = "";

  tmpurl = url;
  protocolend = index (tmpurl, "://") - 1;
  ## a protocol is required
  if protocolend
    protocol = url(1:protocolend);
    tmpurl = url(protocolend+4:length (url));
  else
    error ("urlparts: no protocol found");
  endif

  serverend = index (tmpurl, "/") - 1;
  ## a server is not required (it could be "file:///")
  if serverend
    server = tmpurl(1:serverend);
    ## the +1 keeps the / for the path
    tmpurl = tmpurl(serverend+1:length (tmpurl));

    ## see if there is a user or password within the server part
    usernameend = index (server, "@") - 1;
    if usernameend
      username = server(1:usernameend);
      server = server(usernameend+2:length (server));

      ## see if there is a password within the user:password combo
      passwordbegin = index (username, ":");
      if passwordbegin
        password = username(passwordbegin+1:length (username));
        username = username(1:passwordbegin-1);
      endif
    endif
  endif

  ## get the port out of the server name it's there
  portbegin = index (server, ":");
  if portbegin
    port = server(portbegin+1:length (server));
    server = server(1:portbegin-1);

    portnum = str2double(port);
    if ~ isnan(portnum)
      port = portnum;
    endif
  endif

  ## get the parts of the file
  qsbegin = index (tmpurl, "?");
  if qsbegin
    querystring = tmpurl(qsbegin+1:length (tmpurl));
    tmpurl = tmpurl(1:qsbegin-1)
  end

  [pathstr, name, ext] = fileparts(tmpurl);
  if isempty(pathstr)
    pathstr = "/";
  endif

  ## return the result how the user wants it
  if isempty (varargin)
    varargout = {protocol, username, password, server, port, ...
                 pathstr, name, ext, querystring};
  else
    varargout{1} = struct("protocol", {protocol}, ...
                          "username", {username}, ...
                          "password", {password}, ...
                          "server", {server}, ...
                          "port", {port}, ...
                          "pathstr", {pathstr}, ...
                          "name", {name}, ...
                          "ext", {ext}, ...
                          "querystring", {querystring});
  endif

  
endfunction
## Copyright (C) 2003 Bill Denney <address@hidden>
##
## This file is part of Octave.
##
## Octave is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2, or (at your option)
## any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, write to the Free
## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
## 02110-1301, USA.

## -*- texinfo -*-
## @deftypefn {Function File} address@hidden =} fullurl(@var{protocol}, 
@var{username}, @var{password}, @var{server}, @var{port}, @var{pathstr}, 
@var{file}, @var{querystring})
## @deftypefnx {Function File} address@hidden =} fullurl(@var{urlstruct})
## Return the @var{url} built from its individual parts.
## @seealso{fullfile,urlparts,fileparts}
## @end deftypefn

function url = fullurl(varargin)

  if (nargin > 9) || ...
        ((nargin < 4) && ~ (isstruct (varargin{1}) && (nargin == 1)))
    print_usage ();
  endif

  protocol = "";
  username = "";
  password = "";
  server = "";
  port = "";
  pathstr = "";
  name = "";
  ext = "";
  querystring = "";


  if isstruct (varargin{1})
    protocol = varargin{1}.protocol;
    if isfield (varargin{1}, "username")
      username = varargin{1}.username;
    endif
    if isfield (varargin{1}, "password")
      password = varargin{1}.password;
    endif
    if isfield (varargin{1}, "server")
      server = varargin{1}.server;
    endif
    if isfield (varargin{1}, "port")
      port = varargin{1}.port;
    endif
    if isfield (varargin{1}, "pathstr")
      pathstr = varargin{1}.pathstr;
    endif
    if isfield (varargin{1}, "name")
      name = varargin{1}.name;
    endif
    if isfield (varargin{1}, "ext")
      ext = varargin{1}.ext;
    endif
    if isfield (varargin{1}, "querystring")
      querystring = varargin{1}.querystring;
    endif

  elseif (iscellstr (varargin{[1:4 6:length(varargin)]}) && ...
          ((length (varargin) < 5) || ... 
           (ischar (varargin{5}) || isnumeric (varargin{5}))))
    ## the above says that the port can be either a string or numeric
    protocol = varargin{1};
    username = varargin{2};
    password = varargin{3};
    server = varargin{4};

    if length(varargin) > 4
      port = varargin{5};
    endif
    if length(varargin) > 5
      pathstr = varargin{6};
    endif
    if length(varargin) > 6
      name = varargin{7};
    endif
    if length(varargin) > 7
      ext = varargin{8};
    endif
    if length(varargin) > 8
      querystring = varargin{9};
    endif
  endif

  ## make the port a string if it's a number
  if isnumeric (port)
    port = num2str(port);
  endif

  ## make the path "/" if it's empty
  if isempty (pathstr)
    pathstr = "/";
  endif

  ## reconstruct the url
  url = [protocol "://"];
  if ~ isempty (password)
    if isempty (username)
      error ("fullurl: password cannot be given without a username");
    endif
    username = [username ":" password];
  endif

  if ~ isempty (username)
    url = [url username "@"];
  endif

  url = [url server];

  if ~ isempty (port)
    url = [url ":" port];
  endif

  url = [url pathstr "/" name ext];

  if ~ isempty (querystring)
    url = [url "?" querystring];
  endif
  
endfunction

reply via email to

[Prev in Thread] Current Thread [Next in Thread]