The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
URL Class Reference

#include <url.h>

+ Collaboration diagram for URL:

Public Types

typedef std::map< SWBuf, SWBufParameterMap
 

Public Member Functions

const char * getHostName () const
 
const ParameterMapgetParameters () const
 
const char * getParameterValue (const char *name) const
 
const char * getPath () const
 
const char * getProtocol () const
 
 URL (const char *url)
 

Static Public Member Functions

static const SWBuf decode (const char *encodedText)
 
static const SWBuf encode (const char *urlText)
 

Private Member Functions

void parse ()
 

Private Attributes

SWBuf hostname
 
ParameterMap parameterMap
 
SWBuf path
 
SWBuf protocol
 
SWBuf url
 

Detailed Description

URL provides URL parsing The URL class provides an interface to work on the data of an URL like "http://www.crosswire.org/index.jsp?page=test&amp;user=nobody"

Definition at line 34 of file url.h.

Member Typedef Documentation

typedef std::map<SWBuf, SWBuf> URL::ParameterMap

Definition at line 36 of file url.h.

Constructor & Destructor Documentation

URL::URL ( const char *  url)

Constructor.

Parameters
urlThe url string which should be parsed into protocol, hostname, path and paramters

Constructors/Destructors

Definition at line 61 of file url.cpp.

62  : url(""),
63  protocol(""),
64  hostname(""),
65  path("")
66 {
67  if (url && strlen(url)) {
68  this->url = url;
69  parse();
70  }
71 }
SWBuf hostname
Definition: url.h:87
SWBuf url
Definition: url.h:85
SWBuf protocol
Definition: url.h:86
void parse()
Definition: url.cpp:117
SWBuf path
Definition: url.h:88

Member Function Documentation

const SWBuf URL::decode ( const char *  encodedText)
static

Definition at line 247 of file url.cpp.

247  {
248  /*static*/ SWBuf text;
249  text = encoded;
250 
251  SWBuf decoded;
252  const long length = text.length();
253  int i = 0;
254 
255  while (i < length) {
256  char a = text[i];
257 
258  if ( a == '+' ) { //handle special cases
259  decoded.append(' ');
260  }
261  else if ( (a == '%') && (i+2 < length)) { //decode the %ab hex encoded char
262  const char b = toupper( text[i+1] );
263  const char c = toupper( text[i+2] );
264 
265  if (isxdigit(b) && isxdigit(c)) { //valid %ab part
266  unsigned int dec = 16 * ((b >= 'A' && b <= 'F') ? (b - 'A' + 10) : (b - '0')); //dec value of the most left digit (b)
267  dec += (c >= 'A' && c <= 'F') ? (c - 'A' + 10) : (c - '0'); //dec value of the right digit (c)
268 
269  decoded.append((char)dec); //append the decoded char
270 
271  i += 2; //we jump over the %ab part; we have to leave out three, but the while loop adds one, too
272  }
273  }
274  else { //just append the char
275  decoded.append(a);
276  }
277 
278  i++;
279  }
280 
281  if (decoded.length()) {
282  text = decoded;
283  }
284  return text;
285 }
Definition: swbuf.h:47
unsigned long length() const
Definition: swbuf.h:197
size_t length
Definition: regex.c:7928
SWBuf & append(const char *str, long max=-1)
Definition: swbuf.h:274
const SWBuf URL::encode ( const char *  urlText)
static

Encodes and URL Encodes a string into a valid URL, e.g. changes http://www.crosswire.org/test.jsp?force=1&help=1 into http://www.crosswire.org/test.jsp?force=1&amp;help=1 This function works on the data of the buf parameter.

WARNING: It doesn't check if the URL is encoded already, so http://www.crosswire.org/test.jsp?force=1&amp;help=1 becomes http://www.crosswire.org/test.jsp?force=1&amp;amp;help=1

Definition at line 231 of file url.cpp.

231  {
232  /*static*/ SWBuf url;
233  url = urlText;
234 
235  SWBuf buf;
236  const long length = url.length();
237  for (long i = 0; i < length; i++) { //fill "buf"
238  const char& c = url[i];
239  buf.append( ((m[c].length()) ? m[c] : SWBuf(c)) );
240  }
241 
242  url = buf;
243  return url;
244 }
Definition: swbuf.h:47
unsigned long length() const
Definition: swbuf.h:197
SWBuf url
Definition: url.h:85
size_t length
Definition: regex.c:7928
SWBuf & append(const char *str, long max=-1)
Definition: swbuf.h:274
const char * URL::getHostName ( ) const

Get the hostname

Returns
The hostname, e.g. "www.crosswire.org" for an url like "http://www.crosswire.org/index.jsp?page=help"

Definition at line 79 of file url.cpp.

79  {
80  return hostname.c_str();
81 }
SWBuf hostname
Definition: url.h:87
const char * c_str() const
Definition: swbuf.h:158
const URL::ParameterMap & URL::getParameters ( ) const

All available paramters

Returns
The map which contains the parameters and their values

Definition at line 89 of file url.cpp.

89  {
90  return parameterMap;
91 }
ParameterMap parameterMap
Definition: url.h:89
const char * URL::getParameterValue ( const char *  name) const

Returns the value of an URL parameter. For the URL "http://www.crosswire.org/index.jsp?page=test&amp;user=nobody" the value of the parameter "page" would be "test". If the parameter is not set an empty string is returned.

Parameters
nameThe name of the paramter.
Returns
The value of the given paramter of an empty string if the name could not be found in the list of available paramters

Returns the value of an URL parameter. For the URL "http://www.crosswire.org/index.jsp?page=test&amp;user=nobody" the value of the parameter "page" would be "test". If the parameter is not set an empty string is returned.

Definition at line 98 of file url.cpp.

98  {
99  static SWBuf emptyStr("");
100 
101  ParameterMap::const_iterator it = parameterMap.find(name);
102  static SWBuf retVal;
103 
104  if (it != parameterMap.end())
105  retVal = it->second.c_str();
106  else
107  retVal = emptyStr.c_str();
108 
109  return retVal.c_str();
110 }
Definition: swbuf.h:47
const char * c_str() const
Definition: swbuf.h:158
ParameterMap parameterMap
Definition: url.h:89
const char * URL::getPath ( ) const

Get the path

Returns
The path, e.g. "/index.jsp" for an url like "http://www.crosswire.org/index.jsp?page=help"

Definition at line 84 of file url.cpp.

84  {
85  return path.c_str();
86 }
const char * c_str() const
Definition: swbuf.h:158
SWBuf path
Definition: url.h:88
const char * URL::getProtocol ( ) const

Get the protocol.

Returns
The protocol, e.g. "http" for an url like "http://www.crosswire.org/index.jsp?page=help"

Definition at line 74 of file url.cpp.

74  {
75  return protocol.c_str();
76 }
const char * c_str() const
Definition: swbuf.h:158
SWBuf protocol
Definition: url.h:86
void URL::parse ( )
private

Parse Parse the URL into protocol, hostname, path, page and paramters

Parse the URL. Parse the URL into the protocol, the hostname, the path and the paramters with their values

Definition at line 117 of file url.cpp.

117  {
118  /* format example protocol://hostname/path/path/path.pl?param1=value1&amp;param2=value2
119  * we include the script name in the path, so the path would be /path/path/path.pl in this example
120  * &amp; could also be &
121  */
122 
123  //1. Init
124  const char *urlPtr = url.c_str();
125 
126  protocol = "";
127  hostname = "";
128  path = "";
129  parameterMap.clear();
130 
131  // 2. Get the protocol, which is from the begining to the first ://
132  const char *end = strchr( urlPtr, ':' );
133  if (end) { //protocol was found
134  protocol.append(urlPtr, end-urlPtr);
135  urlPtr = end + 1;
136 
137  //find the end of the protocol separator (e.g. "://")
138  for (; (*urlPtr == ':') || (*urlPtr == '/'); urlPtr++);
139  }
140 
141  //3.Get the hostname part. This is the part from pos up to the first slash
142  bool checkPath = true;
143  bool checkParams = true;
144  bool checkAnchor = true;
145 
146  end = strchr(urlPtr, '/');
147  if (!end) {
148  checkPath = false;
149  end = strchr(urlPtr, '?');
150  }
151  if (!end) {
152  checkParams = false;
153  end = strchr(urlPtr, '#');
154  }
155  if (!end) {
156  checkAnchor = false;
157  end = urlPtr+strlen(urlPtr);
158  }
159 
160  hostname.append(urlPtr, end-urlPtr);
161 
162  urlPtr = end + ((*end)? 1 : 0);
163 
164  if (checkPath) {
165  end = strchr(urlPtr, '?');
166  if (!end) {
167  checkParams = false;
168  end = strchr(urlPtr, '#');
169  }
170  if (!end) {
171  checkAnchor = false;
172  end = urlPtr+strlen(urlPtr);
173  }
174 
175  path.append(urlPtr, end-urlPtr);
176 
177  urlPtr = end + ((*end)? 1 : 0);
178  }
179 
180  if (checkParams) {
181  //5. Fill the map with the parameters and their values
182  SWBuf paramName;
183  SWBuf paramValue;
184 
185  if (checkAnchor) checkAnchor = false;
186 /*
187  end = strchr(urlPtr, '#');
188  if (!end) {
189  checkAnchor = false;
190  end = urlPtr+strlen(urlPtr);
191  }
192 */
193  //end = (start && strchr(start, '?')) ? strchr(start, '?')+1 :0;
194  end = urlPtr;
195  while (end) {
196  paramName = "";
197  paramValue = "";
198 
199  //search for the equal sign to find the value part
200  const char *valueStart = strchr(end, '=');
201  if (valueStart) {
202  const char* valueEnd = strstr(valueStart, "&amp;") ? strstr(valueStart, "&amp;") : strstr(valueStart, "&"); //try to find a new paramter part
203 
204  if (valueEnd) {
205  paramName.append(end, valueStart-end);
206  paramValue.append(valueStart+1, valueEnd-(valueStart+1));
207  }
208  else { //this is the last paramter of the URL
209  paramName.append(end, valueStart-end);
210  paramValue.append(valueStart+1);
211  }
212 
213  if (paramName.length() && paramValue.length()) {//insert the param into the map if it's valid
214  paramName = decode(paramName.c_str());
215  paramValue = decode(paramValue.c_str());
216 
217  parameterMap[ paramName ] = paramValue;
218  }
219  }
220  else {
221  break; //no valid parameter in the url
222  }
223 
224  const char *start = end+1;
225  end = strstr(start, "&amp;") ? strstr(start, "&amp;")+5 : (strstr(start, "&") ? strstr(start, "&")+1 : 0); //try to find a new paramter part
226  }
227  }
228 }
Definition: swbuf.h:47
unsigned long length() const
Definition: swbuf.h:197
SWBuf hostname
Definition: url.h:87
SWBuf url
Definition: url.h:85
const char * c_str() const
Definition: swbuf.h:158
SWBuf & append(const char *str, long max=-1)
Definition: swbuf.h:274
SWBuf protocol
Definition: url.h:86
SWBuf path
Definition: url.h:88
ParameterMap parameterMap
Definition: url.h:89
static const SWBuf decode(const char *encodedText)
Definition: url.cpp:247

Member Data Documentation

SWBuf URL::hostname
private

Definition at line 87 of file url.h.

ParameterMap URL::parameterMap
private

Definition at line 89 of file url.h.

SWBuf URL::path
private

Definition at line 88 of file url.h.

SWBuf URL::protocol
private

Definition at line 86 of file url.h.

SWBuf URL::url
private

Definition at line 85 of file url.h.


The documentation for this class was generated from the following files: