Overpass API regression suite coverage (Saxony) / overpass_api/frontend/user_interface.cc

overpass_api/frontend/user_interface.cc

39 of 292 instrumented lines run by the suite (13.4%) 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 <fstream>
20#include <iostream>
21#include <map>
22#include <sstream>
23#include <string>
24#include <vector>
26#include <stdint.h>
27#include <stdlib.h>
28#include <time.h>
30#include "cgi-helper.h"
31#include "../../expat/expat_justparse_interface.h"
32#include "../data/utils.h"
33#include "user_interface.h"
36namespace
38 std::string autocomplete
39 (std::string& input, Error_Output* error_output, uint32 max_input_size)
402768692 q {
412768692 q std::size_t pos(0), line_number(1);
422768692 q while ((pos < input.size()) && (isspace(input[pos])))
430 {
440 if (input[pos] == '\n')
450 ++line_number;
460 ++pos;
470 }
492768692 q if (pos == input.size())
500 {
510 if (pos == 0)
520 {
530 if (error_output)
540 error_output->add_encoding_error("Your input is empty.");
550 }
560 else
570 {
580 if (error_output)
590 error_output->add_encoding_error("Your input contains only whitespace.");
600 }
610 return "";
620 }
64 // pos now points at the first non-whitespace character
65 // assert length restriction.
662768692 q if (input.size() > max_input_size)
670 {
680 std::ostringstream temp;
690 temp<<"Input Input too long (length: "<<input.size()<<"). The maximum query length is "
700 <<(double)max_input_size/1024/1024<<" MB.";
710 if (error_output)
720 error_output->add_encoding_error(temp.str());
730 return input;
740 }
76 // pos again points at the first non-whitespace character.
772768692 q if (input.substr(pos, 1) == "<" && input.substr(pos, 2) != "<?")
78328 q {
79328 q if (input.find("<osm-script") == std::string::npos)
80 // Add a header line, the root tag 'osm-script' and remove trailing whitespace
81123 q {
82123 q std::ostringstream temp;
83123 q temp<<"Your input starts with a tag but not the root tag. Thus, a line with the\n"
84123 q <<"datatype declaration and a line with the root tag 'osm-script' is\n"
85123 q <<"added. This shifts line numbering by "<<(int)line_number-3<<" line(s).";
86123 q if (error_output)
87123 q error_output->add_encoding_remark(temp.str());
89123 q input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<osm-script>\n"
90123 q + input.substr(pos) + "\n</osm-script>\n";
91123 q }
92205 q else
93 // Add a header line and remove trailing whitespace.
94205 q {
95205 q std::ostringstream temp;
96205 q temp<<"Your input contains an 'osm-script' tag. Thus, a line with the\n"
97205 q <<"datatype declaration is added. This shifts line numbering by "
98205 q <<(int)line_number - 2<<" line(s).";
99205 q if (error_output)
100205 q error_output->add_encoding_remark(temp.str());
102205 q input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + input.substr(pos);
103205 q }
104328 q }
1062768692 q return input;
1072768692 q }
110Input_From_CGI get_xml_cgi(Error_Output* error_output, uint32 max_input_size)
1110{
1120 Input_From_CGI result;
114 // Check for various HTTP headers
1150 char* method = getenv("REQUEST_METHOD");
1160 if (method)
1170 {
1180 if (!strncmp(method, "HEAD", 5))
1190 result.http_method = http_head;
1200 else if (!strncmp(method, "OPTIONS", 8))
1210 result.http_method = http_options;
1220 }
1230 char* allow_header_c = getenv("HTTP_ACCESS_CONTROL_REQUEST_HEADERS");
1240 result.allow_header = ((allow_header_c) ? allow_header_c : "");
1250 char* origin = getenv("HTTP_ORIGIN");
1260 if (origin)
1270 result.origin = origin;
1290 std::size_t line_number(1);
130 // If there is nonempty input from GET method, use GET
1310 std::string input(cgi_get_to_text());
1320 std::size_t pos(0);
1330 while ((pos < input.size()) && (isspace(input[pos])))
1340 {
1350 if (input[pos] == '\n')
1360 ++line_number;
1370 ++pos;
1380 }
1400 if (pos == input.size())
1410 {
1420 if (result.http_method == http_options)
1430 {
144 // if we have an OPTIONS request then assume the query is valid
145 // As a quick hack set the input to a valid dummy value
1460 result.cgi["data"] = "out;";
1470 return result;
1480 }
150 // otherwise use POST input
1510 if (pos == 0)
1520 {
1530 if (error_output)
1540 error_output->add_encoding_remark("No input found from GET method. Trying to retrieve input by POST method.");
1550 }
1560 else
1570 {
1580 if (error_output)
1590 error_output->add_encoding_remark("Only whitespace found from GET method. Trying to retrieve input by POST method.");
1600 }
1620 input = cgi_post_to_text(max_input_size);
1630 pos = 0;
1640 line_number = 1;
1650 while ((pos < input.size()) && (isspace(input[pos])))
1660 {
1670 if (input[pos] == '\n')
1680 ++line_number;
1690 ++pos;
1700 }
1720 if (pos == input.size())
1730 {
1740 if (pos == 0)
1750 {
1760 if (error_output)
1770 error_output->add_encoding_error("Your input is empty.");
1780 }
1790 else
1800 {
1810 if (error_output)
1820 error_output->add_encoding_error("Your input contains only whitespace.");
1830 }
1840 result.cgi["data"] = input;
1850 return result;
1860 }
1870 }
1880 else
1890 {
1900 if (error_output)
1910 error_output->add_encoding_remark("Found input from GET method. Thus, any input from POST is ignored.");
1920 }
194 // pos now points at the first non-whitespace character
1950 if (input[pos] != '<')
196 // reduce input to the part between "data=" and "&"
197 // and remove the character escapings from the form
1980 {
1990 if (error_output)
2000 error_output->add_encoding_remark("The server now removes the CGI character escaping.");
2010 decode_cgi_to_plain(input).swap(result.cgi);
2020 std::string jsonp = result.cgi["jsonp"];
2030 input = result.cgi["data"];
2050 if (result.cgi["bbox"] != "")
2060 {
2070 const std::string& lonlat = result.cgi["bbox"];
2090 std::vector< std::string > coords;
2100 std::string::size_type pos = 0;
2110 std::string::size_type newpos = lonlat.find(",");
2120 while (newpos != std::string::npos)
2130 {
2140 coords.push_back(lonlat.substr(pos, newpos - pos));
2150 pos = newpos + 1;
2160 newpos = lonlat.find(",", pos);
2170 }
2180 coords.push_back(lonlat.substr(pos));
2200 bool numeric = coords.size() == 4;
2210 for (std::size_t i = 0; numeric && i < coords.size(); ++i)
2220 {
2230 double value = 0;
2240 if (try_double(coords[i], value))
2250 coords[i] = to_string(value);
2260 else
2270 numeric = false;
2280 }
2300 if (numeric)
2310 {
2320 std::string bbox_global = "[bbox:" + coords[1] + ',' + coords[0] + ',' + coords[3] + ',' + coords[2] + ']';
2330 std::string bbox_filter = '(' + coords[1] + ',' + coords[0] + ',' + coords[3] + ',' + coords[2] + ')';
2350 std::string modified;
2360 std::string::size_type last = 0;
2370 std::string::size_type paren = input.find("(bbox)");
2380 std::string::size_type bracket = input.find("[bbox]");
2390 while (paren != std::string::npos || bracket != std::string::npos)
2400 {
2410 if (bracket < paren)
2420 {
2430 modified.append(input, last, bracket - last);
2440 modified += bbox_global;
2450 last = bracket + 6;
2460 bracket = std::string::npos;
2470 }
2480 else
2490 {
2500 modified.append(input, last, paren - last);
2510 modified += bbox_filter;
2520 last = paren + 6;
2530 paren = input.find("(bbox)", last);
2540 }
2550 }
2560 modified.append(input, last);
2570 input.swap(modified);
2580 }
2590 }
2600 }
2610 else
2620 {
2630 if (error_output)
2640 error_output->add_encoding_remark("The first non-whitespace character is '<'. Thus, your input will be interpreted verbatim.");
2650 }
2670 input = autocomplete(input, error_output, max_input_size);
2680 result.cgi["data"] = input;
2690 return result;
2700}
273std::string get_xml_console(Error_Output* error_output, uint32 max_input_size)
2752768692 q if (error_output)
2762768692 q error_output->add_encoding_remark("Please enter your query and terminate it with CTRL+D.");
278 // If there is nonempty input from GET method, use GET
2792768692 q std::string input("");
2802768692 q input = cgi_post_to_text(max_input_size);
2812768692 q input = autocomplete(input, error_output, max_input_size);
2822768692 q return input;
286std::string probe_client_identifier()
2870{
2880 char* remote_addr_c = getenv("REMOTE_ADDR");
2890 if (!remote_addr_c)
2900 return "";
2920 return std::string(remote_addr_c);
2930}
296uint32 parse_ipv4_address(const std::string ip_addr)
2970{
2980 if (ip_addr == "")
2990 return 0;
3010 std::string::size_type pos = ip_addr.find(".");
3020 std::string::size_type old_pos = 0;
3030 uint32 client_token = 0;
305 // Try IPv4 address format
3060 while (pos != std::string::npos)
3070 {
3080 client_token = (client_token<<8 |
3090 atoll(ip_addr.substr(old_pos, pos - old_pos).c_str()));
3100 old_pos = pos + 1;
3110 pos = ip_addr.find(".", old_pos);
3120 }
3130 client_token = (client_token<<8 | atoll(ip_addr.substr(old_pos).c_str()));
3150 return client_token;
3160}
319namespace
321 int decode_hex(const std::string& representation)
3220 {
3230 int result = 0;
3240 std::string::size_type pos = 0;
3260 while (pos < representation.size())
3270 {
3280 if (representation[pos] >= '0' && representation[pos] <= '9')
3290 result = (result<<4) | (representation[pos] - '0');
3300 else if (representation[pos] >= 'a' && representation[pos] <= 'f')
3310 result = (result<<4) | (representation[pos] - 'a' + 10);
3320 else if (representation[pos] >= 'A' && representation[pos] <= 'F')
3330 result = (result<<4) | (representation[pos] - 'A' + 10);
3340 ++pos;
3350 }
3360 return result;
3370 }
341std::vector< uint16 > parse_short_ipv6_address(std::string ip_addr)
3420{
3430 std::vector< uint16 > ipv6_address;
345 // Try shortened IPv6 address format
3460 std::string::size_type upper_end = ip_addr.find("::");
3470 std::string::size_type pos = ip_addr.find(":");
3480 std::string::size_type old_pos = 0;
3500 while (pos < upper_end)
3510 {
3520 ipv6_address.push_back(decode_hex(ip_addr.substr(old_pos, pos - old_pos).c_str()));
3530 old_pos = pos + 1;
3540 pos = ip_addr.find(":", old_pos);
3550 }
3560 ipv6_address.push_back(decode_hex(ip_addr.substr(old_pos, upper_end - old_pos).c_str()));
3580 std::vector< uint16 > lower_ipv6_address;
3590 old_pos = upper_end + 2;
3600 pos = ip_addr.find(":", old_pos);
3610 while (pos != std::string::npos)
3620 {
3630 lower_ipv6_address.push_back(decode_hex(ip_addr.substr(old_pos, pos - old_pos).c_str()));
3640 old_pos = pos + 1;
3650 pos = ip_addr.find(":", old_pos);
3660 }
3670 lower_ipv6_address.push_back(decode_hex(ip_addr.substr(old_pos).c_str()));
3690 ipv6_address.resize(8, 0);
3700 for (std::vector< uint16 >::size_type i = 0; i < lower_ipv6_address.size(); ++i)
3710 ipv6_address[i + 8 - lower_ipv6_address.size()] = lower_ipv6_address[i];
3730 return ipv6_address;
3740}
377std::vector< uint16 > parse_full_ipv6_address(std::string ip_addr)
3780{
3790 std::vector< uint16 > ipv6_address;
3810 std::string::size_type pos = ip_addr.find(":");
3820 std::string::size_type old_pos = 0;
3840 while (pos != std::string::npos)
3850 {
3860 ipv6_address.push_back(decode_hex(ip_addr.substr(old_pos, pos - old_pos).c_str()));
3870 old_pos = pos + 1;
3880 pos = ip_addr.find(":", old_pos);
3890 }
3910 ipv6_address.resize(8, 0);
3930 return ipv6_address;
3940}
397uint32 probe_client_token()
3980{
3990 std::string ip_addr = probe_client_identifier();
4000 if (ip_addr == "")
4010 return 0;
4030 if (ip_addr.find(".") != std::string::npos)
4040 return parse_ipv4_address(ip_addr);
4060 std::vector< uint16 > ipv6_address = (ip_addr.find("::") == std::string::npos ?
4070 parse_full_ipv6_address(ip_addr) :
4080 parse_short_ipv6_address(ip_addr));
410 // We only consider the upper 64 bit of an IPv6 address.
411 // For the sake of simplicity we xor these bits to get a 32 bit token.
412 // This shall be reviewed once we know how IPv6 addresses really are distributed.
4130 return ((ipv6_address[0] ^ ipv6_address[2])<<16 | (ipv6_address[1] ^ ipv6_address[3]));
4140}