Overpass API regression suite coverage (Nova Scotia) / overpass_api/frontend/cgi-helper.cc

overpass_api/frontend/cgi-helper.cc

14 of 95 instrumented lines run by the suite (14.7%) at 8edf11b0419c (this file on GitHub).

runnever runcolumns: line, executions, queries (click for the queries that run the line)

1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al.
2 *
3 * This file is part of Overpass_API.
4 *
5 * Overpass_API is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * Overpass_API is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>.
17 */
19#include <algorithm>
20#include <cstdlib>
21#include <cstring>
22#include <iostream>
23#include <string>
24#include <vector>
25#include "cgi-helper.h"
28char hex_digit(char c)
290{
300 if (c <= 57)
310 {
320 if (c >= 48)
330 return c - 48;
340 return 16;
350 }
360 if (c <= 70)
370 {
380 if (c >= 65)
390 return c - 55;
400 return 16;
410 }
420 if (c <= 102)
430 {
440 if (c >= 97)
450 return c - 87;
460 return 16;
470 }
480 return 16;
490}
51std::string cgi_get_to_text()
520{
530 char* method;
540 method = getenv("REQUEST_METHOD");
550 if ((method) && (!strncmp(method, "GET", 4)))
560 return getenv("QUERY_STRING");
570 if ((method) && (!strncmp(method, "OPTIONS", 8)))
580 return getenv("QUERY_STRING");
590 if ((method) && (!strncmp(method, "HEAD", 5)))
600 return getenv("QUERY_STRING");
620 return "";
630}
65std::string cgi_post_to_text(std::string::size_type max_input_size)
662448612 q{
672448612 q std::string raw;
682448612 q const std::streamsize chunk_size = 65536;
692448612 q char chunk[chunk_size];
714896612 q while (raw.size() <= max_input_size)
724896612 q {
734896612 q std::cin.read(chunk, chunk_size);
744896612 q std::streamsize count = std::cin.gcount();
754896612 q if (count <= 0)
762448612 q break;
772448612 q raw.append(chunk, count);
782448612 q }
802448612 q return raw;
812448612 q}
83std::string replace_cgi(const std::string& raw)
840{
850 std::string result;
860 std::string::size_type pos = 0;
880 while (pos < raw.size())
890 {
900 if (raw[pos] == '%' && pos + 2 < raw.size())
910 {
920 char a(hex_digit(raw[pos+1])), b(hex_digit(raw[pos+2]));
930 if ((a < 16) && (b < 16))
940 {
950 result += (char)(a*16 + b);
960 pos += 3;
970 }
980 else
990 result += raw[pos++];
1000 }
1010 else if (raw[pos] == '+')
1020 {
1030 result += ' ';
1040 ++pos;
1050 }
1060 else
1070 result += raw[pos++];
1080 }
1100 return result;
1110}
113std::map< std::string, std::string > decode_cgi_to_plain(const std::string& raw)
1140{
1150 std::map< std::string, std::string > result;
1170 std::string::size_type pos = 0;
1180 do
1190 {
1200 std::string::size_type delim_pos = raw.find('&', pos);
1210 if (delim_pos == std::string::npos)
1220 delim_pos = raw.size();
1240 std::string::size_type middle_pos = std::find(raw.begin() + pos, raw.begin() + delim_pos, '=') - raw.begin();
1250 if (middle_pos != delim_pos)
1260 {
1270 std::string::size_type end_pos = delim_pos;
1280 while (end_pos > 0 && isspace(raw[end_pos-1]))
1290 --end_pos;
1300 result[raw.substr(pos, middle_pos - pos)] = replace_cgi(raw.substr(middle_pos + 1, end_pos - middle_pos - 1));
1310 }
1330 pos = delim_pos + 1;
1340 }
1350 while (pos < raw.size());
1370 if (result["data"] == "")
1380 result["data"] = raw;
1400 return result;
1410}