summaryrefslogtreecommitdiff
path: root/lib/icap/header.cpp
blob: 493e6593f142f5f4ccacc05d9bc2dd9681feec9f (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 *	C++ ICAP library
 *	Copyright (C) 2012	Uditha Atukorala
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "header.h"
#include "util.h"

#include <algorithm>
#include <cstdlib>


namespace icap {

	Header::Header() {

		// initialise defaults
		_encapsulated["req-hdr"]   = -1;
		_encapsulated["req-body"]  = -1;
		_encapsulated["res-hdr"]   = -1;
		_encapsulated["res-body"]  = -1;
		_encapsulated["opt-body"]  = -1;
		_encapsulated["null-body"] = -1;

	}

	Header::~Header() {}


	const Header::headers_t &Header::headers() const throw() {
		return _headers;
	}


	const std::string Header::value( const std::string &key ) throw() {

		std::string value           = "";
		Header::headers_index_t idx = _headers.find( key );

		if ( idx != _headers.end() ) {
			value = idx->second;
		}

		return value;

	}


	const int Header::encapsulated_header( const std::string &entity ) throw() {

		Header::encapsulated_header_index_t idx;

		idx = _encapsulated.find( entity );
		if ( idx == _encapsulated.end() ) {
			return -1;
		}

		return idx->second;

	}


	void Header::attach( std::string key, std::string value ) throw() {

		// trim
		key   = util::trim( key );
		value = util::trim( value );

		// check for 'Encapsulated' headers
		if ( key == "Encapsulated" ) {
			attach_encapsulated( value );
		} else {
			_headers[key] = value;
		}

	}


	bool Header::attach_encapsulated( std::string header_value ) throw() {

		std::vector<std::string> list_data;
		std::vector<std::string> entity_data;

		encapsulated_header_index_t idx;
		bool r_status = true;

		// grab the entity list [ req-hdr=0, null-body=170 ]
		list_data = util::split( util::trim( header_value ), "," );

		for ( size_t i = 0; i < list_data.size(); i++ ) {

			// get entity data [ req-hdr=0 ]
			entity_data = util::split( util::trim( list_data.at( i ) ), "=" );

			if ( entity_data.size() == 2 ) {

				idx = _encapsulated.find( util::trim( entity_data.at( 0 ) ) );
				if ( idx != _encapsulated.end() ) {
					idx->second = atoi( util::trim( entity_data.at( 1 ) ).c_str() );
				}

			} else {
				r_status = false;
			}
		}

		return r_status;

	}


	bool Header::remove( std::string key ) throw() {
		return ( (bool) _headers.erase( util::trim( key ) ) );
	}


	const std::string Header::encapsulated_header_str() throw() {

		/*
		*  Encapsulated request header grammer:
		*   REQMOD  request  encapsulated_list: [reqhdr] reqbody
		*   REQMOD  response encapsulated_list: {[reqhdr] reqbody} |
		*                                       {[reshdr] resbody}
		*   RESPMOD request  encapsulated_list: [reqhdr] [reshdr] resbody
		*   RESPMOD response encapsulated_list: [reshdr] resbody
		*   OPTIONS response encapsulated_list: optbody
		*/

		Header::encapsulated_header_index_t idx;
		std::string encaps_header = "";

		// FIXME: chances are that we will always get the correct order
		//        but should consider sorting
		for ( idx = _encapsulated.begin(); idx != _encapsulated.end(); idx++ ) {

			if ( idx->second > 0 ) {

				if ( encaps_header != "" ) {
					encaps_header.append( ", " );
				}

				encaps_header.append( idx->first ).append( "=" ).append( util::itoa( idx->second ) );

			}

		}

		// sanity check
		if ( encaps_header == "" ) {
			encaps_header = "null-body=0";
		}

		return encaps_header;

	}


	void Header::update_encapsulated( const payload_t &payload ) throw() {

		unsigned int data_length = 0;
		unsigned int data_offset = 0;

		// request header
		if ( payload.req_header.size() > 0 ) {
			_encapsulated["req-hdr"] = data_length;
			data_offset = data_length;
			data_length += payload.req_header.size();
		}

		// request body (POST data)
		if ( payload.req_body.size() > 0 ) {
			_encapsulated["req-body"] = data_length;
			data_offset = data_length;
			data_length += payload.req_body.size();
		}

		// response header
		if ( payload.res_header.size() > 0 ) {
			_encapsulated["res-hdr"] = data_length;
			data_offset = data_length;
			data_length += payload.res_header.size();
		}

		// response body
		if ( payload.res_body.size() > 0 ) {
			_encapsulated["res-body"] = data_length;
			data_offset = data_length;
			data_length += payload.res_body.size();
		}

		// null-body
		if ( data_offset == 0 ) {
			_encapsulated["null-body"] = data_length;
		}

	}


	std::vector<Header::encapsulated_header_data_t> Header::sort_encapsulated_header() {

		std::vector<Header::encapsulated_header_data_t> data( _encapsulated.begin(), _encapsulated.end() );
		std::sort(data.begin(), data.end(), encapsulated_header_compare());

		return data;

	}

}