1/* Copyright (C) 2010 Sergei Golubchik and Monty Program Ab
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA */
15
16#include "feedback.h"
17
18namespace feedback {
19
20Url* http_create(const char *url, size_t url_length);
21
22/**
23 creates an Url object out of an url, if possible.
24
25 This is done by invoking corresponding creator functions
26 of the derived classes, until the first not NULL result.
27*/
28Url* Url::create(const char *url, size_t url_length)
29{
30 url= my_strndup(url, url_length, MYF(MY_WME));
31
32 if (!url)
33 return NULL;
34
35 Url *self= http_create(url, url_length);
36
37 /*
38 here we can add
39
40 if (!self) self= smtp_create(url, url_length);
41 if (!self) self= tftp_create(url, url_length);
42 etc
43 */
44
45 if (!self)
46 my_free(const_cast<char*>(url));
47
48 return self;
49}
50
51int Url::parse_proxy_server(const char *proxy_server, size_t proxy_length,
52 LEX_STRING *host, LEX_STRING *port)
53{
54 const char *s;
55
56 host->length= 0;
57 if (proxy_server == NULL)
58 return 0;
59
60 for (; proxy_length > 0 && my_isspace(system_charset_info, *proxy_server);
61 proxy_server++, proxy_length--) /* no-op */;
62
63 if (proxy_length == 0)
64 return 0;
65
66 for (s=proxy_server; *s && *s != ':'; s++) /* no-op */;
67
68 host->str= const_cast<char*>(proxy_server);
69 if ((host->length= s-proxy_server) == 0)
70 return 0;
71
72 port->length= 0;
73
74 if (*s == ':')
75 {
76 s++;
77 port->str= const_cast<char*>(s);
78 while (*s >= '0' && *s <= '9')
79 {
80 s++;
81 port->length++;
82 }
83 }
84
85 if (port->length == 0)
86 {
87 port->str= const_cast<char*>("80");
88 port->length= 2;
89 }
90
91 host->str= my_strndup(host->str, host->length, MYF(MY_WME));
92 port->str= my_strndup(port->str, port->length, MYF(MY_WME));
93 return 0;
94}
95
96} // namespace feedback
97