summaryrefslogtreecommitdiff
path: root/project2/cgi/FCgiIO.cpp
blob: 9d26329b975e3beb7c98faa6fbef4ba5afe39fe4 (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
/*
 *  $Id: FCgiIO.cpp,v 1.6 2007/07/02 18:48:19 sebdiaz Exp $
 *
 *  Copyright (C) 2002 Steve McAndrewSmith
 *  Copyright (C) 2002 - 2004 Stephen F. Booth
 *                       2007 Sebastien DIAZ <sebastien.diaz@gmail.com>
 *  Part of the GNU cgicc library, http://www.gnu.org/software/cgicc
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 3 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA 
 */

#ifdef __GNUG__
#  pragma implementation
#endif

#include <iostream>
#include <stdexcept>
#include <cstdlib>
#include "safeMapFind.h"

#include "FCgiIO.h"

cgicc::FCgiIO::FCgiIO(FCGX_Request& request) :
	std::ostream(&fOutBuf),
	fRequest(request), 
	fOutBuf(request.out), 
	fErrBuf(request.err), 
	fErr(&fErrBuf)
{
	rdbuf(&fOutBuf);
	fErr.rdbuf(&fErrBuf);

	// Parse environment
	for(char **e = fRequest.envp; *e != NULL; ++e) {
		std::string s(*e);
		std::string::size_type i = s.find('=');
		if(i == std::string::npos)
			throw std::runtime_error("Illegally formed environment");
		fEnv[s.substr(0, i)] = s.substr(i + 1);
	}
}

cgicc::FCgiIO::FCgiIO(const FCgiIO& io) :
	std::ios(),
	CgiInput(io),
	std::ostream(&fOutBuf),
	fRequest(io.fRequest), 
	fErr(&fErrBuf), 
	fEnv(io.fEnv)
{
	rdbuf(&fOutBuf);
	fErr.rdbuf(&fErrBuf);
}

cgicc::FCgiIO::~FCgiIO()
{
}

size_t
cgicc::FCgiIO::read(char *data, size_t length)
{
	return FCGX_GetStr(data, length, fRequest.in);
}

std::string
cgicc::FCgiIO::getenv(const char *varName)
{
	return fEnv[varName];
}

std::string
cgicc::FCgiIO::getenv(const std::string & varName) const
{
	return AdHoc::defaultMapLookup(fEnv, varName, std::string());
}

std::ostream &
cgicc::FCgiIO::err()
{
	return fErr;
}