~ubuntu-branches/ubuntu/intrepid/lurker/intrepid

« back to all changes in this revision

Viewing changes to render/parse.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Meurer
  • Date: 2006-12-20 05:05:31 UTC
  • mfrom: (3.1.6 feisty)
  • Revision ID: james.westby@ubuntu.com-20061220050531-79inzy7o6uu95qx7
Tags: 2.1-7
* updated vi debconf translations, thanks to Clytie Siddall
  <clytie@riverland.net.au>
* fixed typo in one template (that you would like, not that
  would you like), thanks as well to Clytie Siddall
  - unfuzzied all translations

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  $Id: parse.cpp,v 1.5 2003/06/23 14:38:43 terpstra Exp $
 
1
/*  $Id: parse.cpp,v 1.13 2006/03/03 01:04:25 terpstra Exp $
2
2
 *  
3
3
 *  parse.cpp - Deal with CGI ugliness
4
4
 *  
22
22
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
23
 */
24
24
 
25
 
#define _XOPEN_SOURCE 500
26
25
#define _FILE_OFFSET_BITS 64
27
26
 
28
27
#include <iostream>
33
32
{
34
33
        if (c >= 'A' && c <= 'Z') return c - 'A' + 10;
35
34
        if (c >= 'a' && c <= 'z') return c - 'a' + 10;
36
 
        return c - '0';
 
35
        if (c >= '0' && c <= '9') return c - '0';
 
36
        return -1;
37
37
}
38
38
 
39
39
string decipherHalf(const string& str)
41
41
//      cout << "deciper: " << str << endl;
42
42
        
43
43
        string out;
 
44
        int high, low;
44
45
        
45
46
        string::size_type b = 0, e;
46
47
        while ((e = str.find_first_of("%+", b)) != string::npos)
47
48
        {
48
49
                out.append(str, b, e - b);
49
50
                if (str[e] == '+') out.append(" ");
50
 
                else if (str.length() > e+2)
 
51
                else if (str.length() > e+2 &&
 
52
                         (high = fromHex(str[e+1])) != -1 &&
 
53
                         (low = fromHex(str[e+2])) != -1)
51
54
                {
52
 
                        int ch = fromHex(str[e+1]) << 4 | fromHex(str[e+2]);
 
55
                        int ch = high << 4 | low;
53
56
                        out += ((char)ch);
54
57
                        e += 2;
55
58
                }
 
59
                else
 
60
                {       // keep the broken escape char
 
61
                        out.append("%");
 
62
                }
56
63
                
57
64
                b = e+1;
58
65
        }
59
66
        
60
 
        out.append(str, b, str.length() - b);
 
67
        out.append(str, b, string::npos);
61
68
        
62
69
        return out;
63
70
}
78
85
{
79
86
        map<string, string> out;
80
87
        
81
 
        char* x = getenv("QUERY_STRING");
 
88
        const char* x = getenv("QUERY_STRING");
82
89
        
83
90
        string str = x?x:"";
84
91
        
91
98
        {
92
99
                out.insert(splitParam(str.substr(b, e - b)));
93
100
                b = str.find_first_not_of('&', e+1);
94
 
        }
95
 
        out.insert(splitParam(str.substr(b, str.length() - b)));
 
101
                if (b == string::npos) break;
 
102
        }
 
103
        if (b != string::npos) 
 
104
                out.insert(splitParam(str.substr(b, str.length() - b)));
 
105
        
 
106
        return out;
 
107
}
 
108
 
 
109
map<string, string> getCookies()
 
110
{
 
111
        map<string, string> out;
 
112
        
 
113
        const char* x = getenv("HTTP_COOKIE");
 
114
        
 
115
        string str = x?x:"";
 
116
        
 
117
//      cout << "parse: " << str << endl;
 
118
        
 
119
        string::size_type b = str.find_first_not_of("; ", 0), e;
 
120
        if (b == string::npos) return out;
 
121
        
 
122
        while ((e = str.find_first_of("; ", b)) != string::npos)
 
123
        {
 
124
                out.insert(splitParam(str.substr(b, e - b)));
 
125
                b = str.find_first_not_of("; ", e+1);
 
126
                if (b == string::npos) break;
 
127
        }
 
128
        if (b != string::npos)
 
129
                out.insert(splitParam(str.substr(b, str.length() - b)));
 
130
        
 
131
        return out;
 
132
}
 
133
 
 
134
string uriEncode(const string& str)
 
135
{
 
136
        string out;
 
137
        static const char tbl[17] = "0123456789ABCDEF";
 
138
        
 
139
        string::size_type b = 0, e;
 
140
        while ((e = str.find_first_not_of(
 
141
                "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~:/.,@-!", 
 
142
                b)) != string::npos)
 
143
        {
 
144
                out.append(str, b, e - b);
 
145
                
 
146
                out += '%';
 
147
                out += tbl[(str[e] >> 4) & 0xF];
 
148
                out += tbl[(str[e] >> 0) & 0xF];
 
149
                
 
150
                b = e+1;
 
151
        }
 
152
        
 
153
        out.append(str, b, str.length() - b);
96
154
        
97
155
        return out;
98
156
}
99
157
 
100
158
int redirectUrl(const string& url)
101
159
{
102
 
        cout << "Status: 303 Moved Permanently\r\n"
103
 
             << "Location: " << url << "\r\n"
104
 
             << "Content-type: text/html\r\n\r\n"
105
 
             << "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n"
106
 
             << "<html><head>\r\n"
107
 
             << "<title>301 Moved Permanently</title>\r\n"
108
 
             << "</head><body>\r\n"
109
 
             << "<h1>Moved Permanently</h1>\r\n"
110
 
             << "The document has moved <a href=\"" << url << "\">here</a>.\r\n"
111
 
             << "<p><hr>\r\n"
112
 
             << "</body></html>\r\n";
 
160
        string proto(url, 0, 11);
 
161
        for (string::size_type i = 0; i < proto.length(); ++i)
 
162
                if (proto[i] >= 'A' && proto[i] <= 'Z')
 
163
                        proto[i] += 'a' - 'A';
 
164
        
 
165
        if (proto == "javascript:")
 
166
        {
 
167
                cout << "Status: 200 OK\r\n"
 
168
                     << "Content-type: text/plain\r\n\r\n"
 
169
                     << "Javascript go awaaayyyyyaaayyy! GO AWAY!\n";
 
170
        }
 
171
        else
 
172
        {
 
173
                cout << "Status: 303 Moved Permanently\r\n"
 
174
                     << "Location: " << uriEncode(url) << "\r\n"
 
175
                     << "Content-type: text/html\r\n\r\n"
 
176
                     << "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n"
 
177
                     << "<html><head>\r\n"
 
178
                     << "<title>301 Moved Permanently</title>\r\n"
 
179
                     << "</head><body>\r\n"
 
180
                     << "<h1>Moved Permanently</h1>\r\n"
 
181
                     << "The document has moved <a href=\"" << uriEncode(url) << "\">here</a>.\r\n"
 
182
                     << "<p><hr>\r\n"
 
183
                     << "</body></html>\r\n";
 
184
        }
113
185
        
114
186
        return 0;
115
187
}