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

overpass_api/frontend/web_output.cc

13 of 142 instrumented lines run by the suite (9.2%) 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 "../../expat/escape_xml.h"
20#include "output.h"
21#include "web_output.h"
23#include <iomanip>
24#include <iostream>
25#include <sstream>
26#include <string>
30void Web_Output::add_encoding_error(const std::string& error)
310{
320 if (log_level != Error_Output::QUIET)
330 {
340 std::ostringstream out;
350 out<<"encoding error: "<<error;
360 display_error(out.str(), 400);
370 }
380 encoding_errors = true;
390}
42void Web_Output::add_parse_error(const std::string& error, int line_number)
430{
440 if (log_level != Error_Output::QUIET)
450 {
460 std::ostringstream out;
470 out<<"line "<<line_number<<": parse error: "<<error;
480 display_error(out.str(), 400);
490 }
500 parse_errors = true;
510}
54void Web_Output::add_static_error(const std::string& error, int line_number)
550{
560 if (log_level != Error_Output::QUIET)
570 {
580 std::ostringstream out;
590 out<<"line "<<line_number<<": static error: "<<error;
600 display_error(out.str(), 400);
610 }
620 static_errors = true;
630}
66void Web_Output::add_encoding_remark(const std::string& error)
670{
680 if (log_level == Error_Output::VERBOSE)
690 {
700 std::ostringstream out;
710 out<<"encoding remark: "<<error;
720 display_remark(out.str());
730 }
740}
77void Web_Output::add_parse_remark(const std::string& error, int line_number)
780{
790 if (log_level == Error_Output::VERBOSE)
800 {
810 std::ostringstream out;
820 out<<"line "<<line_number<<": parse remark: "<<error;
830 display_remark(out.str());
840 }
850}
88void Web_Output::add_static_remark(const std::string& error, int line_number)
890{
900 if (log_level == Error_Output::VERBOSE)
910 {
920 std::ostringstream out;
930 out<<"line "<<line_number<<": static remark: "<<error;
940 display_remark(out.str());
950 }
960}
99void Web_Output::runtime_error(const std::string& error)
1000{
1010 if (log_level != Error_Output::QUIET)
1020 {
1030 std::ostringstream out;
1040 out<<"runtime error: "<<error;
1050 display_error(out.str(), 200);
1060 }
1070}
110void Web_Output::runtime_remark(const std::string& error)
1110{
1120 if (log_level == Error_Output::VERBOSE)
1130 {
1140 std::ostringstream out;
1150 out<<"runtime remark: "<<error;
1160 display_remark(out.str());
1170 }
1180}
121void Web_Output::enforce_header(uint write_mime)
1220{
1230 if (header_written == not_yet)
1240 write_html_header("", "", write_mime);
1250}
128void Web_Output::write_html_header
129 (const std::string& timestamp, const std::string& area_timestamp, uint write_mime, bool write_js_init,
130 bool write_remarks)
1310{
1320 if (header_written != not_yet)
1330 return;
1340 header_written = html;
1360 if (write_mime)
1370 {
1380 if (allow_headers != "")
1390 std::cout<<"Access-Control-Allow-Headers: "<<allow_headers<<'\n';
1400 if (has_origin)
1410 std::cout<<"Access-Control-Allow-Origin: *\n"
1420 "Access-Control-Max-Age: 600\n";
1430 if (http_method == http_options)
1440 std::cout<<"Access-Control-Allow-Methods: GET, POST, OPTIONS\n"
1450 "Content-Length: 0\n";
1460 if (http_method == http_options || http_method == http_head)
1470 {
1480 std::cout<<"Content-type: text/html; charset=utf-8\n";
1490 return;
1500 }
1510 }
1520 ::write_html_header(timestamp, area_timestamp, write_mime, write_js_init, write_remarks);
1530}
156void Web_Output::write_payload_header
157 (const std::string& db_dir, const std::string& timestamp, const std::string& area_timestamp, bool write_mime)
159673673 q if (header_written != not_yet)
1600 return;
161673673 q header_written = payload;
163673673 q if (write_mime)
1640 {
1650 if (allow_headers != "")
1660 std::cout<<"Access-Control-Allow-Headers: "<<allow_headers<<'\n';
1670 if (has_origin)
1680 std::cout<<"Access-Control-Allow-Origin: *\n"
1690 "Access-Control-Max-Age: 600\n";
1700 if (http_method == http_options)
1710 std::cout<<"Access-Control-Allow-Methods: GET, POST, OPTIONS\n"
1720 "Content-Length: 0\n";
1730 if (!output_handler || output_handler->write_http_headers())
1740 std::cout<<'\n';
1750 if (http_method == http_options || http_method == http_head)
1760 return;
1770 }
179673673 q if (output_handler)
180673673 q output_handler->write_payload_header(db_dir, timestamp, area_timestamp);
184// void Web_Output::write_json_header
185// (const std::string& timestamp, const std::string& area_timestamp, bool write_mime)
186// {
187// if (header_written != not_yet)
188// return;
189// header_written = json;
190//
191// if (write_mime)
192// {
193// if (allow_headers != "")
194// std::cout<<"Access-Control-Allow-Headers: "<<allow_headers<<'\n';
195// if (has_origin)
196// std::cout<<"Access-Control-Allow-Origin: *\n"
197// "Access-Control-Max-Age: 600\n";
198// if (http_method == http_options)
199// std::cout<<"Access-Control-Allow-Methods: GET, POST, OPTIONS\n"
200// "Content-Length: 0\n";
201// std::cout<<"Content-type: application/json\n\n";
202// if (http_method == http_options || http_method == http_head)
203// return;
204// }
205//
206// if (padding != "")
207// std::cout<<padding<<"(";
208//
209// std::cout<<"{\n"
210// " \"version\": 0.6,\n"
211// " \"generator\": \"Overpass API\",\n"
212// " \"osm3s\": {\n"
213// " \"timestamp_osm_base\": \""<<timestamp<<"\",\n";
214// if (area_timestamp != "")
215// std::cout<<" \"timestamp_areas_base\": \""<<area_timestamp<<"\",\n";
216// std::cout<<" \"copyright\": \"The data included in this document is from www.openstreetmap.org."
217// " The data is made available under ODbL.\"\n"
218// " },\n";
219// std::cout<< " \"elements\": [\n\n";
220// }
223// void Web_Output::write_text_header
224// (const std::string& timestamp, const std::string& area_timestamp, bool write_mime)
225// {
226// if (header_written != not_yet)
227// return;
228// header_written = text;
229//
230// if (write_mime)
231// {
232// if (allow_headers != "")
233// std::cout<<"Access-Control-Allow-Headers: "<<allow_headers<<'\n';
234// if (has_origin)
235// std::cout<<"Access-Control-Allow-Origin: *\n"
236// "Access-Control-Max-Age: 600\n";
237// if (http_method == http_options)
238// std::cout<<"Access-Control-Allow-Methods: GET, POST, OPTIONS\n"
239// "Content-Length: 0\n";
240// std::cout<<"Content-type: text/plain\n\n";
241// if (http_method == http_options || http_method == http_head)
242// return;
243// }
244//
245// std::cout<<timestamp<<"\n";
246// }
249// void Web_Output::write_csv_header
250// (const std::string& timestamp, const std::string& area_timestamp, bool write_mime)
251// {
252// if (header_written != not_yet)
253// return;
254// header_written = csv;
255//
256// if (write_mime)
257// {
258// if (allow_headers != "")
259// std::cout<<"Access-Control-Allow-Headers: "<<allow_headers<<'\n';
260// if (has_origin)
261// std::cout<<"Access-Control-Allow-Origin: *\n";
262// if (http_method == http_options)
263// std::cout<<"Access-Control-Allow-Methods: GET, POST, OPTIONS\n"
264// "Content-Length: 0\n";
265// std::cout<<"Content-type: text/csv\n\n";
266// if (http_method == http_options || http_method == http_head)
267// return;
268// }
269// }
272void Web_Output::write_footer()
2741346673 q if (header_written == html)
2750 std::cout<<"\n</body>\n</html>\n";
2761346673 q else if (header_written != final && output_handler)
277673673 q output_handler->write_footer();
2791346673 q header_written = final;
283void Web_Output::display_remark(const std::string& text)
2840{
2850 enforce_header(200);
2860 if (http_method == http_options || http_method == http_head)
2870 return;
2880 if (header_written == html)
2890 std::cout<<"<p><strong style=\"color:#00BB00\">Remark</strong>: "
2900 <<escape_xml(text)<<" </p>\n";
2910 else if (output_handler)
2920 output_handler->display_remark(text);
2930}
296void Web_Output::display_error(const std::string& text, uint write_mime)
2970{
2980 enforce_header(write_mime);
2990 if (http_method == http_options || http_method == http_head)
3000 return;
3010 if (header_written == html)
3020 std::cout<<"<p><strong style=\"color:#FF0000\">Error</strong>: "
3030 <<escape_xml(text)<<" </p>\n";
3040 else if (output_handler)
3050 output_handler->display_error(text);
3060}