Skip to content

Commit fa3bf30

Browse files
committed
fix: fix Content-Type parsing in some error cases
Close: #7
1 parent 0f7913b commit fa3bf30

3 files changed

Lines changed: 113 additions & 28 deletions

File tree

src/mapserverapi.c

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ gboolean mapserverapi_invoke(const gchar *mapfile_content, const gchar *query_st
6767
g_assert(query_string != NULL);
6868
g_assert(strlen(query_string) > 0);
6969
gboolean res;
70+
gsize i;
7071
gchar *mapfile_path = get_tmpfilename();
7172
res = g_file_set_contents(mapfile_path, mapfile_content, -1, NULL);
7273
if (!res) {
@@ -78,7 +79,7 @@ gboolean mapserverapi_invoke(const gchar *mapfile_content, const gchar *query_st
7879
g_string_append_printf(gs, "MAP=%s", mapfile_path);
7980
gs = g_string_append_c(gs, '&');
8081
if (query_string[0] == '?') {
81-
gs = g_string_append(gs, query_string + sizeof(gchar));
82+
gs = g_string_append(gs, query_string + 1);
8283
} else {
8384
gs = g_string_append(gs, query_string);
8485
}
@@ -100,43 +101,46 @@ gboolean mapserverapi_invoke(const gchar *mapfile_content, const gchar *query_st
100101
g_free(mapserver_qs);
101102
return FALSE;
102103
}
103-
if (strncasecmp((const char*) mapserver_buffer, "Content-type: ", 14) != 0) {
104-
g_warning("bad reply from mapserver for query_string = %s (no Content-Type)", mapserver_qs);
105-
g_free(mapserver_qs);
106-
return FALSE;
107-
}
108-
int end_of_ct = 13;
109-
while ((end_of_ct + 1 < mapserver_buffer_length) && (((char*)mapserver_buffer)[end_of_ct + 1] != 10)) {
110-
end_of_ct++;
104+
gsize last_i = 0;
105+
gchar *ct = NULL;
106+
const char *mb = (char*) mapserver_buffer;
107+
for (i = 0 ; i < mapserver_buffer_length - 1 ; i++) {
108+
if (i > 100) {
109+
break;
110+
}
111+
if ((mb[i] == '\r') && (mb[i+1] == '\n')) {
112+
if (i == last_i) {
113+
last_i = i + 2;
114+
break;
115+
}
116+
if (strncmp(mb + last_i, "Content-Type: ", 14) == 0) {
117+
ct = g_malloc(i - last_i - 14 + 1);
118+
ct[i - last_i - 14] = '\0';
119+
memcpy(ct, mapserver_buffer + (last_i + 14), i - last_i - 14);
120+
}
121+
last_i = i + 2;
122+
}
111123
}
112-
if (end_of_ct + 1 == mapserver_buffer_length) {
113-
g_warning("bad reply from mapserver for query_string = %s (bad Content-Type)", query_string);
124+
if (i >= mapserver_buffer_length) {
125+
g_message("bad reply from mapserver for query_string = %s (no body found)", mapserver_qs);
114126
g_free(mapserver_qs);
115127
return FALSE;
116128
}
117-
int start_of_data = end_of_ct + 2;
118-
while ((start_of_data < mapserver_buffer_length) && (((char*)mapserver_buffer)[start_of_data] != 10)) {
119-
start_of_data++;
120-
}
121-
if (start_of_data == mapserver_buffer_length) {
122-
g_warning("bad reply from mapserver for query_string = %s (corrupt Content-Type)", mapserver_qs);
129+
if (ct == NULL) {
130+
g_message("bad reply from mapserver for query_string = %s (no Content-Type found)", mapserver_qs);
123131
g_free(mapserver_qs);
124132
return FALSE;
125133
}
126-
start_of_data++;
127-
gchar *ct = g_malloc((end_of_ct - 14 + 2) * sizeof(gchar));
128-
memcpy(ct, mapserver_buffer + 14 * sizeof(gchar), sizeof(gchar) * (end_of_ct - 14 + 2));
129-
ct[end_of_ct - 14 + 1] = '\0';
130134
if (content_type != NULL) {
131-
*content_type = g_strstrip(ct);
135+
*content_type = ct;
132136
} else {
133137
g_free(ct);
134138
}
135139
if (body != NULL) {
136-
*body = mapserver_buffer + start_of_data * sizeof(gchar);
140+
*body = mapserver_buffer + last_i;
137141
}
138142
if (body_length != NULL) {
139-
*body_length = mapserver_buffer_length - start_of_data;
143+
*body_length = mapserver_buffer_length - last_i;
140144
}
141145
g_free(mapserver_qs);
142146
return res;

src/test_mapserverapi.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ gchar *get_data_path()
1414
return res;
1515
}
1616

17-
gchar *get_mapfile_content()
17+
gchar *get_mapfile_content(const gchar *name)
1818
{
1919
gchar *contents;
2020
gchar *data_path = get_data_path();
21-
gchar *path = g_strdup_printf("%s/test.map", data_path);
21+
gchar *path = g_strdup_printf("%s/%s.map", data_path, name);
2222
g_file_get_contents(path, &contents, NULL, NULL);
2323
g_free(path);
2424
gchar **split = g_strsplit((const gchar*) contents, "{DATAPATH}", -1);
@@ -38,7 +38,7 @@ void test_mapserverapi_init()
3838
void test_mapserverapi_invoke()
3939
{
4040
mapserverapi_init();
41-
gchar *contents = get_mapfile_content();
41+
gchar *contents = get_mapfile_content("test");
4242
void *body;
4343
gchar *content_type = NULL;
4444
gsize body_length;
@@ -52,10 +52,27 @@ void test_mapserverapi_invoke()
5252
mapserverapi_destroy();
5353
}
5454

55+
void test_mapserverapi_invoke2()
56+
{
57+
mapserverapi_init();
58+
gchar *contents = get_mapfile_content("mapfile_bad");
59+
void *body;
60+
gchar *content_type = NULL;
61+
gsize body_length;
62+
gboolean b = mapserverapi_invoke(contents, "version=2.0.0&service=WFS&request=GetFeature&typename=cat_20181231000000", &body, &content_type, &body_length);
63+
g_free(contents);
64+
g_assert(b == TRUE);
65+
g_file_set_contents("./testresult.png", body, body_length, NULL);
66+
g_assert_cmpint(body_length, <, 1000);
67+
g_assert_cmpstr(content_type, ==, "text/xml; charset=UTF-8");
68+
g_free(content_type);
69+
mapserverapi_destroy();
70+
}
71+
5572
void test_mapserverapi_invoke_to_file()
5673
{
5774
mapserverapi_init();
58-
gchar *contents = get_mapfile_content();
75+
gchar *contents = get_mapfile_content("test");
5976
gchar *content_type = mapserverapi_invoke_to_file(contents, "LAYERS=ocean&TRANSPARENT=true&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_xml&SRS=EPSG%3A4326&BBOX=-180.0,-90.0,180.0,90.0&WIDTH=500&HEIGHT=250", "/tmp/test_mapserverapi_invoke_to_file");
6077
g_free(contents);
6178
g_assert(content_type != NULL);
@@ -75,6 +92,7 @@ int main(int argc, char** argv)
7592
g_test_init (&argc, &argv, NULL);
7693
g_test_add_func("/mapserverapi/new_free", test_mapserverapi_init);
7794
g_test_add_func("/mapserverapi/invoke", test_mapserverapi_invoke);
95+
g_test_add_func("/mapserverapi/invoke2", test_mapserverapi_invoke2);
7896
g_test_add_func("/mapserverapi/invoke_to_file", test_mapserverapi_invoke_to_file);
7997
gint res = g_test_run();
8098
return res;

test_datas/mapfile_bad.map

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
MAP
2+
NAME cat
3+
STATUS ON
4+
EXTENT -50 -50 50 50
5+
IMAGECOLOR 255 255 255
6+
7+
WEB
8+
METADATA
9+
"wfs_namespace_uri" "http://meteofrance.fr/metgate"
10+
"wfs_onlineresource" "None/wfs?"
11+
"wfs_namespace_prefix" "cat"
12+
"ows_enable_request" "*"
13+
"wfs_enable_request" "*"
14+
"ows_contactfacsimiletelephone" ""
15+
"ows_fees" ""
16+
"ows_accessconstraints" ""
17+
"ows_keywordlist" ""
18+
"ows_title" "foo"
19+
"ows_abstract" "foo"
20+
"ows_contactorganization" "foo"
21+
"ows_contactelectronicmailaddress" ""
22+
"ows_contactperson" ""
23+
"ows_contactposition" ""
24+
"ows_contactvoicetelephone" ""
25+
"ows_address" ""
26+
"ows_city" ""
27+
"ows_stateorprovince" ""
28+
"ows_postcode" ""
29+
"ows_country" ""
30+
"ows_service_onlineresource" ""
31+
"ows_hoursofservice" ""
32+
"ows_contactinstructions" ""
33+
"ows_role" ""
34+
35+
END
36+
END
37+
#
38+
# Start of layers definitions
39+
#
40+
41+
LAYER
42+
# NAME = PRODUCT_INSTANCE_ID
43+
NAME cat_20181230000000
44+
TYPE POLYGON
45+
STATUS ON
46+
CONNECTIONTYPE POSTGIS
47+
CONNECTION "host=localhost dbname=foo user=foo password=foo port=7432"
48+
METADATA
49+
# wfs_title = PRODUCT_ID PRODUCT_INSTANCE_DATE
50+
"wfs_title" "cat 20181230000000" ##REQUIRED
51+
# wfs_srs = crs of the product
52+
"wfs_srs" "ESPG_7030" ## REQUIRED
53+
54+
"gml_include_items" "all" ## Optional (serves all attributes for layer)
55+
"wfs_enable_request" "*"
56+
END
57+
58+
# DATA = "wkb_geometry from instance_postgis_table_name"
59+
# Note : column name inserted by OGR2OGR wkb_geometry
60+
DATA "wkb_geometry from cat_20181230000000"
61+
END
62+
63+
END # Map File

0 commit comments

Comments
 (0)