summaryrefslogtreecommitdiff
path: root/modules/modpy/interface.cpp
blob: 60e1c7b0a4f73c8650193b0dc554b1737c45a742 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 *	bitz-server, An ICAP server implementation in C++
 *	Copyright (C) 2012	Uditha Atukorala
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "interface.h"

#include <icap/request.h>
#include <icap/response.h>
#include <spdlog/spdlog.h>

static auto _logger = spdlog::get( "bitz-server" );


PyObject * bitz_get_request( PyObject * self, PyObject * pyrequest ) {

	PyObject * pyreturn;
	PyObject * pypayload;
	icap::Request * request;

	_logger->debug( "[modpy.interface] get_request()" );

	// initialise return dictionary
	pyreturn = PyDict_New();

	// grab the request object pointer from args
	void * p = PyCapsule_GetPointer( pyrequest, "request" );

	// sanity check
	if ( p != NULL ) {

		// construct the request
		request = static_cast<icap::Request *>(p);

		PyDict_SetItemString( pyreturn, "request", PyString_FromString( request->header()->method().c_str() ) );
		PyDict_SetItemString( pyreturn, "uri", PyString_FromString( request->header()->uri().c_str() ) );
		PyDict_SetItemString( pyreturn, "protocol", PyString_FromString( request->header()->protocol().c_str() ) );

		// payload dictionary
		pypayload = PyDict_New();
		PyDict_SetItemString( pypayload, "req_header", PyString_FromStringAndSize( request->payload().req_header.c_str(), request->payload().req_header.size() ) );
		PyDict_SetItemString( pypayload, "req_body", PyString_FromStringAndSize( request->payload().req_body.c_str(), request->payload().req_body.size() ) );
		PyDict_SetItemString( pypayload, "res_header", PyString_FromStringAndSize( request->payload().res_header.c_str(), request->payload().res_header.size() ) );
		PyDict_SetItemString( pypayload, "res_body", PyString_FromStringAndSize( request->payload().res_body.c_str(), request->payload().res_body.size() ) );
		PyDict_SetItemString( pypayload, "ieof", PyBool_FromLong( request->payload().ieof ) );

		PyDict_SetItemString( pyreturn, "payload", pypayload );

		// cleanup
		Py_DECREF( pypayload );

	} else {
		_logger->warn( "[modpy.interface] failed to get request object pointer" );
	}

	return pyreturn;

}


PyObject * bitz_get_response_from_status( PyObject * self, PyObject * args ) {

	PyObject * pyresponse;
	icap::Response * response;

	unsigned int resp_status;

	_logger->debug( "[modpy.interface] get_response_from_status()" );

	// parse args
	if ( PyArg_ParseTuple( args, "I", &resp_status ) ) {
		response = new icap::Response( (icap::ResponseHeader::status_t) resp_status );
	} else {
		_logger->warn( "[modpy.interface] failed to parse arguments" );
		response = new icap::Response( icap::ResponseHeader::SERVER_ERROR );
	}

	// convert the response into a capsule
	pyresponse = PyCapsule_New( (void *) response, "response", NULL );

	return pyresponse;

}


PyObject * bitz_get_response( PyObject * self, PyObject * args ) {

	PyObject * pyresponse;
	PyObject * pypayload;
	icap::Response * response = NULL;

	unsigned int resp_status;
	icap::payload_t payload;

	// vars to ferry across strings from python dictionary to c++
	char * pybuffer;
	Py_ssize_t pybuflen;


	_logger->debug( "[modpy.interface] get_response()" );

	// parse args
	if ( PyArg_ParseTuple( args, "IO!", &resp_status, &PyDict_Type, &pypayload ) ) {

		// copy strings from python dictionary
		PyString_AsStringAndSize( PyDict_GetItemString( pypayload, "req_header" ), &pybuffer, &pybuflen );
		payload.req_header.assign( pybuffer, pybuflen );

		PyString_AsStringAndSize( PyDict_GetItemString( pypayload, "req_body" ), &pybuffer, &pybuflen );
		payload.req_body.assign( pybuffer, pybuflen );

		PyString_AsStringAndSize( PyDict_GetItemString( pypayload, "res_header" ), &pybuffer, &pybuflen );
		payload.res_header.assign( pybuffer, pybuflen );

		PyString_AsStringAndSize( PyDict_GetItemString( pypayload, "res_body" ), &pybuffer, &pybuflen );
		payload.res_body.assign( pybuffer, pybuflen );

		// copy other data types from python dictionary
		payload.ieof = PyBool_Check( PyDict_GetItemString( pypayload, "ieof" ) );

		// construct the response object
		response = new icap::Response( (icap::ResponseHeader::status_t) resp_status );
		response->payload( payload );

	} else {
		_logger->warn( "[modpy.interface] failed to parse arguments" );
	}

	// sanity check
	if ( response == NULL ) {
		response = new icap::Response( icap::ResponseHeader::SERVER_ERROR );
	}

	// convert the response into a capsule
	pyresponse = PyCapsule_New( (void *) response, "response", NULL );

	return pyresponse;

}