File: ParseWebData.cpp

package info (click to toggle)
bibledit-cloud 5.0.922-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 235,632 kB
  • sloc: xml: 863,336; ansic: 168,904; cpp: 82,911; javascript: 52,743; sh: 4,594; makefile: 503; php: 69
file content (166 lines) | stat: -rw-r--r-- 5,083 bytes parent folder | download | duplicates (3)
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
/*
 The MIT License (MIT)
 Copyright (c) 2012 Cyril Margorin, Imperx Inc. and other contributors
 https://code.google.com/p/parsewebdata/
 
 Permission is hereby granted, free of charge, to any person obtaining
 a copy of this software and associated documentation files (the
 "Software"), to deal in the Software without restriction, including
 without limitation the rights to use, copy, modify, merge, publish,
 distribute, sublicense, and/or sell copies of the Software, and to
 permit persons to whom the Software is furnished to do so, subject to
 the following conditions:
 
 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#include "ParseWebData.h"
#include "ParseWebData_local.h"

#include "ParseMultipartFormData.h"

#include <algorithm>
#include <sstream>
#include <iterator>

namespace ParseWebData {

typedef bool (*ParserFunc)(const std::string& data, const string_map& content_type, WebDataMap& dataMap);

static bool parse_default(const std::string& data, const string_map& content_type, WebDataMap& dataMap) {
  if (data.empty()) {};
  if (content_type.empty()) {};
  if (sizeof (dataMap)) {};
	return false;
}

bool parse_url_encoded_data(const std::string& data, const string_map& content_type, WebDataMap& dataMap)
{
  if (content_type.empty()) {};
	string_map values = map_pairs(data, "&", "=");
	for(string_map::const_iterator iter = values.begin(); iter != values.end(); ++iter)
	{
		dataMap.insert(std::make_pair((*iter).first, WebData((*iter).second)));
	}
	return true;
}

bool parse_plain_text_data(const std::string& data, const string_map& content_type, WebDataMap& dataMap)
{
  if (content_type.empty()) {};
	string_map values = map_pairs(data, "\r\n", "=");
	for(string_map::const_iterator iter = values.begin(); iter != values.end(); ++iter)
	{
		dataMap.insert(std::make_pair((*iter).first, WebData((*iter).second)));
	}
	return true;
}

typedef std::map<std::string, ParserFunc> ParserFuncsMap;
static const ParserFuncsMap& get_parsers_map() {
	static ParserFuncsMap parsers_map;
	if (parsers_map.empty()) {
		parsers_map.insert(
				std::make_pair("multipart/form-data",
						&ParseMultipartFormData::parse_data));
		parsers_map.insert(
				std::make_pair("application/x-www-form-urlencoded",
						&parse_url_encoded_data));
		parsers_map.insert(
				std::make_pair("text/plain",
						&parse_plain_text_data));
	}

	return parsers_map;
}

static ParserFunc get_parser(const std::string& contentType) {
	ParserFuncsMap::const_iterator iter = get_parsers_map().find(contentType);
	if (get_parsers_map().end() == iter)
		return &parse_default;
	else
		return (*iter).second;

}

bool parse_post_data(const std::string & postData,
		const std::string & contentType, WebDataMap & postDataMap) {
	// 1. parse contentType
	string_map contentTypeMap = map_pairs(
			std::string("content-type=") + contentType, "; ", "=");
	return (*get_parser(contentTypeMap["content-type"]))(postData,
			contentTypeMap, postDataMap);
}

bool parse_get_data(const std::string & getData, WebDataMap & getDataMap) {
	return parse_url_encoded_data(getData, string_map(), getDataMap);
}

namespace map_pairs_helper {
class ParsePairsFunc {
public:
	ParsePairsFunc(const std::string& _pairDelim, string_map& _result) :
			pairDelim(_pairDelim), result(_result) {
	}
	void operator ()(const std::string& value) {
		string_list list = split(value, pairDelim);
		std::pair<std::string, std::string> pair;
		string_list::const_iterator iter = list.begin();
		if (iter != list.end()) {
			pair.first = *iter;
			iter++;
			if (iter != list.end()) {
				pair.second = *iter;
			}
		}

		result.insert(pair);
	}

private:
	const std::string& pairDelim;
	string_map& result;
};
} // namespace map_pairs_helper

string_map map_pairs(const std::string& s, const std::string& elemDelim,
		const std::string& pairDelim) {
	using namespace map_pairs_helper;

	string_list words;
	string_map result;

	words = split(s, elemDelim, false);
	std::for_each(words.begin(), words.end(),
			ParsePairsFunc(pairDelim, result));
	return result;
}

string_list split(const std::string & s, const std::string & delim,
		bool keep_empty) {
	string_list result;
	std::string::const_iterator substart = s.begin(), subend = substart;
	while (true) {
		subend = std::search(substart, s.end(), delim.begin(), delim.end());
		std::string temp(substart, subend);
		if (keep_empty || !temp.empty()) {
			result.push_back(temp);
		}
		if (subend == s.end())
			break;
		substart = subend + delim.size();
	}

	return result;
}

}
// namespace ParseWebData