~ubuntu-branches/ubuntu/raring/lurker/raring

« back to all changes in this revision

Viewing changes to render/parse.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Meurer
  • Date: 2004-09-26 16:27:51 UTC
  • Revision ID: james.westby@ubuntu.com-20040926162751-z1ohcjltv7ojtg6z
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

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 $
 
2
 *  
 
3
 *  parse.cpp - Deal with CGI ugliness
 
4
 *  
 
5
 *  Copyright (C) 2002 - Wesley W. Terpstra
 
6
 *  
 
7
 *  License: GPL
 
8
 *  
 
9
 *  Authors: 'Wesley W. Terpstra' <wesley@terpstra.ca>
 
10
 *  
 
11
 *    This program is free software; you can redistribute it and/or modify
 
12
 *    it under the terms of the GNU General Public License as published by
 
13
 *    the Free Software Foundation; version 2.
 
14
 *    
 
15
 *    This program is distributed in the hope that it will be useful,
 
16
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
 *    GNU General Public License for more details.
 
19
 *    
 
20
 *    You should have received a copy of the GNU General Public License
 
21
 *    along with this program; if not, write to the Free Software
 
22
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
23
 */
 
24
 
 
25
#define _XOPEN_SOURCE 500
 
26
#define _FILE_OFFSET_BITS 64
 
27
 
 
28
#include <iostream>
 
29
 
 
30
#include "parse.h"
 
31
 
 
32
inline int fromHex(char c)
 
33
{
 
34
        if (c >= 'A' && c <= 'Z') return c - 'A' + 10;
 
35
        if (c >= 'a' && c <= 'z') return c - 'a' + 10;
 
36
        return c - '0';
 
37
}
 
38
 
 
39
string decipherHalf(const string& str)
 
40
{
 
41
//      cout << "deciper: " << str << endl;
 
42
        
 
43
        string out;
 
44
        
 
45
        string::size_type b = 0, e;
 
46
        while ((e = str.find_first_of("%+", b)) != string::npos)
 
47
        {
 
48
                out.append(str, b, e - b);
 
49
                if (str[e] == '+') out.append(" ");
 
50
                else if (str.length() > e+2)
 
51
                {
 
52
                        int ch = fromHex(str[e+1]) << 4 | fromHex(str[e+2]);
 
53
                        out += ((char)ch);
 
54
                        e += 2;
 
55
                }
 
56
                
 
57
                b = e+1;
 
58
        }
 
59
        
 
60
        out.append(str, b, str.length() - b);
 
61
        
 
62
        return out;
 
63
}
 
64
 
 
65
pair<string, string> splitParam(const string& str)
 
66
{
 
67
//      cout << "split: " << str << endl;
 
68
        
 
69
        string::size_type split = str.find('=');
 
70
        if (split == string::npos)
 
71
                return pair<string, string>(decipherHalf(str), "");
 
72
        else    return pair<string, string>(
 
73
                        decipherHalf(str.substr(0, split)),
 
74
                        decipherHalf(str.substr(split+1, string::npos)));
 
75
}
 
76
 
 
77
map<string, string> getParams()
 
78
{
 
79
        map<string, string> out;
 
80
        
 
81
        char* x = getenv("QUERY_STRING");
 
82
        
 
83
        string str = x?x:"";
 
84
        
 
85
//      cout << "parse: " << str << endl;
 
86
        
 
87
        string::size_type b = str.find_first_not_of('&', 0), e;
 
88
        if (b == string::npos) return out;
 
89
        
 
90
        while ((e = str.find_first_of('&', b)) != string::npos)
 
91
        {
 
92
                out.insert(splitParam(str.substr(b, e - b)));
 
93
                b = str.find_first_not_of('&', e+1);
 
94
        }
 
95
        out.insert(splitParam(str.substr(b, str.length() - b)));
 
96
        
 
97
        return out;
 
98
}
 
99
 
 
100
int redirectUrl(const string& url)
 
101
{
 
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";
 
113
        
 
114
        return 0;
 
115
}