Skip to content

Commit 2a05a6f

Browse files
smilesa-maurice
authored andcommitted
www form encoder / decoder.
Constructs application/x-www-form-urlencoded forms in a string from a set of key / value pairs. Also, provides a method to parse a form into a vector of key / value pairs. PiperOrigin-RevId: 248589839
1 parent 8872746 commit 2a05a6f

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

app/rest/www_form_url_encoded.cc

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "app/rest/www_form_url_encoded.h"
18+
19+
#include <string.h>
20+
21+
#include <algorithm>
22+
#include <string>
23+
#include <vector>
24+
25+
#include "app/rest/util.h"
26+
27+
namespace firebase {
28+
namespace rest {
29+
30+
void WwwFormUrlEncoded::Add(const char* key, const char* value) {
31+
if (output_->length()) {
32+
*output_ += "&";
33+
}
34+
*output_ += util::EncodeUrl(key);
35+
*output_ += "=";
36+
*output_ += util::EncodeUrl(value);
37+
}
38+
39+
std::vector<WwwFormUrlEncoded::Item> WwwFormUrlEncoded::Parse(
40+
const char* form) {
41+
std::vector<Item> form_data;
42+
const char* current_field = form;
43+
const char* end_of_form = form + strlen(form);
44+
while (current_field < end_of_form) {
45+
const char* end_of_field =
46+
std::find_if(current_field, end_of_form, [](char c) {
47+
return c == '&' || c == '\n' || c == '\r' || c == '\t' || c == ' ';
48+
});
49+
if (end_of_field != current_field) {
50+
const char* separator = std::find(current_field, end_of_field, '=');
51+
if (separator != end_of_field) {
52+
form_data.push_back(
53+
Item(util::DecodeUrl(std::string(current_field, separator)),
54+
util::DecodeUrl(std::string(separator + 1, end_of_field))));
55+
}
56+
}
57+
current_field = end_of_field + 1;
58+
}
59+
return form_data;
60+
}
61+
62+
} // namespace rest
63+
} // namespace firebase

app/rest/www_form_url_encoded.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef FIREBASE_APP_CLIENT_CPP_REST_WWW_FORM_URL_ENCODED_H_
18+
#define FIREBASE_APP_CLIENT_CPP_REST_WWW_FORM_URL_ENCODED_H_
19+
20+
#include <string>
21+
#include <vector>
22+
23+
namespace firebase {
24+
namespace rest {
25+
26+
// Constructs and parses strings of key/value pairs in the
27+
// x-www-form-urlencoded format.
28+
// util::Initialize() must be called before this can be used.
29+
class WwwFormUrlEncoded {
30+
public:
31+
// Form item.
32+
struct Item {
33+
Item() {}
34+
Item(const string& key_, const string& value_) : key(key_), value(value_) {}
35+
36+
std::string key;
37+
std::string value;
38+
};
39+
40+
public:
41+
// Initialize with a string to output constructed form data.
42+
explicit WwwFormUrlEncoded(std::string* output) : output_(output) {}
43+
44+
// Add a key / value pair to the form.
45+
void Add(const char* key, const char* value);
46+
47+
// Add a key / value pair using a form item.
48+
void Add(const Item& item) { Add(item.key.c_str(), item.value.c_str()); }
49+
50+
// Get the form data string constructed by successive calls to Add().
51+
const std::string& form_data() const { return *output_; }
52+
53+
// Parse a x-www-form-urlencoded encoded form into a list of key value pairs.
54+
static std::vector<Item> Parse(const char* form);
55+
56+
private:
57+
std::string* output_;
58+
};
59+
60+
} // namespace rest
61+
} // namespace firebase
62+
63+
#endif // FIREBASE_APP_CLIENT_CPP_REST_WWW_FORM_URL_ENCODED_H_

0 commit comments

Comments
 (0)