summaryrefslogtreecommitdiffabout
path: root/test/kingate_openid_message.h
blob: 37dcdfab8e96015ad45e9bad0db17539fd6e9233 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
template<typename IT>
class join_iterator : public iterator<
		      input_iterator_tag,typename IT::value_type,
		      void,typename IT::pointer,typename IT::reference> {
    public:
	typedef pair<IT,IT> range_t;
	typedef list<range_t> ranges_t;
	ranges_t ranges;

	join_iterator() { }

	bool cleanup() {
	    bool rv = false;
	    while(!(ranges.empty() || ranges.front().first!=ranges.front().second)) {
		ranges.pop_front(); rv = true;
	    }
	    return rv;
	}

	join_iterator<IT>& add_range(const IT& b,const IT& e) {
	    ranges.push_back(typename ranges_t::value_type(b,e));
	    cleanup();
	    return *this;
	}

	bool operator==(const join_iterator<IT>& x) const {
	    return ranges==x.ranges; }
	bool operator!=(const join_iterator<IT>& x) const {
	    return ranges!=x.ranges; }

	typename IT::reference operator*() const {
	    assert(!ranges.empty());
	    assert(ranges.front().first!=ranges.front().second);
	    return *ranges.front().first; }
	typename IT::pointer operator->() const {
	    assert(!ranges.empty());
	    assert(ranges.front().first!=ranges.front().second);
	    return ranges.front().first.operator->(); }

	join_iterator<IT>& operator++() {
	    cleanup();
	    if(ranges.empty()) return *this;
	    do {
		++ranges.front().first;
	    }while(cleanup() && !ranges.empty());
	    return *this;
	}
	join_iterator<IT> operator++(int) {
	    join_iterator<IT> rv(*this);
	    ++(*this); return rv; }
};

template<typename IT>
class cut_prefix_filterator : public opkele::util::basic_filterator<IT> {
    public:
	string pfx;
	mutable string tmp;

	cut_prefix_filterator() { }
	cut_prefix_filterator(const IT& bi,const IT&ei,const string& pfx)
	    : opkele::util::basic_filterator<IT>(bi,ei), pfx(pfx) {
		this->prepare();
	    }

	bool is_interesting() const {
	    return pfx.length()==0 || !strncmp(this->it->c_str(),pfx.c_str(),pfx.length());
	}

	typename IT::reference operator*() const {
	    assert(!this->empty);
	    tmp = *this->it; tmp.erase(0,pfx.length());
	    return tmp; }
	typename IT::pointer operator->() const {
	    assert(!this->empty);
	    return &this->operator*(); }
};

class kingate_openid_message_t : public opkele::basic_openid_message {
    	typedef join_iterator<kingate::cgi_gateway::params_t::const_iterator> jitterator;
	typedef opkele::util::map_keys_iterator<
	    jitterator,
	    fields_iterator::value_type,
	    fields_iterator::reference,
	    fields_iterator::pointer> keys_iterator;
	typedef cut_prefix_filterator<keys_iterator> pfilterator;
    public:
	const kingate::cgi_gateway& gw;

	kingate_openid_message_t(const kingate::cgi_gateway& g) : gw(g) { }

	bool has_field(const string& n) const {
	    return gw.has_param("openid."+n); }
	const string& get_field(const string& n) const try {
	    return gw.get_param("openid."+n); }catch(kingate::exception_notfound& nf) {
		throw opkele::failed_lookup(OPKELE_CP_ nf.what()); }

	fields_iterator fields_begin() const {
	    return
		pfilterator( keys_iterator(
			    jitterator()
			    .add_range( gw.get.begin(), gw.get.end() )
			    .add_range( gw.post.begin(), gw.post.end() ),
			    jitterator()
			    ), keys_iterator(), "openid." );
	}
	fields_iterator fields_end() const {
	    return pfilterator();
	}
};