Skip to content

Commit

Permalink
Add msIO_getAndStripStdoutBufferMimeHeaders to deal with all MIME hea…
Browse files Browse the repository at this point in the history
…ders.

Currently if one wants to use mapscript, one cannot easily strip off all MIME headers,
which can be an issue if setting the "ows_http_max_age" metadata, which emits a
Cache-Control MIME header in WMS output.
  • Loading branch information
rouault committed Jan 17, 2017
1 parent 60fd6de commit 10fe3d7
Show file tree
Hide file tree
Showing 6 changed files with 295 additions and 1 deletion.
120 changes: 120 additions & 0 deletions mapio.c
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,126 @@ void msIO_installStdinFromBuffer()
&group->stderr_context );
}

/************************************************************************/
/* msIO_getAndStripStdoutBufferMimeHeaders() */
/* */
/************************************************************************/

hashTableObj* msIO_getAndStripStdoutBufferMimeHeaders()
{
/* -------------------------------------------------------------------- */
/* Find stdout buffer. */
/* -------------------------------------------------------------------- */
msIOContext *ctx = msIO_getHandler( (FILE *) "stdout" );
msIOBuffer *buf;
int start_of_mime_header, current_pos;
hashTableObj* hashTable;

if( ctx == NULL || ctx->write_channel == MS_FALSE
|| strcmp(ctx->label,"buffer") != 0 ) {
msSetError( MS_MISCERR, "Can't identify msIO buffer.",
"msIO_getAndStripStdoutBufferMimeHeaders" );
return NULL;
}

buf = (msIOBuffer *) ctx->cbData;

hashTable = msCreateHashTable();

/* -------------------------------------------------------------------- */
/* Loop over all headers. */
/* -------------------------------------------------------------------- */
current_pos = 0;
while( TRUE ) {
int pos_of_column = -1;
char* key, *value;

start_of_mime_header = current_pos;
while( current_pos < buf->data_offset )
{
if( buf->data[current_pos] == '\r' )
{
if( current_pos + 1 == buf->data_offset ||
buf->data[current_pos + 1] != '\n' )
{
pos_of_column = -1;
break;
}
break;
}
if( buf->data[current_pos] == ':' )
{
pos_of_column = current_pos;
if( current_pos + 1 == buf->data_offset ||
buf->data[current_pos + 1] != ' ' )
{
pos_of_column = -1;
break;
}
}
current_pos++;
}

if( pos_of_column < 0 || current_pos == buf->data_offset ) {
msSetError( MS_MISCERR, "Corrupt mime headers.",
"msIO_getAndStripStdoutBufferMimeHeaders" );
msFreeHashTable(hashTable);
return NULL;
}

key = (char*) malloc( pos_of_column - start_of_mime_header + 1 );
memcpy( key, buf->data+start_of_mime_header, pos_of_column - start_of_mime_header);
key[pos_of_column - start_of_mime_header] = '\0';

value = (char*) malloc( current_pos - (pos_of_column+2) + 1 );
memcpy( value, buf->data+pos_of_column+2, current_pos - (pos_of_column+2));
value[current_pos - (pos_of_column+2)] = '\0';

msInsertHashTable( hashTable, key, value );

msFree( key );
msFree( value );

/* -------------------------------------------------------------------- */
/* Go to next line. */
/* -------------------------------------------------------------------- */
current_pos += 2;
if( current_pos == buf->data_offset )
{
msSetError( MS_MISCERR, "Corrupt mime headers.",
"msIO_getAndStripStdoutBufferMimeHeaders" );
msFreeHashTable(hashTable);
return NULL;
}

/* If next line is a '\r', this is the end of mime headers. */
if( buf->data[current_pos] == '\r' )
{
current_pos ++;
if( current_pos == buf->data_offset ||
buf->data[current_pos] != '\n' )
{
msSetError( MS_MISCERR, "Corrupt mime headers.",
"msIO_getAndStripStdoutBufferMimeHeaders" );
msFreeHashTable(hashTable);
return NULL;
}
current_pos ++;
break;
}
}

/* -------------------------------------------------------------------- */
/* Move data to front of buffer, and reset length. */
/* -------------------------------------------------------------------- */
memmove( buf->data, buf->data+current_pos,
buf->data_offset - current_pos );
buf->data[buf->data_offset - current_pos] = '\0';
buf->data_offset -= current_pos;

return hashTable;
}

/************************************************************************/
/* msIO_stripStdoutBufferContentType() */
/* */
Expand Down
4 changes: 3 additions & 1 deletion mapio.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
*/

#include <stdarg.h>
#include "maphash.h"

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -105,7 +106,8 @@ extern "C" {
void MS_DLL_EXPORT msIO_Cleanup(void);
char MS_DLL_EXPORT *msIO_stripStdoutBufferContentType(void);
void MS_DLL_EXPORT msIO_stripStdoutBufferContentHeaders(void);

hashTableObj MS_DLL_EXPORT *msIO_getAndStripStdoutBufferMimeHeaders(void);

msIOContext *msIO_pushStdoutToBufferAndGetOldContext(void);
void msIO_restoreOldStdoutContext(msIOContext *context_to_restore);

Expand Down
40 changes: 40 additions & 0 deletions mapscript/python/pymodule.i
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,46 @@ CreateTupleFromDoubleArray( double *first, unsigned int size ) {
$result = t_output_helper($result,r);
}


/*
* Typemap hashTableObj* -> dict
*/
%typemap(out) hashTableObj*
{
/* %typemap(out) hashTableObj* */
const char* key;
hashTableObj *hashTable = $1;
$result = PyDict_New();
key = msFirstKeyFromHashTable(hashTable);
while( key )
{
const char* val = msLookupHashTable(hashTable, key);
if( val )
{
#if PY_VERSION_HEX >= 0x03000000
PyObject *py_key = PyUnicode_FromString(key);
PyObject *py_val = PyUnicode_FromString(val);
#else
PyObject *py_key = PyString_FromString(key);
PyObject *py_val = PyString_FromString(val);
#endif

PyDict_SetItem($result, py_key, py_val );
Py_DECREF(py_key);
Py_DECREF(py_val);
}
key = msNextKeyFromHashTable(hashTable, key);
}
}

%typemap(freearg) hashTableObj*
{
/* %typemap(freearg) hashTableObj* */
msFreeHashTable( $1 );
}



/**************************************************************************
* MapServer Errors and Python Exceptions
**************************************************************************
Expand Down
5 changes: 5 additions & 0 deletions mapscript/swiginc/msio.i
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ void msIO_stripStdoutBufferContentHeaders(void);
const char *msIO_getStdoutBufferString(void);
gdBuffer msIO_getStdoutBufferBytes(void);

#ifdef SWIGPYTHON
%newobject msIO_getAndStripStdoutBufferMimeHeaders;
hashTableObj* msIO_getAndStripStdoutBufferMimeHeaders(void);
#endif

%{

const char *msIO_getStdoutBufferString() {
Expand Down
35 changes: 35 additions & 0 deletions msautotest/mspython/test_mapio.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# REQUIRES: INPUT=GDAL OUTPUT=PNG SUPPORTS=WMS
#
MAP
NAME TEST
STATUS ON
SIZE 80 40
EXTENT -180 -90 180 90

PROJECTION
"init=epsg:4326"
END

OUTPUTFORMAT
NAME png24_t
DRIVER "GDAL/PNG"
IMAGEMODE RGBA
END

WEB
METADATA
"ows_enable_request" "*"
"wms_srs" "EPSG:4326"
"ows_http_max_age" "86400"
END
END

LAYER
NAME grey
TYPE raster
STATUS default
DATA ../gdal/data/grey.tif
END


END # of map file
92 changes: 92 additions & 0 deletions msautotest/mspython/test_mapio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
###############################################################################
# $Id$
#
# Project: MapServer
# Purpose: Regression test for mapio
# Author: Even Rouault
#
###############################################################################
# Copyright (c) 2017, Even Rouault,<even.rouault at spatialys.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
###############################################################################

import sys
import string

sys.path.append( '../pymod' )
import pmstestlib

import mapscript
import os

###############################################################################
#

def test_msIO_getAndStripStdoutBufferMimeHeaders():

if string.find(mapscript.msGetVersion(),'INPUT=GDAL') == -1:
return 'skip'
if string.find(mapscript.msGetVersion(),'SUPPORTS=WMS') == -1:
return 'skip'

map = mapscript.mapObj('test_mapio.map')
request = mapscript.OWSRequest()
mapscript.msIO_installStdoutToBuffer()
request.loadParamsFromURL('service=WMS&version=1.1.1&request=GetMap&layers=grey&srs=EPSG:4326&bbox=-180,-90,180,90&format=image/png&width=80&height=40')
status = map.OWSDispatch(request)
if status != 0:
pmstestlib.post_reason( 'wrong OWSDispatch status' )
return 'fail'
headers = mapscript.msIO_getAndStripStdoutBufferMimeHeaders()
if headers is None:
pmstestlib.post_reason( 'headers is None' )
return 'fail'
if 'Content-Type' not in headers or headers['Content-Type'] != 'image/png':
pmstestlib.post_reason( 'wrong Content-Type' )
print(headers)
return 'fail'
if 'Cache-Control' not in headers or headers['Cache-Control'] != 'max-age=86400':
pmstestlib.post_reason( 'wrong Cache-Control' )
print(headers)
return 'fail'

result = mapscript.msIO_getStdoutBufferBytes()
if result is None or result[1:4] != 'PNG':
pmstestlib.post_reason( 'wrong data' )
return 'fail'

return 'success'

test_list = [
test_msIO_getAndStripStdoutBufferMimeHeaders,
None ]

if __name__ == '__main__':

pmstestlib.setup_run( 'test_mapio' )

pmstestlib.run_tests( test_list )

pmstestlib.summarize()

mapscript.msCleanup()

0 comments on commit 10fe3d7

Please sign in to comment.