|
| 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 |
0 commit comments