1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al. 3 * This file is part of Overpass_API. 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. 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. 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/>. 30#include "cgi-helper.h" 31#include "../../expat/expat_justparse_interface.h" 32#include "../data/utils.h" 33#include "user_interface.h" 38 std::string autocomplete 39 (std::string& input, Error_Output* error_output, uint32 max_input_size) 412448612 q std::size_t pos(0), line_number(1); 422448612 q while ((pos < input.size()) && (isspace(input[pos]))) 440 if (input[pos] == '\n') 492448612 q if (pos == input.size()) 540 error_output->add_encoding_error("Your input is empty."); 590 error_output->add_encoding_error("Your input contains only whitespace."); 64 // pos now points at the first non-whitespace character 65 // assert length restriction. 662448612 q if (input.size() > max_input_size) 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."; 720 error_output->add_encoding_error(temp.str()); 76 // pos again points at the first non-whitespace character. 772448612 q if (input.substr(pos, 1) == "<" && input.substr(pos, 2) != "<?") 79328 q if (input.find("<osm-script") == std::string::npos) 80 // Add a header line, the root tag 'osm-script' and remove trailing whitespace 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"; 93 // Add a header line and remove trailing whitespace. 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); 110Input_From_CGI get_xml_cgi(Error_Output* error_output, uint32 max_input_size) 1120 Input_From_CGI result; 114 // Check for various HTTP headers 1150 char* method = getenv("REQUEST_METHOD"); 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; 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"); 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()); 1330 while ((pos < input.size()) && (isspace(input[pos]))) 1350 if (input[pos] == '\n') 1400 if (pos == input.size()) 1420 if (result.http_method == http_options) 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;"; 150 // otherwise use POST input 1540 error_output->add_encoding_remark("No input found from GET method. Trying to retrieve input by POST method."); 1590 error_output->add_encoding_remark("Only whitespace found from GET method. Trying to retrieve input by POST method."); 1620 input = cgi_post_to_text(max_input_size); 1650 while ((pos < input.size()) && (isspace(input[pos]))) 1670 if (input[pos] == '\n') 1720 if (pos == input.size()) 1770 error_output->add_encoding_error("Your input is empty."); 1820 error_output->add_encoding_error("Your input contains only whitespace."); 1840 result.cgi["data"] = input; 1910 error_output->add_encoding_remark("Found input from GET method. Thus, any input from POST is ignored."); 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 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"] != "") 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) 2140 coords.push_back(lonlat.substr(pos, newpos - pos)); 2160 newpos = lonlat.find(",", pos); 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) 2240 if (try_double(coords[i], value)) 2250 coords[i] = to_string(value); 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) 2410 if (bracket < paren) 2430 modified.append(input, last, bracket - last); 2440 modified += bbox_global; 2460 bracket = std::string::npos; 2500 modified.append(input, last, paren - last); 2510 modified += bbox_filter; 2530 paren = input.find("(bbox)", last); 2560 modified.append(input, last); 2570 input.swap(modified); 2640 error_output->add_encoding_remark("The first non-whitespace character is '<'. Thus, your input will be interpreted verbatim."); 2670 input = autocomplete(input, error_output, max_input_size); 2680 result.cgi["data"] = input; 273std::string get_xml_console(Error_Output* error_output, uint32 max_input_size) 2762448612 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 2802448612 q input = cgi_post_to_text(max_input_size); 2812448612 q input = autocomplete(input, error_output, max_input_size); 286std::string probe_client_identifier() 2880 char* remote_addr_c = getenv("REMOTE_ADDR"); 2920 return std::string(remote_addr_c); 296uint32 parse_ipv4_address(const std::string ip_addr) 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) 3080 client_token = (client_token<<8 | 3090 atoll(ip_addr.substr(old_pos, pos - old_pos).c_str())); 3110 pos = ip_addr.find(".", old_pos); 3130 client_token = (client_token<<8 | atoll(ip_addr.substr(old_pos).c_str())); 3150 return client_token; 321 int decode_hex(const std::string& representation) 3240 std::string::size_type pos = 0; 3260 while (pos < representation.size()) 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); 341std::vector< uint16 > parse_short_ipv6_address(std::string ip_addr) 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) 3520 ipv6_address.push_back(decode_hex(ip_addr.substr(old_pos, pos - old_pos).c_str())); 3540 pos = ip_addr.find(":", old_pos); 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) 3630 lower_ipv6_address.push_back(decode_hex(ip_addr.substr(old_pos, pos - old_pos).c_str())); 3650 pos = ip_addr.find(":", old_pos); 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; 377std::vector< uint16 > parse_full_ipv6_address(std::string ip_addr) 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) 3860 ipv6_address.push_back(decode_hex(ip_addr.substr(old_pos, pos - old_pos).c_str())); 3880 pos = ip_addr.find(":", old_pos); 3910 ipv6_address.resize(8, 0); 3930 return ipv6_address; 397uint32 probe_client_token() 3990 std::string ip_addr = probe_client_identifier(); 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]));