summaryrefslogtreecommitdiff
path: root/CIAO/DAnCE/NodeApplicationManager/URL_Parser.cpp
blob: 5d211603614bf4c47002f6141c2847e58bcbaba0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// $Id$

#include "URL_Parser.h"

#include "ace/ACE.h"
#include "ace/OS_NS_string.h"

bool
URL_Parser::parse_args (int argc, ACE_TCHAR *argv[])
{
  ACE_Get_Opt get_opt (argc, argv, ACE_TEXT ("rwu:h:p:f:d"));

  bool success = true;
  int c;

  while ((c = get_opt ()) != -1)
    {
      switch (c)
        {
          case 'd':
            this->debug_ = 1;
            break;
          case 'u':
            success = parseURL (get_opt.opt_arg ());
            break;
          // Usage fallthrough.
          default:
            success = false;
            break;
        }
    }

  if (this->hostname_ == 0 || this->filename_ == 0)
    {
      success = false;
    }

  return success;
}

URL_Parser::URL_Parser (void)
  : hostname_ (ACE::strnew ("127.0.0.1")),
    port_ (ACE_DEFAULT_HTTP_SERVER_PORT),
    filename_ (0),
    debug_ (0)
{
}

bool URL_Parser::parseURL (char* url)
{
  char* ptr = ACE_OS::strstr (url, "http://");
  bool success = true;

  if (0 != ptr)
    {
      url += ACE_OS::strlen ("http://");
    }

  if (url[0] == '/')
    {
      this->filename_ = ACE_OS::strdup (url);
    }
  else
    {
      ptr = ACE_OS::strstr (url, ":");

      if (0 != ptr)
        {
          this->port_ = ACE_OS::atoi (ptr + 1);
        }
      else
        {
          ptr = ACE_OS::strstr (url, "/");
        }

      if (0 == ptr)
        {
          success = false;
        }
      else
        {
          size_t host_len = ptr - url;
          ACE::strdelete (this->hostname_);
          ACE_NEW_RETURN (this->hostname_, char [host_len + 1], false);
          ACE_OS::strncpy (this->hostname_, url, host_len);
          this->hostname_ [host_len] = '\0';
          ptr = ACE_OS::strstr (ptr, "/");

          if (0 != ptr)
            {
              this->filename_ = ACE_OS::strdup (ptr);
            }
          else
            {
              success = false;
            }
        }
    }

  return success;
}


void URL_Parser::Error (void)
{
  ACE_DEBUG ((LM_DEBUG,
              "./http_client -u http://hostname:port/filename [-d]\n"));
}


URL_Parser::~URL_Parser (void)
{
  delete [] this->hostname_;
  ACE_OS::free (this->filename_);
}