iipsrv  1.1
iipsrv is an advanced high-performance feature-rich image server for web-based streamed viewing and zooming of ultra high-resolution images
URL.h
1 /*
2  Simple URL decoder Class
3 
4  Copyright (C) 2014-2015 Ruven Pillay.
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software Foundation,
18  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20 
21 
22 #ifndef _URL_H
23 #define _URL_H
24 
25 #include <string>
26 #include <iterator>
27 #include <cctype>
28 
29 
31 
32 class URL{
33 
34  private:
35 
37  std::string url;
38 
40  std::string warning_message;
41 
42  // Internal utility function to decode hex values
43  char hexToChar( char first, char second );
44 
45  public:
46 
48 
49  URL( std::string s ){ url = s; };
50 
52  std::string decode();
53 
55  std::string escape();
56 
58  std::string warning(){ return warning_message; };
59 
60 };
61 
62 
63 inline char URL::hexToChar( char first, char second ){
64  int digit;
65  digit = (first >= 'A' ? ((first & 0xDF) - 'A') + 10 : (first - '0'));
66  digit *= 16;
67  digit += (second >= 'A' ? ((second & 0xDF) - 'A') + 10 : (second - '0'));
68  return static_cast<char>(digit);
69 }
70 
71 
72 // The argument is a URL path, which may contain spaces or other hex encoded characters.
73 // So, first decode and filter this path (implementation taken from GNU cgicc: http://www.cgicc.org)
74 inline std::string URL::decode()
75 {
76  std::string argument;
77  std::string::iterator iter;
78  char c;
79 
80  for(iter = url.begin(); iter != url.end(); ++iter) {
81  switch(*iter) {
82  case '+':
83  argument.append(1,' ');
84  break;
85  case '%':
86  // Don't assume well-formed input
87  if( std::distance(iter, url.end()) >= 2 &&
88  std::isxdigit(*(iter + 1)) && std::isxdigit(*(iter + 2)) ){
89 
90  // Filter out embedded NULL bytes of the form %00 from the URL
91  if( (*(iter+1)=='0' && *(iter+2)=='0') ){
92  warning_message = "Warning! Detected embedded NULL byte in URL: " + url;
93  // Wind forward our iterator
94  iter+=2;
95  }
96  // Otherwise decode the character
97  else{
98  c = *++iter;
99  argument.append(1,hexToChar(c,*++iter));
100  }
101  }
102  // Just pass the % through untouched
103  else {
104  argument.append(1,'%');
105  }
106  break;
107 
108  default:
109  argument.append(1,*iter);
110  break;
111  }
112  }
113 
114  return argument;
115 }
116 
117 
118 // Escape strings for JSON etc.
119 inline std::string URL::escape()
120 {
121  std::string json;
122  std::string input = this->decode();
123 
124  for( unsigned int i=0; i<input.length(); i++ ){
125  char c = input[i];
126  switch(c){
127  case '\\':
128  json += "\\\\";
129  break;
130  case '"':
131  json += "\\\"";
132  break;
133  default:
134  json += c;
135  }
136  }
137  return json;
138 }
139 
140 
141 #endif
URL::warning
std::string warning()
Return any warning message.
Definition: URL.h:58
URL::URL
URL(std::string s)
Constructor.
Definition: URL.h:49
URL
Simple utility class to decode and filter URLs.
Definition: URL.h:32
URL::escape
std::string escape()
String escaping for JSON etc.
Definition: URL.h:119
URL::decode
std::string decode()
Decode and filter URL.
Definition: URL.h:74